summaryrefslogtreecommitdiff
path: root/framework/DataAccess
diff options
context:
space:
mode:
Diffstat (limited to 'framework/DataAccess')
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php21
-rw-r--r--framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php30
-rw-r--r--framework/DataAccess/TActiveRecord.php51
-rw-r--r--framework/DataAccess/TAdodb.php5
-rw-r--r--framework/DataAccess/TEzpdo.php251
5 files changed, 348 insertions, 10 deletions
diff --git a/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php b/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php
index 9a44d1ec..ee8f1744 100644
--- a/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php
+++ b/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php
@@ -111,6 +111,7 @@ class TConfigDeserialize
$property->initialize($sqlMap, $resultMap);
$resultMap->addResultProperty($property);
}
+
$discriminator = null;
if(isset($node->discriminator))
{
@@ -118,18 +119,24 @@ class TConfigDeserialize
$this->loadConfiguration($discriminator, $node->discriminator, $file);
$discriminator->initMapping($sqlMap, $resultMap);
}
+
+
+
foreach($node->subMap as $subMapNode)
{
- if(is_null($discriminator))
- throw new TSqlMapConfigurationException(
- 'sqlmap_undefined_discriminator', $resultMap->getID(), $file);
- $subMap = new TSubMap;
- $this->loadConfiguration($subMap, $subMapNode, $file);
- $discriminator->add($subMap);
+ if(isset($subMapNode['value']))
+ {
+ if(is_null($discriminator))
+ throw new TSqlMapConfigurationException(
+ 'sqlmap_undefined_discriminator', $resultMap->getID(), $file);
+
+ $subMap = new TSubMap;
+ $this->loadConfiguration($subMap, $subMapNode, $file);
+ $discriminator->add($subMap);
+ }
}
if(!is_null($discriminator))
$resultMap->setDiscriminator($discriminator);
-
return $resultMap;
}
diff --git a/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php
index 8680601e..4bbe2cb5 100644
--- a/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php
+++ b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php
@@ -39,7 +39,7 @@ class TPropertyAccess
*/
public static function get($object,$path)
{
- if(!is_array($object) && !is_object($object))
+ if(!is_array($object) || !is_object($object))
return $object;
$properties = explode('.', $path);
foreach($properties as $prop)
@@ -67,6 +67,34 @@ class TPropertyAccess
return $object;
}
+ public static function has($object, $path)
+ {
+ if(!is_array($object) || !is_object($object))
+ return false;
+ $properties = explode('.', $path);
+ foreach($properties as $prop)
+ {
+ if(is_array($object) || $object instanceof ArrayAccess)
+ {
+ if(isset($object[$prop]))
+ $object = $object[$prop];
+ else
+ return false;
+ }
+ else if(is_object($object))
+ {
+ $getter = 'get'.$prop;
+ if(is_callable(array($object,$getter)))
+ $object = $object->{$getter}();
+ else if(in_array($prop, array_keys(get_object_vars($object))))
+ $object = $object->{$prop};
+ return false;
+ }
+ else
+ return false;
+ }
+ return true;
+ }
public static function set($object, $path, $value)
{
diff --git a/framework/DataAccess/TActiveRecord.php b/framework/DataAccess/TActiveRecord.php
new file mode 100644
index 00000000..0c33fde3
--- /dev/null
+++ b/framework/DataAccess/TActiveRecord.php
@@ -0,0 +1,51 @@
+<?php
+
+Prado::using('System.3rdParty.adodb.ADOdb_Active_Record');
+
+/**
+ * Adodb's active record implementation is very basic. Example: A table
+ * named "persons"
+ * <code>
+ * CREATE TABLE persons
+ * (
+ * id INTEGER PRIMARY KEY,
+ * first_name TEXT NOT NULL,
+ * last_name TEXT NOT NULL,
+ * favorite_color TEXT NOT NULL
+ * );
+ * </code>
+ * Create a class called <tt>Person</tt>, connect insert some data as follows.
+ * <code>
+ * class Person extends TActiveRecord { }
+ *
+ * $person = new Person();
+ * $person->first_name = 'Andi';
+ * $person->last_name = 'Gutmans';
+ * $person->favorite_color = 'blue';
+ * $person->save(); // this save will perform an INSERT successfully
+ *
+ * $person = new Person();
+ * $person->first_name = 'John';
+ * $person->last_name = 'Lim';
+ * $person->favorite_color = 'lavender';
+ * $person->save(); // this save will perform an INSERT successfully
+ *
+ * // load record where id=2 into a new ADOdb_Active_Record
+ * $person2 = new Person();
+ * $person2->Load('id=2');
+ * var_dump($person2);
+ * </code>
+ *
+ *
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @version $Revision: $ $Date: $
+ * @package System.DataAccess
+ * @since 3.0
+ */
+class TActiveRecord extends ADOdb_Active_Record
+{
+
+}
+
+?> \ No newline at end of file
diff --git a/framework/DataAccess/TAdodb.php b/framework/DataAccess/TAdodb.php
index c7005c76..cd188e49 100644
--- a/framework/DataAccess/TAdodb.php
+++ b/framework/DataAccess/TAdodb.php
@@ -143,6 +143,7 @@ class TAdodb extends TDatabaseProvider
*/
public function getConnection()
{
+ $this->init(null);
return $this->_connection;
}
@@ -303,9 +304,10 @@ class TAdodbConnection extends TDbConnection
*/
public function __construct($provider=null)
{
- parent::__construct($provider);
if(is_string($provider))
$this->initProvider($provider);
+ else
+ parent::__construct($provider);
}
/**
@@ -329,7 +331,6 @@ class TAdodbConnection extends TDbConnection
//close any open connections before serializing.
$this->close();
$this->_connection = null;
- return array_keys(get_object_vars($this));
}
/**
diff --git a/framework/DataAccess/TEzpdo.php b/framework/DataAccess/TEzpdo.php
new file mode 100644
index 00000000..de9d53fe
--- /dev/null
+++ b/framework/DataAccess/TEzpdo.php
@@ -0,0 +1,251 @@
+<?php
+
+class TEzpdo extends TDatabaseProvider
+{
+ /**
+ * @var array list of default ezpdo options.
+ */
+ private $_options = array(
+ 'source_dirs' => null,
+ 'recursive' => true,
+ 'compiled_dir' => null, // default to compiled under current dir
+ 'compiled_file' => 'compiled_file', // the default class map file
+ 'backup_compiled' => true, // whether to backup old compiled file
+ 'check_table_exists' => true, // whether always check if table exists before db operation
+ 'table_prefix' => '', // table prefix (default to none)
+ 'relation_table' => '_ez_relation_', // the table name for object relations
+ 'split_relation_table' => true, // whether to split relation table
+ 'auto_flush' => false, // enable or disable auto flush at the end of script
+ 'flush_before_find' => true, // enable or disable auto flush before find()
+ 'auto_compile' => true, // enable or disable auto compile
+ 'autoload' => false, // enable or disable class autoloading
+ 'log_queries' => false, // enable logging queries (for debug only)
+ 'dispatch_events' => true, // whether to dispatch events (true by default)
+ 'default_oid_column' => 'eoid', // oid column name is default to 'eoid' now
+ );
+
+ /**
+ * @var array List of source directories.
+ */
+ private $_sources = array();
+
+ /**
+ * @var epManager ezpdo manager instance.
+ */
+ private $_manager;
+
+ /**
+ * Initialize the ezpdo module, sets the default compile directory to use
+ * the Prado runtime directory.
+ */
+ public function init($config)
+ {
+ parent::init($config);
+ include($this->getEzpdoLibrary().'/ezpdo.php');
+ $path = $this->getApplication()->getRuntimePath().'/ezpdo';
+ $this->_options['compiled_dir'] = $path;
+ if($this->getApplication()->getMode() != TApplication::STATE_PERFORMANCE)
+ {
+ if(!is_dir($path))
+ throw new TConfigurationException('ezpdo_compile_dir_not_found', $path);
+ $this->_options['auto_compile'] = false;
+ }
+ }
+
+ /**
+ * @return string ezpdo library directory.
+ */
+ protected function getEzpdoLibrary()
+ {
+ return Prado::getPathOfNamespace('System.3rdParty.ezpdo');
+ }
+
+ /**
+ * @return array merged database connection options with the other options.
+ */
+ protected function getOptions()
+ {
+ if(strlen($dsn = $this->getConnectionString()) > 0)
+ $options['default_dsn'] = $dsn;
+ else
+ $options['default_dsn'] = $this->buildDsn();
+
+ return array_merge($this->_options, $options);
+ }
+
+ /**
+ * Initialize the ezManager once.
+ */
+ protected function initialize()
+ {
+ Prado::using('System.3rdParty.ezpdo.src.base.epConfig');
+ include($this->getEzpdoLibrary().'/src/runtime/epManager.php');
+
+ if(is_null($this->_manager))
+ {
+ $this->_manager = new epManager;
+ foreach($this->_sources as $source)
+ Prado::using($source.'.*');
+ $this->_manager->setConfig(new epConfig($this->getOptions()));
+ }
+ }
+
+ /**
+ * @return epManager the ezpdo manager for this module.
+ */
+ public function getConnection()
+ {
+ $this->initialize();
+ return $this->_manager;
+ }
+
+ /**
+ * @param string The intput directory, using dot path aliases, that contains
+ * class source files to be compiled. Use commma for multiple directories
+ */
+ public function setSourceDirs($values)
+ {
+ $paths = array();
+ foreach(explode(',', $values) as $value)
+ {
+ $dot = Prado::getPathOfNamespace($value);
+ $this->_sources[] = $value;
+ if(($path = realpath($dot)) !== false)
+ $paths[] = $path;
+ }
+ $this->_options['source_dirs'] = implode(',', $paths);
+ }
+
+ /**
+ * @return string comma delimited list of source directories.
+ */
+ public function getSourceDirs()
+ {
+ return $this->_options['source_dir'];
+ }
+
+ /**
+ * @param boolean Whether to compile subdirs recursively, default is true.
+ */
+ public function setRecursive($value)
+ {
+ $this->_options['recursive'] = TPropertyValue::ensureBoolean($value);
+ }
+
+ /**
+ * @return boolean true will compile subdirectories recursively.
+ */
+ public function getRecursive()
+ {
+ return $this->_options['recursive'];
+ }
+
+ /**
+ * @param string database table prefix.
+ */
+ public function setTablePrefix($value)
+ {
+ $this->_options['table_prefix'] = $value;
+ }
+
+ /**
+ * @param string the table name for object relations, default is
+ * '_ez_relation_'
+ */
+ public function setRelationTableName($value)
+ {
+ $this->_options['relation_table'] = $value;
+ }
+
+ /**
+ * @return string the table name for object relations.
+ */
+ public function getRelationTableName()
+ {
+ return $this->_options['relation_table'];
+ }
+
+ /**
+ * @param boolean whether to split relation table, default is true.
+ */
+ public function setSplitRelationTable($value)
+ {
+ $this->_options['split_relation_table'] = TPropertyValue::ensureBoolean($value);
+ }
+
+ /**
+ * @string boolean true will split relation table.
+ */
+ public function getSplitRelationTable()
+ {
+ return $this->_options['split_relation_table'];
+ }
+
+ /**
+ * @param boolean enable or disable auto flush at the end of script, default
+ * is false.
+ */
+ public function setAutoFlush($value)
+ {
+ $this->_options['auto_flush'] = TPropertyValue::ensureBoolean($value);
+ }
+
+ /**
+ * @return boolean whether to auto flush at the end of script.
+ */
+ public function getAutoFlush()
+ {
+ return $this->_options['auto_flush'];
+ }
+
+ /**
+ * @param boolean enable or disable auto flush before find(), default is
+ * true.
+ */
+ public function setFlushBeforeFind($value)
+ {
+ $this->_options['flush_before_find'] = TPropertyValue::ensureBoolean($value);
+ }
+
+ /**
+ * @return boolean whether to auto flush before find()
+ */
+ public function getFlushBeforeFind()
+ {
+ return $this->_options['flush_before_find'];
+ }
+
+ /**
+ * @param boolean enable or disable auto compile, default is true.
+ */
+ public function setAutoCompile($value)
+ {
+ $this->_options['auto_compile'] = TPropertyValue::ensureBoolean($value);
+ }
+
+ /**
+ * @return boolean whether to auto compile class files.
+ */
+ public function getAutoCompile()
+ {
+ return $this->_options['auto_compile'];
+ }
+
+ /**
+ * @param string default oid column name, default is 'eoid'.
+ */
+ public function setDefaultOidColumn($value)
+ {
+ $this->_options['default_oid_column'] = $value;
+ }
+
+ /**
+ * @return string default oid column name.
+ */
+ public function getDefaultOidColumn($value)
+ {
+ return $this->_options['default_oid_column'];
+ }
+}
+
+?> \ No newline at end of file