summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php
diff options
context:
space:
mode:
authorwei <>2006-11-26 22:15:58 +0000
committerwei <>2006-11-26 22:15:58 +0000
commita8b3ebe8f62c3888b216d827c1c5dcba8a47d4e1 (patch)
tree5eef79dbc5e2f506047fa463cb427a40a7bd8441 /framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php
parent6773dfe453682d2b39a26fbabef8e706bf6bb412 (diff)
Adding active record implementation.
Diffstat (limited to 'framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php')
-rw-r--r--framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php80
1 files changed, 80 insertions, 0 deletions
diff --git a/framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php b/framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php
new file mode 100644
index 00000000..6075d2bc
--- /dev/null
+++ b/framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * TMysqlMetaDataInspector class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.ActiveRecord.Vendor
+ */
+
+Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataInspector');
+Prado::using('System.Data.ActiveRecord.Vendor.TMysqlColumnMetaData');
+Prado::using('System.Data.ActiveRecord.Vendor.TMysqlMetaData');
+
+/**
+ * TMysqlMetaDataInspector class.
+ *
+ * Gathers table column properties for Mysql database.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.ActiveRecord.Vendor
+ * @since 3.1
+ */
+class TMysqlMetaDataInspector extends TDbMetaDataInspector
+{
+ /**
+ * 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)
+ {
+ $sql="SHOW FULL FIELDS FROM `{$table}`";
+ $conn = $this->getDbConnection();
+ $conn->setActive(true);
+ $command = $conn->createCommand($sql);
+ $command->prepare();
+ foreach($command->query() as $col)
+ $cols[$col['Field']] = $this->getColumnMetaData($col);
+ return $cols;
+ }
+
+ protected function getColumnMetaData($col)
+ {
+ $name = '`'.$col['Field'].'`'; //quote the column names!
+ $type = $col['Type'];
+ $notNull = $col['Null']==='NO';
+ $autoIncrement=is_int(strpos(strtolower($col['Extra']), 'auto_increment'));
+ $default = $col['Default'];
+ $primaryKey = $col['Key']==='PRI';
+ return new TMysqlColumnMetaData($name,$type,$notNull,$autoIncrement,$default,$primaryKey);
+ }
+
+ /**
+ * Not implemented, Mysql 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 TMysqlMetaData($table,$columns,$pks);
+ }
+}
+
+?> \ No newline at end of file