3
class DBRes implements Iterator {
6
public function __construct() {
10
public function push(&$obj) {
11
array_push($this->arr, $obj);
14
public function fetch($flags) {
15
return array_shift($this->arr);
22
return current($this->arr);
25
return key($this->arr);
31
// We don't expect 'false' elements in array (all are arrays)
32
return current($this->arr)?true:false;
44
const GLOBAL_QUERY = 1;
45
const SINGLE_RESULT = 2;
47
function __construct(&$server) {
51
switch($server['driver']) {
53
if ($server['source']) {
54
$this->dbh = new PDO ("odbc:" . $server['source'], $server['user'], $server['password']);
55
$this->dbname = false;
56
$this->connected = true;
58
if ($server['database']) {
59
$dbtext = ";DATABASE=" . $server['database'];
60
$this->dbname = $props['db_name'];
61
$this->connected = true;
64
$this->dbh = new PDO ("odbc:DRIVER=" . $server['subdrv'] . ";SERVER=" . $server['host'] . ";PORT=" . ($server['port']?$server['port']:"1433") . $dbtext . ";PROTOCOL=TCPIP;UID=" . $server['user'] . ";PWD=" . $server['password']);
70
if ($server['database']) {
71
$this->dbh = new PDO ($server['driver'] . ":host=" . $server['host'] . ($server['port']?(":" . $server['port']):"") . ";dbname=" . $server['database'], $server['user'], $server['password']);
72
$this->dbname = $props['db_name'];
73
$this->connected = true;
75
$this->dbh = new PDO ($server['driver'] . ":host=" . $server['host'] . ($server['port']?(":" . $server['port']):""), $server['user'], $server['password']);
76
$this->connected = false;
80
if ($server['sqldrv']) $this->driver = $server['sqldrv'];
81
else $this->driver = $server['driver'];
83
/* Single check for single scheme */
84
switch ($this->driver) {
86
$this->driver="mssql";
89
} catch(PDOException $e) {
91
$this->connected = false;
93
throw new ADEIException(translate("Error connecting to the database") . ": " . $e->getMessage(), $e->getCode());
96
$this->server = $server;
99
static function GetConnectionString(array &$server, array &$options = NULL, &$application = NULL) {
100
if (strtolower($server['driver']) == 'mysql') {
101
if ((!$application)||($application == 'mysqldump')) {
102
return "-h" . $server['host'] . ($server['port']?(":" . $server['port']):"") . " -u" . $server['user'] . " -p" . $server['password'] . " " . $server['database'];
103
} else throw new ADEIException(translate("The required application (\"%s\") is not supported at the moment", $application));
104
} else throw new ADEIException(translate("The supplied connection (\"%s\") is not supported at the moment", $server['driver']));
108
function Query($sql, $flags = 0) {
109
if ((!$this->connected)&&(($flags&DATABASE::GLOBAL_QUERY)==0))
110
throw new ADEIException(translate("The database is not specified"));
112
// echo $sql . "\n\n";
115
// This is to prevent spurious odbc errors:
116
$stmt = $this->Prepare($sql, $flags);
120
if ($flags&DATABASE::SINGLE_RESULT) {
121
$row = $stmt->fetch(PDO::FETCH_BOTH);
122
if ($row) $resp->push($row);
124
while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
129
$resp = $this->dbh->query($sql);
131
} catch (PDOException $e) {
132
throw new ADEIException(translate("SQL Query is failed with error") . ": " . $e->getMessage(), $e->getCode());
137
$e = $this->dbh->errorInfo();
138
throw new ADEIException(translate("SQL Query is failed. SQL Error: %u, Driver Error: %u, Message: %s ", $e[0], $e[1], $e[2]) . "[$sql]");
144
function Prepare($sql, $flags = 0) {
145
if ((!$this->connected)&&(($flags&DATABASE::GLOBAL_QUERY)==0))
146
throw new ADEIException(translate("The database is not specified"));
149
$stmt = $this->dbh->prepare($sql);
150
} catch (PDOException $e) {
151
throw new ADEIException(translate("Can not prepare SQL Query for execution, error: ") . $e->getMessage(), $e->getCode());
155
$e = $this->dbh->errorInfo();
156
throw new ADEIException(translate("Preparation of the SQL Query is failed. SQL Error: %u, Driver Error: %u, Message: %s ", $e[0], $e[1], $e[2]) . "[$sql]");
162
function SelectRequest($table, $columns="*", array $req = NULL) {
165
switch ($this->driver) {
167
$res .= "TOP " . $req['limit'] . " ";
170
$suffix = " LIMIT " . $req['limit'];
173
throw new ADEIException(translate("Don't know how to handle LIMIT for '%s'", $this->driver));
177
if (is_array($columns)) {
178
$last = array_pop($columns);
180
throw new ADEIException(translate("Columns list is empty"));
182
foreach ($columns as $item) {
183
$res .= "\"" . $item['column'] . "\", ";
185
$res .= "\"" . $last . "\"";
187
} else $res .= $columns;
189
$res .= " FROM " . $table;
191
if ($req['condition']) $res .= " WHERE " . $req['condition'];
192
if ($req['order']) $res .= " ORDER BY " . $req['order'];
194
return $res . $suffix;
197
function GetTimeFormat() {
198
// if ($this->time_format) return $this->time_format;
200
switch ($this->driver) {
205
/* Hm. Didn't work from Linux
206
return "Y-d-m H:i:s";*/
207
/* We can't use '.u' since it is formated with 6 digits after the
208
decimal point, while the MSSQL complains if there are more than 3
209
return "Y-d-m H:i:s.u";*/
211
throw new ADEIException("The date format for \"" . $this->driver . "\" is not known");
215
function GetTimeRequest($column_name) {
216
switch ($this->driver) {
218
return "CONVERT(CHAR(24), \"$column_name\", 21)";
220
return "\"column_name\"";
224
function ShowDatabases() {
225
switch ($this->driver) {
227
return $this->Query("SHOW DATABASES", DATABASE::GLOBAL_QUERY);
229
return $this->Query("SELECT name FROM master..sysdatabases");
231
throw new ADEIException("The ShowDatabases for \"" . $this->driver . "\" is not implemented");
235
function ShowTables() {
236
switch ($this->driver) {
238
return $this->Query("SHOW TABLES");
240
/* Bugs in ODBC/MySQL (with segm. in some cases,
241
possibly http://bugs.php.net/bug.php?id=33533&edit=1
244
$db = new DATABASE($this->server);
245
return $db->Query(" SELECT name AS gid FROM sysobjects WHERE type = 'U'");
248
return $this->Query(" SELECT name AS gid FROM sysobjects WHERE type = 'U'");
250
throw new ADEIException("The ShowTables for \"" . $this->driver . "\" is not implemented");
254
function ShowColumns($table) {
255
switch ($this->driver) {
257
/* we could get here problems if several non-equal tables with
258
different prefixes (mda,dbo) are present */
259
$table = preg_replace("/^mda\./", "", $table);
260
return $this->Query("SELECT name FROM (SELECT DISTINCT TOP 65535 name=syscolumns.name, type=systypes.name, length=syscolumns.length, objname=sysobjects.name, colid=syscolumns.colid
262
JOIN syscolumns ON sysobjects.id = syscolumns.id
263
JOIN systypes ON syscolumns.xtype=systypes.xtype
264
WHERE (sysobjects.xtype='U' OR sysobjects.xtype='V') AND sysobjects.name='$table'
265
ORDER BY sysobjects.name,syscolumns.colid) AS tmptable");
267
throw new ADEIException("SHOW COLUMNS not implemented for " . $this->driver);
271
function CreateDatabase($dbname) {
272
$this->Query("CREATE DATABASE `$dbname`", DATABASE::GLOBAL_QUERY);
276
function GetDatabaseList($filter = false) {
277
$resp = $this->ShowDatabases();
281
foreach ($resp as $row) {
283
if ((!$filter)||(preg_match($filter, $name))) {
284
$dblist[$name] = array(
295
/* expecting rows in a form: chan1,chan2,...,chan#,time */
296
class DATABASEData implements Iterator {
302
function __construct(READER &$reader, PDOStatement &$stmt) {
303
$this->reader = &$reader;
304
$this->stmt = &$stmt;
309
$this->stmt->execute();
311
} catch(PDOException $e) {
312
throw new ADEIException(translate("SQL request is failed with error") . ": " . $e->getMessage(), $e->getCode());
322
// current key (PHP rounding to integer :()
326
echo date('Y-m-d', $this->reader->ExportUnixTime($this->time));
328
return $this->reader->ExportUnixTime($this->time);
331
// advvance to next (and returns it or false)
334
$this->row = $this->stmt->fetch(PDO::FETCH_NUM);
336
$last = sizeof($this->row) - 1;
337
$this->time = $this->row[$last];
338
unset($this->row[$last]);
340
} catch(PDOException $e) {
341
throw new ADEIException(translate("SQL error") . ": " . $e->getMessage());
345
// checks if there is current element
347
return $this->row?true:false;
b'\\ No newline at end of file'