summaryrefslogtreecommitdiffstats
path: root/codebase/DataStorage/PDODBDataWrapper.php
blob: 5a08e577434a6a10e79891fd05da76dc7564b5f5 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace DHTMLX\Connector\DataStorage;

/*! Implementation of DataWrapper for PDO

if you plan to use it for Oracle - use Oracle connection type instead
**/
class PDODBDataWrapper extends DBDataWrapper{
	private $last_result;//!< store result or last operation

	public function query($sql){
		LogMaster::log($sql);

		$res=$this->connection->query($sql);
		if ($res===false) {
			$message = $this->connection->errorInfo();
			throw new Exception("PDO - sql execution failed\n".$message[2]);
		}

		return new PDOResultSet($res);
	}

	protected function select_query($select,$from,$where,$sort,$start,$count){
		if (!$from)
			return $select;

		$sql="SELECT ".$select." FROM ".$from;
		if ($where) $sql.=" WHERE ".$where;
		if ($sort) $sql.=" ORDER BY ".$sort;
		if ($start || $count) {
			if ($this->connection->getAttribute(PDO::ATTR_DRIVER_NAME)=="pgsql")
				$sql.=" OFFSET ".$start." LIMIT ".$count;
			else
				$sql.=" LIMIT ".$start.",".$count;
		}
		return $sql;
	}

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

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

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

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

	public function get_next($res){
		$data = $res->next();
		return $data;
	}

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

	public function escape($str){
		$res=$this->connection->quote($str);
		if ($res===false) //not supported by pdo driver
			return str_replace("'","''",$str);
		return substr($res,1,-1);
	}

}