/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/database.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class DBRes implements Iterator {
 
4
 var $arr;
 
5
  
 
6
 public function __construct() {
 
7
    $this->arr = array();
 
8
 }
 
9
 
 
10
 public function push(&$obj) {
 
11
    array_push($this->arr, $obj);
 
12
 }
 
13
 
 
14
 public function fetch($flags) {
 
15
    return array_shift($this->arr);
 
16
 }
 
17
 
 
18
 function rewind() {
 
19
    reset($this->arr);
 
20
 }
 
21
 function current() {
 
22
    return current($this->arr);
 
23
 }
 
24
 function key() {
 
25
    return key($this->arr);
 
26
 }
 
27
 function next() {
 
28
    each($this->arr);    
 
29
 }
 
30
 function valid() {
 
31
        // We don't expect 'false' elements in array (all are arrays)
 
32
    return current($this->arr)?true:false;
 
33
 }
 
34
 
 
35
}
 
36
 
 
37
class DATABASE {
 
38
 var $dbh, $connected;
 
39
 var $driver;
 
40
 var $odbc;
 
41
 
 
42
 var $server;
 
43
 
 
44
 const GLOBAL_QUERY = 1;
 
45
 const SINGLE_RESULT = 2;
 
46
 
 
47
 function __construct(&$server) {
 
48
     try {
 
49
        $this->odbc = false;
 
50
        
 
51
        switch($server['driver']) {
 
52
          case "odbc":
 
53
            if ($server['source']) {
 
54
                $this->dbh = new PDO ("odbc:" . $server['source'], $server['user'], $server['password']);
 
55
                $this->dbname = false;
 
56
                $this->connected = true;
 
57
            } else {
 
58
                if ($server['database']) {
 
59
                    $dbtext = ";DATABASE=" . $server['database'];
 
60
                    $this->dbname = $props['db_name'];
 
61
                    $this->connected = true;
 
62
                } else $dbtext = "";
 
63
 
 
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']);
 
65
            }
 
66
 
 
67
            $this->odbc = true;
 
68
          break;
 
69
          default:      
 
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;
 
74
            } else {
 
75
                $this->dbh = new PDO ($server['driver'] . ":host=" . $server['host'] . ($server['port']?(":" . $server['port']):""), $server['user'], $server['password']);
 
76
                $this->connected = false;
 
77
            }
 
78
        }
 
79
        
 
80
        if ($server['sqldrv']) $this->driver = $server['sqldrv'];
 
81
        else $this->driver = $server['driver'];
 
82
        
 
83
            /* Single check for single scheme */
 
84
        switch ($this->driver) {
 
85
            case "dblib":
 
86
                $this->driver="mssql";
 
87
            break;
 
88
        }
 
89
    } catch(PDOException $e) {
 
90
        $this->dbh = NULL;
 
91
        $this->connected = false;
 
92
 
 
93
        throw new ADEIException(translate("Error connecting to the database") . ": " . $e->getMessage(), $e->getCode());
 
94
    }
 
95
    
 
96
    $this->server = $server;
 
97
 }
 
98
 
 
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']));
 
105
 }
 
106
 
 
107
 
 
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"));
 
111
 
 
112
//    echo $sql . "\n\n";
 
113
    try {
 
114
        if ($this->odbc) {
 
115
                //      This is to prevent spurious odbc errors:
 
116
            $stmt = $this->Prepare($sql, $flags);
 
117
            $stmt->execute();
 
118
        
 
119
            $resp = new DBRes();
 
120
            if ($flags&DATABASE::SINGLE_RESULT) {
 
121
                $row = $stmt->fetch(PDO::FETCH_BOTH);
 
122
                if ($row) $resp->push($row);
 
123
            } else {
 
124
                while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
 
125
                    $resp->push($row);
 
126
                }
 
127
            }
 
128
        } else {
 
129
            $resp = $this->dbh->query($sql);
 
130
        }
 
131
    } catch (PDOException $e) {
 
132
        throw new ADEIException(translate("SQL Query is failed with error") . ": " . $e->getMessage(), $e->getCode());
 
133
    }
 
134
//    echo "done\n";
 
135
    
 
136
    if (!$resp) {
 
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]");
 
139
    }
 
140
 
 
141
    return $resp;
 
142
 }
 
143
 
 
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"));
 
147
 
 
148
    try {
 
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());
 
152
    }
 
153
 
 
154
    if (!$stmt) {
 
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]");
 
157
    }
 
158
 
 
159
    return $stmt;
 
160
 }
 
161
 
 
162
 function SelectRequest($table, $columns="*", array $req = NULL) {
 
163
    $res = "SELECT ";
 
164
    if ($req['limit']) {
 
165
        switch ($this->driver) {
 
166
            case "mssql":
 
167
                $res .= "TOP " . $req['limit'] . " ";
 
168
            break;
 
169
            case "mysql":
 
170
                $suffix = " LIMIT " . $req['limit'];
 
171
            break;
 
172
            default:
 
173
                throw new ADEIException(translate("Don't know how to handle LIMIT for '%s'", $this->driver));
 
174
        }
 
175
    }
 
176
    
 
177
    if (is_array($columns)) {
 
178
        $last = array_pop($columns);
 
179
        if (!$last) 
 
180
            throw new ADEIException(translate("Columns list is empty"));
 
181
        
 
182
        foreach ($columns as $item) {
 
183
            $res .=  "\"" . $item['column'] . "\", ";
 
184
        }
 
185
        $res .= "\"" . $last . "\"";
 
186
    
 
187
    } else $res .= $columns;
 
188
    
 
189
    $res .= " FROM " . $table;
 
190
 
 
191
    if ($req['condition']) $res .= " WHERE " . $req['condition'];
 
192
    if ($req['order']) $res .= " ORDER BY " . $req['order'];
 
193
    
 
194
    return $res . $suffix;
 
195
 }
 
