Parcourir la source

rm not used Core_DataSource_*

Piotr Labudda il y a 6 ans
Parent
commit
081f6133a7

+ 0 - 33
SE/se-lib/Core/DataSource.php

@@ -1,33 +0,0 @@
-<?php
-
-
-class Core_DataSource {/* TODO: abstract */
-
-	var $_conn;
-	var $_errors;
-	var $_database_name;
-	var $_zasob_id;
-
-	public function __construct($host, $user, $password, $database, $names = '', $params = array()) {
-		$this->_conn = null;
-		$this->_errors = array();
-		$this->_database_name = $database;
-		if (!empty($params['zasob_id'])) $this->_zasob_id = $params['zasob_id'];
-	}
-
-	public function getDatabaseName() { return $this->_database_name; }
-
-	public function getListByQuery($sql) {// TODO: query builder, ogc?
-		$list = array();
-		if (!$sql) throw new Exception("Empty query!");
-		$res = $this->query($sql);
-		while ($r = $this->fetch($res)) {
-			$list[] = $r;
-		}
-		return $list;
-	}
-
-	public function insert($table, $data) { throw new Exception("Unimplemented Data Source function 'insert'!"); }
-	public function getById($tableName, $id) { throw new Exception("Unimplemented Data Source function 'getById'!"); }
-
-}

+ 0 - 262
SE/se-lib/Core/DataSource/Mssql.php

