Commit 746d5d34 authored by Manfred Kutas's avatar Manfred Kutas

ZP-230 Create a PDO instance once and reuse it in later calls.

Released under the Affero GNU General Public License (AGPL) version 3.
parent f25a6e62
...@@ -91,8 +91,26 @@ class SqlStateMachine implements IStateMachine { ...@@ -91,8 +91,26 @@ class SqlStateMachine implements IStateMachine {
catch(PDOException $ex) { catch(PDOException $ex) {
throw new FatalMisconfigurationException(sprintf("SqlStateMachine(): not possible to connect to the state database: %s", $ex->getMessage())); throw new FatalMisconfigurationException(sprintf("SqlStateMachine(): not possible to connect to the state database: %s", $ex->getMessage()));
} }
}
$this->clearConnection($this->dbh); /**
* Returns an existing PDO instance or creates new if necessary.
*
* @access public
* @return PDO
* @throws FatalMisconfigurationException
*/
public function getDbh() {
if (!isset($this->dbh) || $this->dbh == null) {
try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->getDbh(): create new PDO instance");
}
catch(PDOException $ex) {
throw new FatalMisconfigurationException(sprintf("SqlStateMachine()->getDbh(): not possible to connect to the state database: %s", $ex->getMessage()));
}
}
return $this->dbh;
} }
/** /**
...@@ -120,9 +138,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -120,9 +138,7 @@ class SqlStateMachine implements IStateMachine {
$sth = null; $sth = null;
$record = null; $record = null;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options); $sth = $this->getDbh()->prepare($sql);
$sth = $this->dbh->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -141,8 +157,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -141,8 +157,6 @@ class SqlStateMachine implements IStateMachine {
throw new StateNotFoundException(sprintf("SqlStateMachine->GetStateHash(): Could not locate state: %s", $ex->getMessage())); throw new StateNotFoundException(sprintf("SqlStateMachine->GetStateHash(): Could not locate state: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->GetStateHash(): return '%s'", $hash)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->GetStateHash(): return '%s'", $hash));
return $hash; return $hash;
...@@ -175,9 +189,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -175,9 +189,7 @@ class SqlStateMachine implements IStateMachine {
$sth = null; $sth = null;
$record = null; $record = null;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options); $sth = $this->getDbh()->prepare($sql);
$sth = $this->dbh->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -203,8 +215,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -203,8 +215,6 @@ class SqlStateMachine implements IStateMachine {
throw new StateNotFoundException(sprintf("SqlStateMachine->GetState(): Could not locate state: %s", $ex->getMessage())); throw new StateNotFoundException(sprintf("SqlStateMachine->GetState(): Could not locate state: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $data; return $data;
} }
...@@ -232,9 +242,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -232,9 +242,7 @@ class SqlStateMachine implements IStateMachine {
$bytes = 0; $bytes = 0;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options); $sth = $this->getDbh()->prepare($sql);
$sth = $this->dbh->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -242,14 +250,14 @@ class SqlStateMachine implements IStateMachine { ...@@ -242,14 +250,14 @@ class SqlStateMachine implements IStateMachine {
// New record // New record
$sql = "INSERT INTO zpush_states (device_id, state_type, uuid, counter, state_data, created_at, updated_at) VALUES (:devid, :type, :key, :counter, :data, :created_at, :updated_at)"; $sql = "INSERT INTO zpush_states (device_id, state_type, uuid, counter, state_data, created_at, updated_at) VALUES (:devid, :type, :key, :counter, :data, :created_at, :updated_at)";
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->bindValue(":created_at", $this->getNow(), PDO::PARAM_STR); $sth->bindValue(":created_at", $this->getNow(), PDO::PARAM_STR);
} }
else { else {
// Existing record, we update it // Existing record, we update it
$sql = "UPDATE zpush_states SET state_data = :data, updated_at = :updated_at WHERE device_id = :devid AND state_type = :type AND uuid " . (($key == null) ? " IS " : " = ") .":key AND counter = :counter"; $sql = "UPDATE zpush_states SET state_data = :data, updated_at = :updated_at WHERE device_id = :devid AND state_type = :type AND uuid " . (($key == null) ? " IS " : " = ") .":key AND counter = :counter";
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
} }
$sth->bindParam(":devid", $devid, PDO::PARAM_STR); $sth->bindParam(":devid", $devid, PDO::PARAM_STR);
...@@ -272,8 +280,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -272,8 +280,6 @@ class SqlStateMachine implements IStateMachine {
throw new FatalMisconfigurationException(sprintf("SqlStateMachine->SetState(): Could not write state: %s", $ex->getMessage())); throw new FatalMisconfigurationException(sprintf("SqlStateMachine->SetState(): Could not write state: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $bytes; return $bytes;
} }
...@@ -306,16 +312,12 @@ class SqlStateMachine implements IStateMachine { ...@@ -306,16 +312,12 @@ class SqlStateMachine implements IStateMachine {
$sth = null; $sth = null;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options); $sth = $this->getDbh()->prepare($sql);
$sth = $this->dbh->prepare($sql);
$sth->execute($params); $sth->execute($params);
} }
catch(PDOException $ex) { catch(PDOException $ex) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->CleanStates(): Error deleting states: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->CleanStates(): Error deleting states: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
} }
/** /**
...@@ -334,12 +336,10 @@ class SqlStateMachine implements IStateMachine { ...@@ -334,12 +336,10 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$changed = false; $changed = false;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT username FROM zpush_users WHERE username = :username AND device_id = :devid"; $sql = "SELECT username FROM zpush_users WHERE username = :username AND device_id = :devid";
$params = array(":username" => $username, ":devid" => $devid); $params = array(":username" => $username, ":devid" => $devid);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -351,7 +351,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -351,7 +351,7 @@ class SqlStateMachine implements IStateMachine {
$sql = "INSERT INTO zpush_users (username, device_id, created_at, updated_at) VALUES (:username, :devid, :created_at, :updated_at)"; $sql = "INSERT INTO zpush_users (username, device_id, created_at, updated_at) VALUES (:username, :devid, :created_at, :updated_at)";
$params[":created_at"] = ($createdAt != null) ? $createdAt : $this->getNow(); $params[":created_at"] = ($createdAt != null) ? $createdAt : $this->getNow();
$params[":updated_at"] = ($updatedAt != null) ? $updatedAt : $this->getNow(); $params[":updated_at"] = ($updatedAt != null) ? $updatedAt : $this->getNow();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params)) { if ($sth->execute($params)) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->LinkUserDevice(): Linked user-device: '%s' '%s'", $username, $devid)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->LinkUserDevice(): Linked user-device: '%s' '%s'", $username, $devid));
$changed = true; $changed = true;
...@@ -365,8 +365,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -365,8 +365,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->LinkUserDevice(): Error linking user-device: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->LinkUserDevice(): Error linking user-device: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $changed; return $changed;
} }
...@@ -385,12 +383,10 @@ class SqlStateMachine implements IStateMachine { ...@@ -385,12 +383,10 @@ class SqlStateMachine implements IStateMachine {
$sth = null; $sth = null;
$changed = false; $changed = false;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "DELETE FROM zpush_users WHERE username = :username AND device_id = :devid"; $sql = "DELETE FROM zpush_users WHERE username = :username AND device_id = :devid";
$params = array(":username" => $username, ":devid" => $devid); $params = array(":username" => $username, ":devid" => $devid);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params)) { if ($sth->execute($params)) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->UnLinkUserDevice(): Unlinked user-device: '%s' '%s'", $username, $devid)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->UnLinkUserDevice(): Unlinked user-device: '%s' '%s'", $username, $devid));
$changed = true; $changed = true;
...@@ -403,8 +399,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -403,8 +399,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->UnLinkUserDevice(): Error unlinking user-device: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->UnLinkUserDevice(): Error unlinking user-device: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth);
return $changed; return $changed;
} }
...@@ -421,10 +415,8 @@ class SqlStateMachine implements IStateMachine { ...@@ -421,10 +415,8 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$out = array(); $out = array();
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT device_id, username FROM zpush_users ORDER BY username"; $sql = "SELECT device_id, username FROM zpush_users ORDER BY username";
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute(); $sth->execute();
while ($record = $sth->fetch(PDO::FETCH_ASSOC)) { while ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
...@@ -438,8 +430,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -438,8 +430,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllUserDevice(): Error listing devices: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllUserDevice(): Error listing devices: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $out; return $out;
} }
...@@ -459,8 +449,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -459,8 +449,6 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$out = array(); $out = array();
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
if ($username === false) { if ($username === false) {
$sql = "SELECT DISTINCT(device_id) FROM zpush_users ORDER BY device_id"; $sql = "SELECT DISTINCT(device_id) FROM zpush_users ORDER BY device_id";
$params = array(); $params = array();
...@@ -469,7 +457,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -469,7 +457,7 @@ class SqlStateMachine implements IStateMachine {
$sql = "SELECT device_id FROM zpush_users WHERE username = :username ORDER BY device_id"; $sql = "SELECT device_id FROM zpush_users WHERE username = :username ORDER BY device_id";
$params = array(":username" => $username); $params = array(":username" => $username);
} }
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
while ($record = $sth->fetch(PDO::FETCH_ASSOC)) { while ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
...@@ -480,8 +468,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -480,8 +468,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllDevices(): Error listing devices: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllDevices(): Error listing devices: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $out; return $out;
} }
...@@ -498,12 +484,10 @@ class SqlStateMachine implements IStateMachine { ...@@ -498,12 +484,10 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$version = IStateMachine::STATEVERSION_01; $version = IStateMachine::STATEVERSION_01;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT key_value FROM zpush_settings WHERE key_name = :key_name"; $sql = "SELECT key_value FROM zpush_settings WHERE key_name = :key_name";
$params = array(":key_name" => self::VERSION); $params = array(":key_name" => self::VERSION);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -519,7 +503,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -519,7 +503,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetStateVersion(): Error getting state version: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetStateVersion(): Error getting state version: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->GetStateVersion(): supporting version '%d'", $version)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("SqlStateMachine->GetStateVersion(): supporting version '%d'", $version));
return $version; return $version;
} }
...@@ -539,12 +522,10 @@ class SqlStateMachine implements IStateMachine { ...@@ -539,12 +522,10 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$status = false; $status = false;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT key_value FROM zpush_settings WHERE key_name = :key_name"; $sql = "SELECT key_value FROM zpush_settings WHERE key_name = :key_name";
$params = array(":key_name" => self::VERSION); $params = array(":key_name" => self::VERSION);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
$record = $sth->fetch(PDO::FETCH_ASSOC); $record = $sth->fetch(PDO::FETCH_ASSOC);
...@@ -554,7 +535,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -554,7 +535,7 @@ class SqlStateMachine implements IStateMachine {
$params[":value"] = $version; $params[":value"] = $version;
$params[":updated_at"] = $this->getNow(); $params[":updated_at"] = $this->getNow();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params)) { if ($sth->execute($params)) {
$status = true; $status = true;
} }
...@@ -565,7 +546,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -565,7 +546,7 @@ class SqlStateMachine implements IStateMachine {
$params[":value"] = $version; $params[":value"] = $version;
$params[":updated_at"] = $params[":created_at"] = $this->getNow(); $params[":updated_at"] = $params[":created_at"] = $this->getNow();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params)) { if ($sth->execute($params)) {
$status = true; $status = true;
} }
...@@ -575,8 +556,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -575,8 +556,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->SetStateVersion(): Error saving state version: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->SetStateVersion(): Error saving state version: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $status; return $status;
} }
...@@ -595,12 +574,10 @@ class SqlStateMachine implements IStateMachine { ...@@ -595,12 +574,10 @@ class SqlStateMachine implements IStateMachine {
$record = null; $record = null;
$out = array(); $out = array();
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT state_type, uuid, counter FROM zpush_states WHERE device_id = :devid ORDER BY id_state"; $sql = "SELECT state_type, uuid, counter FROM zpush_states WHERE device_id = :devid ORDER BY id_state";
$params = array(":devid" => $devid); $params = array(":devid" => $devid);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
while ($record = $sth->fetch(PDO::FETCH_ASSOC)) { while ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
...@@ -626,8 +603,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -626,8 +603,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllStatesForDevice(): Error listing states: %s", $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetAllStatesForDevice(): Error listing states: %s", $ex->getMessage()));
} }
$this->clearConnection($this->dbh, $sth, $record);
return $out; return $out;
} }
...@@ -657,14 +632,12 @@ class SqlStateMachine implements IStateMachine { ...@@ -657,14 +632,12 @@ class SqlStateMachine implements IStateMachine {
$sth = null; $sth = null;
$record = null; $record = null;
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT COUNT(*) AS pcount FROM zpush_preauth_users WHERE username = :user AND device_id != 'authorized' AND authorized = 1"; $sql = "SELECT COUNT(*) AS pcount FROM zpush_preauth_users WHERE username = :user AND device_id != 'authorized' AND authorized = 1";
$params = array(":user" => $user); $params = array(":user" => $user);
// Get number of authorized devices for user // Get number of authorized devices for user
$num_devid_user = 0; $num_devid_user = 0;
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
if ($record = $sth->fetch(PDO::FETCH_ASSOC)) { if ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
$num_devid_user = $record["pcount"]; $num_devid_user = $record["pcount"];
...@@ -677,7 +650,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -677,7 +650,7 @@ class SqlStateMachine implements IStateMachine {
$paramsNewDevid = array(); $paramsNewDevid = array();
$paramsNewUser = array(); $paramsNewUser = array();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
if ($record = $sth->fetch(PDO::FETCH_ASSOC)) { if ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
$userExist = true; $userExist = true;
...@@ -697,7 +670,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -697,7 +670,7 @@ class SqlStateMachine implements IStateMachine {
else { else {
$params[":devid"] = $devid; $params[":devid"] = $devid;
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
$sth->execute($params); $sth->execute($params);
if ($record = $sth->fetch(PDO::FETCH_ASSOC)) { if ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
$deviceExist = true; $deviceExist = true;
...@@ -775,7 +748,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -775,7 +748,7 @@ class SqlStateMachine implements IStateMachine {
$paramsNewUser[":devid"] = "authorized"; $paramsNewUser[":devid"] = "authorized";
$paramsNewUser[":created_at"] = $paramsNewUser[":updated_at"] = $this->getNow(); $paramsNewUser[":created_at"] = $paramsNewUser[":updated_at"] = $this->getNow();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if (!$sth->execute($paramsNewUser)) { if (!$sth->execute($paramsNewUser)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error creating new user %s", print_r($sth->errorInfo(), 1))); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error creating new user %s", print_r($sth->errorInfo(), 1)));
$status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC; $status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC;
...@@ -790,7 +763,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -790,7 +763,7 @@ class SqlStateMachine implements IStateMachine {
$paramsNewDevid[":devid"] = $devid; $paramsNewDevid[":devid"] = $devid;
$paramsNewDevid[":created_at"] = $paramsNewDevid[":updated_at"] = $this->getNow(); $paramsNewDevid[":created_at"] = $paramsNewDevid[":updated_at"] = $this->getNow();
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if (!$sth->execute($paramsNewDevid)) { if (!$sth->execute($paramsNewDevid)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error creating user new device %s", print_r($sth->errorInfo(), 1))); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error creating user new device %s", print_r($sth->errorInfo(), 1)));
$status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC; $status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC;
...@@ -801,8 +774,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -801,8 +774,6 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error checking permission for username '%s' device '%s': %s", $user, $devid, $ex->getMessage())); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->GetUserDevicePermission(): Error checking permission for username '%s' device '%s': %s", $user, $devid, $ex->getMessage()));
$status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC; $status = SYNC_COMMONSTATUS_USERDISABLEDFORSYNC;
} }
$this->clearConnection($this->dbh, $sth, $record);
} }
return $status; return $status;
...@@ -819,19 +790,15 @@ class SqlStateMachine implements IStateMachine { ...@@ -819,19 +790,15 @@ class SqlStateMachine implements IStateMachine {
public function GetMappedUsername($username, $backend) { public function GetMappedUsername($username, $backend) {
$result = null; $result = null;
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "SELECT `mappedname` FROM `zpush_combined_usermap` WHERE `username` = :user AND `backend` = :backend"; $sql = "SELECT `mappedname` FROM `zpush_combined_usermap` WHERE `username` = :user AND `backend` = :backend";
$params = array("user" => $username, "backend" => $backend); $params = array("user" => $username, "backend" => $backend);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params) === false) { if ($sth->execute($params) === false) {
ZLog::Write(LOGLEVEL_ERROR, "SqlStateMachine->GetMappedUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1)); ZLog::Write(LOGLEVEL_ERROR, "SqlStateMachine->GetMappedUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1));
} else if ($record = $sth->fetch(PDO::FETCH_ASSOC)) { } else if ($record = $sth->fetch(PDO::FETCH_ASSOC)) {
$result = $record["mappedname"]; $result = $record["mappedname"];
} }
$this->clearConnection($this->dbh, $sth, $record);
return $result; return $result;
} }
...@@ -845,21 +812,18 @@ class SqlStateMachine implements IStateMachine { ...@@ -845,21 +812,18 @@ class SqlStateMachine implements IStateMachine {
* @return boolean * @return boolean
*/ */
public function MapUsername($username, $backend, $mappedname) { public function MapUsername($username, $backend, $mappedname) {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = " $sql = "
INSERT INTO `zpush_combined_usermap` (`username`, `backend`, `mappedname`, `created_at`, `updated_at`) INSERT INTO `zpush_combined_usermap` (`username`, `backend`, `mappedname`, `created_at`, `updated_at`)
VALUES (:user, :backend, :mappedname, NOW(), NOW()) VALUES (:user, :backend, :mappedname, NOW(), NOW())
ON DUPLICATE KEY UPDATE `mappedname` = :mappedname2, `updated_at` = NOW() ON DUPLICATE KEY UPDATE `mappedname` = :mappedname2, `updated_at` = NOW()
"; ";
$params = array("user" => $username, "backend" => $backend, "mappedname" => $mappedname, "mappedname2" => $mappedname); $params = array("user" => $username, "backend" => $backend, "mappedname" => $mappedname, "mappedname2" => $mappedname);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params) === false) { if ($sth->execute($params) === false) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->MapUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1))); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->MapUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1)));
return false; return false;
} }
$this->clearConnection($this->dbh, $sth);
return true; return true;
} }
...@@ -872,11 +836,9 @@ class SqlStateMachine implements IStateMachine { ...@@ -872,11 +836,9 @@ class SqlStateMachine implements IStateMachine {
* @return boolean * @return boolean
*/ */
public function UnmapUsername($username, $backend) { public function UnmapUsername($username, $backend) {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sql = "DELETE FROM `zpush_combined_usermap` WHERE `username` = :user AND `backend` = :backend"; $sql = "DELETE FROM `zpush_combined_usermap` WHERE `username` = :user AND `backend` = :backend";
$params = array("user" => $username, "backend" => $backend); $params = array("user" => $username, "backend" => $backend);
$sth = $this->dbh->prepare($sql); $sth = $this->getDbh()->prepare($sql);
if ($sth->execute($params) === false) { if ($sth->execute($params) === false) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->UnmapUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1))); ZLog::Write(LOGLEVEL_ERROR, sprintf("SqlStateMachine->UnmapUsername(): Failed to execute query %s", print_r($sth->errorInfo(), 1)));
return false; return false;
...@@ -885,7 +847,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -885,7 +847,6 @@ class SqlStateMachine implements IStateMachine {
return false; return false;
} }
$this->clearConnection($this->dbh, $sth);
return true; return true;
} }
...@@ -948,9 +909,8 @@ class SqlStateMachine implements IStateMachine { ...@@ -948,9 +909,8 @@ class SqlStateMachine implements IStateMachine {
private function checkDbAndTables() { private function checkDbAndTables() {
ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->checkDbAndTables(): Checking if database and tables are available."); ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->checkDbAndTables(): Checking if database and tables are available.");
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sqlStmt = sprintf("SHOW TABLES FROM %s LIKE 'zpush%%'", STATE_SQL_DATABASE); $sqlStmt = sprintf("SHOW TABLES FROM %s LIKE 'zpush%%'", STATE_SQL_DATABASE);
$sth = $this->dbh->prepare($sqlStmt); $sth = $this->getDbh()->prepare($sqlStmt);
$sth->execute(); $sth->execute();
if ($sth->rowCount() != 3) { if ($sth->rowCount() != 3) {
$this->createTables(); $this->createTables();
...@@ -971,7 +931,6 @@ class SqlStateMachine implements IStateMachine { ...@@ -971,7 +931,6 @@ class SqlStateMachine implements IStateMachine {
// try to connect to the db again and do the create tables calls // try to connect to the db again and do the create tables calls
$this->createTables(); $this->createTables();
$this->clearConnection($this->dbh);
} }
/** /**
...@@ -985,12 +944,13 @@ class SqlStateMachine implements IStateMachine { ...@@ -985,12 +944,13 @@ class SqlStateMachine implements IStateMachine {
ZLog::Write(LOGLEVEL_INFO, sprintf("SqlStateMachine->createDB(): %s database is not available, trying to create it.", STATE_SQL_DATABASE)); ZLog::Write(LOGLEVEL_INFO, sprintf("SqlStateMachine->createDB(): %s database is not available, trying to create it.", STATE_SQL_DATABASE));
$dsn = sprintf("%s:host=%s;port=%s", STATE_SQL_ENGINE, STATE_SQL_SERVER, STATE_SQL_PORT); $dsn = sprintf("%s:host=%s;port=%s", STATE_SQL_ENGINE, STATE_SQL_SERVER, STATE_SQL_PORT);
try { try {
$this->dbh = new PDO($dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options); $dbh = new PDO($dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sqlStmt = sprintf("CREATE DATABASE %s", STATE_SQL_DATABASE); $sqlStmt = sprintf("CREATE DATABASE %s", STATE_SQL_DATABASE);
$sth = $this->dbh->prepare($sqlStmt); $sth = $dbh->prepare($sqlStmt);
$sth->execute(); $sth->execute();
ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->createDB(): Database created succesfully."); ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->createDB(): Database created succesfully.");
$this->createTables(); $this->createTables();
$this->clearConnection($dbh);
return true; return true;
} }
catch (PDOException $ex) { catch (PDOException $ex) {
...@@ -1008,9 +968,8 @@ class SqlStateMachine implements IStateMachine { ...@@ -1008,9 +968,8 @@ class SqlStateMachine implements IStateMachine {
private function createTables() { private function createTables() {
ZLog::Write(LOGLEVEL_INFO, "SqlStateMachine->createTables(): tables are not available, trying to create them."); ZLog::Write(LOGLEVEL_INFO, "SqlStateMachine->createTables(): tables are not available, trying to create them.");
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sqlStmt = self::CREATETABLE_ZPUSH_SETTINGS . self::CREATETABLE_ZPUSH_USERS . self::CREATETABLE_ZPUSH_STATES . self::CREATEINDEX_ZPUSH_STATES; $sqlStmt = self::CREATETABLE_ZPUSH_SETTINGS . self::CREATETABLE_ZPUSH_USERS . self::CREATETABLE_ZPUSH_STATES . self::CREATEINDEX_ZPUSH_STATES;
$sth = $this->dbh->prepare($sqlStmt); $sth = $this->getDbh()->prepare($sqlStmt);
$sth->execute(); $sth->execute();
ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->createTables(): tables created succesfully."); ZLog::Write(LOGLEVEL_DEBUG, "SqlStateMachine->createTables(): tables created succesfully.");
return true; return true;
...@@ -1029,9 +988,8 @@ class SqlStateMachine implements IStateMachine { ...@@ -1029,9 +988,8 @@ class SqlStateMachine implements IStateMachine {
*/ */
public function checkTablesHaveData() { public function checkTablesHaveData() {
try { try {
$this->dbh = new PDO($this->dsn, STATE_SQL_USER, STATE_SQL_PASSWORD, $this->options);
$sqlStmt = "SELECT key_name FROM zpush_settings LIMIT 1;"; $sqlStmt = "SELECT key_name FROM zpush_settings LIMIT 1;";
$sth = $this->dbh->prepare($sqlStmt); $sth = $this->getDbh()->prepare($sqlStmt);
$sth->execute(); $sth->execute();
if ($sth->rowCount() > 0) { if ($sth->rowCount() > 0) {
print("There is data in zpush_settings table." . PHP_EOL); print("There is data in zpush_settings table." . PHP_EOL);
...@@ -1042,7 +1000,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -1042,7 +1000,7 @@ class SqlStateMachine implements IStateMachine {
} }
$sqlStmt = "SELECT id_state FROM zpush_states LIMIT 1;"; $sqlStmt = "SELECT id_state FROM zpush_states LIMIT 1;";
$sth = $this->dbh->prepare($sqlStmt); $sth = $this->getDbh()->prepare($sqlStmt);
$sth->execute(); $sth->execute();
if ($sth->rowCount() > 0) { if ($sth->rowCount() > 0) {
print("There is data in zpush_states table." . PHP_EOL); print("There is data in zpush_states table." . PHP_EOL);
...@@ -1053,7 +1011,7 @@ class SqlStateMachine implements IStateMachine { ...@@ -1053,7 +1011,7 @@ class SqlStateMachine implements IStateMachine {
} }
$sqlStmt = "SELECT username FROM zpush_users LIMIT 1;"; $sqlStmt = "SELECT username FROM zpush_users LIMIT 1;";
$sth = $this->dbh->prepare($sqlStmt); $sth = $this->getDbh()->prepare($sqlStmt);
$sth->execute(); $sth->execute();
if ($sth->rowCount() > 0) { if ($sth->rowCount() > 0) {
print("There is data in zpush_users table." . PHP_EOL); print("There is data in zpush_users table." . PHP_EOL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment