Merge namespace development into master
This commit is contained in:
546
www/lib/CoreLibs/DB/Extended/ArrayIO.inc
Normal file
546
www/lib/CoreLibs/DB/Extended/ArrayIO.inc
Normal file
@@ -0,0 +1,546 @@
|
||||
<?php
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org)
|
||||
* CREATED: 2002/12/17
|
||||
* VERSION: 1.0.0
|
||||
* RELEASED LICENSE: GNU GPL 3
|
||||
* SHORT DESC :RIPTION:
|
||||
* DB Array IO Class:
|
||||
* writes, reads or deletes a complete array (one data set) in/out a
|
||||
* table from the connected DB.
|
||||
* you don't have to write any SQL queries, worry over update/insert
|
||||
*
|
||||
* PUBLIC VARIABLES
|
||||
*
|
||||
* PRIVATE VARIABLES
|
||||
*
|
||||
* PUBLIC METHOD:S
|
||||
*
|
||||
* PRIVATE METHOD:S
|
||||
*
|
||||
* HISTORY:
|
||||
* 2005/07/07 (cs) updated array class for postgres: set 0 & NULL if int field given, insert uses () values () syntax
|
||||
* 2005/03/31 (cs) fixed the class call with all debug vars
|
||||
* 2003-03-10: error_ids where still wrong chagned 11->21 and 12->22
|
||||
* 2003-02-26: db_array_io is no longer single class but extens db_io,
|
||||
* as it needs it anyway
|
||||
* moved the class info vars into class_info array into
|
||||
* the constructor, removed info function
|
||||
* 2003-02-24: in db_delete moved query build to top, or pk_name/value
|
||||
* will be reset before delete is done
|
||||
* 2002-12-20: just added info() method
|
||||
* 2002-12-17: splitted the class from other file (with main db wrapper)
|
||||
*********************************************************************/
|
||||
|
||||
// picture upload should be taken out from here and out in media_class
|
||||
// as it actually has nothing to do with this one here ? (or at least
|
||||
// put into separete function in this class)
|
||||
|
||||
namespace CoreLibs\DB\Extended;
|
||||
|
||||
// subclass for one array handling
|
||||
class ArrayIO extends \CoreLibs\DB\IO
|
||||
{
|
||||
// main calss variables
|
||||
public $table_array; // the array from the table to work on
|
||||
public $table_name; // the table_name
|
||||
public $pk_name; // the primary key from this table
|
||||
public $pk_id; // the PK id
|
||||
|
||||
// METHOD: db_array_io
|
||||
// PARAMS: db_config -> db_io class init vars
|
||||
// table_array -> the array from the table
|
||||
// table_name -> name of the table (for the array)
|
||||
// db_debug -> turn on db_io debug output (DB_DEBUG as global var does the same)
|
||||
// RETURN: none
|
||||
// DESC : constructor for the array io class, set the
|
||||
// primary key name automatically (from array)
|
||||
public function __construct($db_config, $table_array, $table_name, $debug = 0, $db_debug = 0, $echo = 1, $print = 0)
|
||||
{
|
||||
// instance db_io class
|
||||
parent::__construct($db_config, $debug, $db_debug, $echo, $print);
|
||||
// more error vars for this class
|
||||
$this->error_string["21"] = "No Primary Key given";
|
||||
$this->error_string["22"] = "Could not run Array Query";
|
||||
|
||||
$this->table_array = $table_array;
|
||||
$this->table_name = $table_name;
|
||||
|
||||
// set primary key for given table_array
|
||||
if ($this->table_array) {
|
||||
foreach ($table_array as $key => $value) {
|
||||
if ($value["pk"]) {
|
||||
$this->pk_name = $key;
|
||||
}
|
||||
}
|
||||
} // set pk_name IF table_array was given
|
||||
// internal
|
||||
$this->class_info["db_array_io"] = array(
|
||||
"class_name" => "DB Array IO",
|
||||
"class_version" => "1.0.0",
|
||||
"class_created" => "2002/12/17",
|
||||
"class_author" => "Clemens Schwaighofer"
|
||||
);
|
||||
}
|
||||
|
||||
// deconstruktor
|
||||
public function __destruct()
|
||||
{
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
// METHOD: convertData
|
||||
// WAS : convert_data
|
||||
// PARAMS: string -> the string that should be changed
|
||||
// RETURN: string -> the altered string
|
||||
// DESC : changes all previously alterd HTML code into visible one,
|
||||
// works for <b>,<i>, and <a> (thought <a> can be / or should
|
||||
// be handled with the magic links functions
|
||||
// used with the read function
|
||||
public function convertData($text)
|
||||
{
|
||||
$text = str_replace('<b>', '<b>', $text);
|
||||
$text = str_replace('</b>', '</b>', $text);
|
||||
$text = str_replace('<i>', '<i>', $text);
|
||||
$text = str_replace('</i>', '</i>', $text);
|
||||
// my need a change
|
||||
$text = str_replace('<a href="', '<a target="_blank" href="', $text);
|
||||
$text = str_replace('">', '">', $text);
|
||||
$text = str_replace('</a>', '</a>', $text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
// METHOD: convertEntities
|
||||
// WAS : convert_entities
|
||||
// PARAMS: string -> string to be changed
|
||||
// RETURN: string -> altered string
|
||||
// DESC : changeds all HTML entities into non HTML ones
|
||||
public function convertEntities($text)
|
||||
{
|
||||
$text = str_replace('<', '<', $text);
|
||||
$text = str_replace('>', '>', $text);
|
||||
$text = str_replace('&', '&', $text);
|
||||
$text = str_replace('"', '"', $text);
|
||||
$text = str_replace(''', "'", $text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
// METHOD: dbDumpArray
|
||||
// WAS : db_dump_array
|
||||
// PARAMS: none
|
||||
// RETURN: returns the current array
|
||||
// DESC : dumps the current data
|
||||
public function dbDumpArray($write = 0)
|
||||
{
|
||||
reset($this->table_array);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
$string .= "<b>".$column."</b> -> ".$data_array["value"]."<br>";
|
||||
}
|
||||
// add output to internal error_msg
|
||||
if ($write) {
|
||||
$this->error_msg['db'] .= $string;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
// METHOD: dbCheckPkSet
|
||||
// WAS : db_check_pk_set
|
||||
// PARAMS: none
|
||||
// RETURN: none
|
||||
// DESC : checks if pk is set and if not, set from pk_id and if this also not set return 0
|
||||
public function dbCheckPkSet()
|
||||
{
|
||||
// if pk_id is set, overrule ...
|
||||
if ($this->pk_id) {
|
||||
$this->table_array[$this->pk_name]["value"] = $this->pk_id;
|
||||
}
|
||||
// if not set ... produce error
|
||||
if (!$this->table_array[$this->pk_name]["value"]) {
|
||||
// if no PK found, error ...
|
||||
$this->error_id = 21;
|
||||
$this->__dbError();
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: dbResetArray
|
||||
// WAS : db_reset_array
|
||||
// PARAMS: reset_pk -> if set reset the pk too
|
||||
// RETURN: none
|
||||
// DESC : resets the whole array
|
||||
public function dbResetArray($reset_pk = 0)
|
||||
{
|
||||
reset($this->table_array);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
if (!$this->table_array[$column]["pk"]) {
|
||||
unset($this->table_array[$column]["value"]);
|
||||
} elseif ($reset_pk) {
|
||||
unset($this->table_array[$column]["value"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: dbDelete
|
||||
// WAS : db_delete
|
||||
// PARAMS: optional the table_array, if not given uses class var
|
||||
// RETURN: 1 for successfull delete or 0 for error
|
||||
// DESC : deletes one dataset
|
||||
public function dbDelete($table_array = 0)
|
||||
{
|
||||
if (is_array($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
return $this->table_array;
|
||||
}
|
||||
// delete query
|
||||
$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);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
// suchen nach bildern und lschen ...
|
||||
if ($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"]);
|
||||
}
|
||||
$dateiname = str_replace("_tn", "", $this->table_array[$column]["value"]);
|
||||
if (file_exists($this->table_array[$column]["path"].$dateiname)) {
|
||||
unlink($this->table_array[$column]["path"].$dateiname);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->table_array[$column]["fk"]) {
|
||||
// zusammenstellen der FKs
|
||||
if ($q_where) {
|
||||
$q_where .= " AND ";
|
||||
}
|
||||
$q_where .= $column." = ".$this->table_array[$column]["value"];
|
||||
}
|
||||
// allgemeines zurcksetzen des arrays
|
||||
unset($this->table_array[$column]["value"]);
|
||||
}
|
||||
|
||||
// attach fk row if there ...
|
||||
if ($q_where) {
|
||||
$q .= " AND ".$q_where;
|
||||
}
|
||||
// if 0, error
|
||||
unset($this->pk_id);
|
||||
if (!$this->dbExec($q)) {
|
||||
$this->error_id=22;
|
||||
$this->__dbError();
|
||||
}
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
// METHOD: dbRead
|
||||
// WAS : db_read
|
||||
// PARAMS: edit -> if 1 data will not be altered for output, optional the table_array, if not given uses class var
|
||||
// RETURN: true or false for reading
|
||||
// DESC : reads one row into the array
|
||||
public function dbRead($edit = 0, $table_array = 0)
|
||||
{
|
||||
// if array give, overrules internal array
|
||||
if (is_array($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
return $this->table_array;
|
||||
}
|
||||
reset($this->table_array);
|
||||
// create select part & addition FK part
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
if ($q_select) {
|
||||
$q_select .= ", ";
|
||||
}
|
||||
$q_select .= $column;
|
||||
|
||||
// check FK ...
|
||||
if ($this->table_array[$column]["fk"] && $this->table_array[$column]["value"]) {
|
||||
if ($q_where) {
|
||||
$q_where .= " AND ";
|
||||
}
|
||||
$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"]." ";
|
||||
if ($q_where) {
|
||||
$q .= " AND ".$q_where;
|
||||
}
|
||||
|
||||
// if query was executed okay, else set error
|
||||
if ($this->dbExec($q)) {
|
||||
if ($res = $this->dbFetchArray()) {
|
||||
reset($this->table_array);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
// wenn "edit" dann gib daten wie in DB zurck, 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>";
|
||||
if ($edit) {
|
||||
$this->table_array[$column]["value"] = $res[$column];
|
||||
// if password, also write to hidden
|
||||
if ($this->table_array[$column]["type"] == "password") {
|
||||
$this->table_array[$column]["HIDDEN_value"] = $res[$column];
|
||||
}
|
||||
} else {
|
||||
$this->table_array[$column]["value"] = $this->convertData(nl2br($res[$column]));
|
||||
// had to put out the htmlentities from the line above as it breaks japanese characters
|
||||
}
|
||||
}
|
||||
}
|
||||
// possible dbFetchArray errors ...
|
||||
$this->pk_id = $this->table_array[$this->pk_name]["value"];
|
||||
} else {
|
||||
$this->error_id = 22;
|
||||
$this->__dbError();
|
||||
}
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
// METHOD: dbWrite
|
||||
// WAS : db_write
|
||||
// PARAMS: addslashes -> if 1 will make an addslashes for each array field, optional the table_array, if not given uses class var
|
||||
// RETURN: true or false on write
|
||||
// DESC : writes on set into DB or updates one set (if PK exists)
|
||||
public function dbWrite($addslashes = 0, $table_array = 0)
|
||||
{
|
||||
if (is_array($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
// PK ID check
|
||||
// if ($this->pk_id && !$this->table_array[$this->pk_name]["value"]) {
|
||||
// $this->table_array[$this->pk_name]["value"]=$this->pk_id;
|
||||
// }
|
||||
// checken ob PKs gesetzt, wenn alle -> update, wenn keiner -> insert, wenn ein paar -> ERROR!
|
||||
if (!$this->table_array[$this->pk_name]["value"]) {
|
||||
$insert = 1;
|
||||
} else {
|
||||
$insert = 0;
|
||||
}
|
||||
|
||||
reset($this->table_array);
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
/********************************* START FILE *************************************/
|
||||
// file upload
|
||||
if ($this->table_array[$column]["file"]) {
|
||||
// falls was im tmp drinnen, sprich ein upload, datei kopieren, Dateinamen in db schreiben
|
||||
// falls datei schon am server (physischer pfad), dann einfach url in db schreiben (update)
|
||||
// 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"]);
|
||||
}
|
||||
$dateiname = str_replace("_tn", "", $this->table_array[$column]["value"]);
|
||||
if (file_exists($this->table_array[$column]["path"].$dateiname)) {
|
||||
unlink($this->table_array[$column]["path"].$dateiname);
|
||||
}
|
||||
$this->table_array[$column]["value"] = "";
|
||||
} else {
|
||||
if ($this->table_array[$column]["tmp"] != "none" && $this->table_array[$column]["tmp"]) {
|
||||
// Dateiname zusammenbasteln: org-name + _pkid liste + .ext
|
||||
list($name, $ext) = explode(".", $this->table_array[$column]["dn"]);
|
||||
|
||||
// mozilla, patch
|
||||
$fn_name = explode("/", $this->table_array[$column]["dn"]);
|
||||
$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));
|
||||
//echo "PK: $pk_ids_file<br>";
|
||||
$dateiname = $name.$pk_ids_file.".".$ext;
|
||||
//echo "Dn: $dateiname";
|
||||
copy($this->table_array[$column]["tmp"], $this->table_array[$column]["path"].$dateiname);
|
||||
// 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") {
|
||||
$dateiname_tn = $name.$pk_ids_file."_tn.".$ext;
|
||||
$eingang = $this->table_array[$column]["path"].$dateiname;
|
||||
$ausgang = $this->table_array[$column]["path"].$dateiname_tn;
|
||||
$com = "convert -geometry 115 $eingang $ausgang";
|
||||
exec($com);
|
||||
$this->table_array[$column]["value"] = $dateiname_tn;
|
||||
} else {
|
||||
$this->table_array[$column]["value"] = $dateiname;
|
||||
}
|
||||
} elseif (file_exists($this->table_array[$column]["path"].$this->table_array[$column]["value"])) {
|
||||
// mach gar nix, wenn bild schon da ???
|
||||
}
|
||||
} // delete or upload
|
||||
} // file IF
|
||||
/********************************* END FILE **************************************/
|
||||
|
||||
// do not write 'pk' (primary key) or 'view' values
|
||||
if (!$this->table_array[$column]["pk"] && $this->table_array[$column]['type'] != 'view' && strlen($column) > 0) {
|
||||
// for password use hidden value if main is not set
|
||||
if ($this->table_array[$column]["type"] == "password" && !$this->table_array[$column]["value"]) {
|
||||
$this->table_array[$column]["value"] = $this->table_array[$column]["HIDDEN_value"];
|
||||
}
|
||||
if (!$insert) {
|
||||
if (strlen($q_data)) {
|
||||
$q_data .= ", ";
|
||||
}
|
||||
$q_data .= $column." = ";
|
||||
} else {
|
||||
// this is insert
|
||||
if (strlen($q_data)) {
|
||||
$q_data .= ", ";
|
||||
}
|
||||
if ($q_vars) {
|
||||
$q_vars .= ", ";
|
||||
}
|
||||
$q_vars .= $column;
|
||||
}
|
||||
// integer is different
|
||||
if ($this->table_array[$column]["int"] || $this->table_array[$column]["int_null"]) {
|
||||
$this->debug('write_check', "[$column][".$this->table_array[$column]["value"]."] Foo: ".isset($this->table_array[$column]["value"])." | ".$this->table_array[$column]["int_null"]);
|
||||
if (!$this->table_array[$column]["value"] && $this->table_array[$column]["int_null"]) {
|
||||
$_value = 'NULL';
|
||||
} elseif (!isset($this->table_array[$column]["value"])) {
|
||||
$_value = 0;
|
||||
} else {
|
||||
$_value = $this->table_array[$column]["value"];
|
||||
}
|
||||
$q_data .= $_value;
|
||||
} elseif ($this->table_array[$column]["interval"]) {
|
||||
// for interval we check if no value, then we set null
|
||||
if (!$this->table_array[$column]["value"]) {
|
||||
$_value = 'NULL';
|
||||
}
|
||||
$q_data .= $_value;
|
||||
} else {
|
||||
// normal string
|
||||
$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"]));
|
||||
} else {
|
||||
$q_data .= $this->dbEscapeString($this->table_array[$column]["value"]);
|
||||
}
|
||||
$q_data .= "'";
|
||||
}
|
||||
}
|
||||
} // while ...
|
||||
|
||||
// NOW get PK, and FK settings (FK only for update query)
|
||||
// get it at the end, cause now we can be more sure of no double IDs, etc
|
||||
reset($this->table_array);
|
||||
// create select part & addition FK part
|
||||
foreach ($this->table_array as $column => $data_array) {
|
||||
// check FK ...
|
||||
if ($this->table_array[$column]["fk"] && $this->table_array[$column]["value"]) {
|
||||
if ($q_where) {
|
||||
$q_where .= " AND ";
|
||||
}
|
||||
$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;
|
||||
$res = $this->dbReturnRow($q);
|
||||
if (!$res["pk_id"]) {
|
||||
$res["pk_id"] = 1;
|
||||
}
|
||||
$this->table_array[$this->pk_name]["value"] = $res["pk_id"];
|
||||
}
|
||||
|
||||
if (!$insert) {
|
||||
$q = "UPDATE ".$this->table_name." SET ";
|
||||
$q .= $q_data;
|
||||
$q .= " WHERE ";
|
||||
$q .= $this->pk_name." = ".$this->table_array[$this->pk_name]["value"]." ";
|
||||
if ($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.")";
|
||||
// write primary key too
|
||||
// if ($q_data)
|
||||
// $q .= ", ";
|
||||
// $q .= $this->pk_name." = ".$this->table_array[$this->pk_name]["value"]." ";
|
||||
// $this->pk_id = $this->table_array[$this->pk_name]["value"];
|
||||
}
|
||||
// return success or not
|
||||
if (!$this->dbExec($q)) {
|
||||
$this->error_id = 22;
|
||||
$this->__dbError();
|
||||
}
|
||||
// set primary key
|
||||
if ($insert) {
|
||||
$this->table_array[$this->pk_name]["value"] = $this->insert_id;
|
||||
$this->ok = $this->insert_id;
|
||||
}
|
||||
// return the table if needed
|
||||
return $this->table_array;
|
||||
}
|
||||
|
||||
// *************************************************************
|
||||
// COMPATIBILITY METHODS
|
||||
// those methods are deprecated function call names
|
||||
// they exist for backwards compatibility only
|
||||
// *************************************************************
|
||||
|
||||
public function convert_data($text)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->convertData($text);
|
||||
}
|
||||
|
||||
public function convert_entities($text)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->convertEntities($text);
|
||||
}
|
||||
|
||||
public function db_dump_array($write = 0)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbDumpArray($write);
|
||||
}
|
||||
|
||||
public function db_check_pk_set()
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbCheckPkSet();
|
||||
}
|
||||
|
||||
public function db_reset_array($reset_pk = 0)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbResetArray($reset_pk);
|
||||
}
|
||||
|
||||
public function db_delete($table_array = 0)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbDelete($table_array);
|
||||
}
|
||||
|
||||
public function db_read($edit = 0, $table_array = 0)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbRead($edit, $table_array);
|
||||
}
|
||||
|
||||
public function db_write($addslashes = 0, $table_array = 0)
|
||||
{
|
||||
error_log('DEPRECATED CALL: '.__METHOD__.', '.__FILE__.':'.__LINE__.', '.debug_backtrace()[0]['file'].':'.debug_backtrace()[0]['line']);
|
||||
return $this->dbWrite($addslashes, $table_array);
|
||||
}
|
||||
} // end of class
|
||||
|
||||
# __END__
|
||||
2092
www/lib/CoreLibs/DB/IO.inc
Normal file
2092
www/lib/CoreLibs/DB/IO.inc
Normal file
File diff suppressed because it is too large
Load Diff
430
www/lib/CoreLibs/DB/SQL/PgSQL.inc
Normal file
430
www/lib/CoreLibs/DB/SQL/PgSQL.inc
Normal file
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
/*********************************************************************
|
||||
* AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org)
|
||||
* CREATED: 2003/04/09
|
||||
* SHORT DESCRIPTION:
|
||||
* 2018/3/23, the whole class system is transformed to namespaces
|
||||
* also all internal class calls are converted to camel case
|
||||
*
|
||||
* pgsql wrapper calls
|
||||
*
|
||||
* 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/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
|
||||
* 2005/07/07 (cs) the default it is table_name _ id
|
||||
* 2005/01/19 (cs) changed the pgsql connect, so it dies if it can't connect to the DB
|
||||
* 2004/09/30 (cs) layout cleanup
|
||||
* /
|
||||
|
||||
* collection of PostgreSQL wrappers
|
||||
* REQUIRES 5.x PHP!!!
|
||||
*
|
||||
* pg_prepare
|
||||
* pg_execute
|
||||
* pg_num_rows
|
||||
* pg_num_fields
|
||||
* pg_field_name
|
||||
* pg_affected_rows (*)
|
||||
* pg_fetch_array
|
||||
* pg_query
|
||||
* pg_send_query
|
||||
* pg_get_result
|
||||
* pg_connection_busy
|
||||
* pg_close
|
||||
* pg_connect (*)
|
||||
* pg_meta_data
|
||||
* pg_escape_string
|
||||
*
|
||||
*/
|
||||
|
||||
namespace CoreLibs\DB\SQL;
|
||||
|
||||
class PgSQL
|
||||
{
|
||||
private $last_error_query;
|
||||
private $dbh;
|
||||
|
||||
// METHOD: __construct
|
||||
// PARAMS: none
|
||||
// RETURN: none
|
||||
// DESC : class constructor
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
// METHOD: __dbLastErrorQuery
|
||||
// WAS : _db_last_error_query
|
||||
// PARAMS: none
|
||||
// RETURN: true/false if last error is set
|
||||
// DESC : queries last error query and returns true or false if error was set
|
||||
public function __dbLastErrorQuery()
|
||||
{
|
||||
if ($this->last_error_query) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbQuery
|
||||
// WAS : _db_query
|
||||
// PARAMS: query
|
||||
// RETURN: query result
|
||||
// DESC : wrapper for gp_query, catches error and stores it in class var
|
||||
public function __dbQuery($query)
|
||||
{
|
||||
$this->last_error_query = '';
|
||||
// read out the query status and save the query if needed
|
||||
$result = pg_query($this->dbh, $query);
|
||||
if (!$result) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// METHOD: __dbSendQuery
|
||||
// WAS : _db_send_query
|
||||
// PARAMS: query
|
||||
// RETURN: true/false if query was sent successful
|
||||
// DESC : sends an async query to the server
|
||||
public function __dbSendQuery($query)
|
||||
{
|
||||
return pg_send_query($this->dbh, $query);
|
||||
}
|
||||
|
||||
// METHOD: __dbGetResult
|
||||
// WAS : _db_get_result
|
||||
// PARAMS: none
|
||||
// RETURN: resource handler
|
||||
// DESC : wrapper for pg_get_result
|
||||
public function __dbGetResult()
|
||||
{
|
||||
$this->last_error_query = '';
|
||||
$result = pg_get_result($this->dbh);
|
||||
if ($error = pg_result_error($result)) {
|
||||
$this->last_error_query = $error;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// METHOD: __dbClose
|
||||
// WAS : _db_close
|
||||
// PARAMS: none
|
||||
// RETURN: none
|
||||
// DESC : wrapper for pg_close
|
||||
public function __dbClose()
|
||||
{
|
||||
if (is_resource($this->dbh)) {
|
||||
if (pg_connection_status($this->dbh) === PGSQL_CONNECTION_OK) {
|
||||
pg_close($this->dbh);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbPrepare
|
||||
// WAS : _db_prepare
|
||||
// PARAMS: prepare name, query
|
||||
// RETURN: prepared statement handler
|
||||
// DESC : wrapper for pg_prepare
|
||||
public function __dbPrepare($name, $query)
|
||||
{
|
||||
$result = pg_prepare($this->dbh, $name, $query);
|
||||
if (!$result) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// METHOD: __dbExecute
|
||||
// WAS : _db_execute
|
||||
// PARAMS: prepare name, data for query
|
||||
// RETURN: returns status
|
||||
// DESC : wrapper for pg_execute for running a prepared statement
|
||||
public function __dbExecute($name, $data)
|
||||
{
|
||||
$result = pg_execute($this->dbh, $name, $data);
|
||||
if (!$result) {
|
||||
$this->last_error_query = $query;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// METHOD: __dbNumRows
|
||||
// WAS : _db_num_rows
|
||||
// PARAMS: cursor
|
||||
// RETURN: rows
|
||||
// DESC : wrapper for pg_num_rows
|
||||
public function __dbNumRows($cursor)
|
||||
{
|
||||
return pg_num_rows($cursor);
|
||||
}
|
||||
|
||||
// METHOD: __dbNumFields
|
||||
// WAS : _db_num_fields
|
||||
// PARAMS: cursor
|
||||
// RETURN: number for fields in query
|
||||
// DESC : wrapper for pg_num_fields
|
||||
public function __dbNumFields($cursor)
|
||||
{
|
||||
return pg_num_fields($cursor);
|
||||
}
|
||||
|
||||
// METHOD: __dbFieldName
|
||||
// WAS : _db_field_name
|
||||
// PARAMS: cursor, field position
|
||||
// RETURN: name of field
|
||||
// DESC : wrapper for pg_field_name
|
||||
public function __dbFieldName($cursor, $i)
|
||||
{
|
||||
return pg_field_name($cursor, $i);
|
||||
}
|
||||
|
||||
// METHOD: __dbFetchArray
|
||||
// WAS : _db_fetch_array
|
||||
// PARAMS: cursor, opt result type
|
||||
// RETURN: row
|
||||
// DESC : wrapper for pg_fetch_array
|
||||
public function __dbFetchArray($cursor, $result_type = '')
|
||||
{
|
||||
// result type is passed on as is [should be checked]
|
||||
if ($result_type) {
|
||||
return pg_fetch_array($cursor, null, $result_type);
|
||||
} else {
|
||||
return pg_fetch_array($cursor);
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbFetchAll
|
||||
// WAS : _db_fetch_all
|
||||
// PARAMS: cursor
|
||||
// RETURN: all rows as array
|
||||
// DESC : wrapper for pg_fetch_array
|
||||
public function __dbFetchAll($cursor)
|
||||
{
|
||||
return pg_fetch_all($cursor);
|
||||
}
|
||||
|
||||
// METHOD: __dbAffectedRows
|
||||
// WAS : _db_affected_rows
|
||||
// PARAMS: cursor
|
||||
// RETURN: number for rows
|
||||
// DESC : wrapper for pg_affected_rows
|
||||
public function __dbAffectedRows($cursor)
|
||||
{
|
||||
return pg_affected_rows($cursor);
|
||||
}
|
||||
|
||||
// METHOD: __dbInsertId
|
||||
// WAS : _db_insert_id
|
||||
// PARAMS: query, primary key name
|
||||
// RETURN: last insert primary key
|
||||
// DESC : reads the last inserted primary key for the query
|
||||
// if ther is no pk_name tries to auto built it from the table name
|
||||
// this only works if db schema is after "no plural names. and pk name is table name + _id
|
||||
// detects schema prefix in table name
|
||||
public function __dbInsertId($query, $pk_name)
|
||||
{
|
||||
// only if an insert has been done
|
||||
if (preg_match("/^insert /i", $query)) {
|
||||
$schema = '';
|
||||
// get table name from insert
|
||||
$array = explode(' ', $query);
|
||||
$_table = $array[2];
|
||||
// if there is a dot inside, we need to split
|
||||
if (strstr($_table, '.')) {
|
||||
list($schema, $table) = explode('.', $_table);
|
||||
} else {
|
||||
$table = $_table;
|
||||
}
|
||||
// no PK name given at all
|
||||
if (!$pk_name) {
|
||||
// if name is plurar, make it singular
|
||||
// if (preg_match("/.*s$/i", $table))
|
||||
// $table = substr($table, 0, -1);
|
||||
// set pk_name to "id"
|
||||
$pk_name = $table."_id";
|
||||
}
|
||||
$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)) {
|
||||
list($id) = $this->__dbFetchArray($q);
|
||||
} else {
|
||||
$id = array(-1, $q);
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbPrimaryKey
|
||||
// WAS : _db_primary_key
|
||||
// PARAMS: table and optional schema
|
||||
// RETURN: primary key name OR false if not possible
|
||||
// DESC : queries database for the primary key name to this table in the selected schema
|
||||
public function __dbPrimaryKey($table, $schema = '')
|
||||
{
|
||||
if ($table) {
|
||||
// check if schema set is different from schema given, only needed if schema is not empty
|
||||
$table_prefix = '';
|
||||
if ($schema) {
|
||||
$q = "SHOW search_path";
|
||||
$cursor = $this->__dbQuery($q);
|
||||
$search_path = $this->__dbFetchArray($cursor)['search_path'];
|
||||
if ($search_path != $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 ";
|
||||
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 ";
|
||||
if ($schema) {
|
||||
$q .= "nspname = '".$schema."' AND ";
|
||||
$q .= "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";
|
||||
$cursor = $this->__dbQuery($q);
|
||||
if ($cursor) {
|
||||
return $this->__dbFetchArray($cursor)['column_name'];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbConnect
|
||||
// WAS : _db_connect
|
||||
// PARAMS: host name, user name, password, database name, optional port (defaults to default postgres port), optional ssl (default allow)
|
||||
// RETURN: database handler
|
||||
// DESC : wrapper for pg_connect, writes out failure to screen if error occurs (hidden var)
|
||||
public function __dbConnect($db_host, $db_user, $db_pass, $db_name, $db_port = 5432, $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);
|
||||
if (!$this->dbh) {
|
||||
die("<!-- Can't connect [host=".$db_host." port=".$db_port." user=".$db_user." password=XXXX dbname=".$db_name." sslmode=".$db_ssl."] //-->");
|
||||
}
|
||||
return $this->dbh;
|
||||
}
|
||||
|
||||
// METHOD: __dbPrintError
|
||||
// WAS : _db_print_error
|
||||
// PARAMS: database handler, cursor
|
||||
// RETURN: error string (HTML)
|
||||
// DESC : reads the last error for this cursor
|
||||
public function __dbPrintError($cursor = '')
|
||||
{
|
||||
// run the query again for the error result here
|
||||
if (!$cursor && $this->last_error_query) {
|
||||
pg_send_query($this->dbh, $this->last_error_query);
|
||||
$this->last_error_query = '';
|
||||
$cursor = pg_get_result($this->dbh);
|
||||
}
|
||||
if (pg_result_error($cursor)) {
|
||||
return "<span style=\"color: red;\"><b>-PostgreSQL-Error-></b> ".pg_result_error($cursor)."</span><br>";
|
||||
}
|
||||
}
|
||||
|
||||
// METHOD: __dbMetaData
|
||||
// WAS : _db_meta_data
|
||||
// PARAMS: table name
|
||||
// RETURN: array with table data
|
||||
// DESC : wrapper for pg_emta_data
|
||||
public function __dbMetaData($table)
|
||||
{
|
||||
return pg_meta_data($this->dbh, $table);
|
||||
}
|
||||
|
||||
// METHOD: __dbEscapeString
|
||||
// WAS : _db_escape_string
|
||||
// PARAMS: string
|
||||
// RETURN: escaped string for postgres
|
||||
// DESC : wrapper for pg_escape_string
|
||||
public function __dbEscapeString($string)
|
||||
{
|
||||
return pg_escape_string($this->dbh, $string);
|
||||
}
|
||||
|
||||
// METHOD: __dbEscapeBytea
|
||||
// WAS : _db_escape_bytea
|
||||
// PARAMS: string
|
||||
// RETURN: escape bytes for postgres
|
||||
// DESC : wrapper for pg_escape_bytea
|
||||
public function __dbEscapeBytea($bytea)
|
||||
{
|
||||
return pg_escape_bytea($this->dbh, $bytea);
|
||||
}
|
||||
|
||||
// METHOD: __dbConnectionBusy
|
||||
// WAS : _db_connection_busy
|
||||
// PARAMS: none
|
||||
// RETURN: true/false for busy connection
|
||||
// DESC : wrapper for pg_connection_busy
|
||||
public function __dbConnectionBusy()
|
||||
{
|
||||
return pg_connection_busy($this->dbh);
|
||||
}
|
||||
|
||||
// METHOD: __dbVersion
|
||||
// WAS : _db_version
|
||||
// PARAMS: none
|
||||
// RETURN: databse version
|
||||
// DESC : wrapper for pg_version
|
||||
public function __dbVersion()
|
||||
{
|
||||
// array has client, protocol, server
|
||||
// we just need the server
|
||||
$v = pg_version($this->dbh);
|
||||
return $v['server'];
|
||||
}
|
||||
|
||||
// METHOD: __dbArrayParse
|
||||
// WAS : _db_array_parse
|
||||
// PARAMS: input text, output array [needed]
|
||||
// [internal] limit: are we at the end of the parse
|
||||
// [internal] offset: shift for {}
|
||||
// RETURN: array with the elements
|
||||
// DESC : postgresql array to php array
|
||||
public function __dbArrayParse($text, &$output, $limit = false, $offset = 1)
|
||||
{
|
||||
if (false === $limit) {
|
||||
$limit = strlen($text) - 1;
|
||||
$output = array();
|
||||
}
|
||||
if ('{}' != $text) {
|
||||
do {
|
||||
if ('{' != $text{$offset}) {
|
||||
preg_match("/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/", $text, $match, 0, $offset);
|
||||
$offset += strlen($match[0]);
|
||||
$output[] = ('"' != $match[1]{0} ? $match[1] : stripcslashes(substr($match[1], 1, -1)));
|
||||
if ('},' == $match[3]) {
|
||||
return $offset;
|
||||
}
|
||||
} else {
|
||||
$offset = pg_array_parse($text, $output[], $limit, $offset + 1);
|
||||
}
|
||||
} while ($limit > $offset);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
# __END__
|
||||
Reference in New Issue
Block a user