summaryrefslogtreecommitdiffstats
path: root/lib/SQL.php
blob: af1968b1d949a4f5634143e8cb6530edbc60b894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php

class SQL {
    static private $instance = array();

    private $sql_uri = "mysql://username:password@hostname/database";

    /* Placeholder for the existing MySQL connection */
    private $conn = FALSE;

    private $sql_stats = array(
        'queries' => 0,
        'query_time' => 0,
        'connections' => 0
    );

    /**
     * This implements the 'singleton' design pattern
     *
     * @return SQL The one and only instance associated with $_conn
     */
    static function get_instance($_conn = 'kolab_wap')
    {
        if (!isset(self::$instance[$_conn])) {
            self::$instance[$_conn] = new SQL($_conn);
        }

        return self::$instance[$_conn];
    }

    public function __construct($_conn = 'kolab_wap')
    {
        $this->name = $_conn;
        $conf = Conf::get_instance();
        $this->sql_uri = $conf->get($_conn, 'sql_uri');
    }

    public function query($query)
    {
        if (!$this->conn) {
            $this->_connect();
        }

        $result = mysql_query($query);
        return $result;
    }

    private function _connect()
    {
        if (!$this->conn) {
            $_uri = parse_url($this->sql_uri);
            $this->_username = $_uri['user'];
            $this->_password = $_uri['pass'];
            $this->_hostname = $_uri['host'];
            $this->_database = str_replace('/','',$_uri['path']);

            $this->conn = mysql_connect($this->_hostname, $this->_username, $this->_password);
            mysql_select_db($this->_database, $this->conn);
        }
    }

}