Update from PSR-2 to PSR-12

- Tabs are indent
- Warning at 120, Error at 240 char length
This commit is contained in:
Clemens Schwaighofer
2021-07-14 10:00:56 +09:00
parent 11daac6d23
commit 6722468bdb
96 changed files with 2855 additions and 1677 deletions

View File

@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
/*********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2002/12/17
@@ -29,6 +30,8 @@
// as it actually has nothing to do with this one here ? (or at least
// put into separete function in this class)
declare(strict_types=1);
namespace CoreLibs\DB\Extended;
// subclass for one array handling
@@ -122,7 +125,7 @@ class ArrayIO extends \CoreLibs\DB\IO
reset($this->table_array);
$string = '';
foreach ($this->table_array as $column => $data_array) {
$string .= '<b>'.$column.'</b> -> '.$data_array['value'].'<br>';
$string .= '<b>' . $column . '</b> -> ' . $data_array['value'] . '<br>';
}
// add output to internal error_msg
if ($write === true) {
@@ -185,22 +188,23 @@ class ArrayIO extends \CoreLibs\DB\IO
return $this->table_array;
}
// delete query
$q = 'DELETE FROM '.$this->table_name.' WHERE ';
$q .= $this->pk_name.' = '.$this->table_array[$this->pk_name]['value'].' ';
$q = 'DELETE FROM ' . $this->table_name . ' WHERE ';
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
// delete files and build FK query
reset($this->table_array);
$q_where = '';
foreach ($this->table_array as $column => $data_array) {
// suchen nach bildern und lschen ...
if (!empty($this->table_array[$column]['file']) &&
file_exists($this->table_array[$column]['url'].$this->table_array[$column]['value'])
if (
!empty($this->table_array[$column]['file']) &&
file_exists($this->table_array[$column]['url'] . $this->table_array[$column]['value'])
) {
if (file_exists($this->table_array[$column]['path'].$this->table_array[$column]['value'])) {
unlink($this->table_array[$column]['path'].$this->table_array[$column]['value']);
if (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
unlink($this->table_array[$column]['path'] . $this->table_array[$column]['value']);
}
$file_name = str_replace('_tn', '', $this->table_array[$column]['value']);
if (file_exists($this->table_array[$column]['path'].$file_name)) {
unlink($this->table_array[$column]['path'].$file_name);
if (file_exists($this->table_array[$column]['path'] . $file_name)) {
unlink($this->table_array[$column]['path'] . $file_name);
}
}
// if we have a foreign key
@@ -209,7 +213,7 @@ class ArrayIO extends \CoreLibs\DB\IO
if ($q_where) {
$q_where .= ' AND ';
}
$q_where .= $column.' = '.$this->table_array[$column]['value'];
$q_where .= $column . ' = ' . $this->table_array[$column]['value'];
}
// allgemeines zurcksetzen des arrays
unset($this->table_array[$column]['value']);
@@ -217,7 +221,7 @@ class ArrayIO extends \CoreLibs\DB\IO
// attach fk row if there ...
if ($q_where) {
$q .= ' AND '.$q_where;
$q .= ' AND ' . $q_where;
}
// if 0, error
$this->pk_id = null;
@@ -258,16 +262,16 @@ class ArrayIO extends \CoreLibs\DB\IO
if ($q_where) {
$q_where .= ' AND ';
}
$q_where .= $column .= ' = '.$this->table_array[$column]['value'];
$q_where .= $column .= ' = ' . $this->table_array[$column]['value'];
}
}
$q = 'SELECT ';
$q .= $q_select;
$q .= ' FROM '.$this->table_name.' WHERE ';
$q .= $this->pk_name.' = '.$this->table_array[$this->pk_name]['value'].' ';
$q .= ' FROM ' . $this->table_name . ' WHERE ';
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
if ($q_where) {
$q .= ' AND '.$q_where;
$q .= ' AND ' . $q_where;
}
// if query was executed okay, else set error
@@ -277,11 +281,13 @@ class ArrayIO extends \CoreLibs\DB\IO
foreach ($this->table_array as $column => $data_array) {
// wenn "edit" dann gib daten wie in DB zurück, ansonten aufbereiten fr ausgabe
// ?? sollte das nicht drauen ??? man weis ja net was da drin steht --> is noch zu berlegen
// echo 'EDIT: $edit | Spalte: $column | type: '.$this->table_array[$column]['type'].' | Res: '.$res[$column].'<br>';
// $this->log->debug('DB READ', 'EDIT: $edit | Spalte: $column | type: '
// .$this->table_array[$column]['type'].' | Res: '.$res[$column]);
if ($edit) {
$this->table_array[$column]['value'] = $res[$column];
// if password, also write to hidden
if (isset($this->table_array[$column]['type']) &&
if (
isset($this->table_array[$column]['type']) &&
$this->table_array[$column]['type'] == 'password'
) {
$this->table_array[$column]['HIDDEN_value'] = $res[$column];
@@ -336,12 +342,12 @@ class ArrayIO extends \CoreLibs\DB\IO
// falls in 'delete' 'ja' dann loeschen (und gibts eh nur beim update)
if ($this->table_array[$column]['delete']) {
unset($this->table_array[$column]['delete']);
if (file_exists($this->table_array[$column]['path'].$this->table_array[$column]['value'])) {
unlink($this->table_array[$column]['path'].$this->table_array[$column]['value']);
if (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
unlink($this->table_array[$column]['path'] . $this->table_array[$column]['value']);
}
$file_name = str_replace('_tn', '', $this->table_array[$column]['value']);
if (file_exists($this->table_array[$column]['path'].$file_name)) {
unlink($this->table_array[$column]['path'].$file_name);
if (file_exists($this->table_array[$column]['path'] . $file_name)) {
unlink($this->table_array[$column]['path'] . $file_name);
}
$this->table_array[$column]['value'] = '';
} else {
@@ -351,26 +357,26 @@ class ArrayIO extends \CoreLibs\DB\IO
// mozilla, patch
$fn_name = explode('/', $this->table_array[$column]['dn']);
$this->table_array[$column]['dn'] = $fn_name[count($fn_name)-1];
$this->table_array[$column]['dn'] = $fn_name[count($fn_name) - 1];
$filename_parts = explode('.', $this->table_array[$column]['dn']);
$ext = end($filename_parts);
array_splice($filename_parts, -1, 1);
$name = str_replace(' ', '_', implode('.', $filename_parts));
$file_name = $name.'.'.$ext;
$file_name = $name . '.' . $ext;
//echo 'Dn: $file_name';
copy($this->table_array[$column]['tmp'], $this->table_array[$column]['path'].$file_name);
copy($this->table_array[$column]['tmp'], $this->table_array[$column]['path'] . $file_name);
// automatisch thumbnail generieren, geht nur mit convert (ImageMagic!!!), aber nur bei bild ..
if (strtolower($ext) == 'jpeg' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif' || strtolower($ext) == 'png') {
$file_name_tn = $name.'_tn.'.$ext;
$input = $this->table_array[$column]['path'].$file_name;
$output = $this->table_array[$column]['path'].$file_name_tn;
$com = 'convert -geometry 115 '.$input.' '.$output;
if (in_array(strtolower($ext), ['jpeg', 'jpg', 'gif', 'png'])) {
$file_name_tn = $name . '_tn.' . $ext;
$input = $this->table_array[$column]['path'] . $file_name;
$output = $this->table_array[$column]['path'] . $file_name_tn;
$com = 'convert -geometry 115 ' . $input . ' ' . $output;
exec($com);
$this->table_array[$column]['value'] = $file_name_tn;
} else {
$this->table_array[$column]['value'] = $file_name;
}
} elseif (file_exists($this->table_array[$column]['path'].$this->table_array[$column]['value'])) {
} elseif (file_exists($this->table_array[$column]['path'] . $this->table_array[$column]['value'])) {
// mach gar nix, wenn bild schon da ???
}
} // delete or upload
@@ -378,13 +384,15 @@ class ArrayIO extends \CoreLibs\DB\IO
/********************************* END FILE **************************************/
// do not write 'pk' (primary key) or 'view' values
if (!isset($this->table_array[$column]['pk']) &&
if (
!isset($this->table_array[$column]['pk']) &&
isset($this->table_array[$column]['type']) &&
$this->table_array[$column]['type'] != 'view' &&
strlen($column) > 0
) {
// for password use hidden value if main is not set
if (isset($this->table_array[$column]['type']) &&
if (
isset($this->table_array[$column]['type']) &&
$this->table_array[$column]['type'] == 'password' &&
empty($this->table_array[$column]['value'])
) {
@@ -394,7 +402,7 @@ class ArrayIO extends \CoreLibs\DB\IO
if (strlen($q_data)) {
$q_data .= ', ';
}
$q_data .= $column.' = ';
$q_data .= $column . ' = ';
} else {
// this is insert
if (strlen($q_data)) {
@@ -407,15 +415,18 @@ class ArrayIO extends \CoreLibs\DB\IO
}
// integer is different
if (isset($this->table_array[$column]['int']) || isset($this->table_array[$column]['int_null'])) {
$this->log->debug('write_check', '['.$column.']['.$this->table_array[$column]['value'].']['.$this->table_array[$column]['type'].'] '.
'VALUE SET: '.(string)isset($this->table_array[$column]['value']).
' | INT NULL: '.(string)isset($this->table_array[$column]['int_null']));
if (isset($this->table_array[$column]['value']) &&
$this->log->debug('WRITE CHECK', '[' . $column . '][' . $this->table_array[$column]['value'] . ']'
. '[' . $this->table_array[$column]['type'] . '] '
. 'VALUE SET: ' . (string)isset($this->table_array[$column]['value'])
. ' | INT NULL: ' . (string)isset($this->table_array[$column]['int_null']));
if (
isset($this->table_array[$column]['value']) &&
!$this->table_array[$column]['value'] &&
isset($this->table_array[$column]['int_null'])
) {
$_value = 'NULL';
} elseif (!isset($this->table_array[$column]['value']) ||
} elseif (
!isset($this->table_array[$column]['value']) ||
(isset($this->table_array[$column]['value']) && !$this->table_array[$column]['value'])
) {
$_value = 0;
@@ -425,10 +436,11 @@ class ArrayIO extends \CoreLibs\DB\IO
$q_data .= $_value;
} elseif (isset($this->table_array[$column]['bool'])) {
// boolean storeage (reverse check on ifset)
$q_data .= "'".$this->dbBoolean($this->table_array[$column]['value'], true)."'";
$q_data .= "'" . $this->dbBoolean($this->table_array[$column]['value'], true) . "'";
} elseif (isset($this->table_array[$column]['interval'])) {
// for interval we check if no value, then we set null
if (!isset($this->table_array[$column]['value']) ||
if (
!isset($this->table_array[$column]['value']) ||
(isset($this->table_array[$column]['value']) && !$this->table_array[$column]['value'])
) {
$_value = 'NULL';
@@ -442,9 +454,14 @@ class ArrayIO extends \CoreLibs\DB\IO
} else {
// if the error check is json, we set field to null if NOT set
// else normal string write
if (isset($this->table_array[$column]['error_check']) &&
if (
isset($this->table_array[$column]['error_check']) &&
$this->table_array[$column]['error_check'] == 'json' &&
(!isset($this->table_array[$column]['value']) || (isset($this->table_array[$column]['value']) && !$this->table_array[$column]['value']))
(
!isset($this->table_array[$column]['value']) ||
(isset($this->table_array[$column]['value']) &&
!$this->table_array[$column]['value'])
)
) {
$q_data .= 'NULL';
} else {
@@ -452,7 +469,9 @@ class ArrayIO extends \CoreLibs\DB\IO
$q_data .= "'";
// if add slashes do convert & add slashes else write AS is
if ($addslashes) {
$q_data .= $this->dbEscapeString($this->convertEntities($this->table_array[$column]['value']));
$q_data .= $this->dbEscapeString(
$this->convertEntities($this->table_array[$column]['value'])
);
} else {
$q_data .= $this->dbEscapeString($this->table_array[$column]['value']);
}
@@ -472,14 +491,14 @@ class ArrayIO extends \CoreLibs\DB\IO
if (!empty($q_where)) {
$q_where .= ' AND ';
}
$q_where .= $column .= ' = '.$this->table_array[$column]['value'];
$q_where .= $column .= ' = ' . $this->table_array[$column]['value'];
}
}
// if no PK set, then get max ID from DB
if (!$this->table_array[$this->pk_name]['value']) {
// max id, falls INSERT
$q = 'SELECT MAX('.$this->pk_name.') + 1 AS pk_id FROM '.$this->table_name;
$q = 'SELECT MAX(' . $this->pk_name . ') + 1 AS pk_id FROM ' . $this->table_name;
$res = $this->dbReturnRow($q);
if (!isset($res['pk_id'])) {
$res['pk_id'] = 1;
@@ -488,19 +507,19 @@ class ArrayIO extends \CoreLibs\DB\IO
}
if (!$insert) {
$q = 'UPDATE '.$this->table_name.' SET ';
$q = 'UPDATE ' . $this->table_name . ' SET ';
$q .= $q_data;
$q .= ' WHERE ';
$q .= $this->pk_name.' = '.$this->table_array[$this->pk_name]['value'].' ';
$q .= $this->pk_name . ' = ' . $this->table_array[$this->pk_name]['value'] . ' ';
if (!empty($q_where)) {
$q .= ' AND '.$q_where;
$q .= ' AND ' . $q_where;
}
// set pk_id ... if it has changed or so
$this->pk_id = $this->table_array[$this->pk_name]['value'];
} else {
$q = 'INSERT INTO '.$this->table_name.' ';
$q .= '('.$q_vars.') ';
$q .= 'VALUES ('.$q_data.')';
$q = 'INSERT INTO ' . $this->table_name . ' ';
$q .= '(' . $q_vars . ') ';
$q .= 'VALUES (' . $q_data . ')';
// write primary key too
// if ($q_data)
// $q .= ", ";
@@ -520,6 +539,7 @@ class ArrayIO extends \CoreLibs\DB\IO
// return the table if needed
return $this->table_array;
}
} // end of class
// end of class
}
// __END__

View File

@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
/********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2000/11/23
@@ -107,9 +108,11 @@
* _db_io()
* - pseudo deconstructor - functionality moved to db_close
* $string info($show=1)
* - returns a string various info about class (version, authoer, etc), if $show set to 0, it will not be appended to the error_msgs string
* - returns a string various info about class (version, authoer, etc)
* - if $show set to 0, it will not be appended to the error_msgs string
* $string db_info($show=1)
* - returns a string with info about db connection, etc, if $show set to 0, it will not be appended to the error_msgs string
* - returns a string with info about db connection, etc, if $show set to 0,
* - it will not be appended to the error_msgs string
* $string db_dump_data($query=0)
* - returns a string with all data of that query or if no query given with all data in the cursor_ext
* 0/$cursor db_exec($query=0)
@@ -135,13 +138,15 @@
* $string db_boolean(string)
* - if the string value is 't' or 'f' it returns correct TRUE/FALSE for php
* $primary_key db_write_data($write_array, $not_write_array, $primary_key, $table, $data = [])
* - writes into one table based on arrays of columns to write and not write, reads data from global vars or optional array
* - writes into one table based on arrays of columns to write and not write,
* - reads data from global vars or optional array
* $boolean db_set_schema(schema)
* - sets search path to a schema
* $boolean db_set_encoding(encoding)
* - sets an encoding for this database output and input
* $string db_time_format($age/datetime diff, $micro_time = false/true)
* - returns a nice formatted time string based on a age or datetime difference (postgres only), micro time is default false
* - returns a nice formatted time string based on a age or datetime difference
* - (postgres only), micro time is default false
*
* PRIVATE METHODS
* _db_error()
@@ -159,10 +164,12 @@
* string _db_debug_prepare($prepare_id, $data_array)
* - returns the prepared statement with the actual data. for debug purposes only
* none _db_debug($debug_id, $string, $id, $type)
* - wrapper for normal debug, adds prefix data from id & type and strips all HTML from the query data (color codes, etc) via flag to debug call
* - wrapper for normal debug, adds prefix data from id & type and strips
* - all HTML from the query data (color codes, etc) via flag to debug call
*
* HISTORY:
* 2008/10/25 (cs) add db_boolean to fix the postgres to php boolean var problem (TODO: implement this in any select return)
* 2008/10/25 (cs) add db_boolean to fix the postgres to php boolean var problem
* (TODO: implement this in any select return)
* 2008/07/03 (cs) add db_write_data function, original written for inventory tool "invSQLWriteData"
* 2008/04/16 (cs) add db_escape_string function for correct string escape
* 2007/11/14 (cs) add a prepare debug statement to replace the placeholders with the actual data in a prepared statement
@@ -241,6 +248,8 @@
* 23.11.2000: erster Test
*********************************************************************/
declare(strict_types=1);
namespace CoreLibs\DB;
class IO extends \CoreLibs\Basic
@@ -324,7 +333,8 @@ class IO extends \CoreLibs\Basic
$this->db_pwd = $db_config['db_pass'] ?? '';
$this->db_host = $db_config['db_host'] ?? '';
$this->db_port = !empty($db_config['db_port']) ? $db_config['db_port'] : 5432;
$this->db_schema = !empty($db_config['db_schema']) ? $db_config['db_schema'] : ''; // do not set to 'public' if not set, because the default is already public
// do not set to 'public' if not set, because the default is already public
$this->db_schema = !empty($db_config['db_schema']) ? $db_config['db_schema'] : '';
$this->db_encoding = !empty($db_config['db_encoding']) ? $db_config['db_encoding'] : '';
$this->db_type = $db_config['db_type'] ?? '';
$this->db_ssl = !empty($db_config['db_ssl']) ? $db_config['db_ssl'] : 'allow';
@@ -343,16 +353,19 @@ class IO extends \CoreLibs\Basic
$this->error_string['14'] = 'Can\'t connect to DB server';
$this->error_string['15'] = 'Can\'t select DB';
$this->error_string['16'] = 'No DB Handler found / connect or reconnect failed';
$this->error_string['17'] = 'All dbReturn* methods work only with SELECT statements, please use dbExec for everything else';
$this->error_string['17'] = 'All dbReturn* methods work only with SELECT statements, '
. 'please use dbExec for everything else';
$this->error_string['18'] = 'Query not found in cache. Nothing has been reset';
$this->error_string['19'] = 'Wrong PK name given or no PK name given at all, can\'t get Insert ID';
$this->error_string['20'] = 'Found given Prepare Statement Name in array, Query not prepared, will use existing one';
$this->error_string['20'] = 'Found given Prepare Statement Name in array, '
. 'Query not prepared, will use existing one';
$this->error_string['21'] = 'Query Prepare failed';
$this->error_string['22'] = 'Query Execute failed';
$this->error_string['23'] = 'Query Execute failed, data array does not match placeholders';
$this->error_string['24'] = 'Missing prepared query entry for execute.';
$this->error_string['25'] = 'Prepare query data is not in array format.';
$this->error_string['30'] = 'Query call in a possible endless loop. Was called more than '.$this->MAX_QUERY_CALL.' times';
$this->error_string['30'] = 'Query call in a possible endless loop. '
. 'Was called more than ' . $this->MAX_QUERY_CALL . ' times';
$this->error_string['31'] = 'Could not fetch PK after query insert';
$this->error_string['32'] = 'Multiple PK return as array';
$this->error_string['33'] = 'Returning PK was not found';
@@ -412,7 +425,14 @@ class IO extends \CoreLibs\Basic
private function __connectToDB(): bool
{
// generate connect string
$this->dbh = $this->db_functions->__dbConnect($this->db_host, $this->db_user, $this->db_pwd, $this->db_name, $this->db_port, $this->db_ssl);
$this->dbh = $this->db_functions->__dbConnect(
$this->db_host,
$this->db_user,
$this->db_pwd,
$this->db_name,
$this->db_port,
$this->db_ssl
);
// if no dbh here, we couldn't connect to the DB itself
if (!$this->dbh) {
$this->error_id = 14;
@@ -515,13 +535,13 @@ class IO extends \CoreLibs\Basic
$array = [];
}
foreach ($array as $key => $value) {
$string .= $this->nbsp.'<b>'.$key.'</b> => ';
$string .= $this->nbsp . '<b>' . $key . '</b> => ';
if (is_array($value)) {
$this->nbsp .= '&nbsp;&nbsp;&nbsp;';
$string .= '<br>';
$string .= $this->__printArray($value);
} else {
$string .= $value.'<br>';
$string .= $value . '<br>';
}
}
$this->nbsp = substr_replace($this->nbsp, '', -18, 18);
@@ -540,10 +560,10 @@ class IO extends \CoreLibs\Basic
{
$prefix = '';
if ($id) {
$prefix .= '[<span style="color: #920069;">'.$id.'</span>] ';
$prefix .= '[<span style="color: #920069;">' . $id . '</span>] ';
}
if ($type) {
$prefix .= '{<span style="font-style: italic; color: #3f0092;">'.$type.'</span>} ';
$prefix .= '{<span style="font-style: italic; color: #3f0092;">' . $type . '</span>} ';
}
if ($prefix) {
$prefix .= '- ';
@@ -576,12 +596,26 @@ class IO extends \CoreLibs\Basic
// okay, an error occured
if ($this->error_id) {
// write error msg ...
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$this->error_id.': '.$this->error_string[$this->error_id].($msg ? ', '.$msg : '').'</span>', 'DB_ERROR', $where_called);
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $this->error_id . ': '
. $this->error_string[$this->error_id] . ($msg ? ', ' . $msg : '')
. '</span>',
'DB_ERROR',
$where_called
);
$this->had_error = $this->error_id;
// write detailed error log
}
if ($this->warning_id) {
$this->__dbDebug('db', '<span style="color: orange;"><b>DB-Warning</b> '.$this->warning_id.': '.$this->error_string[$this->warning_id].($msg ? ', '.$msg : '').'</span>', 'DB_WARNING', $where_called);
$this->__dbDebug(
'db',
'<span style="color: orange;"><b>DB-Warning</b> ' . $this->warning_id . ': '
. $this->error_string[$this->warning_id]
. ($msg ? ', ' . $msg : '') . '</span>',
'DB_WARNING',
$where_called
);
$this->had_warning = $this->warning_id;
}
// unset the error/warning vars
@@ -597,7 +631,8 @@ class IO extends \CoreLibs\Basic
private function __dbConvertEncoding($row)
{
// only do if array, else pass through row (can be false)
if (!is_array($row) || empty($this->to_encoding) || empty($this->db_encoding)
if (
!is_array($row) || empty($this->to_encoding) || empty($this->db_encoding)
) {
return $row;
}
@@ -623,8 +658,8 @@ class IO extends \CoreLibs\Basic
// get the keys from data array
$keys = array_keys($data);
// because the placeholders start with $ and at 1, we need to increase each key and prefix it with a $ char
for ($i = 0, $iMax = count($keys); $i < $iMax; $i ++) {
$keys[$i] = '$'.($keys[$i] + 1);
for ($i = 0, $iMax = count($keys); $i < $iMax; $i++) {
$keys[$i] = '$' . ($keys[$i] + 1);
}
// simply replace the $1, $2, ... with the actual data and return it
return str_replace(array_reverse($keys), array_reverse($data), $this->prepare_cursor[$stm_name]['query']);
@@ -660,7 +695,7 @@ class IO extends \CoreLibs\Basic
*/
private function __dbPrepareExec(string $query, string $pk_name)
{
$matches= [];
$matches = [];
// to either use the returning method or the guess method for getting primary keys
$this->returning_id = false;
// set the query
@@ -702,13 +737,13 @@ class IO extends \CoreLibs\Basic
if (!preg_match("/ returning /i", $this->query) && $this->pk_name && $this->pk_name != 'NULL') {
// check if this query has a ; at the end and remove it
$this->query = preg_replace("/(;\s*)$/", '', $this->query);
$this->query .= " RETURNING ".$this->pk_name;
$this->query .= " RETURNING " . $this->pk_name;
$this->returning_id = true;
} elseif (preg_match("/ returning (.*)/i", $this->query, $matches)) {
if ($this->pk_name && $this->pk_name != 'NULL') {
// add the primary key if it is not in the returning set
if (!preg_match("/$this->pk_name/", $matches[1])) {
$this->query .= " , ".$this->pk_name;
$this->query .= " , " . $this->pk_name;
}
}
$this->returning_id = true;
@@ -736,7 +771,8 @@ class IO extends \CoreLibs\Basic
}
// count up the run, if this is run more than the max_run then exit with error
// if set to -1, then ignore it
if ($this->MAX_QUERY_CALL != -1 &&
if (
$this->MAX_QUERY_CALL != -1 &&
$this->query_called[$md5] > $this->MAX_QUERY_CALL
) {
$this->error_id = 30;
@@ -775,14 +811,15 @@ class IO extends \CoreLibs\Basic
$this->num_fields = $this->db_functions->__dbNumFields($this->cursor);
// set field names
$this->field_names = [];
for ($i = 0; $i < $this->num_fields; $i ++) {
for ($i = 0; $i < $this->num_fields; $i++) {
$this->field_names[] = $this->db_functions->__dbFieldName($this->cursor, $i);
}
} elseif ($this->__checkQueryForInsert($this->query)) {
// if not select do here
// count affected rows
$this->num_rows = $this->db_functions->__dbAffectedRows($this->cursor);
if (($this->__checkQueryForInsert($this->query, true) && $this->pk_name != 'NULL') ||
if (
($this->__checkQueryForInsert($this->query, true) && $this->pk_name != 'NULL') ||
($this->__checkQueryForUpdate($this->query) && $this->returning_id)
) {
// set insert_id
@@ -798,22 +835,30 @@ class IO extends \CoreLibs\Basic
// echo "** PREPARE RETURNING FOR CURSOR: ".$this->cursor."<br>";
// we have returning, now we need to check if we get one or many returned
// we'll need to loop this, if we have multiple insert_id returns
while ($_insert_id = $this->db_functions->__dbFetchArray(
$this->cursor,
$this->db_functions->__dbResultType(true)
)) {
while (
$_insert_id = $this->db_functions->__dbFetchArray(
$this->cursor,
$this->db_functions->__dbResultType(true)
)
) {
// echo "*** RETURNING: ".print_r($_insert_id, true)."<br>";
$this->insert_id[] = $_insert_id;
$this->insert_id_arr[] = $_insert_id;
}
// if we have only one, revert from array to single
if (count($this->insert_id) == 1) {
// echo "* SINGLE DATA CONVERT: ".count($this->insert_id[0])." => ".array_key_exists($this->pk_name, $this->insert_id[0])."<br>";
// echo "* PK DIRECT: ".(isset($this->insert_id[0][$this->pk_name]) ? $this->insert_id[0][$this->pk_name] : '[NO PK NAME SET]' )."<Br>";
// if this has only the pk_name, then only return this, else array of all data (but without the position)
// example if insert_id[0]['foo'] && insert_id[0]['bar'] it will become insert_id['foo'] & insert_id['bar']
// if only ['foo_id'] and it is the PK then the PK is directly written to the insert_id
if (count($this->insert_id[0]) > 1 ||
// $this->log->debug('SINGLE DATA CONVERT', count($this->insert_id[0])." => "
// .array_key_exists($this->pk_name, $this->insert_id[0]));
// $this->log->debug('PK DIRECT', (isset($this->insert_id[0][$this->pk_name]) ?
// $this->insert_id[0][$this->pk_name] : '[NO PK NAME SET]' ));
// if this has only the pk_name, then only return this,
// else array of all data (but without the position)
// example if insert_id[0]['foo'] && insert_id[0]['bar']
// it will become insert_id['foo'] & insert_id['bar']
// if only ['foo_id'] and it is the PK then the
// PK is directly written to the insert_id
if (
count($this->insert_id[0]) > 1 ||
!array_key_exists($this->pk_name, $this->insert_id[0])
) {
$this->insert_id_ext = $this->insert_id[0];
@@ -916,7 +961,8 @@ class IO extends \CoreLibs\Basic
return false;
}
// ok entry, set
if ($max_calls == -1 ||
if (
$max_calls == -1 ||
$max_calls > 0
) {
$this->MAX_QUERY_CALL = $max_calls;
@@ -997,7 +1043,7 @@ class IO extends \CoreLibs\Basic
if (!$db_schema) {
return false;
}
$q = "SET search_path TO '".$this->dbEscapeString($db_schema)."'";
$q = "SET search_path TO '" . $this->dbEscapeString($db_schema) . "'";
return $this->dbExec($q);
}
@@ -1023,7 +1069,7 @@ class IO extends \CoreLibs\Basic
if (!$db_encoding) {
return false;
}
$q = "SET client_encoding TO '".$this->dbEscapeString($db_encoding)."'";
$q = "SET client_encoding TO '" . $this->dbEscapeString($db_encoding) . "'";
return $this->dbExec($q);
}
@@ -1067,18 +1113,18 @@ class IO extends \CoreLibs\Basic
public function dbInfo(bool $show = true): string
{
$string = '';
$string .= '{b}-DB-info->{/b} Connected to db {b}\''.$this->db_name.'\'{/b} ';
$string .= 'with schema {b}\''.$this->db_schema.'\'{/b} ';
$string .= 'as user {b}\''.$this->db_user.'\'{/b} ';
$string .= 'at host {b}\''.$this->db_host.'\'{/b} ';
$string .= 'on port {b}\''.$this->db_port.'\'{/b} ';
$string .= 'with ssl mode {b}\''.$this->db_ssl.'\'{/b}{br}';
$string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}'.($this->db_debug ? 'Yes' : 'No').'{/b}';
$string .= '{b}-DB-info->{/b} Connected to db {b}\'' . $this->db_name . '\'{/b} ';
$string .= 'with schema {b}\'' . $this->db_schema . '\'{/b} ';
$string .= 'as user {b}\'' . $this->db_user . '\'{/b} ';
$string .= 'at host {b}\'' . $this->db_host . '\'{/b} ';
$string .= 'on port {b}\'' . $this->db_port . '\'{/b} ';
$string .= 'with ssl mode {b}\'' . $this->db_ssl . '\'{/b}{br}';
$string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}' . ($this->db_debug ? 'Yes' : 'No') . '{/b}';
if ($show === true) {
// if debug, remove / change b
$this->__dbDebug('db', str_replace(['{b}', '{/b}', '{br}'], ['', '', ' **** '], $string), 'dbInfo');
} else {
$string = $string.'{br}';
$string = $string . '{br}';
}
// for direct print, change to html
return str_replace(['{b}', '{/b}', '{br}'], ['<b>', '</b>', '<br>'], $string);
@@ -1120,8 +1166,12 @@ class IO extends \CoreLibs\Basic
* - if set to 3, after EACH row, the data will be reset,
* no caching is done except for basic (count, etc)
* @param string $query Query string
* @param int $reset reset status: 1: read cache, clean at the end, 2: read new, clean at end, 3: never cache
* @param bool $assoc_only true to only returned the named and not index position ones
* @param int $reset reset status:
* 1: read cache, clean at the end
* 2: read new, clean at end
* 3: never cache
* @param bool $assoc_only true to only returned the named and not
* index position ones
* @return array|bool return array data or false on error/end
* @suppress PhanTypeMismatchDimFetch
*/
@@ -1161,7 +1211,9 @@ class IO extends \CoreLibs\Basic
if ($reset && !$this->cursor_ext[$md5]['pos']) {
$this->cursor_ext[$md5]['cursor'] = null;
}
// $this->debug('MENU', 'Reset: '.$reset.', Cursor: '.$this->cursor_ext[$md5]['cursor'].', Pos: '.$this->cursor_ext[$md5]['pos'].', Query: '.$query);
// $this->debug('MENU', 'Reset: '.$reset.', Cursor: '
// .$this->cursor_ext[$md5]['cursor'].', Pos: '.$this->cursor_ext[$md5]['pos']
// .', Query: '.$query);
// if no cursor yet, execute
if (!$this->cursor_ext[$md5]['cursor']) {
@@ -1203,12 +1255,18 @@ class IO extends \CoreLibs\Basic
if ($this->cursor_ext[$md5]['cursor']) {
if ($this->cursor_ext[$md5]['firstcall'] == 1) {
// count the rows returned (if select)
$this->cursor_ext[$md5]['num_rows'] = $this->db_functions->__dbNumRows($this->cursor_ext[$md5]['cursor']);
$this->cursor_ext[$md5]['num_rows'] =
$this->db_functions->__dbNumRows($this->cursor_ext[$md5]['cursor']);
// count the fields
$this->cursor_ext[$md5]['num_fields'] = $this->db_functions->__dbNumFields($this->cursor_ext[$md5]['cursor']);
$this->cursor_ext[$md5]['num_fields'] =
$this->db_functions->__dbNumFields($this->cursor_ext[$md5]['cursor']);
// set field names
for ($i = 0; $i < $this->cursor_ext[$md5]['num_fields']; $i ++) {
$this->cursor_ext[$md5]['field_names'][] = $this->db_functions->__dbFieldName($this->cursor_ext[$md5]['cursor'], $i);
for ($i = 0; $i < $this->cursor_ext[$md5]['num_fields']; $i++) {
$this->cursor_ext[$md5]['field_names'][] =
$this->db_functions->__dbFieldName(
$this->cursor_ext[$md5]['cursor'],
$i
);
}
// reset first call vars
$this->cursor_ext[$md5]['firstcall'] = 0;
@@ -1237,24 +1295,34 @@ class IO extends \CoreLibs\Basic
// check if end of output ...
if ($this->cursor_ext[$md5]['pos'] >= $this->cursor_ext[$md5]['num_rows']) {
$this->cursor_ext[$md5]['pos'] = 0;
# if not reset given, set the cursor to true, so in a cached call on a different page we don't get problems from DB connection (as those will be LOST)
// if not reset given, set the cursor to true, so in a cached
// call on a different page we don't get problems from
// DB connection (as those will be LOST)
$this->cursor_ext[$md5]['cursor'] = 1;
$return = false;
} else {
// unset return value ...
$return = [];
for ($i = 0; $i < $this->cursor_ext[$md5]['num_fields']; $i ++) {
for ($i = 0; $i < $this->cursor_ext[$md5]['num_fields']; $i++) {
// create mixed return array
if ($assoc_only === false && isset($this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i])) {
if (
$assoc_only === false &&
isset($this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i])
) {
$return[$i] = $this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i];
}
// named part
if (isset($this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i])) {
$return[$this->cursor_ext[$md5]['field_names'][$i]] = $this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i];
$return[$this->cursor_ext[$md5]['field_names'][$i]] =
$this->cursor_ext[$md5]['data']
[$this->cursor_ext[$md5]['pos']][$i];
} else {
// throws PhanTypeMismatchDimFetch error, but in this case we know we will access only named array parts
// throws PhanTypeMismatchDimFetch error, but in this
// case we know we will access only named array parts
// @suppress PhanTypeMismatchDimFetch
$return[$this->cursor_ext[$md5]['field_names'][$i]] = $this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$this->cursor_ext[$md5]['field_names'][$i]];
$return[$this->cursor_ext[$md5]['field_names'][$i]] =
$this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]
['pos']][$this->cursor_ext[$md5]['field_names'][$i]];
}
}
$this->cursor_ext[$md5]['pos'] ++;
@@ -1393,7 +1461,12 @@ class IO extends \CoreLibs\Basic
} else {
// if no async running print error
$this->error_id = 42;
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> No async query has been started yet.</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> No async query '
. 'has been started yet.</span>',
'DB_ERROR'
);
return false;
}
}
@@ -1471,7 +1544,7 @@ class IO extends \CoreLibs\Basic
$rows = [];
while ($res = $this->dbFetchArray($cursor, $assoc_only)) {
$data = [];
for ($i = 0; $i < $this->num_fields; $i ++) {
for ($i = 0; $i < $this->num_fields; $i++) {
$data[$this->field_names[$i]] = $res[$this->field_names[$i]];
}
$rows[] = $data;
@@ -1519,7 +1592,7 @@ class IO extends \CoreLibs\Basic
*/
public function dbShowTableMetaData(string $table, string $schema = '')
{
$table = ($schema ? $schema.'.' : '').$table;
$table = ($schema ? $schema . '.' : '') . $table;
$array = $this->db_functions->__dbMetaData($table);
if (!is_array($array)) {
@@ -1580,12 +1653,15 @@ class IO extends \CoreLibs\Basic
}
// if no returning, then add it
if (!preg_match("/ returning /i", $query) && $this->prepare_cursor[$stm_name]['pk_name']) {
$query .= " RETURNING ".$this->prepare_cursor[$stm_name]['pk_name'];
$query .= " RETURNING " . $this->prepare_cursor[$stm_name]['pk_name'];
$this->prepare_cursor[$stm_name]['returning_id'] = true;
} elseif (preg_match("/ returning (.*)/i", $query, $matches) && $this->prepare_cursor[$stm_name]['pk_name']) {
} elseif (
preg_match("/ returning (.*)/i", $query, $matches) &&
$this->prepare_cursor[$stm_name]['pk_name']
) {
// if returning exists but not pk_name, add it
if (!preg_match("/{$this->prepare_cursor[$stm_name]['pk_name']}/", $matches[1])) {
$query .= " , ".$this->prepare_cursor[$stm_name]['pk_name'];
$query .= " , " . $this->prepare_cursor[$stm_name]['pk_name'];
}
$this->prepare_cursor[$stm_name]['returning_id'] = true;
}
@@ -1606,7 +1682,13 @@ class IO extends \CoreLibs\Basic
} else {
$this->error_id = 21;
$this->__dbError();
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$stm_name.': Prepare field with: '.$stm_name.' | '.$query.'</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $stm_name
. ': Prepare field with: ' . $stm_name . ' | '
. $query . '</span>',
'DB_ERROR'
);
return $result;
}
} else {
@@ -1627,17 +1709,34 @@ class IO extends \CoreLibs\Basic
// if we do not have no prepare cursor array entry for this statement name, abort
if (!is_array($this->prepare_cursor[$stm_name])) {
$this->error_id = 24;
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$stm_name.': We do not have a prepared query entry for this statement name.</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $stm_name
. ': We do not have a prepared query entry for this statement name.</span>',
'DB_ERROR'
);
return false;
}
if (!is_array($data)) {
$this->error_id = 25;
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$stm_name.': Prepared query Data has to be given in array form.</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $stm_name
. ': Prepared query Data has to be given in array form.</span>',
'DB_ERROR'
);
return false;
}
if ($this->prepare_cursor[$stm_name]['count'] != count($data)) {
$this->error_id = 23;
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$stm_name.': Array data count does not match prepared fields. Need: '.$this->prepare_cursor[$stm_name]['count'].', has: '.count($data).'</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $stm_name
. ': Array data count does not match prepared fields. Need: '
. $this->prepare_cursor[$stm_name]['count'] . ', has: '
. count($data) . '</span>',
'DB_ERROR'
);
return false;
}
if ($this->db_debug) {
@@ -1645,17 +1744,28 @@ class IO extends \CoreLibs\Basic
}
$result = $this->db_functions->__dbExecute($stm_name, $data);
if (!$result) {
$this->log->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[$stm_name]['result'].']: '.$this->log->prAr($data));
$this->log->debug('ExecuteData', 'ERROR in STM[' . $stm_name . '|'
. $this->prepare_cursor[$stm_name]['result'] . ']: '
. $this->log->prAr($data));
$this->error_id = 22;
$this->__dbError($this->prepare_cursor[$stm_name]['result']);
$this->__dbDebug('db', '<span style="color: red;"><b>DB-Error</b> '.$stm_name.': Execution failed</span>', 'DB_ERROR');
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> ' . $stm_name
. ': Execution failed</span>',
'DB_ERROR'
);
return false;
}
if ($this->__checkQueryForInsert($this->prepare_cursor[$stm_name]['query'], true) &&
if (
$this->__checkQueryForInsert($this->prepare_cursor[$stm_name]['query'], true) &&
$this->prepare_cursor[$stm_name]['pk_name'] != 'NULL'
) {
if (!$this->prepare_cursor[$stm_name]['returning_id']) {
$this->insert_id = $this->db_functions->__dbInsertId($this->prepare_cursor[$stm_name]['query'], $this->prepare_cursor[$stm_name]['pk_name']);
$this->insert_id = $this->db_functions->__dbInsertId(
$this->prepare_cursor[$stm_name]['query'],
$this->prepare_cursor[$stm_name]['pk_name']
);
$this->insert_id_ext = $this->insert_id;
$this->insert_id_arr[] = $this->insert_id;
} elseif ($result) {
@@ -1664,21 +1774,28 @@ class IO extends \CoreLibs\Basic
$this->insert_id_arr = [];
// we have returning, now we need to check if we get one or many returned
// we'll need to loop this, if we have multiple insert_id returns
while ($_insert_id = $this->db_functions->__dbFetchArray(
$result,
$this->db_functions->__dbResultType(true)
)) {
while (
$_insert_id = $this->db_functions->__dbFetchArray(
$result,
$this->db_functions->__dbResultType(true)
)
) {
$this->insert_id[] = $_insert_id;
$this->insert_id_arr[] = $_insert_id;
}
// if we have only one, revert from arry to single
if (count($this->insert_id) == 1) {
// echo "+ SINGLE DATA CONVERT: ".count($this->insert_id[0])." => ".array_key_exists($this->prepare_cursor[$stm_name]['pk_name'], $this->insert_id[0])."<br>";
// echo "+ PK DIRECT: ".$this->insert_id[0][$this->prepare_cursor[$stm_name]['pk_name']]."<Br>";
// if this has only the pk_name, then only return this, else array of all data (but without the position)
// example if insert_id[0]['foo'] && insert_id[0]['bar'] it will become insert_id['foo'] & insert_id['bar']
// if only ['foo_id'] and it is the PK then the PK is directly written to the insert_id
if (count($this->insert_id[0]) > 1 ||
// $this->log->debug('SINGLE DATA CONVERT', count($this->insert_id[0])." => "
// .array_key_exists($this->prepare_cursor[$stm_name]['pk_name'], $this->insert_id[0]));
// $this->log->debug('PK DIRECT', $this->insert_id[0][$this->prepare_cursor[$stm_name]['pk_name']]);
// if this has only the pk_name, then only return this,
// else array of all data (but without the position)
// example if insert_id[0]['foo'] && insert_id[0]['bar']
// it will become insert_id['foo'] & insert_id['bar']
// if only ['foo_id'] and it is the PK then the PK is directly
// written to the insert_id
if (
count($this->insert_id[0]) > 1 ||
!array_key_exists($this->prepare_cursor[$stm_name]['pk_name'], $this->insert_id[0])
) {
$this->insert_id_ext = $this->insert_id[0];
@@ -1691,19 +1808,34 @@ class IO extends \CoreLibs\Basic
$this->insert_id = '';
$this->warning_id = 33;
$this->__dbError();
$this->__dbDebug('db', '<span style="color: orange;"><b>DB-Warning</b> '.$stm_name.': insert id returned no data</span>', 'DB_WARNING');
$this->__dbDebug(
'db',
'<span style="color: orange;"><b>DB-Warning</b> ' . $stm_name
. ': insert id returned no data</span>',
'DB_WARNING'
);
}
}
// this error handling is only for pgsql
if (is_array($this->insert_id)) {
$this->warning_id = 32;
$this->__dbError();
$this->__dbDebug('db', '<span style="color: orange;"><b>DB-Warning</b> '.$stm_name.': insert id data returned as array</span>', 'DB_WARNING');
$this->__dbDebug(
'db',
'<span style="color: orange;"><b>DB-Warning</b> ' . $stm_name
. ': insert id data returned as array</span>',
'DB_WARNING'
);
} elseif (!$this->insert_id) {
// NOTE should we keep this inside
$this->warning_id = 31;
$this->__dbError();
$this->__dbDebug('db', '<span style="color: orange;"><b>DB-Warning</b> '.$stm_name.': Could not get insert id</span>', 'DB_WARNING');
$this->__dbDebug(
'db',
'<span style="color: orange;"><b>DB-Warning</b> ' . $stm_name
. ': Could not get insert id</span>',
'DB_WARNING'
);
}
}
return $result;
@@ -1765,14 +1897,14 @@ class IO extends \CoreLibs\Basic
if (!$compare || !$to_master || !$to_minor) {
return false;
} else {
$to_version = $to_master.($to_minor < 10 ? '0' : '').$to_minor;
$to_version = $to_master . ($to_minor < 10 ? '0' : '') . $to_minor;
}
// db_version can return X.Y.Z
// we only compare the first two
preg_match("/^(\d{1,})\.(\d{1,})\.?(\d{1,})?/", $this->dbVersion(), $matches);
$master = $matches[1];
$minor = $matches[2];
$version = $master.($minor < 10 ? '0' : '').$minor;
$version = $master . ($minor < 10 ? '0' : '') . $minor;
$return = false;
// compare
switch ($compare) {
@@ -1852,8 +1984,13 @@ class IO extends \CoreLibs\Basic
* @param array $data data array to override _POST data
* @return int|bool primary key
*/
public function dbWriteData(array $write_array, array $not_write_array, $primary_key, string $table, $data = [])
{
public function dbWriteData(
array $write_array,
array $not_write_array,
int $primary_key,
string $table,
array $data = []
) {
if (!is_array($write_array)) {
$write_array = [];
}
@@ -1864,7 +2001,14 @@ class IO extends \CoreLibs\Basic
return false;
}
$not_write_update_array = [];
return $this->dbWriteDataExt($write_array, $primary_key, $table, $not_write_array, $not_write_update_array, $data);
return $this->dbWriteDataExt(
$write_array,
$primary_key,
$table,
$not_write_array,
$not_write_update_array,
$data
);
}
/**
@@ -1890,7 +2034,7 @@ class IO extends \CoreLibs\Basic
) {
if (!is_array($primary_key)) {
$primary_key = [
'row' => $table.'_id',
'row' => $table . '_id',
'value' => $primary_key
];
} else {
@@ -1905,13 +2049,14 @@ class IO extends \CoreLibs\Basic
$q_sub_value = '';
$q_sub_data = '';
// get the table layout and row types
$table_data = $this->dbShowTableMetaData(($this->db_schema ? $this->db_schema.'.' : '').$table);
$table_data = $this->dbShowTableMetaData(($this->db_schema ? $this->db_schema . '.' : '') . $table);
// @phan HACK
$primary_key['value'] = $primary_key['value'] ?? '';
$primary_key['row'] = $primary_key['row'] ?? '';
// loop through the write array and each field to build the query
foreach ($write_array as $field) {
if ((!$primary_key['value'] ||
if (
(!$primary_key['value'] ||
($primary_key['value'] &&
!in_array($field, $not_write_update_array))
) &&
@@ -1936,8 +2081,11 @@ class IO extends \CoreLibs\Basic
}
// we detect bool, so we can force a write on "false"
$is_bool = $table_data[$field]['type'] == 'bool' ? true : false;
// write if the field has to be not null, or if there is no data and the field has no default values or if there is data or if this is an update and there is no data (set null)
if (($not_null && $_data) ||
// write if the field has to be not null, or if there is
// no data and the field has no default values or if there
// is data or if this is an update and there is no data (set null)
if (
($not_null && $_data) ||
(!$has_default && !$_data) ||
(is_numeric($_data) && $_data) ||
($primary_key['value'] && !$_data) ||
@@ -1946,11 +2094,13 @@ class IO extends \CoreLibs\Basic
if ($q_sub_value && !$primary_key['value']) {
$q_sub_value .= ', ';
}
if ($q_sub_data) { // && (!$primary_key || ($primary_key && !in_array($field, $not_write_array))))
if ($q_sub_data) {
// && (!$primary_key ||
// ($primary_key && !in_array($field, $not_write_array))))
$q_sub_data .= ', ';
}
if ($primary_key['value']) {
$q_sub_data .= $field.' = ';
$q_sub_data .= $field . ' = ';
} else {
$q_sub_value .= $field;
}
@@ -1963,7 +2113,13 @@ class IO extends \CoreLibs\Basic
$q_sub_data .= is_numeric($_data) ? $_data : 'NULL';
} else {
// if bool -> set bool, else write data
$q_sub_data .= isset($_data) ? "'".($is_bool ? $this->dbBoolean($_data, true) : $this->dbEscapeString($_data))."'" : 'NULL';
$q_sub_data .= isset($_data) ?
"'" . (
$is_bool ?
$this->dbBoolean($_data, true) :
$this->dbEscapeString($_data)
) . "'" :
'NULL';
}
}
}
@@ -1971,12 +2127,12 @@ class IO extends \CoreLibs\Basic
// first work contact itself (we need contact id for everything else)
if ($primary_key['value'] && $primary_key['row']) {
$q = 'UPDATE '.$table.' SET ';
$q .= $q_sub_data.' ';
$q .= 'WHERE '.$primary_key['row'].' = '.$primary_key['value'];
$q = 'UPDATE ' . $table . ' SET ';
$q .= $q_sub_data . ' ';
$q .= 'WHERE ' . $primary_key['row'] . ' = ' . $primary_key['value'];
$this->temp_sql = $q_sub_data;
} else {
$q = 'INSERT INTO '.$table.' (';
$q = 'INSERT INTO ' . $table . ' (';
$q .= $q_sub_value;
$q .= ') VALUES (';
$q .= $q_sub_data;
@@ -2013,7 +2169,11 @@ class IO extends \CoreLibs\Basic
$seconds = $matches[4] != '00' ? preg_replace('/^0/', '', $matches[4]) : '';
$milliseconds = $matches[6];
return $prefix.($hour ? $hour.'h ' : '').($minutes ? $minutes.'m ' : '').($seconds ? $seconds.'s' : '').($show_micro && $milliseconds? ' '.$milliseconds.'ms' : '');
return $prefix
. ($hour ? $hour . 'h ' : '')
. ($minutes ? $minutes . 'm ' : '')
. ($seconds ? $seconds . 's' : '')
. ($show_micro && $milliseconds ? ' ' . $milliseconds . 'ms' : '');
}
/**
@@ -2043,10 +2203,10 @@ class IO extends \CoreLibs\Basic
$value = $value === '' ? "NULL" : floatval($value);
break;
case 't':
$value = $value === '' ? "NULL" : "'".$this->dbEscapeString($value)."'";
$value = $value === '' ? "NULL" : "'" . $this->dbEscapeString($value) . "'";
break;
case 'd':
$value = $value === '' ? "NULL" : "'".$this->dbEscapeString($value)."'";
$value = $value === '' ? "NULL" : "'" . $this->dbEscapeString($value) . "'";
break;
case 'i2':
$value = $value === '' ? 0 : intval($value);
@@ -2156,7 +2316,7 @@ class IO extends \CoreLibs\Basic
*/
public function getInsertReturn($key = null)
{
trigger_error('Method '.__METHOD__.' is deprecated, use getReturningExt($key = null)', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use getReturningExt($key = null)', E_USER_DEPRECATED);
return $this->dbGetReturningExt($key);
}
@@ -2167,7 +2327,7 @@ class IO extends \CoreLibs\Basic
*/
public function getReturning()
{
trigger_error('Method '.__METHOD__.' is deprecated, use dbGetReturning()', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use dbGetReturning()', E_USER_DEPRECATED);
return $this->dbGetReturning();
}
@@ -2178,7 +2338,7 @@ class IO extends \CoreLibs\Basic
*/
public function getInsertPK()
{
trigger_error('Method '.__METHOD__.' is deprecated, use dbGetInsertPK()', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use dbGetInsertPK()', E_USER_DEPRECATED);
return $this->dbGetReturning();
}
@@ -2190,7 +2350,7 @@ class IO extends \CoreLibs\Basic
*/
public function getReturningExt($key = null)
{
trigger_error('Method '.__METHOD__.' is deprecated, use dbGetReturningExt($key = null)', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use dbGetReturningExt($key = null)', E_USER_DEPRECATED);
return $this->dbGetReturningExt($key);
}
@@ -2202,7 +2362,7 @@ class IO extends \CoreLibs\Basic
*/
public function getCursorExt($q = null)
{
trigger_error('Method '.__METHOD__.' is deprecated, use dbGetCursorExt($q = null)', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use dbGetCursorExt($q = null)', E_USER_DEPRECATED);
return $this->dbGetCursorExt($q);
}
@@ -2213,9 +2373,10 @@ class IO extends \CoreLibs\Basic
*/
public function getNumRows()
{
trigger_error('Method '.__METHOD__.' is deprecated, use dbGetNumRows()', E_USER_DEPRECATED);
trigger_error('Method ' . __METHOD__ . ' is deprecated, use dbGetNumRows()', E_USER_DEPRECATED);
return $this->dbGetNumRows();
}
} // end if db class
// end if db class
}
// __END__

View File

@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
/*********************************************************************
* AUTHOR: Clemens Schwaighofer
* CREATED: 2003/04/09
@@ -11,7 +12,9 @@
* HISTORY:
* 2008/04/16 (cs) wrapper for pg escape string
* 2007/01/11 (cs) add prepare/execute for postgres
* 2006/09/12 (cs) in case db_query retuns false, save the query and run the query through the send/get procedure to get correct error data from the db
* 2006/09/12 (cs) in case db_query retuns false, save the query and
* run the query through the send/get procedure to get
* correct error data from the db
* 2006/06/26 (cs) added port for db connection
* 2006/04/03 (cs) added meta data for table
* 2005/07/25 (cs) removed the plural s remove, not needed and not 100% working
@@ -41,6 +44,8 @@
*
*/
declare(strict_types=1);
namespace CoreLibs\DB\SQL;
class PgSQL
@@ -258,9 +263,9 @@ class PgSQL
// if (preg_match("/.*s$/i", $table))
// $table = substr($table, 0, -1);
// set pk_name to "id"
$pk_name = $table."_id";
$pk_name = $table . "_id";
}
$seq = ($schema ? $schema.'.' : '').$table."_".$pk_name."_seq";
$seq = ($schema ? $schema . '.' : '') . $table . "_" . $pk_name . "_seq";
$q = "SELECT CURRVAL('$seq') AS insert_id";
// I have to do manually or I overwrite the original insert internal vars ...
if ($q = $this->__dbQuery($q)) {
@@ -288,27 +293,28 @@ class PgSQL
$cursor = $this->__dbQuery($q);
$search_path = $this->__dbFetchArray($cursor)['search_path'];
if ($search_path != $schema) {
$table_prefix = $schema.'.';
$table_prefix = $schema . '.';
}
}
// read from table the PK name
// faster primary key get
$q = "SELECT pg_attribute.attname AS column_name, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type ";
$q .= "FROM pg_index, pg_class, pg_attribute ";
$q = "SELECT pg_attribute.attname AS column_name, "
. "format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type "
. "FROM pg_index, pg_class, pg_attribute ";
if ($schema) {
$q .= ", pg_namespace ";
}
$q .= "WHERE ";
// regclass translates the OID to the name
$q .= "pg_class.oid = '".$table_prefix.$table."'::regclass AND ";
$q .= "indrelid = pg_class.oid AND ";
$q .= "WHERE "
// regclass translates the OID to the name
. "pg_class.oid = '" . $table_prefix . $table . "'::regclass AND "
. "indrelid = pg_class.oid AND ";
if ($schema) {
$q .= "nspname = '".$schema."' AND ";
$q .= "pg_class.relnamespace = pg_namespace.oid AND ";
$q .= "nspname = '" . $schema . "' AND "
. "pg_class.relnamespace = pg_namespace.oid AND ";
}
$q .= "pg_attribute.attrelid = pg_class.oid AND ";
$q .= "pg_attribute.attnum = any(pg_index.indkey) ";
$q .= "AND indisprimary";
$q .= "pg_attribute.attrelid = pg_class.oid AND "
. "pg_attribute.attnum = any(pg_index.indkey) "
. "AND indisprimary";
$cursor = $this->__dbQuery($q);
if ($cursor) {
return $this->__dbFetchArray($cursor)['column_name'] ?? false;
@@ -330,15 +336,23 @@ class PgSQL
* @param string $db_ssl SSL (allow is default)
* @return ?resource db handler resource or null on error
*/
public function __dbConnect(string $db_host, string $db_user, string $db_pass, string $db_name, int $db_port = 5432, string $db_ssl = 'allow')
{
public function __dbConnect(
string $db_host,
string $db_user,
string $db_pass,
string $db_name,
int $db_port = 5432,
string $db_ssl = 'allow'
) {
// to avoid empty db_port
if (!$db_port) {
$db_port = 5432;
}
$this->dbh = pg_connect("host=".$db_host." port=".$db_port." user=".$db_user." password=".$db_pass." dbname=".$db_name." sslmode=".$db_ssl);
$this->dbh = pg_connect("host=" . $db_host . " port=" . $db_port . " user="
. $db_user . " password=" . $db_pass . " dbname=" . $db_name . " sslmode=" . $db_ssl);
if (!$this->dbh) {
die("<!-- Can't connect [host=".$db_host." port=".$db_port." user=".$db_user." password=XXXX dbname=".$db_name." sslmode=".$db_ssl."] //-->");
die("<!-- Can't connect [host=" . $db_host . " port=" . $db_port . " user="
. $db_user . " password=XXXX dbname=" . $db_name . " sslmode=" . $db_ssl . "] //-->");
return null;
}
return $this->dbh;
@@ -359,7 +373,7 @@ class PgSQL
$cursor = pg_get_result($this->dbh);
}
if ($cursor && pg_result_error($cursor)) {
return "<span style=\"color: red;\"><b>-PostgreSQL-Error-></b> ".pg_result_error($cursor)."</span><br>";
return "<span style=\"color: red;\"><b>-PostgreSQL-Error-></b> " . pg_result_error($cursor) . "</span><br>";
} else {
return '';
}