mirror of
https://github.com/dg/dibi.git
synced 2025-08-08 07:06:52 +02:00
Connection::alias() & loadFile() moved to Helpers
This commit is contained in:
@@ -69,11 +69,11 @@ class Connection
|
||||
throw new \InvalidArgumentException('Configuration must be array, string or object.');
|
||||
}
|
||||
|
||||
self::alias($config, 'username', 'user');
|
||||
self::alias($config, 'password', 'pass');
|
||||
self::alias($config, 'host', 'hostname');
|
||||
self::alias($config, 'result|formatDate', 'resultDate');
|
||||
self::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||
Helpers::alias($config, 'username', 'user');
|
||||
Helpers::alias($config, 'password', 'pass');
|
||||
Helpers::alias($config, 'host', 'hostname');
|
||||
Helpers::alias($config, 'result|formatDate', 'resultDate');
|
||||
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||
|
||||
if (!isset($config['driver'])) {
|
||||
$config['driver'] = \dibi::$defaultDriver;
|
||||
@@ -204,24 +204,10 @@ class Connection
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply configuration alias or default values.
|
||||
* @param array connect configuration
|
||||
* @param string key
|
||||
* @param string alias key
|
||||
* @return void
|
||||
*/
|
||||
/** @deprecated */
|
||||
public static function alias(& $config, $key, $alias)
|
||||
{
|
||||
$foo = & $config;
|
||||
foreach (explode('|', $key) as $key) {
|
||||
$foo = & $foo[$key];
|
||||
}
|
||||
|
||||
if (!isset($foo) && isset($config[$alias])) {
|
||||
$foo = $config[$alias];
|
||||
unset($config[$alias]);
|
||||
}
|
||||
Helpers::alias($config, $key, $alias);
|
||||
}
|
||||
|
||||
|
||||
@@ -618,44 +604,13 @@ class Connection
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file - extreme fast!
|
||||
* Import SQL dump from file.
|
||||
* @param string filename
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public function loadFile($file)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
@set_time_limit(0); // intentionally @
|
||||
|
||||
$handle = @fopen($file, 'r'); // intentionally @
|
||||
if (!$handle) {
|
||||
throw new \RuntimeException("Cannot open file '$file'.");
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$delimiter = ';';
|
||||
$sql = '';
|
||||
while (!feof($handle)) {
|
||||
$s = rtrim(fgets($handle));
|
||||
if (substr($s, 0, 10) === 'DELIMITER ') {
|
||||
$delimiter = substr($s, 10);
|
||||
|
||||
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
|
||||
$sql .= substr($s, 0, -strlen($delimiter));
|
||||
$this->driver->query($sql);
|
||||
$sql = '';
|
||||
$count++;
|
||||
|
||||
} else {
|
||||
$sql .= $s . "\n";
|
||||
}
|
||||
}
|
||||
if (trim($sql) !== '') {
|
||||
$this->driver->query($sql);
|
||||
$count++;
|
||||
}
|
||||
fclose($handle);
|
||||
return $count;
|
||||
return Helpers::loadFromFile($this, $file);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -62,7 +62,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
*/
|
||||
public function connect(array & $config)
|
||||
{
|
||||
Dibi\Connection::alias($config, 'database', 'db');
|
||||
Dibi\Helpers::alias($config, 'database', 'db');
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
|
@@ -72,7 +72,7 @@ class MySqlDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
|
||||
} else {
|
||||
// default values
|
||||
Dibi\Connection::alias($config, 'flags', 'options');
|
||||
Dibi\Helpers::alias($config, 'flags', 'options');
|
||||
$config += [
|
||||
'charset' => 'utf8',
|
||||
'timezone' => date('P'),
|
||||
|
@@ -63,7 +63,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
{
|
||||
$foo = & $config['dsn'];
|
||||
$foo = & $config['options'];
|
||||
Dibi\Connection::alias($config, 'resource', 'pdo');
|
||||
Dibi\Helpers::alias($config, 'resource', 'pdo');
|
||||
|
||||
if ($config['resource'] instanceof PDO) {
|
||||
$this->connection = $config['resource'];
|
||||
|
@@ -69,8 +69,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
$string = $config['string'];
|
||||
} else {
|
||||
$string = '';
|
||||
Dibi\Connection::alias($config, 'user', 'username');
|
||||
Dibi\Connection::alias($config, 'dbname', 'database');
|
||||
Dibi\Helpers::alias($config, 'user', 'username');
|
||||
Dibi\Helpers::alias($config, 'dbname', 'database');
|
||||
foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) {
|
||||
if (isset($config[$key])) {
|
||||
$string .= $key . '=' . $config[$key] . ' ';
|
||||
|
@@ -61,7 +61,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function connect(array & $config)
|
||||
{
|
||||
Dibi\Connection::alias($config, 'database', 'file');
|
||||
Dibi\Helpers::alias($config, 'database', 'file');
|
||||
$this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U';
|
||||
$this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U';
|
||||
|
||||
|
@@ -9,6 +9,7 @@ namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Connection;
|
||||
use Dibi\Helpers;
|
||||
|
||||
|
||||
/**
|
||||
@@ -62,10 +63,10 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function connect(array & $config)
|
||||
{
|
||||
Connection::alias($config, 'options|UID', 'username');
|
||||
Connection::alias($config, 'options|PWD', 'password');
|
||||
Connection::alias($config, 'options|Database', 'database');
|
||||
Connection::alias($config, 'options|CharacterSet', 'charset');
|
||||
Helpers::alias($config, 'options|UID', 'username');
|
||||
Helpers::alias($config, 'options|PWD', 'password');
|
||||
Helpers::alias($config, 'options|Database', 'database');
|
||||
Helpers::alias($config, 'options|CharacterSet', 'charset');
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
@@ -291,7 +292,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
/** @deprecated */
|
||||
public function escape($value, $type)
|
||||
{
|
||||
return Dibi\Helpers::escape($this, $value, $type);
|
||||
return Helpers::escape($this, $value, $type);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -212,4 +212,66 @@ class Helpers
|
||||
return self::$types;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply configuration alias or default values.
|
||||
* @param array connect configuration
|
||||
* @param string key
|
||||
* @param string alias key
|
||||
* @return void
|
||||
*/
|
||||
public static function alias(& $config, $key, $alias)
|
||||
{
|
||||
$foo = & $config;
|
||||
foreach (explode('|', $key) as $key) {
|
||||
$foo = & $foo[$key];
|
||||
}
|
||||
|
||||
if (!isset($foo) && isset($config[$alias])) {
|
||||
$foo = $config[$alias];
|
||||
unset($config[$alias]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file.
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public static function loadFromFile(Connection $connection, $file)
|
||||
{
|
||||
@set_time_limit(0); // intentionally @
|
||||
|
||||
$handle = @fopen($file, 'r'); // intentionally @
|
||||
if (!$handle) {
|
||||
throw new \RuntimeException("Cannot open file '$file'.");
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$delimiter = ';';
|
||||
$sql = '';
|
||||
$driver = $connection->getDriver();
|
||||
while (!feof($handle)) {
|
||||
$s = rtrim(fgets($handle));
|
||||
if (substr($s, 0, 10) === 'DELIMITER ') {
|
||||
$delimiter = substr($s, 10);
|
||||
|
||||
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
|
||||
$sql .= substr($s, 0, -strlen($delimiter));
|
||||
$driver->query($sql);
|
||||
$sql = '';
|
||||
$count++;
|
||||
|
||||
} else {
|
||||
$sql .= $s . "\n";
|
||||
}
|
||||
}
|
||||
if (trim($sql) !== '') {
|
||||
$driver->query($sql);
|
||||
$count++;
|
||||
}
|
||||
fclose($handle);
|
||||
return $count;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -365,7 +365,7 @@ class dibi
|
||||
*/
|
||||
public static function loadFile($file)
|
||||
{
|
||||
return self::getConnection()->loadFile($file);
|
||||
return Dibi\Helpers::loadFromFile(self::getConnection(), $file);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user