Test Failed
Push — master ( e9c576...010f10 )
by Joe
03:45
created

Db::createDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 2
dl 0
loc 19
ccs 0
cts 16
cp 0
crap 6
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * ADOdb SQL Related Functionality
4
 * @author Joe Huss <[email protected]>
5
 * @copyright 2018
6
 * @package MyAdmin
7
 * @category SQL
8
 */
9
10
namespace MyDb\Adodb;
11
12
use \MyDb\Generic;
13
use \MyDb\Db_Interface;
14
15
/**
16
 * Db
17
 *
18
 * @access public
19
 */
20
class Db extends Generic implements Db_Interface {
21
	public $driver = 'mysql';
22
	public $Rows = [];
23
	public $type = 'adodb';
24
25
	/**
26
	 * Db::connect()
27
	 * @param string $database
28
	 * @param string $host
29
	 * @param string $user
30
	 * @param string $password
31
	 * @param string $driver
32
	 * @return bool|\the
33
	 */
34
	public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') {
35
		/* Handle defaults */
36
		if ('' == $database)
37
			$database = $this->database;
38
		if ('' == $host)
39
			$host = $this->host;
40
		if ('' == $user)
41
			$user = $this->user;
42
		if ('' == $password)
43
			$password = $this->password;
44
		if ('' == $driver)
45
			$driver = $this->driver;
46
		/* establish connection, select database */
47
		if ($this->linkId === false) {
0 ignored issues
show
introduced by
The condition $this->linkId === false is always false.
Loading history...
48
			$this->linkId = NewADOConnection($driver);
49
			$this->linkId->Connect($host, $user, $password, $database);
50
		}
51
		return $this->linkId;
52
	}
53
54
	/* This only affects systems not using persistent connections */
55
56
	/**
57
	 * Db::disconnect()
58
	 * @return void
59
	 */
60
	public function disconnect() {
61
	}
62
63
	/**
64
	 * @param $string
65
	 * @return string
66
	 */
67
	public function real_escape($string) {
68
		return escapeshellarg($string);
69
	}
70
71
	/**
72
	 * @param $string
73
	 * @return string
74
	 */
75
	public function escape($string) {
76
		return escapeshellarg($string);
77
	}
78
79
	/**
80
	 * Db::dbAddslashes()
81
	 * @param mixed $str
82
	 * @return string
83
	 */
84
	public function dbAddslashes($str) {
85
		if (!isset($str) || $str == '')
86
			return '';
87
88
		return addslashes($str);
89
	}
90
91
	/**
92
	 * discard the query result
93
	 * @return void
94
	 */
95
	public function free() {
96
		//			@mysql_free_result($this->queryId);
97
		//			$this->queryId = 0;
98
	}
99
100
	/**
101
	 * Db::queryReturn()
102
	 *
103
	 * Sends an SQL query to the server like the normal query() command but iterates through
104
	 * any rows and returns the row or rows immediately or FALSE on error
105
	 *
106
	 * @param mixed $query SQL Query to be used
107
	 * @param string $line optionally pass __LINE__ calling the query for logging
108
	 * @param string $file optionally pass __FILE__ calling the query for logging
109
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
110
	 */
111
	public function queryReturn($query, $line = '', $file = '') {
112
		$this->query($query, $line, $file);
113
		if ($this->num_rows() == 0) {
114
			return false;
115
		} elseif ($this->num_rows() == 1) {
116
			$this->next_record(MYSQL_ASSOC);
0 ignored issues
show
introduced by
The constant MYSQL_ASSOC has been deprecated: 5.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
			$this->next_record(/** @scrutinizer ignore-deprecated */ MYSQL_ASSOC);
Loading history...
117
			return $this->Record;
118
		} else {
119
			$out = [];
120
			while ($this->next_record(MYSQL_ASSOC))
0 ignored issues
show
introduced by
The constant MYSQL_ASSOC has been deprecated: 5.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
			while ($this->next_record(/** @scrutinizer ignore-deprecated */ MYSQL_ASSOC))
Loading history...
121
				$out[] = $this->Record;
122
			return $out;
123
		}
124
	}
