Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DatabaseOperationsTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\db\traits;
4
9
21
22 abstract public function getDSN();
23
24 public function getDbObject() {
25 return $this->wrapperObject->getDbInstance ();
26 }
27
28 public function _connect() {
29 $this->wrapperObject->connect ( $this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options );
30 }
31
38 public function query($sql) {
39 return $this->wrapperObject->query ( $sql );
40 }
41
51 public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = false, $one = false) {
52 $result = false;
53 if ($cache = ($useCache!==false && (DbCache::$active || $useCache))) {
54 $result = $this->getCacheValue ( $tableName, $condition, $parameters, $cKey );
55 }
56 if ($result === false) {
57 $quote = SqlUtils::$quote;
58 $sql="SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}";
59 Logger::info ( 'Database', $sql, 'prepareAndExecute', $parameters );
60 $result = $this->wrapperObject->_optPrepareAndExecute ( $sql, $parameters, $one );
61 if ($cache) {
62 $this->cache->store ( $tableName, $cKey, $result );
63 }
64 }
65 return $result;
66 }
67
68 private function getCacheValue($tableName, $condition, $parameters, &$cKey) {
69 $cKey = $condition;
70 if (\is_array ( $parameters )) {
71 $cKey .= \implode ( ',', $parameters );
72 }
73 try {
74 $result = $this->cache->fetch ( $tableName, $cKey );
75 Logger::info ( 'Cache', "fetching cache for table {$tableName} with condition : {$condition}", 'Database::prepareAndExecute', $parameters );
76 } catch ( \Exception $e ) {
77 throw new CacheException ( "Cache is not created in Database constructor" );
78 }
79 return $result;
80 }
81
82 public function _optExecuteAndFetch($statement, $tableName, $condition, $parameters = null, $useCache = false, $one = false) {
83 $result = false;
84 if ($cache = ($useCache!==false && (DbCache::$active || $useCache))) {
85 $result = $this->getCacheValue ( $tableName, $condition, $parameters, $cKey );
86 }
87 if ($result === false) {
88 $result = $this->wrapperObject->_optExecuteAndFetch ( $statement, $parameters, $one );
89 if ($cache) {
90 $this->cache->store ( $tableName, $cKey, $result );
91 }
92 }
93 return $result;
94 }
95
96 public function _optExecuteAndFetchNoCache($statement, $parameters = null, $one = false) {
97 return $this->wrapperObject->_optExecuteAndFetch ( $statement, $parameters, $one );
98 }
99
100 public function getDaoPreparedStatement($tableName, $condition, $fields) {
101 $quote = SqlUtils::$quote;
102 return $this->wrapperObject->prepareStatement ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}" );
103 }
104
105 public function prepareAndExecuteNoCache($tableName, $condition, $fields, $parameters = null) {
106 $quote = SqlUtils::$quote;
107 return $this->wrapperObject->_optPrepareAndExecute ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters );
108 }
109
110 public function storeCache() {
111 $this->cache->storeDeferred ();
112 }
113
114 public function prepareAndFetchAll($sql, $parameters = null, $mode = null) {
115 return $this->wrapperObject->fetchAll ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode );
116 }
117
118 public function prepareAndFetchOne($sql, $parameters = null, $mode = null) {
119 return $this->wrapperObject->fetchOne ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode );
120 }
121
122 public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) {
123 return $this->wrapperObject->fetchAllColumn ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $column );
124 }
125
126 public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = 0) {
127 $statement = $this->wrapperObject->_getStatement ( $sql );
128 if ($statement->execute ( $parameters )) {
129 Logger::info ( 'Database', $sql, 'prepareAndFetchColumn', $parameters );
130 return $statement->fetchColumn ( $columnNumber );
131 }
132 return false;
133 }
134
140 private function getStatement($sql) {
141 return $this->wrapperObject->_getStatement ( $sql );
142 }
143
149 public function getUpdateStatement($sql) {
150 return $this->wrapperObject->_getStatement ( $sql );
151 }
152
160 public function prepareAndExecuteUpdate($sql, $parameters = null) {
161 return $this->getUpdateStatement ( $sql )->execute ( $parameters );
162 }
163
170 public function execute($sql) {
171 return $this->wrapperObject->execute ( $sql );
172 }
173
180 public function prepareStatement($sql) {
181 return $this->wrapperObject->prepareStatement ( $sql );
182 }
183
191 public function prepareNamedStatement(string $name, string $sql) {
192 return $this->wrapperObject->prepareNamedStatement ( $name, $sql );
193 }
194
202 public function getNamedStatement(string $name, ?string $sql = null) {
203 return $this->wrapperObject->getNamedStatement ( $name, $sql );
204 }
205
214 public function bindValueFromStatement($statement, $parameter, $value) {
215 return $this->wrapperObject->bindValueFromStatement ( $statement, $parameter, $value );
216 }
217
223 public function lastInserId($name = null) {
224 return $this->wrapperObject->lastInsertId ( $name );
225 }
226
233 public function count($tableName, $condition = '') {
234 if ($condition != ''){
235 $condition = ' WHERE ' . $condition;
236 }
237 return $this->wrapperObject->queryColumn ( 'SELECT COUNT(*) FROM ' . $tableName . $condition );
238 }
239
240 public function queryColumn($query, $columnNumber = null) {
241 return $this->wrapperObject->queryColumn ( $query, $columnNumber );
242 }
243
244 public function fetchAll($query, $mode = null) {
245 return $this->wrapperObject->queryAll ( $query, $mode );
246 }
247
248 public function isConnected() {
249 return ($this->wrapperObject !== null && $this->ping ());
250 }
251
252 public function ping() {
253 return ($this->wrapperObject && $this->wrapperObject->ping ());
254 }
255}
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Definition DbCache.php:20
SQL utilities.
Definition SqlUtils.php:13
lastInserId($name=null)
Returns the last insert id.
execute($sql)
Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
prepareAndExecute($tableName, $condition, $fields, $parameters=null, $useCache=false, $one=false)
prepareAndExecuteUpdate($sql, $parameters=null)
Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...)
prepareAndFetchColumn($sql, $parameters=null, $columnNumber=0)
query($sql)
Executes an SQL statement, returning a result set as a statement object.
prepareStatement($sql)
Prepares a statement for execution and returns a statement object.
bindValueFromStatement($statement, $parameter, $value)
Sets $value to $parameter.
getCacheValue($tableName, $condition, $parameters, &$cKey)
_optExecuteAndFetch($statement, $tableName, $condition, $parameters=null, $useCache=false, $one=false)
_optExecuteAndFetchNoCache($statement, $parameters=null, $one=false)
count($tableName, $condition='')
Returns the number of records in $tableName matching with the condition passed as a parameter.
prepareAndExecuteNoCache($tableName, $condition, $fields, $parameters=null)
prepareAndFetchAll($sql, $parameters=null, $mode=null)
getNamedStatement(string $name, ?string $sql=null)
Returns the statement corresponding to the name.
prepareAndFetchOne($sql, $parameters=null, $mode=null)
getDaoPreparedStatement($tableName, $condition, $fields)
prepareNamedStatement(string $name, string $sql)
Prepares and returns a statement for execution and gives it a name.
prepareAndFetchAllColumn($sql, $parameters=null, $column=null)
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14