11require_once(
'AwlDatabase.php');
40function _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;
194 public $location_line;
195 public $location_file;
205 public $query_time_warning = 5;
216 global $_awl_dbconn, $c;
218 $this->execution_time = 0;
219 $this->error_info =
null;
220 if ( isset($c->default_query_warning_threshold) ) {
221 $this->query_time_warning = $c->default_query_warning_threshold;
225 if ( isset($_awl_dbconn) ) $this->connection = $_awl_dbconn;
226 else $this->connection =
null;
228 $argc = func_num_args();
229 $args = func_get_args();
231 $this->querystring = array_shift($args);
233 if ( is_array($args[0]) )
234 $this->bound_parameters = $args[0];
236 $this->bound_parameters = $args;
249 if ( is_string($new_connection) || is_array($new_connection) ) {
252 if ( is_array($new_connection) ) {
253 $dsn = $new_connection[
'dsn'];
254 if ( isset($new_connection[
'dbuser']) ) $dbuser = $new_connection[
'dbuser'];
255 if ( isset($new_connection[
'dbpass']) ) $dbpass = $new_connection[
'dbpass'];
257 elseif ( preg_match(
'/^(\S+:)?(.*)( user=(\S+))?( password=(\S+))?$/', $new_connection, $matches ) ) {
259 if ( isset($matches[1]) && $matches[1] !=
'' ) {
260 $dsn = $matches[1] . $dsn;
263 $dsn =
'pgsql:' . $dsn;
265 if ( isset($matches[4]) && $matches[4] !=
'' ) $dbuser = $matches[4];
266 if ( isset($matches[6]) && $matches[6] !=
'' ) $dbpass = $matches[6];
268 if ( ! $new_connection =
new AwlDatabase( $dsn, $dbuser, $dbpass, $options ) )
return;
270 $this->connection = $new_connection;
271 return $new_connection;
280 return $this->connection;
296 function _log_query( $locn, $tag, $string, $line = 0, $file =
"") {
298 $string = preg_replace(
'/\s+/',
' ', $string);
300 if ( ($tag ==
'QF' || $tag ==
'SQ') && ( $line != 0 && $file !=
"" ) ) {
301 dbg_error_log(
"LOG-$locn",
" Query: %s: %s in '%s' on line %d", ($tag ==
'QF' ?
'Error' :
'Possible slow query'), $tag, $file, $line );
304 while( strlen( $string ) > 0 ) {
305 dbg_error_log(
"LOG-$locn",
" Query: %s: %s", $tag, substr( $string, 0, 240) );
306 $string = substr(
"$string", 240 );
318 public static function quote($str =
null) {
320 if ( !isset($_awl_dbconn) ) {
321 _awl_connect_configured_database();
323 return $_awl_dbconn->Quote($str);
338 $argc = func_num_args();
339 $args = func_get_args();
342 if ( gettype($args[0]) ==
'array' ) {
343 $this->bound_parameters = $args[0];
346 $this->bound_parameters[] = $args[0];
350 $this->bound_parameters[$args[0]] = $args[1];
361 if ( isset($this->sth) )
return;
362 if ( isset($c->expand_pdo_parameters) && $c->expand_pdo_parameters )
return;
364 if ( !isset($this->connection) ) {
365 _awl_connect_configured_database();
366 $this->connection = $GLOBALS[
'_awl_dbconn'];
369 $this->sth = $this->connection->prepare( $this->querystring );
371 if ( ! $this->sth ) {
372 $this->error_info = $this->connection->errorInfo();
374 else $this->error_info =
null;
383 if ( !isset($this->connection) ) {
384 _awl_connect_configured_database();
385 $this->connection = $GLOBALS[
'_awl_dbconn'];
387 if ( !is_object($this->connection) )
throw new Exception(
'Database not connected.');
389 if ( isset($c->expand_pdo_parameters) && $c->expand_pdo_parameters ) {
390 $this->bound_querystring = $this->querystring;
391 if ( isset($this->bound_parameters) ) {
392 $this->bound_querystring = $this->connection->ReplaceParameters($this->querystring,$this->bound_parameters);
397 $t1 = microtime(
true);
398 $execute_result = $this->sth = $this->connection->query($this->bound_querystring);
401 $t1 = microtime(
true);
402 $execute_result = $this->sth = $this->connection->prepare($this->querystring);
403 if ( $this->sth ) $execute_result = $this->sth->execute($this->bound_parameters);
407 $this->bound_querystring =
null;
409 if ( $execute_result ===
false ) {
410 $this->error_info = $this->connection->errorInfo();
413 $this->
rows = $this->sth->rowCount();
415 $i_took = microtime(
true) - $t1;
416 $c->total_query_time += $i_took;
417 $this->execution_time = sprintf(
"%2.06lf", $i_took);
419 $this->error_info =
null;
428 return $this->querystring;
436 return $this->bound_parameters;
452 return $this->rownum;
463 if ( !isset($this->connection) ) {
464 if ( !isset($_awl_dbconn) ) _awl_connect_configured_database();
465 $this->connection = $_awl_dbconn;
467 return $this->connection->TransactionState();
476 if ( !isset($this->connection) ) {
477 if ( !isset($_awl_dbconn) ) _awl_connect_configured_database();
478 $this->connection = $_awl_dbconn;
480 return $this->connection->Begin();
488 if ( !isset($this->connection) ) {
489 trigger_error(
"Cannot commit a transaction without an active statement.", E_USER_ERROR);
491 return $this->connection->Commit();
499 if ( !isset($this->connection) ) {
500 trigger_error(
"Cannot rollback a transaction without an active statement.", E_USER_ERROR);
502 return $this->connection->Rollback();
512 $this->execution_time = 0;
513 $this->error_info =
null;
515 $this->bound_parameters =
null;
516 $this->bound_querystring =
null;
519 $this->querystring = $sql;
531 $argc = func_num_args();
532 $args = func_get_args();
534 $this->
SetSql( array_shift($args) );
536 if ( is_array($args[0]) )
537 $this->bound_parameters = $args[0];
539 $this->bound_parameters = $args;
542 return $this->
Exec();
563 function Exec( $location =
null, $line =
null, $file =
null ) {
565 if ( isset($location) ) $this->location = trim($location);
566 if ( !isset($this->location) || $this->location ==
"" ) $this->location = substr($_SERVER[
'PHP_SELF'],1);
568 if ( isset($line) ) $this->location_line = intval($line);
569 else if ( isset($this->location_line) ) $line = $this->location_line;
571 if ( isset($file) ) $this->location_file = trim($file);
572 else if ( isset($this->location_file) ) $file = $this->location_file;
574 if ( isset($c->dbg[
'querystring']) || isset($c->dbg[
'ALL']) ) {
575 $this->
_log_query( $this->location,
'DBGQ', $this->querystring, $line, $file );
576 if ( isset($this->bound_parameters) && !isset($this->sth) ) {
577 foreach( $this->bound_parameters AS $k => $v ) {
578 $this->
_log_query( $this->location,
'DBGQ', sprintf(
' "%s" => "%s"', $k, $v), $line, $file );
583 if ( isset($this->bound_parameters) ) {
591 $this->errorstring = sprintf(
'SQL error "%s" - %s"', $this->error_info[0], (isset($this->error_info[2]) ? $this->error_info[2] :
''));
592 if ( isset($c->dbg[
'print_query_errors']) && $c->dbg[
'print_query_errors'] ) {
593 printf(
"\n=====================\n" );
594 printf(
"%s[%d] QF: %s\n", $file, $line, $this->errorstring);
595 printf(
"%s\n", $this->querystring );
596 if ( isset($this->bound_parameters) ) {
597 foreach( $this->bound_parameters AS $k => $v ) {
598 printf(
" %-18s \t=> '%s'\n",
"'$k'", $v );
601 printf(
".....................\n" );
603 $this->
_log_query( $this->location,
'QF', $this->errorstring, $line, $file );
604 $this->
_log_query( $this->location,
'QF', $this->querystring, $line, $file );
605 if ( isset($this->bound_parameters) && ! ( isset($c->dbg[
'querystring']) || isset($c->dbg[
'ALL']) ) ) {
606 foreach( $this->bound_parameters AS $k => $v ) {
607 dbg_error_log(
'LOG-'.$this->location,
' Query: QF: "%s" => "%s"', $k, $v);
611 elseif ( $this->execution_time > $this->query_time_warning ) {
613 $this->
_log_query( $this->location,
'SQ',
"Took: $this->execution_time for $this->querystring", $line, $file );
615 elseif ( isset($c->dbg[
'querystring']) || isset($c->dbg[strtolower($this->location)]) || isset($c->dbg[
'ALL']) ) {
617 $this->
_log_query( $this->location,
'DBGQ',
"Took: $this->execution_time to find $this->rows rows.", $line, $file );
631 if ( ! $this->sth || $this->
rows == 0 )
return false;
633 if ( ($this->
rownum + 1) >= $this->
rows )
return false;
636 $row = $this->sth->fetch( ($as_array ? PDO::FETCH_NUM : PDO::FETCH_OBJ) );
646 return $this->error_info;
657 $old = $this->query_time_warning;
658 $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)