Skip to content

Commit

Permalink
Encapsulate some db operations in try-catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
arnemorken committed Dec 13, 2023
1 parent 1cb3311 commit 67756de
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions data/mysql/db/dbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ public function dbCreate($type,$tableFields,$unique)
if (isset($unique))
$sql .= ",UNIQUE KEY(".$unique.")";
$sql .= ")";
//error_log("dbCreate,sql:$sql");
if ($this->query($sql)) {
error_log("Table $tableName created successfully");
return true;
error_log("dbCreate,sql:$sql");
try {
if ($this->query($sql)) {
error_log("Table $tableName created successfully");
return true;
}
$this->mError = "dbTable::dbCreate: Error creating table $tableName:".$this->getError();
error_log($this->mError);
return false;
}
catch (Exception $e) {
$this->mError = "dbCreate::query: Exception: ".$e->getMessage();
$this->mError .= "\nQuery was: ".$sql;
error_log($this->mError);
}
$this->mError = "dbTable::dbCreate: Error creating table $tableName:".$this->getError();
error_log($this->mError);
return false;
} // dbCreate

/**
Expand All @@ -195,28 +202,35 @@ public function query($stmt)
return false;
}
//error_log("dbTable.query:".$stmt);
if (substr($stmt,0,6) == "SELECT") {
try {
$this->mDBResult = $this->mDBConnection->mDBHandle->query($stmt);
if (!$this->mDBResult) {
$this->mError = "dbTable::query3: ".$this->mDBConnection->getError();
$this->mError .= "<br/>\nQuery was: ".$stmt;
try {
if (substr($stmt,0,6) == "SELECT") {
try {
$this->mDBResult = $this->mDBConnection->mDBHandle->query($stmt);
if (!$this->mDBResult) {
$this->mError = "dbTable::query3: ".$this->mDBConnection->getError();
$this->mError .= "<br/>\nQuery was: ".$stmt;
error_log($this->mError);
}
}
catch (Exception $e) {
$this->mError = "dbTable::query4: Exception: ".$e->getMessage();
$this->mError .= "\nQuery was: ".$stmt;
error_log($this->mError);
}
}
catch (Exception $e) {
$this->mError = "dbTable::query4: Exception: ".$e->getMessage();
$this->mError .= "\nQuery was: ".$stmt;
error_log($this->mError);
else {
$this->mNumRowsChanged = $this->mDBConnection->mDBHandle->exec($stmt);
if ($this->mNumRowsChanged === false) {
$this->mError = "dbTable::query5: ".$this->mDBConnection->getError();
$this->mError .= "\nQuery was: ".$stmt;
error_log($this->mError);
}
}
}
else {
$this->mNumRowsChanged = $this->mDBConnection->mDBHandle->exec($stmt);
if ($this->mNumRowsChanged === false) {
$this->mError = "dbTable::query5: ".$this->mDBConnection->getError();
catch (Exception $e) {
$this->mError = "query::query: Exception: ".$e->getMessage();
$this->mError .= "\nQuery was: ".$stmt;
error_log($this->mError);
}
}
return !$this->isError(); // Return true if ok
} // query
Expand Down

0 comments on commit 67756de

Please sign in to comment.