summaryrefslogtreecommitdiff
path: root/framework/DataAccess
diff options
context:
space:
mode:
Diffstat (limited to 'framework/DataAccess')
-rw-r--r--framework/DataAccess/SQLMap/TSqlMapper.php2
-rw-r--r--framework/DataAccess/TAdodb.php (renamed from framework/DataAccess/TAdodbProvider.php)99
-rw-r--r--framework/DataAccess/TCreoleProvider.php83
-rw-r--r--framework/DataAccess/TDatabaseProvider.php40
4 files changed, 75 insertions, 149 deletions
diff --git a/framework/DataAccess/SQLMap/TSqlMapper.php b/framework/DataAccess/SQLMap/TSqlMapper.php
index 26e6c115..2c760c1e 100644
--- a/framework/DataAccess/SQLMap/TSqlMapper.php
+++ b/framework/DataAccess/SQLMap/TSqlMapper.php
@@ -7,7 +7,7 @@ Prado::using('System.Collections.*');
Prado::using('System.DataAccess.SQLMap.DataMapper.TTypeHandlerFactory');
Prado::using('System.DataAccess.SQLMap.DataMapper.TSqlMapCache');
Prado::using('System.DataAccess.SQLMap.DataMapper.TDataMapperException');
-Prado::using('System.DataAccess.TAdodbProvider');
+Prado::using('System.DataAccess.TAdodb');
/**
* DataMapper client, a facade to provide access the rest of the DataMapper
diff --git a/framework/DataAccess/TAdodbProvider.php b/framework/DataAccess/TAdodb.php
index 9d9453f9..f6b486e4 100644
--- a/framework/DataAccess/TAdodbProvider.php
+++ b/framework/DataAccess/TAdodb.php
@@ -7,9 +7,9 @@ Prado::using('System.DataAccess.TDatabaseProvider');
*
* Usage:
* <code>
- * $provider = new TAdodbProvider;
- * $provider->setConnectionString($dsn);
- * $connection = $provider->getConnection();
+ * $provider = new TAdodb;
+ * $provider->setConnectionString($dsn); $
+ * connection = $provider->getConnection();
* $resultSet = $connection->execute('....');
* </code>
*
@@ -18,7 +18,7 @@ Prado::using('System.DataAccess.TDatabaseProvider');
* @package System.DataAccess
* @since 3.0
*/
-class TAdodbProvider extends TDatabaseProvider
+class TAdodb extends TDatabaseProvider
{
const FETCH_ASSOCIATIVE='associative';
const FETCH_NUMERIC='numeric';
@@ -29,52 +29,51 @@ class TAdodbProvider extends TDatabaseProvider
private $_cachedir='';
private $_fetchMode = 'associative';
- private static $_hasImported=false;
-
- private $_adodbLibrary='';
-
public function getConnection()
{
- if(is_null($this->_connection) || is_null($this->_connection->getProvider()))
- {
- $this->importAdodbLibrary();
- $this->_connection = new TAdodbConnection($this);
- }
+ $this->initialize();
return $this->_connection;
}
- /**
- * @return string the cache directory for adodb module
- */
- public function getCacheDir()
+ public function initialize()
{
- return $this->_cachedir;
+ if(!class_exists('ADOConnection', false))
+ $this->importAdodbLibrary();
+ if(is_null($this->_connection))
+ $this->_connection = new TAdodbConnection($this);
}
- public function getAdodbLibrary()
+ public function enableActiveRecords()
{
- if(strlen($this->_adodbLibrary) < 1)
- return dirname(__FILE__).'/adodb';
- else
- return $this->_adodbLibrary;
+ $conn = $this->getConnection();
+ if(is_null($conn->getInternalConnection()) && $conn->open())
+ {
+ Prado::using('System.DataAccess.TActiveRecord');
+ TActiveRecord::setDatabaseAdapter($conn->getInternalConnection());
+ }
}
- public function setAdodbLibrary($path)
+ protected function getAdodbLibrary()
{
- $this->_adodbLibrary = Prado::getPathOfNamespace($path);
+ return Prado::getPathOfNamespace('System.3rdParty.adodb');
}
- public function importAdodbLibrary()
+ protected function importAdodbLibrary()
{
- if(!self::$_hasImported)
- {
- require($this->getAdodbLibrary().'/adodb-exceptions.inc.php');
- require($this->getAdodbLibrary().'/adodb.inc.php');
- self::$_hasImported = true;
- }
+ $path = $this->getAdodbLibrary();
+ require($path.'/adodb-exceptions.inc.php');
+ require($path.'/adodb.inc.php');
}
/**
+ * @return string the cache directory for adodb module
+ */
+ public function getCacheDir()
+ {
+ return $this->_cachedir;
+ }
+
+ /**
* Sets the cache directory for ADODB (in adodb it is
* called to $ADODB_CACHE_DIR)
* @param string the cache directory for adodb module
@@ -126,8 +125,16 @@ class TAdodbConnection extends TDbConnection
private $_connection;
/**
+ * Gets the internal connection.
+ */
+ public function getInternalConnection()
+ {
+ return $this->_connection;
+ }
+
+ /**
* Constructor, initialize a new Adodb connection.
- * @param string|TAdodbProvider DSN connection string or a TAdodbProvider
+ * @param string|TAdodb DSN connection string or a TAdodb
*/
public function __construct($provider=null)
{
@@ -142,7 +149,7 @@ class TAdodbConnection extends TDbConnection
*/
protected function initProvider($connectionString)
{
- $provider = new TAdodbProvider();
+ $provider = new TAdodb();
$provider->setConnectionString($connectionString);
$this->setProvider($provider);
}
@@ -214,21 +221,6 @@ class TAdodbConnection extends TDbConnection
return $this->connection->CompleteTrans();
}
- /**
- * Fail the current transaction.
- */
- public function failTransaction()
- {
- return $this->connection->FailTrans();
- }
-
- /**
- * @return boolean true if transaction has failed.
- */
- public function getHasTransactionFailed()
- {
- return $this->connection->HasFailedTrans();
- }
public function commit()
{
@@ -250,12 +242,11 @@ class TAdodbConnection extends TDbConnection
if($this->getIsClosed())
{
$provider = $this->getProvider();
- $provider->importAdodbLibrary();
+ $provider->initialize();
if(strlen($provider->getConnectionString()) < 1)
{
if(strlen($provider->getDriver()) < 1)
throw new TDbConnectionException('db_driver_required');
-
$this->_connection=ADONewConnection($provider->getDriver());
$this->initConnection();
}
@@ -295,11 +286,11 @@ class TAdodbConnection extends TDbConnection
{
global $ADODB_FETCH_MODE;
$provider = $this->getProvider();
- if($provider->getFetchMode()===TAdodbProvider::FETCH_ASSOCIATIVE)
+ if($provider->getFetchMode()===TAdodb::FETCH_ASSOCIATIVE)
$ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
- else if($provider->fetchMode===TAdodbProvider::FETCH_NUMERIC)
+ else if($provider->fetchMode===TAdodb::FETCH_NUMERIC)
$ADODB_FETCH_MODE=ADODB_FETCH_NUM;
- else if($provider->fetchMode===TAdodbProvider::FETCH_BOTH)
+ else if($provider->fetchMode===TAdodb::FETCH_BOTH)
$ADODB_FETCH_MODE=ADODB_FETCH_BOTH;
else
$ADODB_FETCH_MODE=ADODB_FETCH_DEFAULT;
diff --git a/framework/DataAccess/TCreoleProvider.php b/framework/DataAccess/TCreoleProvider.php
deleted file mode 100644
index f4ccd4c2..00000000
--- a/framework/DataAccess/TCreoleProvider.php
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-Prado::using('System.DataAccess.TDatabaseProvider');
-
-Prado::using('System.DataAccess.creole.*');
-
-class TCreoleProvider extends TDatabaseProvider
-{
- private $_connection = null;
-
- public function getConnection()
- {
- if(is_null($this->_connection))
- $this->_connection = new TCreoleConnection($this);
- return $this->_connection;
- }
-
- public function getConnectionString()
- {
- if(strlen(parent::getConnectionString()) > 0)
- return parent::getConnectionString();
- else
- return $this->generateConnectionString();
- }
-
- protected function generateConnectionString()
- {
- $driver = $this->getDriver();
- $user = $this->getUsername();
- $pass = $this->getPassword();
- $host = $this->getHost();
- $database = $this->getDatabase();
-
- $pass = strlen($pass) > 0 ? ':'.$pass : '';
- $username_password = strlen($user) > 0 ? $user.$pass.'@' : '';
- $database = strlen($database) > 0 ? '/'.$database : '';
-
- return "{$driver}://{$username_password}{$host}{$database}";
- }
-}
-
-class TCreoleConnection extends TDbConnection
-{
- private $_connection = null;
-
- protected function beginDbTransaction()
- {
- }
-
- /**
- * Closes the connection to the database.
- */
- public function close()
- {
- $this->_connection->close();
- }
-
- public function prepare($statement)
- {
- return $this->_connection->prepareStatement($statement);
- }
-
- //public function execute($sql,
-
- /**
- * Opens a database connection with settings provided in the ConnectionString.
- */
- public function open()
- {
- if(is_null($this->_connection))
- {
- $connectionString = $this->getProvider()->getConnectionString();
- if(strlen($connectionString) < 1 || strcmp($connectionString,'://') === 0)
- throw new TDbConnectionException('db_driver_required');
- $class = 'System.DataAccess.creole.creole.Creole';
- $creole = Prado::createComponent($class);
- $this->_connection = $creole->getConnection($connectionString);
- }
- return $this->_connection;
- }
-}
-
-?> \ No newline at end of file
diff --git a/framework/DataAccess/TDatabaseProvider.php b/framework/DataAccess/TDatabaseProvider.php
index ac637d83..2a62c03a 100644
--- a/framework/DataAccess/TDatabaseProvider.php
+++ b/framework/DataAccess/TDatabaseProvider.php
@@ -52,11 +52,16 @@ abstract class TDatabaseProvider extends TModule
}
/**
+ * If the driver is <tt>sqlite</tt>, the host must be dot path to the sqlite
+ * file.
* @return string the DB host name/IP (and port number) in the format "host[:port]"
*/
public function getHost()
{
- return $this->_host;
+ if(strtolower($this->getDriver()) == "sqlite")
+ return Prado::getPathOfNamespace($this->_host);
+ else
+ return $this->_host;
}
/**
@@ -136,6 +141,24 @@ abstract class TDatabaseProvider extends TModule
$this->_persistent=$value;
}
+ protected function buildDsn()
+ {
+ $driver = $this->getDriver().'://';
+ $user = $this->getUsername();
+ $pass = rawurlencode($this->getPassword());
+ $pass = strlen($pass) > 0 ? ':'.$pass : $pass;
+ $host = $this->getHost().'/';
+ if(strtolower($this->getDriver()) == 'sqlite')
+ $host = rawurlencode($host);
+ else
+ $host = '@'.$host;
+ $db = $this->getDatabase();
+ $db = strlen($db) > 0 ? '/'.$db : $db;
+ $persist = $this->getUsePersistentConnection() ? '': '?persit';
+
+ return $driver.$user.$pass.$host.$persist;
+ }
+
/**
* @return TDbConnection a database connection
*/
@@ -194,16 +217,6 @@ interface IDbConnection
public function completeTransaction();
/**
- * Fail the current transaction.
- */
- public function failTransaction();
-
- /**
- * @return boolean true if transaction has failed.
- */
- public function getHasTransactionFailed();
-
- /**
* Makes all changes made since the previous commit/rollback permanent and
* releases any database locks.
*/
@@ -253,6 +266,11 @@ abstract class TDbConnection extends TComponent implements IDbConnection
{
return $this->_provider;
}
+
+ public function __destruct()
+ {
+ $this->close();
+ }
}
?> \ No newline at end of file