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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
|
<?php
/**
* $Header: /cvsroot/bitweaver/_bit_kernel/Attic/BitDb.php,v 1.2 2005/06/19 08:07:25 lsces Exp $
*
* Copyright (c) 2004 bitweaver.org
* Copyright (c) 2003 tikwiki.org
* Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
* All Rights Reserved. See copyright.txt for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
*
* $Id: BitDb.php,v 1.2 2005/06/19 08:07:25 lsces Exp $
*/
// ensure your AdoDB install is a subdirectory off your include path
require_once(UTIL_PKG_PATH."adodb/adodb.inc.php");
require_once( KERNEL_PKG_PATH.'bit_error_inc.php' );
define( 'BIT_QUERY_DEFAULT', -1 );
/**
* This class is used for database access and provides a number of functions to help
* with database portability.
*
* Currently used as a base class, this class should be optional to ensure bitweaver
* continues to function correctly, without a valid database connection.
*
* @date created
*
* @author spider <spider@steelsun.com>
*
* @version $Revision: 1.2 $ $Date: 2005/06/19 08:07:25 $ $Author: lsces $
*
* @class BitDb
*/
class BitDb
{
/**
* Used to store the ADODB db object used to access the database.
* This is just a pointer to a single global variable used by all classes.
* This limits database connections to just one per request.
* @private
*/
var $mDb;
/**
* Used to identify the ADODB db object
* @private
*/
var $mName;
/**
* Used to store the ADODB db object type
* @private
*/
var $mType;
/**
* Used to store failed commands
* @private
*/
var $mFailed = array();
/**
* Used to store the number of queries executed.
* @private
*/
var $mNumQueries = 0;
/**
* Used to enable AdoDB caching
* @private
*/
var $mCacheFlag;
/**
* During initialisation, database parameters are passed to the class.
* If these parameters are not valid, class will not be initialised.
*/
function BitDb()
{
global $gBitDbType, $gBitDbHost, $gBitDbUser, $gBitDbPassword, $gBitDbName, $ADODB_FETCH_MODE;
$this->mCacheFlag = FALSE;
$this->mNumQueries = 0;
global $ADODB_CACHE_DIR;
if( empty( $ADODB_CACHE_DIR ) ) {
$ADODB_CACHE_DIR = getTempDir().'/adodb/'.$_SERVER['HTTP_HOST'].'/';
}
mkdir_p( $ADODB_CACHE_DIR );
// Get all the ADODB stuff included
if (!defined("ADODB_FORCE_NULLS"))
define("ADODB_FORCE_NULLS", 1);
if (!defined("ADODB_ASSOC_CASE"))
define("ADODB_ASSOC_CASE", 0);
if (!defined("ADODB_CASE_ASSOC"))
define("ADODB_CASE_ASSOC", 0);
// typo in adodb's driver for sybase?
if (!defined("ADODB_FETCH_MODE")) {
define("ADODB_FETCH_MODE", ADODB_CASE_ASSOC);
}
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
if( !empty( $gBitDbName ) && !empty( $gBitDbType ) ) {
$this->mType = $gBitDbType;
$this->mName = $gBitDbName;
if(!isset($this->mName)) {
die("No database name specified");
}
$this->preDBConnection();
$this->mDb = ADONewConnection($gBitDbType);
$this->mDb->Connect($gBitDbHost, $gBitDbUser, $gBitDbPassword, $gBitDbName);
if(!$this->mDb)
{
die("Unable to login to the database '<?=$gBitDbName?>' on '<?=$gBitDbHost?>' as `user` '<?=$gBitDbUser?>'<p>".$this->mDb->ErrorMsg());
}
$this->postDBConnection();
unset ($pDSN);
if( defined( "DB_PERFORMANCE_STATS" ) && constant( "DB_PERFORMANCE_STATS" ) )
{
$this->mDb->LogSQL();
}
}
}
/**
* This function contains any pre-connection work
* @private
* @todo investigate if this is the correct way to do it.
*/
function preDBConnection()
{
// Pre connection setup
if(isset($this->mType)) {
// we have a db we're gonna try to load
switch ($this->mType)
{
case "pgsql":
case "postgres7":
$this->mType = "pgsql";
break;
case "sybase":
// avoid database change messages
ini_set("sybct.min_server_severity", "11");
break;
}
} else {
die("No database type specified");
}
}
/**
* This function contains any post-connection work
* @private
* @todo investigate if this is the correct way to do it.
* @todo remove the BIT_DB_PREFIX, change to a member variable
* @todo get spiderr to explain the schema line
*/
function postDBConnection()
{
// Post connection setup
switch ($this->mType)
{
case "sybase":
case "mssql":
$this->mDb->Execute("set quoted_identifier on");
break;
case "pgsql":
// Do a little prep work for postgres, no break, cause we want default case too
if (defined("BIT_DB_PREFIX") && preg_match( "/\./", BIT_DB_PREFIX) )
{
$schema = preg_replace("/[`\.]/", "", BIT_DB_PREFIX);
// Assume we want to dump in a schema, so set the search path and nuke the prefix here.
// $result = $this->mDb->Execute( "SET search_path TO $schema,public" );
}
break;
}
}
/**
* Determines if the database connection is valid
* @return true if DB connection is valid, false if not
*/
function isValid() {
return( !empty( $this->mDb ) );
}
/**
* Used to create tables - most commonly from package/schema_inc.php files
* @todo remove references to BIT_DB_PREFIX, us a member function
* @param pTables an array of tables and creation information in DataDict
* style
* @param pOptions an array of options used while creating the tables
* @return true if created with no errors
* @return false if errors are stored in $this->mFailed
*/
function createTables($pTables, $pOptions = array())
{
switch ($this->mType)
{
case "mysql":
// SHOULD HANDLE INNODB so foreign keys are cool - XOXO spiderr
$pOptions['mysql'] = 'TYPE=INNODB';
default:
//$pOptions[] = 'REPLACE';
}
$dict = NewDataDictionary($this->mDb);
$this->mFailed = array();
$result = true;
foreach(array_keys($pTables) AS $tableName)
{
$completeTableName = (defined("BIT_DB_PREFIX")) ? BIT_DB_PREFIX.$tableName : $tableName;
$sql = $dict->CreateTableSQL($completeTableName, $pTables[$tableName], $pOptions);
if ($sql && ($dict->ExecuteSQLArray($sql) > 0))
{
// Success
}
else
{
// Failure
$result = false;
array_push($this->mFailed, $sql.": ".$this->mDb->ErrorMsg());
}
}
return $result;
}
/**
* Used to check if tables already exists.
* @todo should be used to confirm tables are already created
* @param pTable the table name
* @return true if table already exists
*/
function tableExists($pTable)
{
$dict = NewDataDictionary($this->mDb);
$pTable = preg_replace("/`/", "", $pTable);
$tables = $dict->MetaTables(false, false, $pTable);
return array_search($pTable, $tables) !== FALSE;
}
/**
* Used to drop tables
* @todo remove references to BIT_DB_PREFIX, us a member function
* @param pTables an array of table names to drop
* @return true if dropped with no errors
* @return false if errors are stored in $this->mFailed
*/
function dropTables($pTables)
{
$dict = NewDataDictionary($this->mDb);
$this->mFailed = array();
$return = true;
foreach($pTables AS $tableName)
{
$completeTableName = (defined("BIT_DB_PREFIX")) ? BIT_DB_PREFIX.$tableName : $tableName;
$sql = $dict->DropTableSQL($completeTableName);
if ($sql && ($dict->ExecuteSQLArray($sql) > 0))
{
//echo "Success<br>";
}
else
{
//echo "Failure<br>";
$return = false;
array_push($this->mFailed, $sql);
}
}
return $return;
}
/**
* Function to set ADODB query caching member variable
* @param pCacheExecute flag to enable or disable ADODB query caching
* @return nothing
*/
function setCacheState( $pCacheFlag=TRUE ) {
$this->mCacheFlag = $pCacheFlag;
}
/**
* Function to set ADODB query caching member variable
* @param pCacheExecute flag to enable or disable ADODB query caching
* @return nothing
*/
function getCacheState() {
return( $this->mCacheFlag );
}
/**
* Quotes a string to be sent to the database which is
* passed to function on to AdoDB->qstr().
* @todo not sure what its supposed to do
* @param pStr string to be quotes
* @return quoted string using AdoDB->qstr()
*/
function qstr($pStr)
{
return $this->mDb->qstr($pStr);
}
/** Queries the database, returning an error if one occurs, rather
* than exiting while printing the error. -rlpowell
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pError the error string to modify and return
* @param pValues an array of values used in a parameterised query
* @param pNumRows the number of rows (LIMIT) to return in this query
* @param pOffset the row number to begin returning rows from. Used in
* @return an AdoDB RecordSet object
* conjunction with $pNumRows
* @todo currently not used anywhere.
*/
function queryError( $pQuery, &$pError, $pValues = NULL, $pNumRows = -1,
$pOffset = -1 )
{
$this->convertQuery($pQuery);
if ($pNumRows == -1 && $pOffset == -1)
$result = $this->mDb->Execute($pQuery, $pValues);
else
$result = $this->mDb->SelectLimit($pQuery, $pNumRows, $pOffset, $pValues);
if (!$result)
{
$pError = $this->mDb->ErrorMsg();
$result=false;
}
//count the number of queries made
$this->mNumQueries++;
//$this->debugger_log($pQuery, $pValues);
return $result;
}
/** Queries the database reporting an error if detected
* than exiting while printing the error. -rlpowell
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pNumRows the number of rows (LIMIT) to return in this query
* @param pOffset the row number to begin returning rows from. Used in
* conjunction with $pNumRows
* @return an AdoDB RecordSet object
*/
function query($query, $values = null, $numrows = BIT_QUERY_DEFAULT, $offset = BIT_QUERY_DEFAULT, $pCacheTime=BIT_QUERY_DEFAULT )
{
$this->convertQuery($query);
if( empty( $this->mDb ) ) {
return FALSE;
}
$this->queryStart();
if( !is_numeric( $numrows ) ) {
$numrows = BIT_QUERY_DEFAULT;
}
if( !is_numeric( $offset ) ) {
$offset = BIT_QUERY_DEFAULT;
}
if( $numrows == BIT_QUERY_DEFAULT && $offset == BIT_QUERY_DEFAULT ) {
if( !defined( 'IS_LIVE' ) || $pCacheTime == BIT_QUERY_DEFAULT ) {
$result = $this->mDb->Execute( $query, $values );
} else {
$result = $this->mDb->CacheExecute( $pCacheTime, $query, $values );
}
} else {
if( !defined( 'IS_LIVE' ) || $pCacheTime == BIT_QUERY_DEFAULT ) {
$result = $this->mDb->SelectLimit( $query, $numrows, $offset, $values );
} else {
$result = $this->mDb->CacheSelectLimit( $pCacheTime, $query, $numrows, $offset, $values );
}
}
$this->queryComplete();
return $result;
}
/** Executes the SQL and returns all elements of the first column as a 1-dimensional array. The recordset is discarded for you automatically. If an error occurs, false is returned.
* See AdoDB GetCol() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pForceArray if set to true, when an array is created for each value
* @param pFirst2Cols if set to true, only returns the first two columns
* @return the associative array, or false if an error occurs
* @todo not currently used anywhere
*/
function getCol( $pQuery, $pValues=FALSE, $pTrim=FALSE )
{
if( empty( $this->mDb ) ) {
return FALSE;
}
$this->queryStart();
$this->convertQuery($pQuery);
$execFunction = ( !defined( 'IS_LIVE' ) || $pCacheTime == BIT_QUERY_DEFAULT ? 'GetAssoc' : 'CacheGetAssoc' );
$result = $this->mDb->GetCol( $pQuery, $pValues, $pTrim );
//count the number of queries made
$this->queryComplete();
return $result;
}
/** Returns an associative array for the given query.
* See AdoDB GetAssoc() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pForceArray if set to true, when an array is created for each value
* @param pFirst2Cols if set to true, only returns the first two columns
* @return the associative array, or false if an error occurs
*/
function getAssoc( $pQuery, $pValues=FALSE, $pForceArray=FALSE, $pFirst2Cols=FALSE, $pCacheTime=BIT_QUERY_DEFAULT )
{
if( empty( $this->mDb ) ) {
return FALSE;
}
$this->queryStart();
$this->convertQuery($pQuery);
$execFunction = ( !defined( 'IS_LIVE' ) || $pCacheTime == BIT_QUERY_DEFAULT ? 'GetAssoc' : 'CacheGetAssoc' );
$result = $this->mDb->GetAssoc( $pQuery, $pValues, $pForceArray, $pFirst2Cols );
$this->queryComplete();
return $result;
}
/** Executes the SQL and returns the first row as an array. The recordset and remaining rows are discarded for you automatically. If an error occurs, false is returned.
* See AdoDB GetRow() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @return returns the first row as an array, or false if an error occurs
*/
function getRow( $pQuery, $pValues=FALSE, $pCacheTime=BIT_QUERY_DEFAULT )
{
if( empty( $this->mDb ) ) {
return FALSE;
}
$this->queryStart();
$this->convertQuery($pQuery);
if( !defined( 'IS_LIVE' ) || $pCacheTime == BIT_QUERY_DEFAULT ) {
$result = $this->mDb->GetRow( $pQuery, $pValues );
} else {
$result = $this->mDb->CacheGetRow( $pCacheTime, $pQuery, $pValues );
}
$this->queryComplete();
return $result;
}
function queryStart() {
global $gDebug;
if( $gDebug ) {
global $gBitSystem;
$this->mQueryLap = $gBitSystem->mTimer->elapsed();
$this->mDb->debug = 99;
}
}
function queryComplete() {
global $num_queries;
//count the number of queries made
$num_queries++;
$this->mNumQueries++;
global $gDebug;
if( $gDebug ) {
global $gBitSystem;
$interval = $gBitSystem->mTimer->elapsed() - $this->mQueryLap;
$style = ( $interval > .5 ) ? 'color:red;' : (( $interval > .15 ) ? 'color:orange;' : '');
print '<p style="'.$style.'">### Query: '.$num_queries.' Start time: '.$this->mQueryLap.' ### Query run time: '.$interval.'</p>';
$this->mQueryLap = 0;
}
}
/** Returns a single column value from the database.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pReportErrors report errors to STDOUT
* @param pOffset the row number to begin returning rows from.
* @return the associative array, or false if an error occurs
*/
function getOne($pQuery, $pValues, $pNumRows, $pOffset, $pCacheTime = BIT_QUERY_DEFAULT )
{
$result = $this->query($pQuery, $pValues, 1, $pOffset, $pCacheTime );
$res = ($result != NULL) ? $result->fetchRow() : false;
if ($res === false)
{
return (NULL);
//simulate pears behaviour
}
//$this->debugger_log($pQuery, $pValues);
list($key, $value) = each($res);
return $value;
}
// This function will take a set of fields identified by an associative array - $insertData
// generate a suitable SQL script
// and insert the data into the specified table - $insertTable
function associateInsert( $insertTable, $insertData ) {
$setSql = ( '`'.implode( array_keys( $insertData ), '`, `' ).'`' );
//stupid little loop to generate question marks. Start at one, and tack at the end to ease dealing with comma
$valueSql = '';
for( $i = 1; $i < count( $insertData ); $i++ ) {
$valueSql .= '?, ';
}
$valueSql .= '?';
$query = "INSERT INTO `$insertTable` ( $setSql ) VALUES ( $valueSql )";
$result = $this->query( $query, array_values( $insertData ) );
}
// This function will take a set of fields identified by an associative array - $updateData
// generate a suitable SQL script
// update the data into the specified table - $insertTable
// at the location identified in updateId which holds a name and value entry
function associateUpdate( $updateTable, $updateData, $updateId ) {
$setSql = ( '`'.implode( array_keys( $updateData ), '`=?, `' ).'`=?' );
$bindVars = array_values( $updateData );
array_push( $bindVars, $updateId["value"] );
$query = "UPDATE `$updateTable` SET $setSql WHERE `".$updateId["name"]."`=?";
$result = $this->query( $query, $bindVars );
}
function GenID( $pSequenceName ) {
if( empty( $this->mDb ) ) {
return FALSE;
}
return $this->mDb->GenID( str_replace("`","",BIT_DB_PREFIX).$pSequenceName );
}
/** A database portable IFNULL function.
* @param pField argument to compare to NULL
* @param pNullRepl the NULL replacement value
* @return a string that represents the function that checks whether
* $pField is NULL for the given database, and if NULL, change the
* value returned to $pNullRepl.
*/
function ifNull($pField, $pNullRepl)
{
return $this->mDb->ifNull($pField, $pNullRepl);
}
/** Format the timestamp in the format the database accepts.
* @param pDate a Unix integer timestamp or an ISO format Y-m-d H:i:s
* @return the timestamp as a quoted string.
* @todo could be used to later convert all int timestamps into db
* timestamps. Currently not used anywhere.
*/
function ts($pDate)
{
// not sure what this did - maybe someone can comment why its here
//return preg_replace("/'/","", $this->mDb->DBTimeStamp($pDate));
return $this->mDb->DBTimeStamp($pDate);
}
/** Converts backtick (`) quotes to the appropriate quote for the
* database.
* @private
* @param pQuery the SQL query using backticks (`)
* @return the correctly quoted SQL statement
* @todo investigate replacement by AdoDB NameQuote() function
*/
function convertQuery(&$pQuery)
{
if( !empty( $this->mType ) ) {
switch ($this->mType) {
case "oci8":
$pQuery = preg_replace("/`/", "\"", $pQuery);
// convert bind variables - adodb does not do that
$qe = explode("?", $pQuery);
$pQuery = "";
for ($i = 0;
$i < sizeof($qe) - 1;
$i++)
{
$pQuery .= $qe[$i] . ":" . $i;
}
$pQuery .= $qe[$i];
break;
case "firebird":
case "mssql":
case "pgsql":
case "sybase":
$pQuery = preg_replace("/`/", "\"", $pQuery);
break;
case "sqlite":
$pQuery = preg_replace("/`/", "", $pQuery);
break;
}
}
}
/** Converts field sorting abbreviation to SQL
* @param pSortMode fieldname and sort order string (eg name_asc)
* @return the correctly quoted SQL ORDER statement
*/
function convert_sortmode($pSortMode) {
/* functionMap allows us to things like sort by random rows. If the
sortMode requested is 'random' it will insert the properly named
db-specific function to achieve this. - ATS
*/
$pSortMode = preg_replace( '/lastModif/', 'last_modified', $pSortMode );
$pSortMode = preg_replace( '/pageName/', 'title', $pSortMode );
$pSortMode = preg_replace( '/^user_/', 'login_', $pSortMode );
$bIsFunction = FALSE;
$functionMap = array( 'random' => array("pgsql" => "RANDOM()",
"mysql3" => "RAND()",
"mysql" => "RAND()",
"mssql" => "NEWID()",
"firebird" => "1"));
//"oci8" => "" STILL NEEDED
//"sqlite" => "" STILL NEEDED
//"sybase" => "" STILL NEEDED
foreach ($functionMap as $funcName=>$funcMethods) {
if ($pSortMode == $funcName) {
if (in_array($this->mType, array_keys($funcMethods))) {
$pSortMode = $funcMethods[$this->mType];
$bIsFunction = TRUE;
}
}
}
if (!$bIsFunction) {
switch ($this->mType)
{
case "firebird":
if ( $pSortMode == 'page_name_asc' ) $pSortMode = 'title_asc';
if ( $pSortMode == 'page_name_desc' ) $pSortMode = 'title_desc';
case "oci8":
case "sybase":
case "mssql":
case "sqlite":
case "mysql3":
case "pgsql":
case "mysql":
default:
$pSortMode = preg_replace("/_asc$/", "` ASC", $pSortMode);
$pSortMode = preg_replace("/_desc$/", "` DESC", $pSortMode);
$pSortMode = str_replace(",", "`,`",$pSortMode);
if( strpos( $pSortMode, '.' ) ) {
$pSortMode = str_replace(".", ".`",$pSortMode);
} else {
$pSortMode = "`" . $pSortMode;
}
break;
}
}
return $pSortMode;
}
/** Returns the keyword to force a column comparison to be case sensitive
* for none case-sensitive databases (eg MySQL)
* @return the SQL keyword
* @todo only used in gBitSystem and users_lib to compare login names
*/
function convert_binary()
{
switch ($this->mType)
{
case "oci8":
case "firebird":
case "sqlite":
break;
case "mysql3":
case "mysql":
return "BINARY";
break;
}
}
/** Used to cast variable types for certain databases (ie SyBase & MSSQL)
* @param pVar the variable value to cast
* @param pType the current variable type
* @return the SQL casting statement
*/
function sql_cast($pVar,$pType)
{
switch ($this->mType)
{
case "sybase":
case "mssql":
switch ($pType)
{
case "int":
return " CONVERT(numeric(14,0),$pVar) ";
break;
case "string":
return " CONVERT(varchar(255),$pVar) ";
break;
case "float":
return " CONVERT(numeric(10,5),$pVar) ";
break;
}
break;
default:
return($pVar);
break;
}
}
/** will activate ADODB's native debugging output
* @param pLevel debugging level - FALSE is off, TRUE is on, 99 is verbose
**/
function debug( $pLevel=99 ) {
if( is_object( $this->mDb ) ) {
$this->mDb->debug = $pLevel;
}
}
/**
*
*/
function debugger_log($query, $values)
{
// Will spam only if debug parameter present in URL
// \todo DON'T FORGET TO REMOVE THIS BEFORE 1.8 RELEASE
if (!isset($_REQUEST["debug"])) return;
// spam to debugger log
include_once( DEBUG_PKG_PATH.'debugger.php' );
global $debugger;
if (is_array($values) && strpos($query, '?'))
foreach ($values as $v)
{
$q = strpos($query, '?');
if ($q)
{
$tmp = substr($query, 0, $q)."'".$v."'".substr($query, $q + 1);
$query = $tmp;
}
}
$debugger->msg($this->num_queries.': '.$query);
}
/**
* Used to encode blob data (eg PostgreSQL)
* @todo had a lot of trouble with AdoDB BlobEncode and BlobDecode
* the code works but will need work for dbs other than PgSQL
* @param pData a string of raw blob data
* @return escaped blob data
*/
function db_byte_encode( &$pData ) {
global $ADODB_LASTDB;
switch ($ADODB_LASTDB) {
case "postgres7":
case "pgsql":
$search = array(chr(92), chr(0), chr(39));
$replace = array('\\\134', '\\\000', '\\\047');
$ret = str_replace($search, $replace, $pData);
break;
default:
$ret = &$pData;
break;
}
return $ret;
}
/**
* Used to decode blob data (eg PostgreSQL)
* @todo had a lot of trouble with AdoDB BlobEncode and BlobDecode
* the code works but will need work for dbs other than PgSQL
* @param pData escaped blob data
* @return a string of raw blob data
*/
function db_byte_decode( &$pData ) {
global $ADODB_LASTDB;
switch ($ADODB_LASTDB) {
case "postgres7":
case "pgsql":
$ret = stripcslashes( $pData );
break;
default:
$ret = &$pData;
break;
}
return $ret;
}
function StartTrans() {
return $this->mDb->StartTrans();
}
function CompleteTrans() {
return $this->mDb->CompleteTrans();
}
function RollbackTrans() {
return $this->mDb->RollbackTrans();
}
function MetaTables( $ttype = false, $showSchema = false, $mask=false ) {
return $this->mDb->MetaTables( $ttype, $showSchema, $mask );
}
}
?>
|