Skip to content

Commit a166df6

Browse files
committed
code cleanup, refactor register_error
1 parent f807240 commit a166df6

File tree

9 files changed

+99
-100
lines changed

9 files changed

+99
-100
lines changed

lib/Database/ez_mysqli.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public function connect(
109109
// Try to establish the server database handle
110110
if ( ! $this->dbh = \mysqli_connect($host, $user, $password, $this->database->getName(), (int) $port) ) {
111111
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
112-
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
113112
} else {
114113
\mysqli_set_charset($this->dbh, $charset);
115114
$this->_connected = true;
@@ -132,16 +131,13 @@ public function select($name = '', $charset = '')
132131
if ( ! $this->dbh ) {
133132
// Must have an active database connection
134133
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
135-
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
136134
} elseif ( !\mysqli_select_db($this->dbh, $name) ) {
137135
// Try to connect to the database
138136
// Try to get error supplied by mysql if not use our own
139137
if ( !$str = \mysqli_error($this->dbh)) {
140138
$str = 'Unexpected error while trying to select database';
141139
}
142-
143140
$this->register_error($str . ' in ' .__FILE__ . ' on line ' . __LINE__);
144-
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
145141
} else {
146142
$this->database->setName($name);
147143
if ( $charset == '') {
@@ -241,7 +237,6 @@ private function fetch_prepared_result(&$stmt, $query)
241237
if ( $str = $stmt->error ) {
242238
$is_insert = true;
243239
$this->register_error($str);
244-
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
245240

246241
// If debug ALL queries
247242
$this->trace || $this->debug_all ? $this->debug() : null ;
@@ -359,7 +354,6 @@ public function query(string $query, bool $use_prepare = false)
359354
if ( $str = \mysqli_error($this->dbh) ) {
360355
$is_insert = true;
361356
$this->register_error($str);
362-
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
363357

364358
// If debug ALL queries
365359
$this->trace || $this->debug_all ? $this->debug() : null ;

lib/Database/ez_pdo.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ public function connect(
122122
$this->_connected = true;
123123
}
124124
} catch (\PDOException $e) {
125-
$this->register_error($e->getMessage());
126-
$this->show_errors ? \trigger_error($e->getMessage() . '- $dsn: ' . $dsn, \E_USER_WARNING) : null;
125+
$this->register_error($e->getMessage(). '- $dsn: ' . $dsn);
127126
}
128127

129128
return $this->_connected;
@@ -217,8 +216,7 @@ public function catch_error()
217216

218217
$error_str = \substr($error_str, 0, -2);
219218

220-
$this->register_error($error_str);
221-
$this->show_errors ? \trigger_error($error_str . ' ' . $this->last_query, \E_USER_WARNING) : null;
219+
$this->register_error($error_str. ' ' . $this->last_query);
222220

223221
return true;
224222
}

lib/Database/ez_pgsql.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public function connect(
9999
// Try to establish the server database handle
100100
if (!$this->dbh = \pg_connect($connect_string, true)) {
101101
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
102-
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
103102
} else {
104103
$this->_connected = true;
105104
}
@@ -210,9 +209,7 @@ public function query(string $query, bool $use_prepare = false)
210209

211210
// If there is an error then take note of it..
212211
if ($str = @\pg_last_error($this->dbh)) {
213-
$this->register_error($str);
214-
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
215-
return false;
212+
return $this->register_error($str);
216213
}
217214
// Query was an insert, delete, update, replace
218215
$is_insert = false;

lib/Database/ez_sqlite3.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ public function getArgType($arg)
118118
case 'string':return \SQLITE3_TEXT;
119119
default:
120120
$type_error = 'Argument is of invalid type ' . \gettype($arg);
121-
$this->register_error($type_error);
122-
$this->show_errors ? \trigger_error($type_error, \E_USER_WARNING) : null;
123-
return false;
121+
return $this->register_error($type_error);
124122
}
125123
}
126124

@@ -143,9 +141,7 @@ public function query_prepared(string $query, array $param = null)
143141

144142
if (!$ok) {
145143
$type_error = "Unable to bind param: $val";
146-
$this->register_error($type_error);
147-
$this->show_errors ? \trigger_error($type_error, \E_USER_WARNING) : null;
148-
return false;
144+
return $this->register_error($type_error);
149145
}
150146
}
151147

@@ -201,9 +197,7 @@ public function query(string $query, bool $use_prepare = false)
201197
// If there is an error then take note of it..
202198
if (@$this->dbh->lastErrorCode()) {
203199
$err_str = $this->dbh->lastErrorMsg();
204-
$this->register_error($err_str);
205-
$this->show_errors ? \trigger_error($err_str, \E_USER_WARNING) : null;
206-
return false;
200+
return $this->register_error($err_str);
207201
}
208202

209203
// Query was an insert, delete, update, replace

lib/Database/ez_sqlsrv.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public function connect($user = '', $password = '', $name = '', $host = 'localho
105105
// Try to establish the server database handle
106106
if (($this->dbh = @\sqlsrv_connect($host, $connectionOptions)) === false) {
107107
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
108-
$this->show_errors ? \trigger_error(\FAILED_CONNECTION, \E_USER_WARNING) : null;
109108
} else {
110109
$this->_connected = true;
111110
$this->conn_queries = 0;
@@ -207,7 +206,6 @@ public function query(string $query, bool $use_prepare = false)
207206
foreach ($errors as $error) {
208207
$sqlError = "ErrorCode: " . $error['code'] . " ### State: " . $error['SQLSTATE'] . " ### Error Message: " . $error['message'] . " ### Query: " . $query;
209208
$this->register_error($sqlError);
210-
$this->show_errors ? \trigger_error($sqlError, \E_USER_WARNING) : null;
211209
}
212210
}
213211

lib/ezsqlModel.php

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class ezsqlModel extends ezQuery implements ezsqlModelInterface
1313
{
14-
protected $isSecure = false;
14+
protected $isSecure = false;
1515
protected $secureOptions = null;
1616
protected $sslKey = null;
1717
protected $sslCert = null;
@@ -139,7 +139,7 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
139139
* Needed for echo of debug function
140140
* @var boolean Default is false
141141
*/
142-
protected $debug_echo_is_on = true;
142+
protected $debug_echo_is_on = false;
143143

144144
/**
145145
* Whether the database connection is established, or not
@@ -214,10 +214,7 @@ public function get_host_port( $host, $default = false )
214214
return array( $host, $port );
215215
}
216216

217-
/**
218-
* Print SQL/DB error - over-ridden by specific DB class
219-
*/
220-
public function register_error($err_str)
217+
public function register_error(string $err_str, bool $displayError = true)
221218
{
222219
// Keep track of last error
223220
$this->last_error = $err_str;
@@ -226,7 +223,12 @@ public function register_error($err_str)
226223
$this->captured_errors[] = array(
227224
'error_str' => $err_str,
228225
'query' => $this->last_query
229-
);
226+
);
227+
228+
if ($this->show_errors && $displayError)
229+
\trigger_error(\htmlentities($err_str), \E_USER_WARNING);
230+
231+
return false;
230232
}
231233

232234
public function show_errors()
@@ -265,8 +267,7 @@ public function log_query(string $query)
265267
\array_push($this->all_func_calls, $this->func_call);
266268
}
267269

268-
public function get_var(string $query = null, int $x = 0, int $y = 0,
269-
bool $use_prepare = false)
270+
public function get_var(string $query = null, int $x = 0, int $y = 0, bool $use_prepare = false)
270271
{
271272
// Log how the function was called
272273
$this->log_query("\$db->get_var(\"$query\",$x,$y)");
@@ -285,8 +286,7 @@ public function get_var(string $query = null, int $x = 0, int $y = 0,
285286
return (isset($values[$x]) && $values[$x] !== null) ? $values[$x] :null;
286287
}
287288

288-
public function get_row(string $query = null, $output = OBJECT, int $y = 0,
289-
bool $use_prepare = false)
289+
public function get_row(string $query = null, $output = OBJECT, int $y = 0, bool $use_prepare = false)
290290
{
291291
// Log how the function was called
292292
$this->log_query("\$db->get_row(\"$query\",$output,$y)");
@@ -311,8 +311,7 @@ public function get_row(string $query = null, $output = OBJECT, int $y = 0,
311311
}
312312
}
313313

314-
public function get_col(string $query = null, int $x = 0,
315-
bool $use_prepare = false)
314+
public function get_col(string $query = null, int $x = 0, bool $use_prepare = false)
316315
{
317316
$new_array = array();
318317

@@ -332,8 +331,7 @@ public function get_col(string $query = null, int $x = 0,
332331
return $new_array;
333332
}
334333

335-
public function get_results(string $query = null, $output = \OBJECT,
336-
bool $use_prepare = false)
334+
public function get_results(string $query = null, $output = \OBJECT, bool $use_prepare = false)
337335
{
338336
// Log how the function was called
339337
$this->log_query("\$db->get_results(\"$query\", $output, $use_prepare)");
@@ -358,10 +356,8 @@ public function get_results(string $query = null, $output = \OBJECT,
358356
}
359357
$i++;
360358
}
361-
return $new_array;
362-
} else {
363-
return array();
364359
}
360+
return $new_array;
365361
}
366362
}
367363

@@ -377,14 +373,15 @@ public function get_col_info(string $info_type = "name", int $col_offset = -1)
377373
}
378374

