Completed
Push — master ( 213254...a7e545 )
by Nazar
04:39
created

SQLite::n()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\DB;
9
class SQLite extends _Abstract {
10
	/**
11
	 * @var \SQLite3 Instance of DB connection
12
	 */
13
	protected $instance;
14
	/**
15
	 * @param string $database Ignored for SQLite
16
	 * @param string $user     Ignored for SQLite
17
	 * @param string $password Ignored for SQLite
18
	 * @param string $host     Path to database file, relatively to website root or absolute
19
	 * @param string $prefix
20
	 */
21
	function __construct ($database, $user = '', $password = '', $host = '', $prefix = '') {
22
		$start = microtime(true);
23
		try {
24
			$this->instance        = new \SQLite3($host);
25
			$this->database        = $database;
26
			$this->connected       = true;
27
			$this->connecting_time = microtime(true) - $start;
28
			$this->db_type         = 'sqlite';
29
			$this->prefix          = $prefix;
30
		} catch (\Exception $e) {
31
		}
32
	}
33
	/**
34
	 * @inheritdoc
35
	 */
36
	function q ($query, $params = [], ...$param) {
37
		// Hack to convert small subset of MySQL queries into SQLite-compatible syntax
38
		$query = str_replace('INSERT IGNORE', 'INSERT OR IGNORE', $query);
39
		return parent::q(...([$query] + func_get_args()));
40
	}
41
	/**
42
	 * @inheritdoc
43
	 *
44
	 * @return bool|\SQLite3Result
45
	 */
46
	protected function q_internal ($query) {
47
		if (!$query) {
48
			return false;
49
		}
50
		return $this->instance->query($query);
51
	}
52
	/**
53
	 * @inheritdoc
54
	 */
55
	protected function q_multi_internal ($query) {
56
		$result = true;
57
		foreach ($query as $q) {
58
			$result = $result && $this->q_internal($q);
59
		}
60
		return $result;
61
	}
62
	/**
63
	 * @inheritdoc
64
	 *
65
	 * @param false|\SQLite3Result $query_result
66
	 */
67
	function f ($query_result, $single_column = false, $array = false, $indexed = false) {
68
		if (!is_object($query_result)) {
69
			return false;
70
		}
71
		$result_type = $single_column || $indexed ? SQLITE3_NUM : SQLITE3_ASSOC;
72
		if ($array) {
73
			$result = [];
74
			while ($current = $query_result->fetchArray($result_type)) {
75
				$result[] = $single_column ? $current[0] : $current;
76
			}
77
			$this->free($query_result);
78
			return $result;
79
		}
80
		$result = $query_result->fetchArray($result_type);
81
		return $single_column && $result ? $result[0] : $result;
82
	}
83
	/**
84
	 * @inheritdoc
85
	 */
86
	function id () {
87
		return $this->instance->lastInsertRowID();
88
	}
89
	/**
90
	 * @inheritdoc
91
	 */
92
	function affected () {
93
		return $this->instance->changes();
94
	}
95
	/**
96
	 * @inheritdoc
97
	 *
98
	 * @param false|\SQLite3Result $query_result
99
	 */
100
	function free ($query_result) {
101
		if (is_object($query_result)) {
102
			return $query_result->finalize();
103
		}
104
		return true;
105
	}
106
	/**
107
	 * @inheritdoc
108
	 */
109
	function columns ($table, $like = false) {
110
		if (!$table) {
111
			return false;
112
		}
113
		$columns = $this->qfa("PRAGMA table_info(`$table`)") ?: [];
114
		foreach ($columns as &$column) {
115
			$column = $column['name'];
116
		}
117
		/**
118
		 * Only support the most common cases
119
		 */
120
		if ($like) {
121
			if (substr($like, -1) == '%') {
122
				$like = substr($like, 0, -1);
123
				return array_values(
124
					array_filter(
125
						$columns,
126
						function ($column) use ($like) {
127
							return strpos($column, $like) === 0;
128
						}
129
					)
130
				);
131
			} elseif (strpos($like, '%') === false) {
132
				return in_array($like, $columns) ? [$like] : [];
133
			} else {
134
				trigger_error("Can't get columns like $like, SQLite engine doesn't support such conditions", E_USER_WARNING);
135
				return [];
136
			}
137
		}
138
		return $columns;
139
	}
140
	/**
141
	 * @inheritdoc
142
	 */
143
	function tables ($like = false) {
144
		if ($like) {
145
			$like = $this->s($like);
146
			return $this->qfas(
147
				"SELECT `name`
148
				FROM `sqlite_master`
149
				WHERE
150
					`type` = 'table' AND
151
					`name` != 'sqlite_sequence' AND
152
					`name` LIKE $like
153
				ORDER BY `name` ASC"
154
			) ?: [];
155
		} else {
156
			return $this->qfas(
157
				"SELECT `name`
158
				FROM `sqlite_master`
159
				WHERE
160
					`type` = 'table' AND
161
					`name` != 'sqlite_sequence'
162
				ORDER BY `name` ASC"
163
			) ?: [];
164
		}
165
	}
166
	/**
167
	 * @inheritdoc
168
	 */
169
	protected function s_internal ($string, $single_quotes_around) {
170
		$return = \SQLite3::escapeString($string);
171
		return $single_quotes_around ? "'$return'" : $return;
172
	}
173
	/**
174
	 * @inheritdoc
175
	 */
176
	function server () {
177
		return \SQLite3::version()['versionString'];
178
	}
179
	/**
180
	 * @inheritdoc
181
	 */
182
	function __destruct () {
183
		if ($this->connected && is_object($this->instance)) {
184
			$this->instance->close();
185
			$this->connected = false;
186
		}
187
	}
188
}
189