-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_pgsql_lite.php
157 lines (130 loc) · 4.22 KB
/
db_pgsql_lite.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// CubicleSoft PostgreSQL database interface.
// (C) 2016 CubicleSoft. All Rights Reserved.
if (!class_exists("CSDB", false)) require_once str_replace("\\", "/", dirname(__FILE__)) . "/db.php";
class CSDB_pgsql_lite extends CSDB
{
protected $lastid;
public function IsAvailable()
{
return (class_exists("PDO") && in_array("pgsql", PDO::getAvailableDrivers()) ? "pgsql" : false);
}
public function GetDisplayName()
{
return CSDB::DB_Translate("PostgreSQL (via PDO, Lite version)");
}
public function Connect($dsn, $username = false, $password = false, $options = array())
{
$this->lastid = 0;
parent::Connect($dsn, $username, $password, $options);
// Set Unicode support.
$this->Query("SET", "client_encoding TO 'UTF-8'");
}
public function GetInsertID($name = null)
{
return $this->lastid;
}
public function QuoteIdentifier($str)
{
return "\"" . str_replace(array("\"", "?"), array("\"\"", ""), $str) . "\"";
}
protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args, $subquery)
{
switch ($cmd)
{
case "SELECT":
{
$supported = array(
"PRECOLUMN" => array("DISTINCT" => "bool", "SUBQUERIES" => true),
"FROM" => array("SUBQUERIES" => true),
"WHERE" => array("SUBQUERIES" => true),
"GROUP BY" => true,
"HAVING" => true,
"ORDER BY" => true,
"LIMIT" => " OFFSET "
);
return $this->ProcessSELECT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
}
case "INSERT":
{
$supported = array(
"PREINTO" => array(),
"POSTVALUES" => array("RETURNING" => "key_identifier"),
"SELECT" => true,
"BULKINSERT" => true
);
// To get the last insert ID via GetInsertID(), the field that contains a 'serial' (auto increment) field must be specified.
if (isset($queryinfo["AUTO INCREMENT"])) $queryinfo["RETURNING"] = $queryinfo["AUTO INCREMENT"];
$this->lastid = 0;
$result = $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
if ($result["success"] && isset($queryinfo["AUTO INCREMENT"])) $result["filter_opts"] = array("mode" => "INSERT", "queryinfo" => $queryinfo);
return $result;
}
case "UPDATE":
{
// No ORDER BY or LIMIT support.
$supported = array(
"PRETABLE" => array("ONLY" => "bool"),
"WHERE" => array("SUBQUERIES" => true)
);
return $this->ProcessUPDATE($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
}
case "DELETE":
{
// No ORDER BY or LIMIT support.
$supported = array(
"PREFROM" => array("ONLY" => "bool"),
"WHERE" => array("SUBQUERIES" => true)
);
return $this->ProcessDELETE($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
}
case "SET":
{
$sql = "SET " . $queryinfo;
return array("success" => true);
}
case "USE":
{
// Fake multiple databases with PostgreSQL schemas.
// http://www.postgresql.org/docs/7.3/static/ddl-schemas.html
$sql = "SET search_path TO " . ($queryinfo != "" ? $this->QuoteIdentifier($queryinfo) . "," : "") . "\"\$user\",public";
return array("success" => true);
}
case "TRUNCATE TABLE":
{
$master = true;
$sql = "TRUNCATE TABLE " . $this->QuoteIdentifier($queryinfo[0]);
return array("success" => true);
}
}
return array("success" => false, "error" => CSDB::DB_Translate("Unknown query command '%s'.", $cmd), "errorcode" => "unknown_query_command");
}
protected function RunStatementFilter(&$stmt, &$filteropts)
{
if ($filteropts["mode"] == "INSERT")
{
// Force the last ID value to be extracted for INSERT queries.
$result = new CSDB_PDO_Statement($this, $stmt, $filteropts);
$row = $result->NextRow();
$stmt = false;
}
parent::RunStatementFilter($stmt, $filteropts);
}
public function RunRowFilter(&$row, &$filteropts, &$fetchnext)
{
switch ($filteropts["mode"])
{
case "INSERT":
{
if ($row !== false)
{
$key = $filteropts["queryinfo"]["AUTO INCREMENT"];
$this->lastid = $row->$key;
}
break;
}
}
if (!$fetchnext) parent::RunRowFilter($row, $filteropts, $fetchnext);
}
}
?>