function q($sql) { if (!$this->db || !$this->connected) { return false; } if (!strpos($sql, ';')) { $sql .= ';'; } if (strpos($sql, '`')) { // this is a hack. quoted identifiers should be replaced everywhere in the code with dbesc_identifier(), remove this once it is $sql = str_replace('`', '"', $sql); } $this->error = ''; $result = @pg_query($this->db, $sql); if (file_exists('db-allqueries.out')) { $bt = debug_backtrace(); $trace = array(); foreach ($bt as $frame) { if (!empty($frame['file']) && @strstr($frame['file'], $_SERVER['DOCUMENT_ROOT'])) { $frame['file'] = substr($frame['file'], strlen($_SERVER['DOCUMENT_ROOT']) + 1); } $trace[] = $frame['file'] . ':' . $frame['function'] . '():' . $frame['line']; } $compact = join(', ', $trace); file_put_contents('db-allqueries.out', datetime_convert() . ": " . $sql . ' is_resource: ' . var_export(is_resource($result), true) . ', backtrace: ' . $compact . "\n\n", FILE_APPEND); } if ($result === false) { $this->error = pg_last_error($this->db); } if ($result === false || $this->error) { //db_logger('dba_postgres: ' . printable($sql) . ' returned false.' . "\n" . $this->error); if (file_exists('dbfail.out')) { file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND); } } if ($result === true || $result === false) { return $result; } if (pg_result_status($result) == PGSQL_COMMAND_OK) { return true; } $r = array(); if (pg_num_rows($result)) { while ($x = pg_fetch_array($result, null, PGSQL_ASSOC)) { $r[] = $x; } pg_free_result($result); if ($this->debug) { db_logger('dba_postgres: ' . printable(print_r($r, true))); } } return $r; }
function q($sql) { if (!$this->db || !$this->connected) { return false; } $this->error = ''; $select = stripos($sql, 'select') === 0 ? true : false; try { $result = $this->db->query($sql); } catch (PDOException $e) { $this->error = $e->getMessage(); if ($this->error) { db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR); if (file_exists('dbfail.out')) { file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND); } } } if (!$select) { if ($this->debug) { db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . ($result ? 'true' : 'false'), LOGGER_NORMAL, $result ? LOG_INFO : LOG_ERR); } return $result; } if ($this->debug) { db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); } $r = array(); if ($result) { foreach ($result as $x) { $r[] = $x; } if ($this->debug) { db_logger('dba_pdo: ' . printable(print_r($r, true)), LOGGER_NORMAL, LOG_INFO); } } return $r; }
function q($sql) { if (!$this->db || !$this->connected) { return false; } $this->error = ''; $result = $this->db->query($sql); if ($this->db->errno) { $this->error = $this->db->error; } if ($this->error) { db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR); if (file_exists('dbfail.out')) { file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND); } } if ($result === true || $result === false) { if ($this->debug) { db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . ($result ? 'true' : 'false'), LOGGER_NORMAL, $result ? LOG_INFO : LOG_ERR); } return $result; } if ($this->debug) { db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . $result->num_rows . ' results.', LOGGER_NORMAL, LOG_INFO); } $r = array(); if ($result->num_rows) { while ($x = $result->fetch_array(MYSQLI_ASSOC)) { $r[] = $x; } $result->free_result(); if ($this->debug) { db_logger('dba_mysqli: ' . printable(print_r($r, true)), LOGGER_NORMAL, LOG_INFO); } } return $r; }
function db_getfunc($f) { $lookup = array('rand' => array(DBTYPE_MYSQL => 'RAND()', DBTYPE_POSTGRES => 'RANDOM()'), 'utc_timestamp' => array(DBTYPE_MYSQL => 'UTC_TIMESTAMP()', DBTYPE_POSTGRES => "now() at time zone 'UTC'"), 'regexp' => array(DBTYPE_MYSQL => 'REGEXP', DBTYPE_POSTGRES => '~'), '^' => array(DBTYPE_MYSQL => '^', DBTYPE_POSTGRES => '#')); $f = strtolower($f); if (isset($lookup[$f]) && isset($lookup[$f][ACTIVE_DBTYPE])) { return $lookup[$f][ACTIVE_DBTYPE]; } db_logger('Unable to abstract DB function "' . $f . '" for dbtype ' . ACTIVE_DBTYPE, LOGGER_DEBUG, LOG_ERR); return $f; }