125
126
	/**
127
	 * db:qr()
128
	 *
129
	 *  alias of queryReturn()
130
	 *
131
	 * @param mixed $query SQL Query to be used
132
	 * @param string $line optionally pass __LINE__ calling the query for logging
133
	 * @param string $file optionally pass __FILE__ calling the query for logging
134
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
135
	 */
136
	public function qr($query, $line = '', $file = '') {
137
		return $this->queryReturn($query, $line, $file);
138
	}
139
140
	/**
141
	 * Db::query()
142
	 *
143
	 *  Sends an SQL query to the database
144
	 *
145
	 * @param mixed $queryString
146
	 * @param string $line
147
	 * @param string $file
148
	 * @return mixed 0 if no query or query id handler, safe to ignore this return
149
	 */
150
	public function query($queryString, $line = '', $file = '') {
151
		/* No empty queries, please, since PHP4 chokes on them. */
152
		/* The empty query string is passed on from the constructor,
153
		* when calling the class without a query, e.g. in situations
154
		* like these: '$db = new db_Subclass;'
155
		*/
156
		if ($queryString == '')
157
			return 0;
158
		if (!$this->connect()) {
159
			return 0;
160
			/* we already complained in connect() about that. */
161
		}
162
163
		// New query, discard previous result.
164
		if ($this->queryId !== false)
0 ignored issues
show
introduced by
The condition $this->queryId !== false is always true.
Loading history...
165
			$this->free();
166
167
		if ($this->Debug)
168
			printf("Debug: query = %s<br>\n", $queryString);
169
		if ($GLOBALS['log_queries'] !== false) {
170
			$this->log($queryString, $line, $file);
171
172
		}
173
174
		try {
175
			$this->queryId = $this->linkId->Execute($queryString);
0 ignored issues
show
Bug introduced by
The method Execute() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
			/** @scrutinizer ignore-call */ 
176
   $this->queryId = $this->linkId->Execute($queryString);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
		} catch (exception $e) {
0 ignored issues
show
Bug introduced by
The type MyDb\Adodb\exception was not found. Did you mean exception? If so, make sure to prefix the type with \.
Loading history...
177
			$this->emailError($queryString, $e, $line, $file);            
178
		}
179
		$this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $success seems to be never defined.
Loading history...
180
		$this->Row = 0;
181
182
		// Will return nada if it fails. That's fine.
183
		return $this->queryId;
184
	}
185
186
	/**
187
	 * Db::next_record()
188
	 * @param mixed $resultType
189
	 * @return bool
190
	 */