@@ -1,262 +0,0 @@
-<?php
-
-Lib::loadClass('Core_DataSource');
-
-class Core_DataSource_Mssql extends Core_DataSource {
-
-	function __construct($host, $user, $password, $database, $names = '', $params = array()) {
-		if ($names != '') {
-			if (strtolower($names) == 'utf8') {
-				$names = 'UTF-8';// @see http://stackoverflow.com/questions/1322421/php-sql-server-how-to-set-charset-for-connection
-			}
-			ini_set('mssql.charset', $names);
-		}
-
-		if (!empty($params['tdsver'])) {
-			$tdsver = V::get('tdsver', '', $params);
-			putenv("TDSVER={$tdsver}");
-		}
-
-		$this->_database_name = $database;
-		$this->_conn = @mssql_connect($host, $user, $password);
-		if (!is_resource($this->_conn)) {
-			$this->_set_error('CREATE CONNECTION FAILED');
-			return;
-		}
-		if (false === mssql_select_db($database, $this->_conn)) {
-			$this->_set_error('SELECT DATABASE FAILED');
-			return;
-		}
-
-
-		if ($names != '') {
-			//$this->query(" SET NAMES '$names' ");
-		}
-	}
-
-	function getConnection() {
-		return $this->_conn;
-	}
-
-	function getVersion($version) {
-		if (!$this->_version) {
-			// TODO: get version sql
-		}
-		return $this->_version;
-	}
-
-	/**
-	 * Wykonuje podane zapytanie i zwraca wynik mssql_query().
-	 */
-	function query($query, $msg = 'Query ERROR.') {
-		$null = null;
-		if (!$this->_conn) { return $null; }
-		$res = mssql_query($query, $this->_conn);
-		if (!$res) {
-			$this->_set_error('SQL QUERY FAILED: ' . mssql_get_last_message());
-			return $null;
-		}
-		return $res;
-	}
-
-	function fetch($res) {
-		if (!is_resource($res)) return null;
-		return mssql_fetch_object($res);
-	}
-
-	function fetch_row($res) {
-		if (!is_resource($res)) return null;
-		return mssql_fetch_row($res);
-	}
-
-	/**
-	 * Returns an associative array that corresponds to the fetched row and moves
-	 * the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling
-	 * mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter.
-	 * It only returns an associative array.
-	 */
-	function fetch_assoc($res) {
-		$ret = null;
-		if (!is_resource($res)) {
-			return null;
-		} else {
-			$ret = mssql_fetch_assoc($res);
-		}
-		return $ret;
-	}
-
-	function fetch_array($res, $result_type = null) {
-		if (!is_resource($res)) return null;
-		return ($result_type)? mssql_fetch_array($res, $result_type) : mssql_fetch_array($res);
-	}
-
-	function count($res) {
-		if (!is_resource($res)) return null;
-		return mssql_num_rows($res);
-	}
-
-	function num_rows($res) {
-		if (!is_resource($res)) return null;
-		return mssql_num_rows($res);
-	}
-
-	function insert_id() {
-		$id = 0;
-		$res = $this->query("SELECT @@identity AS id");
-		if ($row = $this->fetch_assoc($res)) {
-			$id = $row["id"];
-		}
-		return $id;
-	}
-
-	function affected_rows() {
-		return mssql_rows_affected($this->_conn);
-	}
-
-	function _($str) {
-		//TODO: return mssql_real_escape_string($str, $this->_conn);
-		return $str;
-	}
-
-	function error() {
-		return "#".mssql_errno($this->_conn).": ".mssql_error($this->_conn);
-	}
-
-	function get_by_id( $table, $id ) {
-		$null = null;
-		$sql = "select p.*
-			from `".$table."` as p
-			where p.`ID`='".$id."'
-		";
-		$res = $this->query( $sql );
-		if ($r = $this->fetch( $res )) {
-			return $r;
-		}
-		return $null;
-	}
-
-	/**
-	 * @returns int
-	 *   1 - changed but without add hist
-	 *   2 - changed and add hist
-	 *   0 - nothing to change
-	 *   -1 - error ID not set
-	 *   -2 - error id not exists in DB
-	 *
-	 * TODO: sprawdzac czy w hist mozna odczytac aktualny stan, jesli nie to dodac caly rekord do HIST, jako 'procesy-fix-hist-data'
-	 */
-	function UPDATE_OBJ( $table, &$sql_obj ) {
-		if (!isset($sql_obj->ID) || $sql_obj->ID <= 0) {
-			return -1;
-		}
-		$id = $sql_obj->ID;
-
-		// check id record $id exists
-		if (($curr_obj = $this->get_by_id( $table, $sql_obj->ID )) == null) {
-			return -2;
-		}
-
-		// check if enything changed
-		$changed = false;
-		$fields_to_change = get_object_vars($sql_obj);
-		foreach ($fields_to_change as $k => $v) {
-			if ($k == 'ID') continue;
-			if ($v == $curr_obj->$k) {// === ?
-				unset($sql_obj->$k);
-			} else {
-				$changed = true;
-			}
-		}
-		if ($changed == false) {
-			return 0;// record not changed
-		}
-
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col []= 'A_RECORD_CREATE_DATE';
-		$admin_col []= 'A_RECORD_CREATE_AUTHOR';
-		// ...
-		$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
-		$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-			if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL') {
-				$v = 'NULL';
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr [] = "`{$k}`={$v}";
-		}
-		$sql = "update `{$table}` set ".implode(",", $sql_arr)." where `ID`='{$id}' limit 1; ";
-		$this->query($sql);
-
-		if ($this->has_errors()) {
-			//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());echo'</pre>';
-		}
-
-		$ret = $this->affected_rows();
-		if ($ret) {
-			$sql_obj->ID_USERS2 = $sql_obj->ID;
-			unset($sql_obj->ID);
-			$new_id = $this->ADD_NEW_OBJ("{$table}_HIST", $sql_obj);
-			if ($new_id) {
-				$ret += 1;
-			}
-		}
-		return $ret;
-	}
-
-	function ADD_NEW_OBJ( $table, $sql_obj ) {
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col []= 'ID';
-		$admin_col []= 'A_RECORD_CREATE_DATE';
-		$admin_col []= 'A_RECORD_CREATE_AUTHOR';
-		$admin_col []= 'A_RECORD_UPDATE_DATE';
-		$admin_col []= 'A_RECORD_UPDATE_AUTHOR';
-		// ...
-		$sql_arr["`ID`"] = "NULL";// add default value for ID, NULL in all inserts
-		if (substr($table, 0, -5) == '_HIST') {
-			$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
-			$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		} else {
-			$sql_obj->A_RECORD_CREATE_DATE = date('Y-m-d-H:i');
-			$sql_obj->A_RECORD_CREATE_AUTHOR = User::getName();
-		}
-
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-			if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL' && substr($table, -5) != '_HIST') {
-				$v = 'NULL';
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr ["`{$k}`"] = $v;
-		}
-		$sql = "insert into `{$table}` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
-		$this->query($sql);
-
-		if ($this->has_errors()) {
-			//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());echo'</pre>';
-		}
-
-		$ret_id = $this->insert_id();
-		if (substr($table, -5) == '_HIST') {
-			return $ret_id;
-		}
-		if ($ret_id) {
-			$sql_obj->ID_USERS2 = $ret_id;
-			unset($sql_obj->ID);
-			$new_id_hist = $this->ADD_NEW_OBJ($table . '_HIST', $sql_obj);
-			// error jesli nie udalo sie dodac rekordu do tabeli _HIST
-		}
-		return $ret_id;
-	}
-
-}

