Ver Fonte

added type and zasob_id if missing for PDO

Piotr Labudda há 10 anos atrás
pai
commit
51158eff02
2 ficheiros alterados com 30 adições e 9 exclusões
  1. 19 1
      SE/se-lib/Core/Pdo.php
  2. 11 8
      SE/se-lib/DB.php

+ 19 - 1
SE/se-lib/Core/Pdo.php

@@ -4,11 +4,13 @@ class Core_Pdo extends PDO {
 
 	protected $_database_name;
 	protected $_zasob_id;
+	protected $_type;
 
 	// public PDO::__construct ( string $dsn [, string $username [, string $password [, array $options ]]] )
 	public function __construct($dsn, $username, $password, $options = array()) {
 		$this->_database_name = $options['database'];
 		$this->_zasob_id = $options['zasob_id'];
+		$this->_type = $options['type'];
 		unset($options['database']);
 		unset($options['zasob_id']);
 		parent::__construct($dsn, $username, $password, $options);
@@ -24,7 +26,11 @@ class Core_Pdo extends PDO {
 		return $this->_zasob_id;
 	}
 
-	public function getTableStruct($tblName) {
+	public function getType() {
+		return $this->_type;
+	}
+
+	public function getTableStruct($tblName) {// TODO: mved to Core_Storage_*
 		$sth = $this->prepare("
 			-- show fields from {$tblName}
 			select cols.COLUMN_NAME as name
@@ -411,6 +417,18 @@ EOF_STRUCT_MYSQL;
 		return $sth->fetchAll();
 	}
 
+	public function fetchAllByKey($sql, $key = 'ID') {
+		$rowsByKey = array();
+		$sth = $this->prepare($sql);
+		$sth->execute();
+		$rows = $sth->fetchAll();
+		foreach ($rows as $row) {
+			$keyRow = V::get($key, null, $row);
+			$rowsByKey[$keyRow] = $row;
+		}
+		return $rowsByKey;
+	}
+
 	public function bindValues($sth, $values) {
 		foreach ($values as $name => $value) {
 			$val = $value;

+ 11 - 8
SE/se-lib/DB.php

@@ -83,9 +83,9 @@ class DB {
 
 	/**
 	 * Get database object.
-	 * 
+	 *
 	 * @param string $db Zasob ID or database name (require config file @see Config::getZasobConf($db))
-	 * 
+	 *
 	 * @returns object Database
 	 */
 	public static function getDB($db = null) {
@@ -102,8 +102,8 @@ class DB {
 			$dbConfName = "test_db";
 		}  else if ($db == 'billing_db') {
 			$dbConfName = "billing_db";
-		} 
-		
+		}
+
 
 		if (!array_key_exists($dbConfName, $_instance)) {
 			$_instance[$dbConfName] = null;
@@ -148,9 +148,11 @@ class DB {
 	public static function getPDO($db = null) {
 		static $_instance;
 		if (!is_array($_instance)) $_instance = array();
+		$zasob_id = '';
 		$dbConfName = 'default_db';
 		if (is_numeric($db) && $db > 0) {
-			$dbConfName = "zasob_{$db}";
+			$zasob_id = $db;
+			$dbConfName = "zasob_{$zasob_id}";
 		} else if ($db == 'import_db') {
 			$dbConfName = "import_db";
 		} else if ($db == 'test_db') {
@@ -171,7 +173,7 @@ class DB {
 			$user = V::get('user', '', $conf);
 			$pass = V::get('pass', '', $conf);
 			$database = V::get('database', '', $conf);
-			$zasob_id = V::get('zasob_id', '', $conf);
+			$zasob_id = V::get('zasob_id', $zasob_id, $conf);
 			if (empty($host)) throw new Exception("Brak zdefiniowanego pola 'host' dla bazy danych '{$dbConfName}'");
 			if (empty($user)) throw new Exception("Brak zdefiniowanego loginu usera dla bazy danych '{$dbConfName}'");
 			if (empty($pass)) throw new Exception("Brak zdefiniowanego hasła usera dla bazy danych '{$dbConfName}'");
@@ -188,6 +190,7 @@ class DB {
 			//$pdo = new PDO($type . ':host=' . $host . ';dbname=' . $database, $user, $pass);
 			//$pdo->exec("SET NAMES 'utf8'");
 			//$sdb = new Core_Pdo($pdo);
+			$options['type'] = $type;
 			$sdb = new Core_Pdo($type . ':host=' . $host . ';dbname=' . $database, $user, $pass, $options);
 			$sdb->exec("SET NAMES 'utf8'");
 			$_instance[$dbConfName] = $sdb;
@@ -245,7 +248,7 @@ public static function query($sql, $die_on_error = true) {
 public static function fetch($res) {
 	$ret = null;
 	if ($res) $ret = mysql_fetch_object($res);
-	return $ret; 
+	return $ret;
 }
 
 
@@ -321,7 +324,7 @@ public static function get_by_id($table, $id) {
  *   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'
  */
 public static function UPDATE_OBJ($table, &$sql_obj) {