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;
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 );
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;
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 );