+ 0 - 339
SE/se-lib/Core/DataSource/Mysql.php

@@ -1,339 +0,0 @@
-<?php
-
-Lib::loadClass('Core_DataSource');
-Lib::loadClass('DataSourceException');
-
-class Core_DataSource_Mysql extends Core_DataSource {
-
-	function __construct($host, $user, $password, $database, $names = '', $params = array()) {
-		parent::__construct($host, $user, $password, $database, $names, $params);
-
-		$this->_conn = @mysql_pconnect($host, $user, $password);
-		if (!is_resource($this->_conn)) throw new Exception("Create connection failed!");
-		if (false === mysql_select_db($database, $this->_conn)) throw new Exception("Select database failed!");
-
-		if ($names != '') {
-			$this->query(" SET NAMES '$names' ");
-		}
-	}
-
-	public function getVersion($version) {
-		if (!$this->_version) {
-			$sql = "SHOW VARIABLES LIKE 'version';";
-			$res = $this->query($sql);
-			if ($r = $this->fetch($res)) {
-				// [Variable_name] => version, [Value] => 4.0.26-log
-				$this->_version = $r->Value;
-			}
-		}
-		return $this->_version;
-	}
-
-	public function insert($tableName, $data) {
-		$sqlTableName = $this->_($tableName);
-		if (!$sqlTableName) throw new Exception("Wrong table name!");
-		if (is_object($data)) $data = (array)$data;
-		else if (!is_array($data)) throw new Exception("Wrong data type to insert.");
-		$sqlFields = array();
-		$sqlValues = array();
-		foreach ($data as $fldName => $fldValue) {
-			$sqlFields[] = "`{$fldName}`";
-			$sqlValues[] = $this->parseValue($fldValue);
-		}
-		$sqlFields = implode(", ", $sqlFields);
-		$sqlValues = implode(", ", $sqlValues);
-		$sql = "insert into `{$sqlTableName}` ({$sqlFields})
-			values ({$sqlValues})
-		";
-		$this->query($sql);
-		return mysql_insert_id($this->_conn);
-	}
-
-	public function getById($tableName, $id) {
-		$sqlTableName = $this->_($tableName);
-		$sqlId = (int)$this->_($id);
-		if (!$sqlTableName) throw new Exception("Wrong table name!");
-		if ($sqlId <= 0) throw new Exception("Wrong record id!");
-		$sql = "select t.*
-			from `{$sqlTableName}` as t
-			where t.`ID`='{$sqlId}'
-		";
-		$res = $this->query($sql);
-		if ($r = $this->fetch($res)) {
-			return $r;
-		} else throw new Exception("Nie naleziono rekordu nr '{$sqlId}'");
-	}
-
-	public function parseValue($value) {
-		$parsedValue = 'NULL';
-		if ('NOW()' == strtoupper($value)) {
-			$parsedValue = 'NOW()';
-		} else if ('GeomFromText' == substr($value, 0, strlen('GeomFromText'))) {
-		} else {
-			$parsedValue = "'" . $this->_($value) . "'";
-		}
-		return $parsedValue;
-	}
-
-	public function query($query) {
-		$null = null;
-		if (!$this->_conn) throw new Exception("Connection not exists!");
-		$res = mysql_query($query, $this->_conn);
-		if (!$res) {
-			$exception = new DataSourceException("Query error: " . mysql_error($this->_conn), mysql_errno($this->_conn));
-			$exception->setQuery($query);
-			throw $exception;
-		}
-		return $res;
-	}
-
-	public function fetch($res) {
-		if (!$res) return null;
-		return mysql_fetch_object($res);
-	}
-
-	public function _($str) {
-		//return $this->_pdo->quote($str);// TODO: PDO
-		return mysql_real_escape_string($str, $this->_conn);
-	}
-
-	function fetch_row( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$ret = null;
-		if ($res) $ret = mysql_fetch_row( $res );
-		return $ret;
-	}
-
-	function fetch_assoc( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$ret = null;
-		if ($res) $ret = mysql_fetch_assoc( $res );
-		return $ret;
-	}
-
-	function fetch_array($res) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$ret = null;
-		if ($res) $ret = mysql_fetch_array($res);
-		return $ret;
-	}
-
-	function count( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return mysql_num_rows( $res );
-	}
-
-	function num_rows( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return mysql_num_rows( $res );
-	}
-
-	function insert_id() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return mysql_insert_id( $this->_conn );
-	}
-
-	function show_tables($table=null) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		if(!empty($table)) $sql="show tables like '".$table."'";
-		else $sql="show tables";
-		$res = $this->query($sql);
-		return $res;
-	}
-
-	function describe_table($table) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$sql='SHOW FIELDS FROM `'.$table.'`';
-		$res = $this->query($sql);
-		return $res;
-	}
-
-	function describe_table_value($table) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-
-		$res = self::describe_table($table);
-		while($h=self::fetch($res)) {
-			$result[$h->Field]=$h;
-		}
-		return $result;
-	}
-
-	function show_index($table,$only_primary_flag=false) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$sql="show index from `".$table."`";
-		if($only_primary_flag) $sql.=" WHERE  `Key_name` =  'PRIMARY' ";
-
-		$res=self::query($sql);
-		return $res;
-
-	}
-
-	function show_index_value($table) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$res=self::show_index($table,true);
-		while($h=self::fetch($res)) {
-			return $h->Column_name;
-		}
-	}
-
-	function affected_rows($needed_in_psql_only=null) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return mysql_affected_rows( $this->_conn );
-	}
-
-	function error() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return "#".mysql_errno($this->_conn).": ".mysql_error($this->_conn);
-	}
-
-	function errno() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		return mysql_errno($this->_conn);
-	}
-
-	/**
-	 * @returns int
-	 *   1 - changed but without add hist
-	 *   2 - changed and add hist
-	 *   0 - nothing to change
-	 *   -1 - sql errors
-	 *   -2 - error id not exists in DB
-	 *   -3 - error ID not set
-	 *
-	 * TODO: sprawdzac czy w hist mozna odczytac aktualny stan, jesli nie to dodac caly rekord do HIST, jako 'procesy-fix-hist-data'
-	 */
-	public function UPDATE_OBJ($table, $sql_obj,$timestamp=null,$skip_author=null) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-		$structure=self::describe_table_value($table); //todo to cache optimize
-		$primary=self::show_index_value($table); //todo to cache optimize
-
-		if (!isset($sql_obj->$primary) || $sql_obj->$primary <= 0) {
-			return -3;
-		}
-		$id = $sql_obj->$primary;
-
-		// check id record $id exists
-		if (($curr_obj = $this->get_by_id( $table, $sql_obj->$primary )) == null) {
-			return -2;
-		}
-
-		// check if enything changed
-		$changed = false;
-		$fields_to_change = get_object_vars($sql_obj);
-		foreach ($fields_to_change as $k => $v) {
-			if ($k == $primary) continue;
-			if ($v == $curr_obj->$k) {// === ?
-				unset($sql_obj->$k);
-			} else {
-				$changed = true;
-			}
-		}
-		if ($changed == false) {
-			return 0;// record not changed
-		}
-
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col[] = 'A_RECORD_CREATE_DATE';
-		$admin_col[] = 'A_RECORD_CREATE_AUTHOR';
-		// ...
-		$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
-// OFF - BUG w _HIST		$sql_obj->A_RECORD_UPDATE_DATE = "FROM_UNIXTIME(".$this->get_current_time().")";
-
-		$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-		   if(!empty($skip_author)) {
-			   if($k=='A_RECORD_UPDATE_AUTHOR') continue;
-			   if($k=='A_RECORD_UPDATE_DATE') continue;
-		   }
-
-
-			if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL') {
-				$v = 'NULL';
-			} else if (substr($v, 0, strlen('GeomFromText')) == 'GeomFromText') {
-
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr [] = "`{$k}`={$v}";
-		}
-		$sql = "update `{$table}` set ".implode(",", $sql_arr)." where `ID`='{$id}' limit 1; ";
-		$this->query($sql);
-
-		$returnError = false;
-		$skipDbErrorAddHist = false;
-		if ($this->has_errors()) {
-			$returnError = true;
-			if (1146 == $this->errno()) {
-				$skipDbErrorAddHist = true;
-			}
-		}
-
-		$returnCode = 0;
-		$affected = $this->affected_rows();
-		if ($affected || $skipDbErrorAddHist) {
-			$returnCode = 1;
-			$sql_obj->ID_USERS2 = $sql_obj->$primary;
-			unset($sql_obj->$primary);
-			$new_id = $this->ADD_NEW_OBJ("{$table}_HIST", $sql_obj);
-			if ($new_id) {
-				$returnCode += 1;
-			}
-		}
-		if ($returnError) {
-			return -1;
-		}
-		return $returnCode;
-	}
-
-	function ADD_NEW_OBJ( $table, $sql_obj,$dieonerror=null ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
-
-		//TODO to optimize:
-		$structure=self::describe_table_value($table);
-		$primary=self::show_index_value($table);
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col []= 'ID';
-		$admin_col []= 'A_RECORD_CREATE_DATE';
-		$admin_col []= 'A_RECORD_CREATE_AUTHOR';
-		$admin_col []= 'A_RECORD_UPDATE_DATE';
-		$admin_col []= 'A_RECORD_UPDATE_AUTHOR';
-		// ...
-		$sql_arr["`ID`"] = "NULL";// add default value for ID, NULL in all inserts
-		if (substr($table, 0, -5) == '_HIST') {
-			$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
-			$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		} else {
-			$sql_obj->A_RECORD_CREATE_DATE = date('Y-m-d-H:i');
-			$sql_obj->A_RECORD_CREATE_AUTHOR = User::getName();
-		}
-
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-			if($k==$primary) $v='0';
-			else if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL' && substr($table, -5) != '_HIST') {
-				$v = 'NULL';
-			} else if (substr($v, 0, strlen('GeomFromText')) == 'GeomFromText') {
-
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr ["`{$k}`"] = $v;
-		}
-		$sql = "insert into `{$table}` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
-		//error_log($sql);
-		$this->query($sql);
-
-		if ($this->has_errors()) {
-			if(!empty($dieonerror)) {
-			  echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());echo'</pre>';
-			  DEBUG_S(-3,'Bledne zapytanie sql',$sql,__FILE__,__FUNCTION__,__LINE__);
-			}
-		}
-
-		$ret_id = $this->insert_id();
-		if (substr($table, -5) == '_HIST') {
-			return $ret_id;
-		}
-		if ($ret_id) {
-			$sql_obj->ID_USERS2 = $ret_id;
-			unset($sql_obj->ID);
-			$new_id_hist = $this->ADD_NEW_OBJ($table . '_HIST', $sql_obj);
-			// error jesli nie udalo sie dodac rekordu do tabeli _HIST
-		}
-		return $ret_id;
-	}
-
-}

