forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres7_adodb_moodle_database.php
542 lines (465 loc) · 18.8 KB
/
postgres7_adodb_moodle_database.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Postgresql database class using adodb backend
*
* TODO: delete before branching 2.0
*
* @package moodlecore
* @subpackage DML
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/dml/moodle_database.php');
require_once($CFG->libdir.'/dml/adodb_moodle_database.php');
/**
* Postgresql database class using adodb backend
*/
class postgres7_adodb_moodle_database extends adodb_moodle_database {
public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
if ($prefix == '' and !$this->external) {
//Enforce prefixes for everybody but mysql
throw new dml_exception('prefixcannotbeempty', $this->get_dbfamily());
}
return parent::connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
}
/**
* Detects if all needed PHP stuff installed.
* Do not connect to connect to db if this test fails.
* @return mixed true if ok, string if something
*/
public function driver_installed() {
if (!extension_loaded('pgsql')) {
return get_string('pgsqlextensionisnotpresentinphp', 'install');
}
return true;
}
protected function preconfigure_dbconnection() {
if (!defined('ADODB_ASSOC_CASE')) {
define ('ADODB_ASSOC_CASE', 2);
}
}
protected function configure_dbconnection() {
$this->adodb->SetFetchMode(ADODB_FETCH_ASSOC);
$sql = "SET NAMES 'utf8'";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = $this->adodb->Execute($sql);
$this->query_end($result);
return true;
}
/**
* Returns database family type
* @return string db family name (mysql, postgres, mssql, oracle, etc.)
*/
public function get_dbfamily() {
return 'postgres';
}
/**
* Returns database type
* @return string db type mysql, mysqli, postgres7
*/
protected function get_dbtype() {
return 'postgres7';
}
/**
* Returns localised database description
* Note: can be used before connect()
* @return string
*/
public function get_configuration_hints() {
return get_string('databasesettingssub_postgres7', 'install');
}
/**
* Returns db related part of config.php
* @return object
*/
public function export_dbconfig() {
$cfg = new stdClass();
$cfg->dbtype = $this->get_dbtype();
$cfg->dblibrary = $this->get_dblibrary();
if ($this->dbhost == 'localhost' or $this->dbhost == '127.0.0.1') {
$cfg->dbhost = "user='{$this->dbuser}' password='{$this->dbpass}' dbname='{$this->dbname}'";
$cfg->dbname = '';
$cfg->dbuser = '';
$cfg->dbpass = '';
} else {
$cfg->dbhost = $this->dbhost;
$cfg->dbname = $this->dbname;
$cfg->dbuser = $this->dbuser;
$cfg->dbpass = $this->dbpass;
}
$cfg->prefix = $this->prefix;
if ($this->dboptions) {
$cfg->dboptions = $this->dboptions;
}
return $cfg;
}
/**
* Returns supported query parameter types
* @return bitmask
*/
protected function allowed_param_types() {
return SQL_PARAMS_QM;
}
public function get_columns($table, $usecache=true) {
if ($usecache and isset($this->columns[$table])) {
return $this->columns[$table];
}
$this->query_start("--adodb-MetaColumns", null, SQL_QUERY_AUX);
$columns = $this->adodb->MetaColumns($this->prefix.$table);
$this->query_end(true);
if (!$columns) {
return array();
}
$this->columns[$table] = array();
foreach ($columns as $column) {
// colum names must be lowercase
$column->meta_type = substr($this->adodb->MetaType($column), 0 ,1); // only 1 character
// Let's fix the wrong meta type retrieved because of default blobSize=100 in AdoDB
if ($column->type == 'varchar' && $column->meta_type == 'X') {
$column->meta_type = 'C';
}
if ($column->has_default) {
if ($pos = strpos($column->default_value, '::')) {
if (strpos($column->default_value, "'") === 0) {
$column->default_value = substr($column->default_value, 1, $pos-2);
} else {
$column->default_value = substr($column->default_value, 0, $pos);
}
}
} else {
$column->default_value = null;
}
$this->columns[$table][$column->name] = new database_column_info($column);
}
return $this->columns[$table];
}
/**
* This method will introspect inside DB to detect it it's a UTF-8 DB or no
* Used from setup.php to set correctly "set names" when the installation
* process is performed without the initial and beautiful installer
* @return bool true if db in unicode mode
*/
function setup_is_unicodedb() {
/// Get PostgreSQL server_encoding value
$sql = "SHOW server_encoding";
$this->query_start($sql, null, SQL_QUERY_AUX);
$rs = $this->adodb->Execute($sql);
$this->query_end($rs);
if ($rs && !$rs->EOF) {
$encoding = $rs->fields['server_encoding'];
if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
return true;
}
}
return false;
}
/**
* Insert new record into database, as fast as possible, no safety checks, lobs not supported.
* (overloaded from adodb_moodle_database because of sequence numbers
* @param string $table name
* @param mixed $params data record as object or array
* @param bool $returnit return it of inserted record
* @param bool $bulk true means repeated inserts expected
* @param bool $customsequence true if 'id' included in $params, disables $returnid
* @return true or new id
* @throws dml_exception if error
*/
public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {
if (!is_array($params)) {
$params = (array)$params;
}
if ($customsequence) {
if (!isset($params['id'])) {
throw new coding_exception('moodle_database::insert_record_raw() id field must be specified if custom sequences used.');
}
$returnid = false;
} else {
unset($params['id']);
/// Postgres doesn't have the concept of primary key built in
/// and will return the OID which isn't what we want.
/// The efficient and transaction-safe strategy is to
/// move the sequence forward first, and make the insert
/// with an explicit id.
if ($returnid) {
$seqname = "{$this->prefix}{$table}_id_seq";
$this->query_start('--adodb-GenID', null, SQL_QUERY_AUX);
$nextval = $this->adodb->GenID($seqname);
$this->query_end(true);
if ($nextval) {
$params['id'] = (int)$nextval;
}
}
}
if (empty($params)) {
throw new coding_exception('moodle_database::insert_record_raw() no fields found.');
}
$fields = implode(',', array_keys($params));
$qms = array_fill(0, count($params), '?');
$qms = implode(',', $qms);
$sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";
$this->query_start($sql, $params, SQL_QUERY_INSERT);
$rs = $this->adodb->Execute($sql, $params);
$this->query_end($rs);
if (!$returnid) {
return true;
}
if (!empty($params['id'])) {
return (int)$params['id'];
}
$oid = $this->adodb->Insert_ID();
// try to get the primary key based on id
$sql = "SELECT id FROM {$this->prefix}$table WHERE oid = $oid";
$this->query_start($sql, $params, SQL_QUERY_AUX);
$rs = $this->adodb->Execute($sql, $params);
$this->query_end($rs);
if ( $rs && ($rs->RecordCount() == 1) ) {
trigger_error("Retrieved id using oid on table $table because we could not find the sequence.");
return (integer)reset($rs->fields);
}
throw new dml_write_exception('unknown error fetching inserted id');
}
/**
* Insert a record into a table and return the "id" field if required,
* Some conversions and safety checks are carried out. Lobs are supported.
* If the return ID isn't required, then this just reports success as true/false.
* $data is an object containing needed data
* @param string $table The database table to be inserted into
* @param object $data A data object with values for one or more fields in the record
* @param bool $returnid Should the id of the newly created record entry be returned? If this option is not requested then true/false is returned.
* @param bool $bulk true means repeated inserts expected
* @return true or new id
* @throws dml_exception if error
*/
public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
//TODO: add support for blobs BYTEA
if (!is_object($dataobject)) {
$dataobject = (object)$dataobject;
}
$columns = $this->get_columns($table);
unset($dataobject->id);
$cleaned = array();
$blobs = array();
foreach ($dataobject as $field=>$value) {
if (!isset($columns[$field])) {
continue;
}
$column = $columns[$field];
if ($column->meta_type == 'B') {
if (is_null($value)) {
$cleaned[$field] = null;
} else {
$blobs[$field] = $value;
$cleaned[$field] = '@#BLOB#@';
}
continue;
} else if (is_bool($value)) {
$value = (int)$value; // prevent false '' problems
} else if ($value === '') {
if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
$value = 0; // prevent '' problems in numeric fields
}
}
$cleaned[$field] = $value;
}
if (empty($blobs)) {
return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
}
$id = $this->insert_record_raw($table, $cleaned, true, $bulk);
foreach ($blobs as $key=>$value) {
$this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE);
$result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id", 'BLOB');// adodb does not use bound parameters for blob updates :-(
$this->query_end($result);
}
return ($returnid ? $id : true);
}
/**
* Update a record in a table
*
* $dataobject is an object containing needed data
* Relies on $dataobject having a variable "id" to
* specify the record to update
*
* @param string $table The database table to be checked against.
* @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
* @param bool true means repeated updates expected
* @return bool true
* @throws dml_exception if error
*/
public function update_record($table, $dataobject, $bulk=false) {
//TODO: add support for blobs BYTEA
if (!is_object($dataobject)) {
$dataobject = (object)$dataobject;
}
$columns = $this->get_columns($table);
$cleaned = array();
$blobs = array();
foreach ($dataobject as $field=>$value) {
if (!isset($columns[$field])) {
continue;
}
$column = $columns[$field];
if ($column->meta_type == 'B') {
if (is_null($value)) {
$cleaned[$field] = null;
} else {
$blobs[$field] = $value;
$cleaned[$field] = '@#BLOB#@';
}
continue;
} else if (is_bool($value)) {
$value = (int)$value; // prevent "false" problems
} else if ($value === '') {
if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
$value = 0; // prevent '' problems in numeric fields
}
}
$cleaned[$field] = $value;
}
$this->update_record_raw($table, $cleaned, $bulk);
if (empty($blobs)) {
return true;
}
$id = $dataobject->id;
foreach ($blobs as $key=>$value) {
$this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE);
$result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id", 'BLOB');// adodb does not use bound parameters for blob updates :-(
$this->query_end($result);
}
return true;
}
/**
* Set a single field in every table row where the select statement evaluates to true.
*
* @param string $table The database table to be checked against.
* @param string $newfield the field to set.
* @param string $newvalue the value to set the field to.
* @param string $select A fragment of SQL to be used in a where clause in the SQL call.
* @param array $params array of sql parameters
* @return bool true
* @throws dml_exception if error
*/
public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
$params = (array)$params;
list($select, $params, $type) = $this->fix_sql_params($select, $params);
$columns = $this->get_columns($table);
$column = $columns[$newfield];
if ($column->meta_type == 'B') {
/// update blobs and return
$select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-(
$this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE);
$result = $this->adodb->UpdateBlob($this->prefix.$table, $newfield, $newvalue, $select, 'BLOB');
$this->query_end($result);
return true;
}
if ($select) {
$select = "WHERE $select";
}
/// normal field update
if (is_null($newvalue)) {
$newfield = "$newfield = NULL";
} else {
if (is_bool($newvalue)) {
$newvalue = (int)$newvalue; // prevent "false" problems
} else if ($newvalue === '') {
if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
$newvalue = 0; // prevent '' problems in numeric fields
}
}
$newfield = "$newfield = ?";
array_unshift($params, $newvalue); // add as first param
}
$sql = "UPDATE {$this->prefix}$table SET $newfield $select";
$this->query_start($sql, $params, SQL_QUERY_UPDATE);
$rs = $this->adodb->Execute($sql, $params);
$this->query_end($rs);
return true;
}
public function sql_ilike() {
return 'ILIKE';
}
public function sql_concat() {
$args = func_get_args();
/// PostgreSQL requires at least one char element in the concat, let's add it
/// here (at the beginning of the array) until ADOdb fixes it
if (is_array($args)) {
array_unshift($args , "''");
}
return call_user_func_array(array($this->adodb, 'Concat'), $args);
}
public function sql_bitxor($int1, $int2) {
return '(' . $this->sql_bitor($int1, $int2) . ' - ' . $this->sql_bitand($int1, $int2) . ')';
}
public function sql_cast_char2int($fieldname, $text=false) {
return ' CAST(' . $fieldname . ' AS INT) ';
}
public function sql_cast_char2real($fieldname, $text=false) {
return " $fieldname::real ";
}
/**
* Does this driver suppoer regex syntax when searching
*/
public function sql_regex_supported() {
return true;
}
/**
* Return regex positive or negative match sql
* @param bool $positivematch
* @return string or empty if not supported
*/
public function sql_regex($positivematch=true) {
return $positivematch ? '~*' : '!~*';
}
/**
* Import a record into a table, id field is required.
* Basic safety checks only. Lobs are supported.
* @param string $table name of database table to be inserted into
* @param mixed $dataobject object or array with fields in the record
* @return bool true
* @throws dml_exception if error
*/
public function import_record($table, $dataobject) {
$dataobject = (object)$dataobject;
$columns = $this->get_columns($table);
$cleaned = array();
$blobs = array();
foreach ($dataobject as $field=>$value) {
if (!isset($columns[$field])) {
continue;
}
$column = $columns[$field];
if ($column->meta_type == 'B') {
if (!is_null($value)) {
$blobs[$field] = $value;
$cleaned[$field] = '@#BLOB#@';
continue;
}
}
$cleaned[$field] = $value;
}
$this->insert_record_raw($table, $cleaned, false, true, true);
if (empty($blobs)) {
return true;
}
/// We have BLOBs to postprocess
foreach ($blobs as $key=>$value) {
$this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE);
$result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id", 'BLOB');
$this->query_end($result);
}
return true;
}
}