summaryrefslogtreecommitdiff
path: root/framework/Data/Common
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/Common')
-rw-r--r--framework/Data/Common/IbmDb2/TIbmColumnMetaData.php156
-rw-r--r--framework/Data/Common/IbmDb2/TIbmMetaData.php112
-rw-r--r--framework/Data/Common/IbmDb2/TIbmMetaDataInspector.php112
-rw-r--r--framework/Data/Common/Mysql/TMysqlMetaData.php2
-rw-r--r--framework/Data/Common/Oracle/TOracleCommandBuilder.php41
-rw-r--r--framework/Data/Common/Oracle/TOracleMetaData.php43
-rw-r--r--framework/Data/Common/Pgsql/TPgsqlMetaData.php4
-rw-r--r--framework/Data/Common/Pgsql/TPgsqlTableInfo.php2
8 files changed, 25 insertions, 447 deletions
diff --git a/framework/Data/Common/IbmDb2/TIbmColumnMetaData.php b/framework/Data/Common/IbmDb2/TIbmColumnMetaData.php
deleted file mode 100644
index 3b7001ab..00000000
--- a/framework/Data/Common/IbmDb2/TIbmColumnMetaData.php
+++ /dev/null
@@ -1,156 +0,0 @@
-<?php
-/**
- * TIbmColumnMetaData class file.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id: TIbmColumnMetaData.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- */
-
-/**
- * TIbmColumnMetaData class.
- *
- * Column details for IBM DB2 database. Using php_pdo_ibm.dll extension.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @version $Id: TIbmColumnMetaData.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- * @since 3.1
- */
-class TIbmColumnMetaData extends TComponent
-{
- private $_name;
- private $_type;
- private $_length;
- private $_autoIncrement;
- private $_default;
- private $_notNull=true;
-
- private $_isPrimary=null;
-
- private $_property;
-
- /**
- * Initialize column meta data.
- *
- * @param string column name.
- * @param string column data type.
- * @param string column data length.
- * @param boolean column can not be null.
- * @param string serial name.
- * @param string default value.
- */
- public function __construct($name,$type,$length,$notNull,$autoIncrement,$default,$primary)
- {
- $this->_property=$name;
- $this->_name=$name;
- $this->_type=$type;
- $this->_length=$length;
- $this->_notNull=$notNull;
- $this->_autoIncrement=$autoIncrement;
- $this->_default=$default;
- $this->_isPrimary=$primary;
- }
-
- /**
- * @return string quoted column name.
- */
- public function getName()
- {
- return $this->_name;
- }
-
- /**
- * @return integer length.
- */
- public function getLength()
- {
- return $this->_length;
- }
-
- /**
- * @return string active record property name
- */
- public function getProperty()
- {
- return $this->_property;
- }
-
- /**
- * @return boolean true if column is a sequence, false otherwise.
- */
- public function hasSequence()
- {
- return $this->_autoIncrement;
- }
-
- /**
- * @return null no sequence name.
- */
- public function getSequenceName()
- {
- return null;
- }
-
- /**
- * @return boolean true if the column is a primary key, or part of a composite primary key.
- */
- public function getIsPrimaryKey()
- {
- return $this->_isPrimary;
- }
-
- /**
- * @return string column type
- */
- public function getType()
- {
- return $this->_type;
- }
-
-
- /**
- * @return boolean false if column can be null, true otherwise.
- */
- public function getNotNull()
- {
- return $this->_notNull;
- }
-
- /**
- * @return boolean true if column has default value, false otherwise.
- */
- public function hasDefault()
- {
- return $this->_default !== null;
- }
-
- /**
- * @return string default column value.
- */
- public function getDefaultValue()
- {
- return $this->_default;
- }
-
- /**
- * @return string PHP primative type derived from the column type.
- */
- public function getPHPType()
- {
- switch(strtolower($this->_type))
- {
- case 'smallint': case 'integer':
- return 'integer';
- case 'real': case 'float': case 'double': case 'decimal': case 'bigint':
- return 'float';
- default:
- return 'string';
- }
- }
-
-}
-
diff --git a/framework/Data/Common/IbmDb2/TIbmMetaData.php b/framework/Data/Common/IbmDb2/TIbmMetaData.php
deleted file mode 100644
index 496bb12f..00000000
--- a/framework/Data/Common/IbmDb2/TIbmMetaData.php
+++ /dev/null
@@ -1,112 +0,0 @@
-<?php
-
-/**
- * TIbmMetaData class file.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id: TIbmMetaData.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- */
-Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataCommon');
-
-/**
- * TIbmMetaData class.
- *
- * Column details for IBM DB2 database. Using php_pdo_ibm.dll extension.
- *
- * Does not support LIMIT and OFFSET criterias.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @version $Id: TIbmMetaData.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- * @since 3.1
- */
-class TIbmMetaData extends TDbMetaDataCommon
-{
- /**
- * Build the SQL search string from the criteria object for IBM DB2 database.
- * @param TDbConnection database connection.
- * @param TActiveRecordCriteria search criteria.
- * @return string SQL search.
- */
- protected function getSqlFromCriteria($conn, $criteria)
- {
- if($criteria===null) return '';
- $sql = '';
- if(($condition = $criteria->getCondition())!==null)
- $sql .= ' WHERE '.$condition;
- $orders=array();
- foreach($criteria->getOrdersBy() as $by=>$ordering)
- $orders[] = $this->getOrdering($by, $ordering);
- if(count($orders) > 0)
- $sql .= ' ORDER BY '.implode(', ', $orders);
- //if(($limit = $criteria->getLimit())!==null)
- //{
- // $sql .= ' FETCH FIRST '.intval($limit).' ROWS ONLY';
- //}
- return strlen($sql) > 0 ? $sql : '';
- }
-
- /**
- * Lowercase the data keys, IBM DB2 returns uppercase column names
- * @param mixed record row
- * @return array record row
- */
- public function postQueryRow($row)
- {
- if(!is_array($row)) return $row;
- $result=array();
- foreach($row as $k=>$v)
- $result[strtolower($k)]=$v;
- return $result;
- }
-
- /**
- * Lowercase the data keys, IBM DB2 returns uppercase column names
- * @param mixed record row
- * @return array record row
- */
- public function postQuery($rows)
- {
- $data = array();
- foreach($rows as $k=>$v)
- $data[$k] = $this->postQueryRow($v);
- return $data;
- }
-
- public function getSearchRegExpCriteria($fields, $keywords)
- {
- if(strlen(trim($keywords)) == 0) return '';
- $words = array();
- preg_match_all('/([a-zA-Z0-9-+]+)/', $keywords, $words);
- $result = array();
- foreach($fields as $field)
- {
- $column = $this->getColumn($field);
- if($this->isSearchableColumn($column))
- $result[] = $this->getLikeCriteriaStr($column->getName(), $words[0]);
- }
- $a = '('.implode(' OR ', $result).')';
- error_log($a);
- return '('.implode(' OR ', $result).')';
- }
-
- protected function isSearchableColumn($column)
- {
- $type = strtolower($column->getType());
- return $type === 'char' || $type === 'varchar';
- }
-
- protected function getLikeCriteriaStr($column, $words)
- {
- $result=array();
- foreach($words as $word)
- $result[] = "{$column} LIKE '%{$word}%'";
- return '('.implode(' AND ', $result).')';
- }
-
-
-}
diff --git a/framework/Data/Common/IbmDb2/TIbmMetaDataInspector.php b/framework/Data/Common/IbmDb2/TIbmMetaDataInspector.php
deleted file mode 100644
index a37fad6e..00000000
--- a/framework/Data/Common/IbmDb2/TIbmMetaDataInspector.php
+++ /dev/null
@@ -1,112 +0,0 @@
-<?php
-/**
- * TIbmMetaDataInspector class file.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id: TIbmMetaDataInspector.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- */
-Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataInspector');
-Prado::using('System.Data.ActiveRecord.Vendor.TIbmColumnMetaData');
-Prado::using('System.Data.ActiveRecord.Vendor.TIbmMetaData');
-
-/**
- * TIbmMetaDataInspector class.
- *
- * Column details for IBM DB2 database. Using php_pdo_ibm.dll extension.
- *
- * @author Cesar Ramos <cramos[at]gmail[dot]com>
- * @version $Id: TIbmMetaDataInspector.php 1807 2007-03-31 06:42:15Z wei $
- * @package System.Data.ActiveRecord.Vendor
- * @since 3.1
- */
-class TIbmMetaDataInspector extends TDbMetaDataInspector
-{
- private $_schema;
-
- /**
- * @param string default schema.
- */
- public function setSchema($schema)
- {
- $this->_schema=$schema;
- }
-
- /**
- * @return string default schema.
- */
- public function getSchema()
- {
- return $this->_schema;
- }
- /**
- * Get the column definitions for given table.
- * @param string table name.
- * @return array column name value pairs of column meta data.
- */
- protected function getColumnDefinitions($table)
- {
- if(count($parts= explode('.', $table)) > 1)
- {
- $tablename = $parts[1];
- $schema = $parts[0];
- }
- else
- {
- $tablename = $parts[0];
- $schema = $this->getSchema();
- }
- $sql="SELECT * FROM SYSCAT.COLUMNS WHERE TABNAME='".strtoupper($tablename)."'";
- if ($schema)
- $sql=$sql." AND TABSCHEMA='".strtoupper($schema)."'";
-
- $conn = $this->getDbConnection();
- $conn->setActive(true);
- $command = $conn->createCommand($sql);
- $command->prepare();
- $result=$command->query($sql);
- foreach ($result as $col)
- $cols[strtolower($col['COLNAME'])] = $this->getColumnMetaData($col);
- return $cols;
- }
-
- protected function getColumnMetaData($col)
- {
- $name = strtolower($col['COLNAME']);
- $type = $col['TYPENAME'];
- $length = $col['LENGTH'];
- $notNull = $col['NULLS']==='N'?1:0;
- $autoIncrement=$col['IDENTITY']==='Y'?1:0;
- $default = $col['DEFAULT'];
- $primaryKey = $col['KEYSEQ']?1:0;
- return new TIbmColumnMetaData($name,$type,$length,$notNull,$autoIncrement,$default,$primaryKey);
- }
-
- /**
- * Not implemented, IBM does not always have foreign key constraints.
- */
- protected function getConstraintKeys($table)
- {
- return array('primary'=>array(), 'foreign'=>array());
- }
-
- /**
- * Create a new instance of meta data.
- * @param string table name
- * @param array column meta data
- * @param array primary key meta data
- * @param array foreign key meta data.
- * @return TDbMetaData table meta data.
- */
- protected function createMetaData($table, $columns, $primary, $foreign)
- {
- $pks = array();
- foreach($columns as $name=>$column)
- if($column->getIsPrimaryKey())
- $pks[] = $name;
- return new TIbmMetaData($table,$columns,$pks);
- }
-}
diff --git a/framework/Data/Common/Mysql/TMysqlMetaData.php b/framework/Data/Common/Mysql/TMysqlMetaData.php
index 75f7a7cf..ae552cf3 100644
--- a/framework/Data/Common/Mysql/TMysqlMetaData.php
+++ b/framework/Data/Common/Mysql/TMysqlMetaData.php
@@ -310,7 +310,7 @@ EOD;
$sql = "SHOW CREATE TABLE `{$tableName}`";
$command = $this->getDbConnection()->createCommand($sql);
$result = $command->queryRow();
- return $result['Create Table'];
+ return isset($result['Create Table']) ? $result['Create Table'] : (isset($result['Create View']) ? $result['Create View'] : '');
}
/**
diff --git a/framework/Data/Common/Oracle/TOracleCommandBuilder.php b/framework/Data/Common/Oracle/TOracleCommandBuilder.php
index 8c60751a..a22c0f08 100644
--- a/framework/Data/Common/Oracle/TOracleCommandBuilder.php
+++ b/framework/Data/Common/Oracle/TOracleCommandBuilder.php
@@ -86,20 +86,14 @@ class TOracleCommandBuilder extends TDbCommandBuilder {
$niniDoSelect = strpos($sql, 'SELECT') + 6;
$nfimDoSelect = (strpos($sql, 'FROM') !== false ? strpos($sql, 'FROM') : $nfimDaSQL);
- $niniDoWhere = strpos($sql, 'WHERE') + 5;
-
- $WhereConstraint = substr($sql, $niniDoWhere, $nfimDoWhere - $niniDoWhere);
-
- $WhereInSubSelect = "";
- if (trim($WhereConstraint) !== "") {
- $WhereInSubSelect = "WHERE " . $WhereConstraint;
- }
+ $WhereInSubSelect="";
+ if(strpos($sql, 'WHERE')!==false)
+ $WhereInSubSelect = "WHERE " .substr($sql, strpos($sql, 'WHERE')+5, $nfimDoWhere - $niniDoWhere);
$sORDERBY = '';
if (stripos($sql, 'ORDER') !== false) {
$p = stripos($sql, 'ORDER');
- $sORDERBY = substr($sql, $p +8, 10000);
-
+ $sORDERBY = substr($sql, $p +8);
}
$fields = substr($sql, 0, $nfimDoSelect);
@@ -142,23 +136,18 @@ class TOracleCommandBuilder extends TDbCommandBuilder {
") WHERE {$pradoNUMLIN} >= {$offset} ";
************************* */
- $toReg = $offset + $limit -1;
+ $offset=(int)$offset;
+ $toReg = $offset + $limit ;
$fullTableName = $this->getTableInfo()->getTableFullName();
- if (empty ($sORDERBY)) {
- $newSql = " SELECT $fields FROM " .
- "( " .
- " SELECT ROW_NUMBER() OVER ( ORDER BY ROWNUM ) as {$pradoNUMLIN} {$aliasedFields} " .
- " FROM {$fullTableName} {$fieldsALIAS}" .
- ") nn " .
- " WHERE nn.{$pradoNUMLIN} >= {$offset} AND nn.{$pradoNUMLIN} <= {$toReg} ";
- } else {
- $newSql = " SELECT $fields FROM " .
- "( " .
- " SELECT ROW_NUMBER() OVER ( ORDER BY {$sORDERBY} ) as {$pradoNUMLIN} {$aliasedFields} " .
- " FROM {$fullTableName} {$fieldsALIAS} $WhereInSubSelect" .
- ") nn " .
- " WHERE nn.{$pradoNUMLIN} >= {$offset} AND nn.{$pradoNUMLIN} <= {$toReg} ";
- }
+ if (empty ($sORDERBY))
+ $sORDERBY="ROWNUM";
+
+ $newSql = " SELECT $fields FROM " .
+ "( " .
+ " SELECT ROW_NUMBER() OVER ( ORDER BY {$sORDERBY} ) -1 as {$pradoNUMLIN} {$aliasedFields} " .
+ " FROM {$fullTableName} {$fieldsALIAS} $WhereInSubSelect" .
+ ") nn " .
+ " WHERE nn.{$pradoNUMLIN} >= {$offset} AND nn.{$pradoNUMLIN} < {$toReg} ";
//echo $newSql."\n<br>\n";
return $newSql;
}
diff --git a/framework/Data/Common/Oracle/TOracleMetaData.php b/framework/Data/Common/Oracle/TOracleMetaData.php
index bb8e9b7b..5fdf1d5c 100644
--- a/framework/Data/Common/Oracle/TOracleMetaData.php
+++ b/framework/Data/Common/Oracle/TOracleMetaData.php
@@ -10,7 +10,10 @@
* @package System.Data.Common.Oracle
*/
-
+/**
+ * Load the base TDbMetaData class.
+ */
+Prado::using('System.Data.Common.TDbMetaData');
Prado::using('System.Data.Common.Oracle.TOracleTableInfo');
Prado::using('System.Data.Common.Oracle.TOracleTableColumn');
@@ -22,28 +25,11 @@ Prado::using('System.Data.Common.Oracle.TOracleTableColumn');
* @package System.Data.Common.Oracle
* @since 3.1
*/
-class TOracleMetaData extends TComponent
+class TOracleMetaData extends TDbMetaData
{
- private $_tableInfoCache=array();
- private $_connection;
private $_defaultSchema = 'system';
- /**
- * @param TDbConnection database connection.
- */
- public function __construct($conn)
- {
- $this->_connection=$conn;
- }
-
- /**
- * @return TDbConnection database connection.
- */
- public function getDbConnection()
- {
- return $this->_connection;
- }
-
+
/**
* @return string TDbTableInfo class name.
*/
@@ -69,23 +55,6 @@ class TOracleMetaData extends TComponent
}
/**
- * Obtains table meta data information for the current connection and given table name.
- * @param string table or view name
- * @return TDbTableInfo table information.
- */
- public function getTableInfo($tableName=null)
- {
- $key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
- if(!isset($this->_tableInfoCache[$key]))
- {
- $class = $this->getTableInfoClass();
- $tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
- $this->_tableInfoCache[$key] = $tableInfo;
- }
- return $this->_tableInfoCache[$key];
- }
-
- /**
* @param string table name with optional schema name prefix, uses default schema name prefix is not provided.
* @return array tuple as ($schemaName,$tableName)
*/
diff --git a/framework/Data/Common/Pgsql/TPgsqlMetaData.php b/framework/Data/Common/Pgsql/TPgsqlMetaData.php
index a2243531..3ab872a5 100644
--- a/framework/Data/Common/Pgsql/TPgsqlMetaData.php
+++ b/framework/Data/Common/Pgsql/TPgsqlMetaData.php
@@ -330,7 +330,7 @@ EOD;
*/
protected function getPrimaryKeys($tableName, $schemaName, $columnIndex)
{
- $index = join(', ', split(' ', $columnIndex));
+ $index = join(', ', explode(' ', $columnIndex));
$sql =
<<<EOD
SELECT attnum, attname FROM pg_catalog.pg_attribute WHERE
@@ -344,7 +344,7 @@ EOD;
$command = $this->getDbConnection()->createCommand($sql);
$command->bindValue(':table', $tableName);
$command->bindValue(':schema', $schemaName);
-// $command->bindValue(':columnIndex', join(', ', split(' ', $columnIndex)));
+// $command->bindValue(':columnIndex', join(', ', explode(' ', $columnIndex)));
$primary = array();
foreach($command->query() as $row)
{
diff --git a/framework/Data/Common/Pgsql/TPgsqlTableInfo.php b/framework/Data/Common/Pgsql/TPgsqlTableInfo.php
index 2447c141..1a634c19 100644
--- a/framework/Data/Common/Pgsql/TPgsqlTableInfo.php
+++ b/framework/Data/Common/Pgsql/TPgsqlTableInfo.php
@@ -42,7 +42,7 @@ class TPgsqlTableInfo extends TDbTableInfo
if(($schema=$this->getSchemaName())!==null)
return $schema.'.'.$this->getTableName();
else
- $this->getTableName();
+ return $this->getTableName();
}
/**