50 if (\is_callable (
$cache )) {
53 if (\class_exists (
$cache )) {
54 $this->cache =
new $cache ();
70 }
catch (\PDOException $e){
76 $this->options[\PDO::ATTR_ERRMODE]=\PDO::ERRMODE_EXCEPTION;
77 $this->pdoObject = new \PDO ( $this->
getDSN(), $this->user, $this->password, $this->options );
81 return $this->dbType .
':dbname=' . $this->dbName .
';host=' . $this->serverName .
';charset=UTF8;port=' .
$this->port;
91 return $this->pdoObject->query ( $sql );
106 $result = $this->cache->fetch ( $tableName, $condition );
108 if ($result ===
false) {
110 $statement = $this->
getStatement (
"SELECT {$fields} FROM " . $tableName . $condition );
111 $statement->execute ();
112 $result = $statement->fetchAll ();
113 $statement->closeCursor ();
115 $this->cache->store ( $tableName, $condition, $result );
127 if (! isset ( $this->statements [$sql] )) {
128 $this->statements [$sql] = $this->pdoObject->prepare ( $sql );
129 $this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
131 return $this->statements [$sql];
141 return $this->pdoObject->exec ( $sql );
159 return $this->pdoObject->prepare ( $sql );
171 return $statement->bindValue (
":" . $parameter, $value );
180 return $this->pdoObject->lastInsertId ();
184 $sql =
'SHOW TABLES';
185 $query = $this->pdoObject->query ( $sql );
186 return $query->fetchAll ( \PDO::FETCH_COLUMN );
196 public function count($tableName, $condition =
'') {
197 if ($condition !=
'')
198 $condition =
" WHERE " . $condition;
199 return $this->
query (
"SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
203 return $this->
query ( $query )->fetchColumn ();
207 return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->
ping());
216 return (1 === intval($this->pdoObject->query(
'SELECT 1')->fetchColumn(0)));
236 return \PDO::getAvailableDrivers();
connect()
Creates the PDO instance and realize a safe connection.
static getAvailableDrivers()
setServerName($serverName)
prepareStatement($sql)
Prepares a statement for execution and returns a statement object.
execute($sql)
Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) ...
prepareAndExecute($tableName, $condition, $fields, $useCache=NULL)
query($sql)
Executes an SQL statement, returning a result set as a PDOStatement object.
static getFieldList($fields, $tableName=false)
lastInserId()
Returns the last insert id.
__construct($dbType, $dbName, $serverName="localhost", $port="3306", $user="root", $password="", $options=[], $cache=false)
Constructor.
count($tableName, $condition='')
Returns the number of records in $tableName that respects the condition passed as a parameter...
bindValueFromStatement(\PDOStatement $statement, $parameter, $value)
Sets $value to $parameter.