1: <?php
2: namespace Define\DBDrivers;
3: use Define\DBDrivers\IDatabase;
4: use Define\Traits\SingletonTrait;
5: use Define\Exceptions\FrameworkException;
6:
7: if(!defined('DIRECT_ACCESS')) {
8: die("Direct access is forbidden.");
9: }
10:
11: /**
12: * SQLITE DRIVER
13: *
14: * Sqlite Driver class
15: *
16: * @category Define
17: * @package DBDrivers
18: * @author Nitesh Apte <me@niteshapte.com>
19: * @copyright 2015 Nitesh Apte
20: * @version 1.0.0
21: * @since 1.0.0
22: * @license https://www.gnu.org/licenses/gpl.txt GNU General Public License v3
23: *
24: * This program is free software: you can redistribute it and/or modify
25: * it under the terms of the GNU General Public License as published by
26: * the Free Software Foundation, either version 3 of the License, or
27: * (at your option) any later version.
28: *
29: * This program is distributed in the hope that it will be useful,
30: * but WITHOUT ANY WARRANTY; without even the implied warranty of
31: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32: * GNU General Public License for more details.
33: *
34: * You should have received a copy of the GNU General Public License
35: * along with this program. If not, see <http://www.gnu.org/licenses/>.
36: */
37: class SQLITEDriver implements IDatabase {
38:
39: use SingletonTrait;
40:
41: /**
42: * Open a connection to MySQL server
43: *
44: * @param none
45: * @return Object $this
46: */
47: public function getConnection(DatabaseBean $bean) {
48: $sqliteError = '';
49: $this->sqlLink = sqlite_open($bean->sqlDB, 0666, $sqliteError);
50: if($sqliteError != '') {
51: throw new FrameworkException("Unable to connect to Sqlite database.");
52: }
53: return $this;
54: }
55:
56: /**
57: * Execute the SQL
58: *
59: * @param String $query
60: * @return Object $this
61: */
62: public function executeSql($query) {
63: $this->sqlExec = sqlite_query($this->sqlLink, $query);
64: return $this;
65: }
66:
67: /**
68: * Starts a transaction
69: *
70: * @param none
71: * @return Object $this Current object
72: */
73: public function beginTransaction() {
74: throw new \Exception('Transaction is not supported in Sqlite. Hence, transaction initialization quitted.');
75: }
76:
77: /**
78: * Commit the changes
79: *
80: * @param none
81: * @return Object $this Current object
82: */
83: public function commitTransaction() {
84: throw new \Exception('Transaction is not supported in Sqlite. Hence, transaction commit quitted.');
85: }
86:
87: /**
88: * Rollback the changes
89: *
90: * @param none
91: * @return Object $this Current object
92: */
93: public function rollbackTransaction() {
94: throw new \Exception('Transaction is not supported in Sqlite. Hence, transaction rollback quitted.');
95: }
96:
97: /**
98: * Fetch a result row as an associative array
99: *
100: * @param none
101: * @return Object $this
102: */
103: public function fetchAssoc() {
104: $sqlStoreValues = array();
105: while($this->sqlRows = sqlite_fetch_array($this->sqlExec, SQLITE_ASSOC)) {
106: $sqlStoreValues[] = $this->sqlRows;
107: }
108: $this->freeResult();
109: return $sqlStoreValues;
110: }
111:
112: /**
113: * Fetch a result row as an associative array and as an enumerated array
114: *
115: * @param none
116: * @return Array $this->sqlStoreValues
117: */
118: public function fetchArray() {
119: $sqlStoreValues = array();
120: while($this->sqlArray = sqlite_fetch_array($this->sqlExec, SQLITE_BOTH)) {
121: $sqlStoreValues[] = $this->sqlArray;
122: }
123: $this->freeResult();
124: return $sqlStoreValues;
125: }
126:
127: /**
128: * Fetch a result row as an object
129: *
130: * @param none
131: * @return Array $this->sqlStoreValues
132: */
133: public function fetchObject() {
134: $sqlStoreValues = array();
135: while($this->sqlRows = sqlite_fetch_object($this->sqlExec)) {
136: $sqlStoreValues[] = $this->sqlRows;
137: }
138: $this->freeResult();
139: return $sqlStoreValues;
140: }
141:
142: /**
143: * Fetch number of affected rows in previous MySQL operation
144: *
145: * @param none
146: * @return Object $this
147: */
148: public function affectedRows() {
149: return sqlite_num_rows($this->sqlExec);
150: }
151:
152: /**
153: * Method to return the id of last affected row
154: *
155: * @param none
156: * @return Int $this->lastID Last id
157: */
158: public function lastID() {
159: return sqlite_last_insert_rowid($this->sqlExec);
160: }
161:
162: /**
163: * Method to return id of last multiple insert statements executed
164: *
165: * @param Int $size Count of statements
166: * @return array
167: */
168: public function multipleID($size) {
169: $lastId = $this->lastID();
170: $lastIDs = array();
171: for($i = $lastId; $i< ($lastId + $size); $i++){
172: $lastIDs[] = $i;
173: }
174: return $lastIDs;
175: }
176:
177: /**
178: * Method to free the results from memory
179: *
180: * @param none
181: * @return void
182: */
183: public function freeResult() {
184: unset($this->sqlExec);
185: }
186:
187: /**
188: * Destroy MySQL connection
189: *
190: * @param none
191: * @return void
192: */
193: public function __destruct() {
194: sqlite_close($this->sqlLink);
195: }
196: }