Browse Source

updated DB DataSource

Piotr Labudda 10 years ago
parent
commit
2d11a8fb8a
2 changed files with 46 additions and 84 deletions
  1. 15 41
      SE/se-lib/Core/DataSource.php
  2. 31 43
      SE/se-lib/Core/DataSource/Mysql.php

+ 15 - 41
SE/se-lib/Core/DataSource.php

@@ -1,59 +1,33 @@
 <?php
 
 
-class Core_DataSource {
+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'];
+		if (!empty($params['zasob_id'])) $this->_zasob_id = $params['zasob_id'];
 	}
 
-	public function getDatabaseName() {
-		return $this->_database_name;
-	}
-
-	/**
-	 * Wykonuje podane zapytanie i zwraca wynik mysql_query().
-	 */
-	public function query($query) {
-		return null;
-	}
-
-	function fetch($res) {
-		return null;
-	}
-
-	function _($str) {
-		return $str;
-	}
-
-	function error() {
-		return "#unknown";
-	}
-
-	function _set_error($err) {
-		$this->_errors[] = $err;
-	}
-
-	function get_errors() {
-		return $this->_errors;
-	}
-
-	function has_errors() {
-		return !empty($this->_errors);
-	}
+	public function getDatabaseName() { return $this->_database_name; }
 
-	function get_last_error() {
-		return end($this->_errors);
+	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;
 	}
 
-	function get_by_id($table, $id) {
-		return null;
-	}
+	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'!"); }
 
 }

+ 31 - 43
SE/se-lib/Core/DataSource/Mysql.php

@@ -17,11 +17,7 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		}
 	}
 
-	function getConnection() {
-		return $this->_conn;
-	}
-
-	function getVersion($version) {
+	public function getVersion($version) {
 		if (!$this->_version) {
 			$sql = "SHOW VARIABLES LIKE 'version';";
 			$res = $this->query($sql);
@@ -33,7 +29,9 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		return $this->_version;
 	}
 
-	function insert($table, $data) {
+	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();
@@ -44,14 +42,14 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		}
 		$sqlFields = implode(", ", $sqlFields);
 		$sqlValues = implode(", ", $sqlValues);
-		$sql = "insert into `{$table}` ({$sqlFields})
+		$sql = "insert into `{$sqlTableName}` ({$sqlFields})
 			values ({$sqlValues})
 		";
 		$this->query($sql);
 		return mysql_insert_id($this->_conn);
 	}
 
-	function getById($tableName, $id) {
+	public function getById($tableName, $id) {
 		$sqlTableName = $this->_($tableName);
 		$sqlId = (int)$this->_($id);
 		if (!$sqlTableName) throw new Exception("Wrong table name!");
@@ -66,16 +64,6 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		} else throw new Exception("Nie naleziono rekordu nr '{$sqlId}'");
 	}
 
-	function getListByQuery($sql) {
-		$list = array();
-		if (!$sql) throw new Exception("Empty query!");
-		$res = $this->query($sql);
-		while ($r = $this->fetch($res)) {
-			$list[] = $r;
-		}
-		return $list;
-	}
-
 	public function parseValue($value) {
 		$parsedValue = 'NULL';
 		if ('NOW()' == strtoupper($value)) {
@@ -87,7 +75,7 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		return $parsedValue;
 	}
 
-	function query($query) {
+	public function query($query) {
 		$null = null;
 		if (!$this->_conn) throw new Exception("Connection not exists!");
 		$res = mysql_query($query, $this->_conn);
@@ -99,55 +87,59 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		return $res;
 	}
 
-	function fetch( $res ) {
-		$ret = null;
-		if ($res) $ret = mysql_fetch_object( $res );
-		return $ret;
+	public function fetch($res) {
+		if (!$res) return null;
+		return mysql_fetch_object($res);
+	}
+
+	public function _($str) {
+		return mysql_real_escape_string($str, $this->_conn);
 	}
 
-	function fetch_row( $res ) {
+	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 ) {
+	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) {
+	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 ) {
+	function count( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return mysql_num_rows( $res );
 	}
 
-	function num_rows( $res ) {
+	function num_rows( $res ) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return mysql_num_rows( $res );
 	}
 
-	function insert_id() {
+	function insert_id() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return mysql_insert_id( $this->_conn );
 	}
-	function show_tables($table=null) {
+
+	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) {
+	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) {
+	function describe_table_value($table) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 
 		$res = self::describe_table($table);
 		while($h=self::fetch($res)) {
@@ -156,7 +148,7 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		return $result;
 	}
 
-	function show_index($table,$only_primary_flag=false) {
+	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' ";
 
@@ -165,26 +157,22 @@ class Core_DataSource_Mysql extends Core_DataSource {
 
 	}
 
-	function show_index_value($table) {
+	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) {
+	function affected_rows($needed_in_psql_only=null) { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return mysql_affected_rows( $this->_conn );
 	}
 
-	function _( $str ) {
-		return mysql_real_escape_string( $str, $this->_conn );
-	}
-
-	function error() {
+	function error() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return "#".mysql_errno($this->_conn).": ".mysql_error($this->_conn);
 	}
 
-	function errno() {
+	function errno() { throw new Exception("Unimplemented Data Source function '" . __FUNCTION__ . "'!");
 		return mysql_errno($this->_conn);
 	}
 
@@ -199,7 +187,7 @@ class Core_DataSource_Mysql extends Core_DataSource {
 	 *
 	 * 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) {
+	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
 
@@ -286,7 +274,7 @@ class Core_DataSource_Mysql extends Core_DataSource {
 		return $returnCode;
 	}
 
-	function ADD_NEW_OBJ( $table, $sql_obj,$dieonerror=null ) {
+	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);