Skip to content

Commit

Permalink
Merge branch 'siberex'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Borzenkov committed Dec 14, 2013
2 parents fb13c9d + 888ffca commit 5df722c
Show file tree
Hide file tree
Showing 50 changed files with 636 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.idea
8 changes: 8 additions & 0 deletions README.txt
Expand Up @@ -107,6 +107,14 @@ OR
require_once "DbSimple/Connect.php";
$DB = new DbSimple_Connect("pgsql://login:password@host/database");

OR

You can use PDO DSN syntax (socket connections currently supported only with MySQL providers):

require_once "DbSimple/Connect.php";
$DB = new DbSimple_Connect("mysqli:unix_socket=/cloudsql/app:instance;user=root;pass=;dbname=testdb");


Listing 2: Fetch all resulting rows

$rows = $DB->select('SELECT * FROM ?_user LIMIT 10');
Expand Down
45 changes: 45 additions & 0 deletions lib/DbSimple/CacherImpl.php
@@ -0,0 +1,45 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: sib
* Date: 14.10.13
* Time: 17:40
* To change this template use File | Settings | File Templates.
*/

class CacherImpl implements Zend_Cache_Backend_Interface {

protected $callback;

public function __construct($callback) {
if ( is_callable($callback) ) {
$this->callback = $callback;
} else {
$this->callback = $this->callbackDummy;
}
}

public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) {}

public function remove($id) {}

public function test($id) {}

public function save($data, $id, $tags = array(), $specificLifetime = false)
{
return call_user_func($this->callback, $id, $data);
}

public function load($id, $doNotTestCacheValidity = false)
{
return call_user_func($this->callback, $id);
}

public function setDirectives($directives) {}

protected function callbackDummy($k, $v)
{
return null;
}

} // CacherImpl class
17 changes: 3 additions & 14 deletions lib/DbSimple/Connect.php
Expand Up @@ -118,7 +118,7 @@ protected function connect($dsn)
if (!isset($parsed['scheme']))
$this->errorHandler('Невозможно загрузить драйвер базы данных', $parsed);
$this->shema = ucfirst($parsed['scheme']);
require_once dirname(__FILE__).'/'.$this->shema.'.php';
require_once __DIR__.'/'.$this->shema.'.php';
$class = 'DbSimple_'.$this->shema;
$this->DbSimple = new $class($parsed);
$this->errmsg = &$this->DbSimple->errmsg;
Expand Down Expand Up @@ -256,18 +256,7 @@ public function setCachePrefix($prx)
*/
protected function parseDSN($dsn)
{
$parsed = parse_url($dsn);
if (!$parsed)
return null;
$params = null;
if (!empty($parsed['query']))
{
parse_str($parsed['query'], $params);
$parsed += $params;
}
$parsed['dsn'] = $dsn;
return $parsed;
return DbSimple_Generic::parseDSN($dsn);
}
}

?>
}
32 changes: 27 additions & 5 deletions lib/DbSimple/Database.php 100644 → 100755
Expand Up @@ -74,6 +74,15 @@
if (!defined('DBSIMPLE_PARENT_KEY'))
define('DBSIMPLE_PARENT_KEY', 'PARENT_KEY'); // forrest-based resultset support


if ( !interface_exists('Zend_Cache_Backend_Interface', false) ) {
require_once __DIR__ . '/Zend/Cache.php';
require_once __DIR__ . '/Zend/Cache/Backend/Interface.php';
}

require_once __DIR__ . '/CacherImpl.php';


/**
*
* Base class for all databases.
Expand Down Expand Up @@ -262,10 +271,24 @@ public function setLogger($logger)
* Set cache mechanism called during each query if specified.
* Returns previous handler.
*/
public function setCacher(Zend_Cache_Backend_Interface $cacher=null)
public function setCacher($cacher=null)
{
$prev = $this->_cacher;
$this->_cacher = $cacher;

if ( is_null($cacher) ) {
return $prev;
}

if ($cacher instanceof Zend_Cache_Backend_Interface) {
$this->_cacher = $cacher;
return $prev;
}

if ( is_callable($cacher) ) {
$this->_cacher = new CacherImpl($cacher);
return $prev;
}

return $prev;
}