379375
return $new_array;
380-
} else {
381-
return $this->col_info[$col_offset]->{$info_type};
382376
}
377+
378+
return $this->col_info[$col_offset]->{$info_type};
383379
}
384380
}
385381

386382
/**
387383
* create cache directory if doesn't exists
384+
*
388385
* @param string $path
389386
*/
390387
public function create_cache(string $path = null)
@@ -396,10 +393,7 @@ public function create_cache(string $path = null)
396393
}
397394
}
398395

399-
/**
400-
* store_cache
401-
*/
402-
public function store_cache(string $query, bool $is_insert)
396+
public function store_cache(string $query, bool $is_insert = false)
403397
{
404398
// The would be cache file for this query
405399
$cache_file = $this->cache_dir.\_DS.\md5($query);
@@ -410,8 +404,7 @@ public function store_cache(string $query, bool $is_insert)
410404
) {
411405
$this->create_cache();
412406
if ( ! \is_dir($this->cache_dir) ) {
413-
$this->register_error("Could not open cache dir: $this->cache_dir");
414-
$this->show_errors ? \trigger_error("Could not open cache dir: $this->cache_dir", \E_USER_WARNING) : null;
407+
return $this->register_error("Could not open cache dir: $this->cache_dir");
415408
} else {
416409
// Cache all result values
417410
$result_cache = array(
@@ -428,9 +421,6 @@ public function store_cache(string $query, bool $is_insert)
428421
}
429422
}
430423

431-
/**
432-
* get_cache
433-
*/
434424
public function get_cache(string $query)
435425
{
436426
// The would be cache file for this query

0 commit comments

Comments
 (0)