+ 0 - 307
SE/se-lib/Core/DataSource/Pgsql.php

@@ -1,307 +0,0 @@
-<?php
-
-Lib::loadClass('Core_DataSource');
-
-class Core_DataSource_Pgsql extends Core_DataSource {
-
-	function __construct($host, $user, $password, $database, $names = '', $params = array()) {
-		parent::__construct($host, $user, $password, $database, $names, $params);
-
-		list($host,$port)=explode(":", $host);
-		$this->_conn = @pg_connect("host=".$host." port=".$port." dbname=".$database." user=".$user." password=".$password);
-
-		//DEBUG_S(-3,'conn'.$names." ",$params);
-		if (!is_resource($this->_conn)) {
-			$this->_set_error('CREATE CONNECTION FAILED');
-			return;
-		}
-		//if (false === mysql_select_db($database, $this->_conn)) {
-		//	$this->_set_error('SELECT DATABASE FAILED');
-		//	return;
-		//}
-
-		if ($names != '') {
-			$this->query("SET CLIENT_ENCODING TO '".$names."';");
-		}
-	}
-
-	function getConnection() {
-		return $this->_conn;
-	}
-
-	function getVersion($version) {
-		if (!$this->_version) {
-			//$sql = "SHOW VARIABLES LIKE 'version';";
-			$this->_version=pg_version($this->_conn);
-			//if ($r = $this->fetch($res)) {
-			//	// [Variable_name] => version, [Value] => 4.0.26-log
-			//	$this->_version = $r->Value;
-			//}
-		}
-		return $this->_version;
-	}
-
-	/**
-	 * Wykonuje podane zapytanie i zwraca wynik mysql_query().
-	 */
-	function query( $query, $msg = 'Query ERROR.' ) {
-		$null = null;
-		if (!$this->_conn) { return $null; }
-		$res = pg_query($this->_conn,$query);
-		if (!$res) {
-			DEBUG_S(-3,'error ',array($query),__FILE__,__FUNCTION__,__LINE__);
-			$this->_set_error('SQL QUERY FAILED: '.pg_result_error($this->_conn)."(".$query.")".pg_last_error($this->_conn));
-			return $null;
-		}
-		return $res;
-	}
-
-	function fetch( $res ) {
-		$ret = null;
-		if ($res) $ret = pg_fetch_object( $res );
-		return $ret;
-	}
-
-	function fetch_row( $res ) {
-		$ret = null;
-		if ($res) $ret = pg_fetch_row( $res );
-		return $ret;
-	}
-
-	function fetch_assoc( $res ) {
-		$ret = null;
-		if ($res) $ret = pg_fetch_assoc( $res );
-		return $ret;
-	}
-
-	function fetch_array($res) {
-		$ret = null;
-		if ($res) $ret = pg_fetch_array($res);
-		return $ret;
-	}
-
-	function count( $res ) {
-		return pg_num_rows( $res );
-	}
-
-	function num_rows( $res ) {
-		return pg_num_rows( $res );
-	}
-
-	function insert_id() {
-		$sql="select lastval();";
-		$res = $this->query($sql);
-		$last=$this->fetch_array($res);
-		return $last[0];
-		//return fetch_row( $res ); //TODO TESTING!!!
-		//die('TEST THIS FUNCTION!!!');
-		//return mysql_insert_id( $this->_conn );
-	}
-
-	function affected_rows($res) {
-		return pg_affected_rows( $res );
-	}
-
-	function _( $str ) {
-		return pg_escape_string( $str);
-	}
-
-	function error() {
-		return "#".pg_result_error_field($this->_conn).": ".pg_result_error($this->_conn);
-	}
-
-	function get_by_id( $table, $id ) {
-		$primary=self::show_index_value($table); //TODO to optimalize cache
-
-		$null = null;
-		$sql = "select p.*
-			from \"".$table."\" as p
-			where p.\"".$primary."\"='".$id."'
-		";
-		$res = $this->query( $sql );
-		if ($r = $this->fetch( $res )) {
-			return $r;
-		}
-		return $null;
-	}
-
-	/**
-	 * @returns int
-	 *   1 - changed but without add hist
-	 *   2 - changed and add hist
-	 *   0 - nothing to change
-	 *   -1 - sql errors
-	 *   -2 - error id not exists in DB
-	 *   -3 - error ID not set
-	 *
-	 * TODO: sprawdzac czy w hist mozna odczytac aktualny stan, jesli nie to dodac caly rekord do HIST, jako 'procesy-fix-hist-data'
-	 */
-	public function UPDATE_OBJ($table, $sql_obj,$timestamp=null,$skip_author=null) {
-		$structure=self::describe_table_value($table);
-		$primary=self::show_index_value($table);
-
-		if (!isset($sql_obj->$primary) || $sql_obj->$primary <= 0) {
-			return -3;
-		}
-		$id = $sql_obj->$primary;
-
-		// check id record $id exists
-		if (($curr_obj = $this->get_by_id( $table, $sql_obj->$primary )) == null) {
-			return -2;
-		}
-
-		// check if enything changed
-		$changed = false;
-		$fields_to_change = get_object_vars($sql_obj);
-		//DEBUG_S(-3,'chk',array($fields_to_change),__FILE__,__FUNCTION__,__LINE__);
-
-		foreach ($fields_to_change as $k => $v) {
-			if ($k == $primary) continue;
-			if ($v == $curr_obj->$k) {// === ?
-				unset($sql_obj->$k);
-			} else {
-				$changed = true;
-			}
-		}
-		if ($changed == false) {
-			return 0;// record not changed
-		}
-
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col []= 'A_RECORD_CREATE_DATE';
-		$admin_col []= 'A_RECORD_CREATE_AUTHOR';
-		// ...
-//		$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d H:i');
-		if(!empty($timestamp))  //fixed timestamp option TODO @2015-01-not working due to trigger?
-			$sql_obj->A_RECORD_UPDATE_DATE = $timestamp;
-		else
-		$sql_obj->A_RECORD_UPDATE_DATE = self::get_current_timestamp();
-
-		$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-
-			if($k=='A_RECORD_CREATE_DATE'&& $v=='0000-00-00 00:00:00') {
-				unset($k);
-				unset($v);
-				continue;
-
-			} else if(strstr(trim($structure[$k]->Type),'date') && (strstr($v,'-00'))) {
-				$v="'".str_replace("-00","-01",$v)."'" ;
-			} else if(strstr(trim($structure[$k]->Type),'date') && (strstr($v,'0000'))) {
-				$v="'".str_replace("0000","1970",$v)."'" ;
-			} else if (substr($v, 0, strlen('ST_GeomFromText')) == 'ST_GeomFromText') {
-
-			} else if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL') {
-				$v = 'NULL';
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr [] = '"'.$k.'"='.$v;
-		}
-
-		$sql = "update \"{$table}\" set ".implode(",", $sql_arr)." where \"".$primary."\"='{$id}'  ; ";
-	//	DEBUG_S(-3,'update',array($sql),__FILE__,__FUNCTION__,__LINE__);
-	//	die();
-		$res=$this->query($sql);
-
-		if ($this->has_errors()) {
-			//DEBUG_S(-3,'errors',$this->has_errors,__FILE__,__FUNCTION__,__LINE__);
-			return -1;
-		}
-
-		$ret = $this->affected_rows($res);
-				//	DEBUG_S(-3,'affected',$ret,__FILE__,__FUNCTION__,__LINE__);
-
-		if ($ret) {
-			$sql_obj->ID_USERS2 = $sql_obj->$primary;
-			unset($sql_obj->$primary);
-			$new_id = $this->ADD_NEW_OBJ("{$table}_HIST", $sql_obj);
-			if ($new_id) {
-				$ret += 1;
-			}
-		}
-		return $ret;
-	}
-
-	function ADD_NEW_OBJ( $table, $sql_obj,$dieonerror=null ) {
-		$structure=self::describe_table_value($table);
-		$primary=self::show_index_value($table);
-		$sql_arr = array();
-		// TODO: add admin columns if exists in table - search in session
-		$admin_col = array();
-		$admin_col []= 'A_RECORD_CREATE_DATE';
-		$admin_col []= 'A_RECORD_CREATE_AUTHOR';
-		$admin_col []= 'A_RECORD_UPDATE_DATE';
-		$admin_col []= 'A_RECORD_UPDATE_AUTHOR';
-		// ...
-		if (substr($table, 0, -5) == '_HIST') {
-			$sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d H:i');
-			$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
-		} else {
-			$sql_obj->A_RECORD_CREATE_DATE = date('Y-m-d H:i');
-			$sql_obj->A_RECORD_CREATE_AUTHOR = User::getName();
-		}
-
-		foreach (get_object_vars($sql_obj) as $k => $v) {
-			if($k==$primary) {
-				unset($k);
-				unset($v);
-				continue;
-			}  else if (substr($v, 0, strlen('ST_GeomFromText')) == 'ST_GeomFromText') {
-
-			} else if(strstr(trim($structure[$k]->Type),'int')) {
-				if(strlen($v)>0) {
-					$v=$v;
-				} else if(empty($v)) $v='null';
-				else $v = $v;
-			} else if(trim($structure[$k]->Type)=='datetime') {
-				if(empty($v)) $v='null';
-				else if(strstr($v,'-00')) $v="'".str_replace("-00","-01",$v)."'" ;
-				else $v = "'".$v."'::timestamp";
-			} else if(trim($structure[$k]->Type)=='date') {
-				if($v=='0000-00-00') $v='null';
-				else if(strstr($v,'-00')) $v="'".str_replace("-00","-01",$v)."'" ;
-				else $v = "'".$v."'";
-			} else if (strtoupper($v) == 'NOW()') {
-				$v = 'NOW()';
-			} else if (strtoupper($v) == 'NULL' && substr($table, -5) != '_HIST') {
-				$v = 'NULL';
-			} else if(strstr($structure[$k]->Type,'int(')) {
-				$v = $v ;
-			} else {
-				$v = $this->_($v);
-				$v = "'{$v}'";
-			}
-			$sql_arr ['"'.$k.'"'] = $v;
-		}
-		$sql = "insert into \"{$table}\" (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
-		//DEBUG_S(-3,' insert sql ',$sql,__FILE__,__FUNCTION__,__LINE__);
-		$this->query($sql);
-
-		if ($this->has_errors()) {
-			if($dieonerror) {
-				DEBUG_S(-3,'Has errors died',$this->get_errors(),__FILE__,__FUNCTION__,__LINE__);
-				die();
-			}
-		//	echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());print_r($sql);echo'</pre>';
-		}
-
-		$ret_id = $this->insert_id();
-		if (substr($table, -5) == '_HIST') {
-			return $ret_id;
-		}
-		if ($ret_id) {
-			$sql_obj->ID_USERS2 = $ret_id;
-			unset($sql_obj->ID);
-			$new_id_hist = $this->ADD_NEW_OBJ($table . '_HIST', $sql_obj);
-			// error jesli nie udalo sie dodac rekordu do tabeli _HIST
-		}
-		return $ret_id;
-	}
-
-}