Expand Down Expand Up @@ -444,7 +467,7 @@ private function _query($query, &$total)
$rows = false;
$cache_it = false;
// Кешер у нас либо null либо соответствует Zend интерфейсу
if (!empty($this->attributes['CACHE']) && $this->_cacher)
if ( !empty($this->attributes['CACHE']) && ($this->_cacher instanceof Zend_Cache_Backend_Interface) )
{

$hash = $this->_cachePrefix . md5(serialize($query));
Expand Down Expand Up @@ -1323,7 +1346,7 @@ public function addIgnoreInTrace($name)
* Return part of stacktrace before calling first library method.
* Used in debug purposes (query logging etc.).
*/
protected function findLibraryCaller()
public function findLibraryCaller()
{
$caller = call_user_func(
array(&$this, 'debug_backtrace_smart'),
Expand Down Expand Up @@ -1387,4 +1410,3 @@ private function debug_backtrace_smart($ignoresRe=null, $returnCaller=false)
}

}
?>
69 changes: 63 additions & 6 deletions lib/DbSimple/Generic.php 100644 → 100755
Expand Up @@ -85,8 +85,17 @@ class DbSimple_Generic
* Universal static function to connect ANY database using DSN syntax.
* Choose database driver according to DSN. Return new instance
* of this driver.
*
* You can connect to MySQL by socket using this new syntax (like PDO DSN):
* $dsn = 'mysqli:unix_socket=/cloudsql/app:instance;user=root;pass=;dbname=testdb';
* $dsn = 'mypdo:unix_socket=/cloudsql/app:instance;charset=utf8;user=testuser;pass=mypassword;dbname=testdb';
*
* Connection by host also can be made with this syntax.
* Or you can use old syntax:
* $dsn = 'mysql://testuser:mypassword@127.0.0.1/testdb';
*
*/
function connect($dsn)
public static function connect($dsn)
{
// Load database driver and create its instance.
$parsed = DbSimple_Generic::parseDSN($dsn);
Expand All @@ -96,7 +105,7 @@ function connect($dsn)
}
$class = 'DbSimple_'.ucfirst($parsed['scheme']);
if (!class_exists($class)) {
$file = dirname(__FILE__).'/'.ucfirst($parsed['scheme']). ".php";
$file = __DIR__.'/'.ucfirst($parsed['scheme']). ".php";
if (is_file($file)) {
require_once($file);
} else {
Expand All @@ -118,19 +127,67 @@ function connect($dsn)
* Parse a data source name.
* See parse_url() for details.
*/
function parseDSN($dsn)
public static function parseDSN($dsn)
{
if (is_array($dsn)) return $dsn;
$parsed = parse_url($dsn);
if (!$parsed) return null;

$params = null;
if (!empty($parsed['query'])) {
parse_str($parsed['query'], $params);
$parsed += $params;
}

if ( empty($parsed['host']) && empty($parsed['socket']) ) {
// Parse as DBO DSN string
$parsedPdo = self::parseDsnPdo($parsed['path']);
unset($parsed['path']);
$parsed = array_merge($parsed, $parsedPdo);
}

$parsed['dsn'] = $dsn;
return $parsed;
}
}
} // parseDSN


/**
* Parse string as DBO DSN string.
*
* @param $str
* @return array
*/
public static function parseDsnPdo($str) {

if (substr($str, 0, strlen('mysql:')) == 'mysql:') {
$str = substr($str, strlen('mysql:'));
}

$arr = explode(';', $str);

$result = array();
foreach ($arr as $k=>$v) {
$v = explode('=', $v);
if (count($v) == 2)
$result[ $v[0] ] = $v[1];
}

if ( isset($result['unix_socket']) ) {
$result['socket'] = $result['unix_socket'];
unset($result['unix_socket']);
}

if ( isset($result['dbname']) ) {
$result['path'] = $result['dbname'];
unset($result['dbname']);
}

if ( isset($result['charset']) ) {
$result['enc'] = $result['charset'];
unset($result['charset']);
}

return $result;
} // parseDsnPdo

?>
} // DbSimple_Generic class
4 changes: 1 addition & 3 deletions lib/DbSimple/Ibase.php
Expand Up @@ -16,7 +16,7 @@
*
* @version 2.x $Id$
*/
require_once dirname(__FILE__) . '/Database.php';
require_once __DIR__ . '/Database.php';

