Explorar el Código

added schema to db config, use in Pdo::tableNameQuote

Piotr Labudda hace 8 años
padre
commit
420df1503a
Se han modificado 2 ficheros con 17 adiciones y 3 borrados
  1. 15 3
      SE/se-lib/Core/Pdo.php
  2. 2 0
      SE/se-lib/DB.php

+ 15 - 3
SE/se-lib/Core/Pdo.php

@@ -7,12 +7,14 @@ class Core_Pdo extends PDO {
 	protected $_database_name;
 	protected $_zasob_id;
 	protected $_type;
+	protected $_schema;
 
 	// 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'];
+		$this->_schema = (!empty($options['schema'])) ? $options['schema'] : null;
 		unset($options['database']);
 		unset($options['zasob_id']);
 		parent::__construct($dsn, $username, $password, $options);
@@ -40,6 +42,14 @@ class Core_Pdo extends PDO {
 		return $identifier;
 	}
 
+	public function tableNameQuote($tableName) {
+		switch (strtolower($this->_type)) {
+			case 'pgsql': return ($this->_schema) ? "{$this->_schema}.{$tableName}" : $tableName; // "'{$identifier}'";
+			case 'mysql': return "`{$tableName}`";
+		}
+		return $tableName;
+	}
+
 	public function getTableStruct($tblName) {// TODO: mved to Core_Storage_*
 		$sth = $this->prepare("
 			-- show fields from {$tblName}
@@ -561,12 +571,14 @@ EOF_STRUCT_MYSQL;
 		$sqlPrimaryKey = $this->quote($primaryKey, PDO::PARAM_STR);
 		$sqlUpdateSet = [];
 		foreach ($item as $field => $val) {
-			$sqlUpdateSet[] = "`{$field}` = " . $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
+			$sqlUpdateSet[] = $this->identifierQuote($field) . " = " . $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
 		}
+		$sqlTableName = $this->identifierQuote($tableName);
+		$sqlPkName = $this->identifierQuote($primaryKeyName);
 		$sql = "
-			update `{$tableName}`
+			update {$sqlTableName}
 			set " . implode("\n , ", $sqlUpdateSet) . "
-			where `{$primaryKeyName}` = {$sqlPrimaryKey}
+			where {$sqlPkName} = {$sqlPrimaryKey}
 		";
 		return $this->execSql($sql);
 	}

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

@@ -193,6 +193,7 @@ class DB {
 			$pass = V::get('pass', '', $conf);
 			$database = V::get('database', '', $conf);
 			$zasob_id = V::get('zasob_id', $zasob_id, $conf);
+			$schema = V::get('schema', '', $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}'");
@@ -206,6 +207,7 @@ class DB {
 			if (!empty($tdsver)) $options['tdsver'] = $tdsver;
 			$options['zasob_id'] = $zasob_id;
 			if (!empty($database)) $options['database'] = $database;
+			if (!empty($schema)) $options['schema'] = $schema;
 			//$pdo = new PDO($type . ':host=' . $host . ';dbname=' . $database, $user, $pass);
 			//$pdo->exec("SET NAMES 'utf8'");
 			//$sdb = new Core_Pdo($pdo);