11 require_once(
'AwlDatabase.php');
40 function _awl_connect_configured_database() {
41 global $c, $_awl_dbconn;
48 if ( isset($c->db_connect) ) {
49 $connection_strings = $c->db_connect;
51 elseif ( isset($c->pg_connect) ) {
52 $connection_strings = $c->pg_connect;
55 foreach( $connection_strings AS $k => $v ) {
60 if ( isset($v[
'dbuser']) ) $dbuser = $v[
'dbuser'];
61 if ( isset($v[
'dbpass']) ) $dbpass = $v[
'dbpass'];
63 elseif ( preg_match(
'/^(\S+:)?(.*)( user=(\S+))?( password=(\S+))?$/', $v, $matches ) ) {
65 if ( isset($matches[1]) && $matches[1] !=
'' ) {
66 $dsn = $matches[1] . $dsn;
69 $dsn =
'pgsql:' . $dsn;
71 if ( isset($matches[4]) && $matches[4] !=
'' ) $dbuser = $matches[4];
72 if ( isset($matches[6]) && $matches[6] !=
'' ) $dbpass = $matches[6];
74 if ( $_awl_dbconn =
new AwlDatabase( $dsn, $dbuser, $dbpass, (isset($c->use_persistent) && $c->use_persistent ? array(PDO::ATTR_PERSISTENT =>
true) :
null) ) )
break;
77 if ( ! $_awl_dbconn ) {
79 <html><head><title>Database Connection Failure</title></head><body>
80 <h1>Database Error</h1>
81 <h3>Could not connect to database</h3>
88 if ( isset($c->db_schema) && $c->db_schema !=
'' ) {
89 $_awl_dbconn->SetSearchPath( $c->db_schema .
',public' );
92 $c->_awl_dbversion = $_awl_dbconn->GetVersion();
126 protected $connection;
132 protected $querystring;
138 protected $bound_querystring;
144 protected $bound_parameters;
162 protected $rownum =
null;
174 protected $error_info;
181 protected $execution_time;
203 public $query_time_warning = 5;
214 global $_awl_dbconn, $c;
216 $this->execution_time = 0;
217 $this->error_info =
null;
218 if ( isset($c->default_query_warning_threshold) ) {
219 $this->query_time_warning = $c->default_query_warning_threshold;
223 if ( isset($_awl_dbconn) ) $this->connection = $_awl_dbconn;
224 else $this->connection =
null;
226 $argc = func_num_args();
227 $args = func_get_args();
229 $this->querystring = array_shift($args);
231 if ( is_array($args[0]) )
232 $this->bound_parameters = $args[0];
234 $this->bound_parameters = $args;
247 if ( is_string($new_connection) || is_array($new_connection) ) {
250 if ( is_array($new_connection) ) {
251 $dsn = $new_connection[
'dsn'];
252 if ( isset($new_connection[
'dbuser']) ) $dbuser = $new_connection[
'dbuser'];
253 if ( isset($new_connection[
'dbpass']) ) $dbpass = $new_connection[
'dbpass'];
255 elseif ( preg_match(
'/^(\S+:)?(.*)( user=(\S+))?( password=(\S+))?$/', $new_connection, $matches ) ) {
257 if ( isset($matches[1]) && $matches[1] !=
'' ) {
258 $dsn = $matches[1] . $dsn;
261 $dsn =
'pgsql:' . $dsn;
263 if ( isset($matches[4]) && $matches[4] !=
'' ) $dbuser = $matches[4];
264 if ( isset($matches[6]) && $matches[6] !=
'' ) $dbpass = $matches[6];
266 if ( ! $new_connection =
new AwlDatabase( $dsn, $dbuser, $dbpass, $options ) )
return;
268 $this->connection = $new_connection;
269 return $new_connection;
278 return $this->connection;
294 function _log_query( $locn, $tag, $string, $line = 0, $file =
"") {
296 $string = preg_replace(
'/\s+/',
' ', $string);
298 if ( ($tag ==
'QF' || $tag ==
'SQ') && ( $line != 0 && $file !=
"" ) ) {
299 dbg_error_log(
"LOG-$locn",
" Query: %s: %s in '%s' on line %d", ($tag ==
'QF' ?
'Error' :
'Possible slow query'), $tag, $file, $line );
302 while( strlen( $string ) > 0 ) {
303 dbg_error_log(
"LOG-$locn",
" Query: %s: %s", $tag, substr( $string, 0, 240) );
304 $string = substr(
"$string", 240 );
316 public static function quote($str =
null) {
318 if ( !isset($_awl_dbconn) ) {
319 _awl_connect_configured_database();
321 return $_awl_dbconn->Quote($str);
336 $argc = func_num_args();
337 $args = func_get_args();
340 if ( gettype($args[0]) ==
'array' ) {
341 $this->bound_parameters = $args[0];
344 $this->bound_parameters[] = $args[0];
348 $this->bound_parameters[$args[0]] = $args[1];
359 if ( isset($this->sth) )
return;
360 if ( isset($c->expand_pdo_parameters) && $c->expand_pdo_parameters )
return;
362 if ( !isset($this->connection) ) {
363 _awl_connect_configured_database();
364 $this->connection = $GLOBALS[
'_awl_dbconn'];
367 $this->sth = $this->connection->prepare( $this->querystring );
369 if ( ! $this->sth ) {
370 $this->error_info = $this->connection->errorInfo();
372 else $this->error_info =
null;
381 if ( !isset($this->connection) ) {
382 _awl_connect_configured_database();
383 $this->connection = $GLOBALS[
'_awl_dbconn'];
385 if ( !is_object($this->connection) )
throw new Exception(
'Database not connected.');
387 if ( isset($c->expand_pdo_parameters) && $c->expand_pdo_parameters ) {
388 $this->bound_querystring = $this->querystring;
389 if ( isset($this->bound_parameters) ) {
390 $this->bound_querystring = $this->connection->ReplaceParameters($this->querystring,$this->bound_parameters);
395 $t1 = microtime(
true);
396 $execute_result = $this->sth = $this->connection->query($this->bound_querystring);
399 $t1 = microtime(
true);
400 $execute_result = $this->sth = $this->connection->prepare($this->querystring);
401 if ( $this->sth ) $execute_result = $this->sth->execute($this->bound_parameters);
405 $this->bound_querystring =
null;
407 if ( $execute_result ===
false ) {
408 $this->error_info = $this->connection->errorInfo();
411 $this->
rows = $this->sth->rowCount();
413 $i_took = microtime(
true) - $t1;
414 $c->total_query_time += $i_took;
415 $this->execution_time = sprintf(
"%2.06lf", $i_took);
417 $this->error_info =
null;
426 return $this->querystring;
434 return $this->bound_parameters;
450 return $this->rownum;
461 if ( !isset($this->connection) ) {
462 if ( !isset($_awl_dbconn) ) _awl_connect_configured_database();
463 $this->connection = $_awl_dbconn;
465 return $this->connection->TransactionState();
474 if ( !isset($this->connection) ) {
475 if ( !isset($_awl_dbconn) ) _awl_connect_configured_database();
476 $this->connection = $_awl_dbconn;
478 return $this->connection->Begin();
486 if ( !isset($this->connection) ) {
487 trigger_error(
"Cannot commit a transaction without an active statement.", E_USER_ERROR);
489 return $this->connection->Commit();
497 if ( !isset($this->connection) ) {
498 trigger_error(
"Cannot rollback a transaction without an active statement.", E_USER_ERROR);
500 return $this->connection->Rollback();
510 $this->execution_time = 0;
511 $this->error_info =
null;
513 $this->bound_parameters =
null;
514 $this->bound_querystring =
null;
517 $this->querystring = $sql;
529 $argc = func_num_args();
530 $args = func_get_args();
532 $this->
SetSql( array_shift($args) );
534 if ( is_array($args[0]) )
535 $this->bound_parameters = $args[0];
537 $this->bound_parameters = $args;
540 return $this->
Exec();
561 function Exec( $location =
null, $line =
null, $file =
null ) {
563 if ( isset($location) ) $this->location = trim($location);
564 if ( !isset($this->location) || $this->location ==
"" ) $this->location = substr($_SERVER[
'PHP_SELF'],1);
566 if ( isset($line) ) $this->location_line = intval($line);
567 else if ( isset($this->location_line) ) $line = $this->location_line;
569 if ( isset($file) ) $this->location_file = trim($file);
570 else if ( isset($this->location_file) ) $file = $this->location_file;
572 if ( isset($c->dbg[
'querystring']) || isset($c->dbg[
'ALL']) ) {
573 $this->
_log_query( $this->location,
'DBGQ', $this->querystring, $line, $file );
574 if ( isset($this->bound_parameters) && !isset($this->sth) ) {
575 foreach( $this->bound_parameters AS $k => $v ) {
576 $this->
_log_query( $this->location,
'DBGQ', sprintf(
' "%s" => "%s"', $k, $v), $line, $file );
581 if ( isset($this->bound_parameters) ) {
589 $this->errorstring = sprintf(
'SQL error "%s" - %s"', $this->error_info[0], (isset($this->error_info[2]) ? $this->error_info[2] :
''));
590 if ( isset($c->dbg[
'print_query_errors']) && $c->dbg[
'print_query_errors'] ) {
591 printf(
"\n=====================\n" );
592 printf(
"%s[%d] QF: %s\n", $file, $line, $this->errorstring);
593 printf(
"%s\n", $this->querystring );
594 if ( isset($this->bound_parameters) ) {
595 foreach( $this->bound_parameters AS $k => $v ) {
596 printf(
" %-18s \t=> '%s'\n",
"'$k'", $v );
599 printf(
".....................\n" );
601 $this->
_log_query( $this->location,
'QF', $this->errorstring, $line, $file );
602 $this->
_log_query( $this->location,
'QF', $this->querystring, $line, $file );
603 if ( isset($this->bound_parameters) && ! ( isset($c->dbg[
'querystring']) || isset($c->dbg[
'ALL']) ) ) {
604 foreach( $this->bound_parameters AS $k => $v ) {
605 dbg_error_log(
'LOG-'.$this->location,
' Query: QF: "%s" => "%s"', $k, $v);
609 elseif ( $this->execution_time > $this->query_time_warning ) {
611 $this->
_log_query( $this->location,
'SQ',
"Took: $this->execution_time for $this->querystring", $line, $file );
613 elseif ( isset($c->dbg[
'querystring']) || isset($c->dbg[strtolower($this->location)]) || isset($c->dbg[
'ALL']) ) {
615 $this->
_log_query( $this->location,
'DBGQ',
"Took: $this->execution_time to find $this->rows rows.", $line, $file );
629 if ( ! $this->sth || $this->
rows == 0 )
return false;
631 if ( ($this->
rownum + 1) >= $this->
rows )
return false;
634 $row = $this->sth->fetch( ($as_array ? PDO::FETCH_NUM : PDO::FETCH_OBJ) );
644 return $this->error_info;
655 $old = $this->query_time_warning;
656 $this->query_time_warning = $new_threshold;
SetConnection( $new_connection, $options=null)
Exec( $location=null, $line=null, $file=null)
_log_query( $locn, $tag, $string, $line=0, $file="")
SetSlowQueryThreshold( $new_threshold)