/**
* Best transaction parameters for script queries.
Expand Down Expand Up @@ -284,5 +284,3 @@ function _firstUse()
return true;
}
}

?>
4 changes: 1 addition & 3 deletions lib/DbSimple/Litepdo.php
Expand Up @@ -15,7 +15,7 @@
*
* @version 2.x $Id$
*/
require_once dirname(__FILE__).'/Database.php';
require_once __DIR__.'/Database.php';

/**
* Database class for SQLite.
Expand Down Expand Up @@ -150,5 +150,3 @@ protected function _performFetch($result)
}

}

?>
5 changes: 2 additions & 3 deletions lib/DbSimple/Mssql.php
Expand Up @@ -16,7 +16,7 @@
*
* @version 2.x $Id: Mssql.php 163 2007-01-10 09:47:49Z dk $
*/
require_once dirname(__FILE__) . '/Generic.php';
require_once __DIR__ . '/Generic.php';


/**
Expand All @@ -43,7 +43,7 @@ function DbSimple_Mssql($dsn)
);
$this->_resetLastError();
if (!$ok) return $this->_setDbError('mssql_connect()');
$ok = @mssql_select_db(preg_replace('{^/}s', '', $p['path']), $this->link);
$ok = @mssql_select_db(preg_replace('{^/}s', '', $dsn['path']), $this->link);
if (!$ok) return $this->_setDbError('mssql_select_db()');
}

Expand Down Expand Up @@ -241,4 +241,3 @@ function length()
return strlen($this->blobdata);
}
}
?>
62 changes: 51 additions & 11 deletions lib/DbSimple/Mypdo.php
Expand Up @@ -15,7 +15,7 @@
*
* @version 2.x $Id$
*/
require_once dirname(__FILE__).'/Database.php';
require_once __DIR__.'/Database.php';

/**
* Database class for MySQL.
Expand All @@ -31,15 +31,57 @@ public function DbSimple_Mypdo($dsn)
return $this->_setLastError("-1", "PDO extension is not loaded", "PDO");

try {
$this->link = new PDO('mysql:host='.$dsn['host'].(empty($dsn['port'])?'':';port='.$dsn['port']).';dbname='.$base,
$dsn['user'], isset($dsn['pass'])?$dsn['pass']:'', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_PERSISTENT => isset($dsn['persist']) && $dsn['persist'],
PDO::ATTR_TIMEOUT => isset($dsn['timeout']) && $dsn['timeout'] ? $dsn['timeout'] : 0,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.(isset($dsn['enc']) ? $dsn['enc'] : 'UTF8'),
));
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_PERSISTENT => isset($dsn['persist']) && $dsn['persist'],
PDO::ATTR_TIMEOUT => isset($dsn['timeout']) && $dsn['timeout'] ? $dsn['timeout'] : 0
);

if (version_compare(PHP_VERSION, '5.3.6') < 0) {
$phpNew = false;
$options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . (!empty($dsn['enc']) ? $dsn['enc'] : 'utf8');
} else {
$phpNew = true;
}

if ( !empty($dsn['socket']) ) {
// Socket connection

$dsnPdo = 'mysql:unix_socket=' . $dsn['socket']
. ';dbname=' . $base;

if ($phpNew) {
$dsnPdo .= ';charset=' . (!empty($dsn['enc']) ? $dsn['enc'] : 'utf8');
}

$this->link = new PDO(
$dsnPdo,
isset($dsn['user']) ? $dsn['user'] : 'root',
isset($dsn['pass']) ? $dsn['pass'] : '',
$options
);
} else if ( !empty($dsn['host']) ) {
// Host connection

$dsnPdo = 'mysql:host=' . $dsn['host']
. (empty($dsn['port']) ? '' : ';port='.$dsn['port'])
. ';dbname=' . $base;

if ($phpNew) {
$dsnPdo .= ';charset=' . (!empty($dsn['enc']) ? $dsn['enc'] : 'utf8');
}

$this->link = new PDO(
$dsnPdo,
$dsn['user'],
isset($dsn['pass']) ? $dsn['pass'] : '',
$options
);
} else {
throw new Exception("Could not find hostname nor socket in DSN string");
}
} catch (PDOException $e) {
$this->_setLastError($e->getCode() , $e->getMessage(), 'new PDO');
return $this->_setLastError($e->getCode() , $e->getMessage(), 'new PDO');
}
}

Expand Down Expand Up @@ -140,5 +182,3 @@ protected function _performFetch($result)
}

}

?>

0 comments on commit 5df722c

Please sign in to comment.