New function to check for the existance of data

This commit is contained in:
Michael 2017-04-24 19:23:49 +00:00
parent 0c8a2fd345
commit d67338a895

View file

@ -529,27 +529,26 @@ class dba {
switch (self::$dbo->driver) { switch (self::$dbo->driver) {
case 'pdo': case 'pdo':
$stmt = self::$dbo->db->prepare($sql); if (!$stmt = self::$dbo->db->prepare($sql)) {
$errorInfo = self::$dbo->db->errorInfo();
self::$dbo->error = $errorInfo[2];
self::$dbo->errorno = $errorInfo[1];
$retval = false;
break;
}
foreach ($args AS $param => $value) { foreach ($args AS $param => $value) {
$stmt->bindParam($param, $args[$param]); $stmt->bindParam($param, $args[$param]);
} }
$success = $stmt->execute(); if (!$stmt->execute()) {
if ($success) {
$retval = $stmt;
} else {
$retval = false;
}
$errorInfo = self::$dbo->db->errorInfo(); $errorInfo = self::$dbo->db->errorInfo();
if ($errorInfo) {
self::$dbo->error = $errorInfo[2]; self::$dbo->error = $errorInfo[2];
self::$dbo->errorno = $errorInfo[1]; self::$dbo->errorno = $errorInfo[1];
$retval = false;
} else {
$retval = $stmt;
} }
break; break;
case 'mysqli': case 'mysqli':
$stmt = self::$dbo->db->stmt_init(); $stmt = self::$dbo->db->stmt_init();
@ -614,15 +613,22 @@ class dba {
break; break;
} }
$a->save_timestamp($stamp1, 'database');
if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
$stamp2 = microtime(true); $stamp2 = microtime(true);
$duration = (float)($stamp2 - $stamp1); $duration = (float)($stamp2 - $stamp1);
$a->save_timestamp($stamp1, 'database'); if (($duration > $a->config["system"]["db_loglimit"])) {
$duration = round($duration, 3);
if (strtolower(substr($orig_sql, 0, 6)) != "select") { $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$a->save_timestamp($stamp1, "database_write"); @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
basename($backtrace[1]["file"])."\t".
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
substr($sql, 0, 2000)."\n", FILE_APPEND);
}
} }
return $retval; return $retval;
} }
@ -633,10 +639,49 @@ class dba {
* @return boolean Was the query successfull? * @return boolean Was the query successfull?
*/ */
static public function e($sql) { static public function e($sql) {
$a = get_app();
$stamp = microtime(true);
$args = func_get_args(); $args = func_get_args();
$stmt = self::p(); $stmt = call_user_func_array('self::p', $args);
if (is_bool($stmt)) {
$retval = $stmt;
} elseif (is_object($stmt)) {
$retval = true;
} else {
$retval = false;
}
self::close($stmt); self::close($stmt);
$a->save_timestamp($stamp, "database_write");
return $retval;
}
/**
* @brief Check if data exists
*
* @param string $sql SQL statement
* @return boolean Are there rows for that query?
*/
static public function exists($sql) {
$args = func_get_args();
$stmt = call_user_func_array('self::p', $args);
if (is_bool($stmt)) {
$retval = $stmt;
} else {
$retval = is_array(self::fetch($stmt));
}
self::close($stmt);
return $retval;
} }
/** /**
@ -646,6 +691,10 @@ class dba {
* @return array current row * @return array current row
*/ */
static public function fetch($stmt) { static public function fetch($stmt) {
if (!is_object($stmt)) {
return false;
}
switch (self::$dbo->driver) { switch (self::$dbo->driver) {
case 'pdo': case 'pdo':
return $stmt->fetch(PDO::FETCH_ASSOC); return $stmt->fetch(PDO::FETCH_ASSOC);
@ -696,6 +745,10 @@ class dba {
* @return boolean was the close successfull? * @return boolean was the close successfull?
*/ */
static public function close($stmt) { static public function close($stmt) {
if (!is_object($stmt)) {
return false;
}
switch (self::$dbo->driver) { switch (self::$dbo->driver) {
case 'pdo': case 'pdo':
return $stmt->closeCursor(); return $stmt->closeCursor();