191
	public function next_record($resultType = MYSQL_ASSOC) {
0 ignored issues
show
Unused Code introduced by
The parameter $resultType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

191
	public function next_record(/** @scrutinizer ignore-unused */ $resultType = MYSQL_ASSOC) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
The constant MYSQL_ASSOC has been deprecated: 5.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

191
	public function next_record($resultType = /** @scrutinizer ignore-deprecated */ MYSQL_ASSOC) {
Loading history...
192
		if (!$this->queryId) {
193
			$this->halt('next_record called with no query pending.');
194
			return 0;
195
		}
196
		++$this->Row;
197
		$this->Record = $this->queryId->FetchRow();
0 ignored issues
show
Bug introduced by
The method FetchRow() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
		/** @scrutinizer ignore-call */ 
198
  $this->Record = $this->queryId->FetchRow();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
		$stat = is_array($this->Record);
199
		if (!$stat && $this->autoFree)
200
			$this->free();
201
		return $stat;
202
	}
203
204
	/* public: position in result set */
205
206
	/**
207
	 * Db::seek()
208
	 * @param integer $pos
209
	 * @return int
210
	 */
211
	public function seek($pos = 0) {
212
		if (isset($this->Rows[$pos])) {
213
			$this->Row = $pos;
214
		} else {
215
			$this->halt("seek($pos) failed: result has ".count($this->Rows).' rows');
216
			/* half assed attempt to save the day,
217
			* but do not consider this documented or even
218
			* desirable behaviour.
219
			*/
220
			return 0;
221
		}
222
		return 1;
223
	}
224
225
	/**
226
	 * Db::transactionBegin()
227
	 * @return bool
228
	 */
229
	public function transactionBegin() {
230
		return true;
231
	}
232
233
	/**
234
	 * Db::transactionCommit()
235
	 * @return bool
236
	 */
237
	public function transactionCommit() {
238
		return true;
239
	}
240
241
	/**
242
	 * Db::transactionAbort()
243
	 * @return bool
244
	 */
245
	public function transactionAbort() {
246
		return true;
247
	}
248
249
	/**
250
	 * Db::getLastInsertId()
251
	 *
252
	 * @param mixed $table
253
	 * @param mixed $field
254
	 * @return mixed
255
	 */
256
	public function getLastInsertId($table, $field) {
257
		return $this->linkId->Insert_ID($table, $field);
258
	}
259
260
	/* public: table locking */
261
262
	/**
263
	 * Db::lock()
264
	 * @param mixed  $table
265
	 * @param string $mode
266
	 * @return void
267
	 */
268
	public function lock($table, $mode = 'write') {
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

268
	public function lock($table, /** @scrutinizer ignore-unused */ $mode = 'write') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $table is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

268
	public function lock(/** @scrutinizer ignore-unused */ $table, $mode = 'write') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
269
		/*			$this->connect();
270
271
		* $query = "lock tables ";
272
		* if (is_array($table))
273
		* {
274
		* while (list($key,$value)=each($table))
275
		* {
276
		* if ($key == "read" && $key!=0)
277
		* {
278
		* $query .= "$value read, ";
279
		* }
280
		* else
281
		* {
282
		* $query .= "$value $mode, ";
283
		* }
284
		* }
285
		* $query = mb_substr($query,0,-2);
286
		* }
287
		* else
288
		* {
289
		* $query .= "$table $mode";
290
		* }
291
		* $res = @mysql_query($query, $this->linkId);
292
		* if (!$res)
293
		* {
294
		* $this->halt("lock($table, $mode) failed.");
295
		* return 0;
296
		* }
297
		* return $res;
298
		*/
299
	}
300
301
	/**
302
	 * Db::unlock()
303
	 * @return void
304
	 */
305
	public function unlock() {
306
		/*			$this->connect();
307
308
		* $res = @mysql_query("unlock tables");
309
		* if (!$res)
310
		* {
311
		* $this->halt("unlock() failed.");
312
		* return 0;
313
		* }
314
		* return $res;
315
		*/
316
	}
317
318
	/* public: evaluate the result (size, width) */
319
320
	/**
321
	 * Db::affectedRows()
322
	 *
323
	 * @return mixed
324
	 */
325
	public function affectedRows() {
326
		return @$this->linkId->Affected_Rows();
327
		//			return @$this->queryId->rowCount();
328
	}
329
330
	/**
331
	 * Db::num_rows()
332
	 *
333
	 * @return mixed
334
	 */
335
	public function num_rows() {
336
		return $this->queryId->NumRows();
337
	}
338
339
	/**
340
	 * Db::num_fields()
341
	 *
342
	 * @return mixed
343
	 */
344
	public function num_fields() {
345
		return $this->queryId->NumCols();
346
	}
347
348
	/**
349
	 * @param mixed $msg
350
	 * @param string $line
351
	 * @param string $file
352
	 * @return mixed|void
353
	 */
354
	public function haltmsg($msg, $line = '', $file = '') {
355
		$this->log("Database error: $msg", $line, $file, 'error');
356
		if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '')
357
			$this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error');
358
		$this->logBackTrace($msg, $line, $file);
359
	}
360
361
	/**
362
	 * Db::tableNames()
363
	 *
364
	 * @return array
365
	 */
366
	public function tableNames() {
367
		$return = [];
368
		$this->query('SHOW TABLES');
369
		$i = 0;
370
		while ($info = $this->queryId->FetchRow()) {
371
			$return[$i]['table_name'] = $info[0];
372
			$return[$i]['tablespace_name'] = $this->database;
373
			$return[$i]['database'] = $this->database;
374
			++$i;
375
		}
376
		return $return;
377
	}
378
}
379