Completed
Push — master ( 15dd9f...8679ea )
by smiley
03:53
created

SQLite3Driver::multi_callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * Class SQLite3Driver
4
 *
5
 * @filesource   SQLite3Driver.php
6
 * @created      21.02.2016
7
 * @package      chillerlan\Database\Drivers\SQLite
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Drivers\SQLite;
14
15
use chillerlan\Database\DBException;
16
use chillerlan\Database\Drivers\DBDriverAbstract;
17
use Exception;
18
use SQLite3;
19
20
/**
21
 *
22
 */
23
class SQLite3Driver extends DBDriverAbstract{
24
25
	/**
26
	 * Holds the database resource object
27
	 *
28
	 * @var SQLite3
29
	 */
30
	protected $db;
31
32
	/**
33
	 * Establishes a database connection and returns the connection object
34
	 *
35
	 * @return \SQLite3 the database resource object
36
	 * @throws \chillerlan\Database\DBException
37
	 */
38
	public function connect():SQLite3{
39
40
		if($this->db instanceof SQLite3){
41
			return $this->db;
42
		}
43
44
		try{
45
			$this->db = new SQLite3(
46
				$this->options->database,
47
				$this->options->sqlite_flags,
48
				$this->options->sqlite_encryption_key
49
			);
50
		}
51
		catch(Exception $Exception){
52
			throw new DBException($Exception->getMessage());
53
		}
54
55
		return $this->db;
56
	}
57
58
	/**
59
	 * Closes a database connection
60
	 *
61
	 * @return bool
62
	 */
63
	public function disconnect():bool{
64
		return $this->db->close();
65
	}
66
67
	/**
68
	 * Returns info about the used php client
69
	 *
70
	 * @return string php's database client string
71
	 */
72
	public function getClientInfo():string{
73
		return 'SQLite '.SQLite3::version()['versionString'];
74
	}
75
76
	/**
77
	 * Returns info about the database server
78
	 *
79
	 * @return string database's serverinfo string
80
	 */
81
	public function getServerInfo():string{
82
		return 'N/A (SQLite '.SQLite3::version()['versionString'].')';
83
	}
84
85
	/**
86
	 * Sanitizer.
87
	 *
88
	 * Recursively escapes string values, optional htmlspecialchars()
89
	 *
90
	 * @param array|string $data         array or string to escape
91
	 * @param bool         $specialchars [optional] if true, it performs a htmlspecialchars() on each value given
92
	 *
93
	 * @return array|string array or string. escaped. obviously.
94
	 */
95 View Code Duplication
	public function escape($data, bool $specialchars = false){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
97
		if(is_array($data)){
98
99
			foreach($data as $key => $value){
100
				$data[$key] = $this->escape($value, $specialchars);
101
			}
102
103
		}
104
		else if(is_object($data)){
105
106
			foreach($data as $key => $value){
107
				$data->{$key} = $this->escape($value, $specialchars);
108
			}
109
110
		}
111
		else{
112
113
			if($specialchars){
114
				$data = htmlspecialchars($data, ENT_HTML5, 'UTF-8', false);
115
			}
116
117
			$data = SQLite3::escapeString($data);
118
		}
119
120
		return $data;
121
	}
122
123
	/**
124
	 * Basic SQL query for non prepared statements
125
	 *
126
	 * There is no escaping in here, so make sure, your SQL is clean/escaped.
127
	 * Also, your SQL should NEVER contain user input, use prepared statements in this case.
128
	 *
129
	 * If the query was successful it returns either an array of results or true
130
	 * if it was a void query. On errors, a false will be returned, obviously.
131
	 *
132
	 * @param string $sql         The SQL statement
133
	 * @param string $index       [optional] an index column to assingn as the result's keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $index not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
134
	 * @param bool   $assoc       [optional] If true, the fields are named with the respective column names, otherwise numbered
135
	 * @param bool   $fetch_array [optional] fetch the vaues as array instead of object
136
	 *
137
	 * @return array|bool array with results, true on void query success, otherwise false.
138
	 * @throws \mysqli_sql_exception
139
	 */
140
	public function raw(string $sql, string $index = null, bool $assoc = true, bool $fetch_array = false){
141
		// TODO: Implement raw() method.
142
		return false;
143
	}
144
145
	/**
146
	 * Prepared statements wrapper
147
	 *
148
	 * Does everything for you: prepares the statement and fetches the results as an object or array
149
	 * just pass a query along with values and you're done. Not meant for multi-inserts.
150
	 *
151
	 * @param string $sql         The SQL statement to prepare
152
	 * @param array  $values      [optional] the value for each "?" in the statement - in the respective order, of course
153
	 * @param string $index       [optional] an index column to assingn as the result's keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $index not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
154
	 * @param bool   $assoc       [optional] If true, the fields are named with the respective column names, otherwise numbered
155
	 * @param bool   $fetch_array [optional] fetch the vaues as array instead of object
156
	 *
157
	 * @return array|bool Array with results, true on void query success, otherwise false
158
	 */
159
	public function prepared(string $sql, array $values = [], string $index = null, bool $assoc = true, bool $fetch_array = false){
160
		// TODO: Implement prepared() method.
161
		return false;
162
	}
163
164
	/**
165
	 * Prepared multi line insert
166
	 *
167
	 * Prepared statement multi insert/update
168
	 *
169
	 * @param string $sql    The SQL statement to prepare
170
	 * @param array  $values a multidimensional array with the values, each row represents one line to insert.
171
	 *
172
	 * @return bool true query success, otherwise false
173
	 */
174
	public function multi(string $sql, array $values){
175
		// TODO: Implement multi() method.
176
		return false;
177
	}
178
179
	/**
180
	 * Prepared multi line insert/update with callback
181
	 *
182
	 * @todo: multi treading
183
	 * @see https://gist.github.com/krakjoe/6437782
184
	 * @see https://gist.github.com/krakjoe/9384409
185
	 *
186
	 * @param string         $sql      The SQL statement to prepare
187
	 * @param array          $data     an array with the (raw) data to insert, each row represents one line to insert.
188
	 * @param callable|array $callback a callback that processes the values for each row.
189
	 *
190
	 * @return bool true query success, otherwise false
191
	 * @throws \chillerlan\Database\DBException
192
	 */
193
	public function multi_callback(string $sql, array $data, $callback){
194
		// TODO: Implement multi_callback() method.
195
		return false;
196
	}
197
}
198