summaryrefslogtreecommitdiffstats
path: root/codebase/DataStorage/MySQLDBDataWrapper.php
blob: 3f13492b4f599de6343fc02af8a193a27308fa34 (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
63
64
65
66
67
68
69
<?php
namespace DHTMLX\Connector\DataStorage;


/*! Implementation of DataWrapper for MySQL
**/
class MySQLDBDataWrapper extends DBDataWrapper{
    protected $last_result;
    public function query($sql){
        LogMaster::log($sql);
        $res=mysql_query($sql,$this->connection);
        if ($res===false) throw new Exception("MySQL operation failed\n".mysql_error($this->connection));
        $this->last_result = $res;
        return $res;
    }

    public function get_next($res){
        if (!$res)
            $res = $this->last_result;

        return mysql_fetch_assoc($res);
    }

    public function get_new_id(){
        return mysql_insert_id($this->connection);
    }

    public function escape($data){
        return mysql_real_escape_string($data, $this->connection);
    }

    public function tables_list() {
        $result = mysql_query("SHOW TABLES");
        if ($result===false) throw new Exception("MySQL operation failed\n".mysql_error($this->connection));

        $tables = array();
        while ($table = mysql_fetch_array($result)) {
            $tables[] = $table[0];
        }
        return $tables;
    }

    public function fields_list($table) {
        $result = mysql_query("SHOW COLUMNS FROM `".$table."`");
        if ($result===false) throw new Exception("MySQL operation failed\n".mysql_error($this->connection));

        $fields = array();
        $id = "";
        while ($field = mysql_fetch_assoc($result)) {
            if ($field['Key'] == "PRI")
                $id = $field["Field"];
            else
                $fields[] = $field["Field"];
        }
        return array("fields" => $fields, "key" => $id );
    }

    /*! escape field name to prevent sql reserved words conflict
        @param data
            unescaped data
        @return
            escaped data
    */
    public function escape_name($data){
        if ((strpos($data,"`")!==false || is_int($data)) || (strpos($data,".")!==false))
            return $data;
        return '`'.$data.'`';
    }
}