summaryrefslogtreecommitdiff
path: root/framework/Data
diff options
context:
space:
mode:
authorwei <>2007-01-17 08:01:40 +0000
committerwei <>2007-01-17 08:01:40 +0000
commit3dc598bc7c2604e24b9e0be1189d9d78b43737ea (patch)
treefa3bc532607b6c70af3737fec80ebfed62debc5a /framework/Data
parentd919dfa1674ddd5226834b81728e1772917d6240 (diff)
Add active record generator.
Diffstat (limited to 'framework/Data')
-rw-r--r--framework/Data/ActiveRecord/TActiveRecord.php20
-rw-r--r--framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php4
-rw-r--r--framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php15
-rw-r--r--framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php3
-rw-r--r--framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php15
-rw-r--r--framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php3
-rw-r--r--framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php15
-rw-r--r--framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php3
-rw-r--r--framework/Data/TDbCommand.php4
9 files changed, 73 insertions, 9 deletions
diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php
index d3e25dcf..9272e1b9 100644
--- a/framework/Data/ActiveRecord/TActiveRecord.php
+++ b/framework/Data/ActiveRecord/TActiveRecord.php
@@ -97,13 +97,27 @@ abstract class TActiveRecord extends TComponent
*/
public function __construct($data=array(), $connection=null)
{
- foreach($data as $name=>$value)
- $this->$name = $value;
+ $this->copyFrom($data);
if($connection!==null)
$this->_connection=$connection;
}
/**
+ * Copies data from an array or another object.
+ * @throws TActiveRecordException if data is not array or not object.
+ * @return TActiveRecord current instance.
+ */
+ public function copyFrom($data)
+ {
+ $data = is_object($data) ? get_object_vars($data) : $data;
+ if(!is_array($data))
+ throw new TActiveRecordException('ar_must_copy_from_array_or_object', get_class($this));
+ foreach($data as $name=>$value)
+ $this->$name = $value;
+ return $this;
+ }
+
+ /**
* Gets the current Db connection, the connection object is obtained from
* the TActiveRecordManager if connection is currently null.
* @return TDbConnection current db connection for this object.
@@ -212,7 +226,7 @@ abstract class TActiveRecord extends TComponent
/**
- * Delete multiple records using a criteria.
+ * Delete multiple records using a criteria.
* @param string|TActiveRecordCriteria SQL condition or criteria object.
* @param mixed parameter values.
* @return int number of records deleted.
diff --git a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php
index 7f7dad8b..6e157785 100644
--- a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php
+++ b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php
@@ -67,7 +67,7 @@ abstract class TDbMetaDataCommon extends TDbMetaData
public function getFindByCriteriaCommand($conn, $criteria=null)
{
$columns = $this->getSelectionColumns();
- $conditions = $criteria!==null?$this->getSqlFromCriteria($conn,$criteria) : '';
+ $conditions = $this->getSqlFromCriteria($conn,$criteria);
$table = $this->getTableName();
$sql = "SELECT {$columns} FROM {$table} {$conditions}";
return $this->createCriteriaBindedCommand($conn,$sql, $criteria);
@@ -88,7 +88,7 @@ abstract class TDbMetaDataCommon extends TDbMetaData
return $this->createCriteriaBindedCommand($conn,$sql, $criteria);
}
- abstract protected function getSqlFromCriteria($conn,TActiveRecordCriteria $criteria);
+ abstract protected function getSqlFromCriteria($conn, $criteria);
/**
* Sql command with parameters binded.
diff --git a/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php
index b5bd4050..e7e7ceb8 100644
--- a/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TMysqlColumnMetaData.php
@@ -56,6 +56,21 @@ class TMysqlColumnMetaData extends TComponent
return $this->_name;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'tinyint': case 'smallint': case 'mediumint': case 'int': case 'year':
+ return 'integer';
+ case 'bool':
+ return 'boolean';
+ case 'bigint': case 'float': case 'double': case 'decimal':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return boolean true if column is a sequence, false otherwise.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php b/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php
index b0b2ef87..0cbd0b7c 100644
--- a/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TMysqlMetaData.php
@@ -28,8 +28,9 @@ class TMysqlMetaData extends TDbMetaDataCommon
* @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php
index d6ea8ca7..59a2d43e 100644
--- a/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TPgsqlColumnMetaData.php
@@ -57,6 +57,21 @@ class TPgsqlColumnMetaData extends TComponent
return $this->_name;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'bit': case 'bit varying': case 'real': case 'serial':
+ return 'integer';
+ case 'boolean':
+ return 'boolean';
+ case 'bigint': case 'bigserial': case 'double precision': case 'money': case 'numeric':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return boolean true if column is a sequence, false otherwise.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php b/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php
index ffb2fc31..d968267a 100644
--- a/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TPgsqlMetaData.php
@@ -30,8 +30,9 @@ class TPgsqlMetaData extends TDbMetaDataCommon
* @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php b/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php
index 366bfce2..abb50caf 100644
--- a/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TSqliteColumnMetaData.php
@@ -39,6 +39,21 @@ class TSqliteColumnMetaData extends TComponent
$this->_primary=$primary;
}
+ public function getPHPType()
+ {
+ switch(strtolower($this->_type))
+ {
+ case 'int': case 'integer': case 'mediumint': case 'smallint': case 'tinyint': case 'year':
+ return 'integer';
+ case 'boolean':
+ return 'boolean';
+ case 'decimal': case 'double': case 'float': case 'bigint':
+ return 'float';
+ default:
+ return 'string';
+ }
+ }
+
/**
* @return string quoted column name.
*/
diff --git a/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php b/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php
index 129a0acf..c82a99ad 100644
--- a/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php
+++ b/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php
@@ -28,8 +28,9 @@ class TSqliteMetaData extends TDbMetaDataCommon
* @param TActiveRecordCriteria search criteria.
* @return string SQL search.
*/
- protected function getSqlFromCriteria($conn, TActiveRecordCriteria $criteria)
+ protected function getSqlFromCriteria($conn, $criteria)
{
+ if($criteria===null) return '';
$sql = '';
if(($condition = $criteria->getCondition())!==null)
$sql .= ' WHERE '.$condition;
diff --git a/framework/Data/TDbCommand.php b/framework/Data/TDbCommand.php
index 054aa653..5c824b8f 100644
--- a/framework/Data/TDbCommand.php
+++ b/framework/Data/TDbCommand.php
@@ -200,7 +200,9 @@ class TDbCommand extends TComponent
protected function getDebugStatementText()
{
if(Prado::getApplication()->getMode() === TApplicationMode::Debug)
- return $this->_statement instanceof PDOStatement ? $this->getConnection()->getPdoInstance().$this->_statement->queryString : $this->getText();
+ return $this->_statement instanceof PDOStatement ?
+ $this->_statement->queryString
+ : $this->getText();
}
/**