FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SqliteE.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
10 
12 use SQLite3;
13 use SQLite3Stmt;
14 
18 class SqliteE implements Driver
19 {
21  private $dbConnection = false;
23  private $preparedStmt = array();
25  private $varPrefix = ':var';
26 
27  public function __construct($dbConnection)
28  {
29  $this->dbConnection = $dbConnection;
30  }
31 
37  public function prepare($statementName, $sqlStatement)
38  {
39  $paramCnt = 0;
40  $pgStyleVar = '$' . ($paramCnt + 1);
41  while (false !== strpos($sqlStatement, $pgStyleVar)) {
42  $paramCnt++;
43  $sqlStatement = str_replace($pgStyleVar, $this->varPrefix . chr(64 + $paramCnt), $sqlStatement);
44  $pgStyleVar = '$' . ($paramCnt + 1);
45  if ($paramCnt == 9) { // limited number of replaced place holders
46  break;
47  }
48  }
49  $sqlStatementWithoutPostgresKeyWord = str_replace(' ONLY ',' ',$sqlStatement);
50  $stmt = $this->dbConnection->prepare($sqlStatementWithoutPostgresKeyWord);
51  $this->preparedStmt[$statementName] = & $stmt;
52  return $stmt;
53  }
54 
60  public function execute($statementName, $parameters)
61  {
62  if (! array_key_exists($statementName, $this->preparedStmt)) {
63  return false;
64  }
65  $params = array_values($parameters);
66  /* @var $stmt SQLite3Stmt */
67  $stmt = $this->preparedStmt[$statementName];
68  for ($idx = 0; $idx < $stmt->paramCount(); $idx++) {
69  $variableName = $this->varPrefix . chr(65 + $idx);
70  $stmt->bindValue($variableName, $params[$idx]);
71  }
72  return $stmt->execute();
73  }
74 
79  public function query($sqlStatement)
80  {
81  return $this->dbConnection->query($sqlStatement);
82  }
83 
87  public function isConnected()
88  {
89  return (false !== $this->dbConnection);
90  }
91 
95  public function getLastError()
96  {
97  return SQLite3::lastErrorMsg();
98  }
99 
104  public function freeResult($res)
105  {
106  return $res->finalize();
107  }
108 
113  public function fetchArray($res)
114  {
115  return $res->fetchArray(SQLITE3_ASSOC);
116  }
117 
122  public function fetchAll($res)
123  {
124  $result = array();
125  while ($row = $res->fetchArray(SQLITE3_ASSOC)) { // do not SQLITE3_NUM !
126  $result[] = $row;
127  }
128  return $result;
129  }
130 
134  public function begin()
135  {
136  $this->dbConnection->query("BEGIN");
137  return;
138  }
139 
143  public function commit()
144  {
145  $this->dbConnection->query("COMMIT");
146  return;
147  }
148 
152  public function rollback()
153  {
154  $this->dbConnection->query("ROLLBACK");
155  return;
156  }
157 
162  public function booleanFromDb($booleanValue)
163  {
164  return $booleanValue === 1;
165  }
166 
171  public function booleanToDb($booleanValue)
172  {
173  return $booleanValue ? 1 : 0;
174  }
175 
180  public function escapeString($string)
181  {
182  return SQLite3::escapeString($string);
183  }
184 
189  public function existsTable($tableName)
190  {
191  $sql = "SELECT count(*) cnt FROM sqlite_master WHERE type='table' AND name='$tableName'";
192  $row = SQLite3::querySingle($sql);
193  if (! $row && $this->isConnected()) {
194  throw new \Exception($this->getLastError());
195  } else if (!$row) {
196  throw new \Exception('DB connection lost');
197  }
198  return($row['cnt']>0);
199  }
200 
206  public function existsColumn($tableName, $columnName)
207  {
208  // TODO: Implement existsColumn() method.
209  throw new \Exception("Method not implemented yet!");
210  }
211 
218  public function insertPreparedAndReturn($stmt, $sql, $params, $colName)
219  {
220  $this->prepare($stmt,$sql);
221  $res = $this->execute($stmt,$params);
222  $this->freeResult($res);
223  return SQLiteDatabase::lastInsertRowid();
224  }
225 }
execute($statementName, $parameters)
Definition: SqliteE.php:60
prepare($statementName, $sqlStatement)
Definition: SqliteE.php:37
insertPreparedAndReturn($stmt, $sql, $params, $colName)
Definition: SqliteE.php:218
existsColumn($tableName, $columnName)
Definition: SqliteE.php:206
booleanFromDb($booleanValue)
Definition: SqliteE.php:162