+ 0 - 49
SE/se-lib/DB.php

@@ -32,55 +32,6 @@ Lib::loadClass('Core_Pdo');
 
 class DB {
 
-	public static function getDataSource($db = null) {
-		static $_instanceList;
-		if (!is_array($_instanceList)) {
-			$_instanceList = array();
-		}
-
-		if (null === $db) {
-			$configName = 'default_db';
-		} else if (is_numeric($db) && $db > 0) {
-			$configName = "zasob_{$db}";
-		} else if ($db == 'import_db') {
-			$configName = "import_db";
-		} else if ($db == 'test_db') {
-			$configName = "test_db";
-		}  else if ($db == 'billing_db') {
-			$configName = "billing_db";
-		} else {// TODO: check by name from zasoby
-			throw new Exception("Unknown data source name!");
-		}
-
-		if (array_key_exists($configName, $_instanceList)) {
-			return $_instanceList[$configName];
-		}
-		$_instanceList[$configName] = null;
-
-		$conf = Config::getConfFile($configName);
-		if (!$conf) throw new Exception("Config for data source '{$configName}' not found!");
-		$type = V::get('type', 'mysql', $conf);
-		$host = V::get('host', '', $conf);
-		$port = V::get('port', '', $conf);
-		$user = V::get('user', '', $conf);
-		$pass = V::get('pass', '', $conf);
-		$zasob_id = V::get('zasob_id', '', $conf);
-
-		$database = V::get('database', '', $conf);
-		if ($port && $host) $host .= ":{$port}";
-		$names = 'utf8';
-		$db_class = 'Core_DataSource_' . ucfirst($type);
-		Lib::loadClass($db_class);
-		if (!class_exists($db_class)) throw new Exception("Data source class for type '{$type}' not found!");
-		$params = array();
-		$tdsver = V::get('tdsver', '', $conf);
-		if (!empty($tdsver)) $params['tdsver'] = $tdsver;
-		if (!empty($zasob_id)) $params['zasob_id'] = $zasob_id;
-
-		$_instanceList[$configName] = new $db_class($host, $user, $pass, $database, $names, $params);
-		return $_instanceList[$configName];
-	}
-
 	/**
 	 * Get database object.
 	 *