FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
SqliteE.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Steffen Weber
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Db\Driver;
21 
23 use SQLite3;
24 use SQLite3Stmt;
25 
29 class SqliteE implements Driver
30 {
32  private $dbConnection = false;
34  private $preparedStmt = array();
36  private $varPrefix = ':var';
37 
38  public function __construct($dbConnection)
39  {
40  $this->dbConnection = $dbConnection;
41  }
42 
48  public function prepare($statementName, $sqlStatement)
49  {
50  $paramCnt = 0;
51  $pgStyleVar = '$' . ($paramCnt + 1);
52  while (false !== strpos($sqlStatement, $pgStyleVar)) {
53  $paramCnt++;
54  $sqlStatement = str_replace($pgStyleVar, $this->varPrefix . chr(64 + $paramCnt), $sqlStatement);
55  $pgStyleVar = '$' . ($paramCnt + 1);
56  if ($paramCnt == 9) { // limited number of replaced place holders
57  break;
58  }
59  }
60  $sqlStatementWithoutPostgresKeyWord = str_replace(' ONLY ',' ',$sqlStatement);
61  $stmt = $this->dbConnection->prepare($sqlStatementWithoutPostgresKeyWord);
62  $this->preparedStmt[$statementName] = & $stmt;
63  return $stmt;
64  }
65 
71  public function execute($statementName, $parameters)
72  {
73  if (! array_key_exists($statementName, $this->preparedStmt)) {
74  return false;
75  }
76  $params = array_values($parameters);
77  /* @var $stmt SQLite3Stmt */
78  $stmt = $this->preparedStmt[$statementName];
79  for ($idx = 0; $idx < $stmt->paramCount(); $idx++) {
80  $variableName = $this->varPrefix . chr(65 + $idx);
81  $stmt->bindValue($variableName, $params[$idx]);
82  }
83  return $stmt->execute();
84  }
85 
90  public function query($sqlStatement)
91  {
92  return $this->dbConnection->query($sqlStatement);
93  }
94 
98  public function isConnected()
99  {
100  return (false !== $this->dbConnection);
101  }
102 
106  public function getLastError()
107  {
108  return SQLite3::lastErrorMsg();
109  }
110 
115  public function freeResult($res)
116  {
117  return $res->finalize();
118  }
119 
124  public function fetchArray($res)
125  {
126  return $res->fetchArray(SQLITE3_ASSOC);
127  }
128 
133  public function fetchAll($res)
134  {
135  $result = array();
136  while ($row = $res->fetchArray(SQLITE3_ASSOC)) { // do not SQLITE3_NUM !
137  $result[] = $row;
138  }
139  return $result;
140  }
141 
145  public function begin()
146  {
147  $this->dbConnection->query("BEGIN");
148  return;
149  }
150 
154  public function commit()
155  {
156  $this->dbConnection->query("COMMIT");
157  return;
158  }
159 
163  public function rollback()
164  {
165  $this->dbConnection->query("ROLLBACK");
166  return;
167  }
168 
173  public function booleanFromDb($booleanValue)
174  {
175  return $booleanValue === 1;
176  }
177 
182  public function booleanToDb($booleanValue)
183  {
184  return $booleanValue ? 1 : 0;
185  }
186 
191  public function escapeString($string)
192  {
193  return SQLite3::escapeString($string);
194  }
195 
200  public function existsTable($tableName)
201  {
202  $sql = "SELECT count(*) cnt FROM sqlite_master WHERE type='table' AND name='$tableName'";
203  $row = SQLite3::querySingle($sql);
204  if (! $row && $this->isConnected()) {
205  throw new \Exception($this->getLastError());
206  } else if (!$row) {
207  throw new \Exception('DB connection lost');
208  }
209  return($row['cnt']>0);
210  }
211 
217  public function existsColumn($tableName, $columnName)
218  {
219  // TODO: Implement existsColumn() method.
220  throw new \Exception("Method not implemented yet!");
221  }
222 
229  public function insertPreparedAndReturn($stmt, $sql, $params, $colName)
230  {
231  $this->prepare($stmt,$sql);
232  $res = $this->execute($stmt,$params);
233  $this->freeResult($res);
234  return SQLiteDatabase::lastInsertRowid();
235  }
236 }
insertPreparedAndReturn($stmt, $sql, $params, $colName)
Definition: SqliteE.php:229
booleanFromDb($booleanValue)
Definition: SqliteE.php:173
existsColumn($tableName, $columnName)
Definition: SqliteE.php:217
prepare($statementName, $sqlStatement)
Definition: SqliteE.php:48
execute($statementName, $parameters)
Definition: SqliteE.php:71