196
 
 
197
 function GetTimeFormat() {
 
198
//    if ($this->time_format) return $this->time_format;
 
199
    
 
200
    switch ($this->driver) {
 
201
        case "mysql":
 
202
            return "YmdHis";
 
203
        case "mssql":
 
204
            return "Ymd H:i:s";
 
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";*/
 
210
        default:
 
211
            throw new ADEIException("The date format for \"" . $this->driver . "\" is not known");
 
212
    }
 
213
 }
 
214
 
 
215
 function GetTimeRequest($column_name) {
 
216
    switch ($this->driver) {
 
217
        case "mssql":
 
218
            return "CONVERT(CHAR(24), \"$column_name\", 21)";
 
219
        default:
 
220
            return "\"column_name\"";
 
221
    }
 
222
 }
 
223
 
 
224
 function ShowDatabases() {
 
225
    switch ($this->driver) {
 
226
        case "mysql":
 
227
            return $this->Query("SHOW DATABASES", DATABASE::GLOBAL_QUERY);
 
228
        case "mssql":
 
229
            return $this->Query("SELECT name FROM master..sysdatabases");
 
230
        default:
 
231
            throw new ADEIException("The ShowDatabases for \"" . $this->driver . "\" is not implemented");
 
232
    }
 
233
 } 
 
234
 
 
235
 function ShowTables() {
 
236
    switch ($this->driver) {
 
237
        case "mysql":
 
238
            return $this->Query("SHOW TABLES");
 
239
        case "mssql":
 
240
                /* Bugs in ODBC/MySQL (with segm. in some cases,
 
241
                possibly http://bugs.php.net/bug.php?id=33533&edit=1
 
242
                */
 
243
/*
 
244
            $db = new DATABASE($this->server);
 
245
            return $db->Query(" SELECT name AS gid FROM sysobjects WHERE type = 'U'");
 
246
*/
 
247
 
 
248
            return $this->Query(" SELECT name AS gid FROM sysobjects WHERE type = 'U'");
 
249
        default:
 
250
            throw new ADEIException("The ShowTables for \"" . $this->driver . "\" is not implemented");
 
251
    }
 
252
 }
 
253
 
 
254
 function ShowColumns($table) {
 
255
    switch ($this->driver) {
 
256
        case "mssql":
 
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
 
261
                FROM sysobjects 
 
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");
 
266
        default:
 
267
            throw new ADEIException("SHOW COLUMNS not implemented for " . $this->driver);
 
268
    }
 
269
 }
 
270
 
 
271
 function CreateDatabase($dbname) {
 
272
    $this->Query("CREATE DATABASE `$dbname`", DATABASE::GLOBAL_QUERY);
 
273
 }
 
274
 
 
275
 
 
276
 function GetDatabaseList($filter = false) {
 
277
    $resp = $this->ShowDatabases();
 
278
 
 
279
    $dblist = array();
 
280
 
 
281
    foreach ($resp as $row) {
 
282
        $name = $row[0];
 
283
        if ((!$filter)||(preg_match($filter, $name))) {
 
284
            $dblist[$name] = array(
 
285
                'name' => $name
 
286
            );
 
287
        }
 
288
    }
 
289
    
 
290
    return $dblist;
 
291
 }
 
292
}
 
293
 
 
294
 
 
295
/* expecting rows in a form: chan1,chan2,...,chan#,time  */
 
296
class DATABASEData implements Iterator {
 
297
 var $reader;
 
298
 var $stmt;
 
299
 var $row;
 
300
 var $time;
 
301
 
 
302
 function __construct(READER &$reader, PDOStatement &$stmt) {
 
303
    $this->reader = &$reader;
 
304
    $this->stmt = &$stmt;
 
305
 }
 
306
 
 
307
 function rewind() {
 
308
    try {
 
309
        $this->stmt->execute();
 
310
        $this->next();
 
311
    } catch(PDOException $e) {
 
312
        throw new ADEIException(translate("SQL request is failed with error") . ": " . $e->getMessage(), $e->getCode());
 
313
    }
 
314
 
 
315
 }
 
316
 
 
317
    // current element
 
318
 function current() {
 
319
    return $this->row;
 
320
 }
 
321
 
 
322
    // current key (PHP rounding to integer :()
 
323
 function key() {
 
324
/*    echo $this->time;
 
325
    echo "  -  ";
 
326
    echo date('Y-m-d', $this->reader->ExportUnixTime($this->time));
 
327
    echo "\n";*/
 
328
    return $this->reader->ExportUnixTime($this->time);
 
329
 }
 
330
 
 
331
    // advvance to next (and returns it or false)
 
332
 function next() {
 
333
    try {
 
334
        $this->row = $this->stmt->fetch(PDO::FETCH_NUM);
 
335
        if ($this->row) {
 
336
            $last = sizeof($this->row) - 1;
 
337
            $this->time = $this->row[$last];
 
338
            unset($this->row[$last]);
 
339
        }
 
340
    } catch(PDOException $e) {
 
341
        throw new ADEIException(translate("SQL error") . ": " . $e->getMessage());
 
342
    }
 
343
 }
 
344
 
 
345
    // checks if there is current element
 
346
 function valid() {
 
347
    return $this->row?true:false;
 
348
 }
 
349
}
 
350
 
 
351
?>
 
 
b'\\ No newline at end of file'