Init ci
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
// 123456 --> /www/beatdb/1/2/3/4/5/123456.json
|
||||
|
||||
class BeatDB {
|
||||
|
||||
private static $_db_root = '/www/beatdb/';
|
||||
|
||||
public static function get($key) {
|
||||
if (self::exists($key)) {
|
||||
return json_decode(
|
||||
file_get_contents(
|
||||
self::$_db_root . implode('/', self::getFilePathByKey($key))
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function set($key, $data) {
|
||||
$path = self::getFilePathByKey($key);
|
||||
$part = self::$_db_root;
|
||||
for ($i = 0; $i < count($path) - 1; $i++) {
|
||||
$part .= $path[$i] .'/';
|
||||
}
|
||||
if (!file_exists($part)) {
|
||||
if (!mkdir($part, 0777, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (bool) file_put_contents($part . $path[$i], json_encode($data));
|
||||
}
|
||||
|
||||
public static function delete($key) {
|
||||
$path = self::getFilePathByKey($key);
|
||||
$ret = array();
|
||||
for ($i = count($path) - 1; $i >= 0; $i--) {
|
||||
$part = self::$_db_root . implode('/', $path);
|
||||
unset($path[$i]);
|
||||
if (is_file($part)) {
|
||||
unlink($part);
|
||||
} elseif (count(scandir($part)) == 2) {
|
||||
rmdir($part);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function exists($key) {
|
||||
return file_exists(self::$_db_root . implode('/', self::getFilePathByKey($key)));
|
||||
}
|
||||
|
||||
private static function getFilePathByKey($key) {
|
||||
$key = strval($key);
|
||||
$path = array();
|
||||
for ($i = 0; $i < strlen($key) - 1; $i++) {
|
||||
$path[] = substr($key, $i, 1);
|
||||
}
|
||||
$path[] = $key .'.json';
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
class Db extends Singleton {
|
||||
|
||||
private $_driver;
|
||||
|
||||
public function init() {
|
||||
$params = Config::get('app:DB');
|
||||
$driver = $params['engine'];
|
||||
unset($params['engine']);
|
||||
switch ($driver) {
|
||||
case 'pgsql' : default :
|
||||
$this->_driver = new Pgsql(); break;
|
||||
}
|
||||
call_user_func_array(array($this->_driver, 'connect'), $params);
|
||||
}
|
||||
|
||||
public function q($q, $params = array()) {
|
||||
if (! $this->_driver) {
|
||||
$this->init();
|
||||
}
|
||||
return $this->_driver->q($q);
|
||||
}
|
||||
|
||||
public function getRows($result, $key = false) {
|
||||
if (! $this->_driver) {
|
||||
$this->init();
|
||||
}
|
||||
return $this->_driver->getRows($result, $key);
|
||||
}
|
||||
|
||||
public function getRow($result) {
|
||||
if (! $this->_driver) {
|
||||
$this->init();
|
||||
}
|
||||
return $this->_driver->getRow($result);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
class Pgsql implements iDbDriver {
|
||||
|
||||
private $_connection = false;
|
||||
|
||||
public function connect($host, $port, $dbname, $login, $pass) {
|
||||
$this->connection = pg_connect('host='. $host .' port='. $port .' dbname='. $dbname .' user='. $login .' password='. $pass);
|
||||
}
|
||||
|
||||
public function q($q) {
|
||||
return pg_query($q);
|
||||
}
|
||||
|
||||
public function getRows($r, $key = false) {
|
||||
$rows = array();
|
||||
while($row = pg_fetch_assoc($r)) {
|
||||
if (!$key) {
|
||||
$rows[] = $row;
|
||||
} else {
|
||||
$rows[$row[$key]] = $row;
|
||||
}
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function getRow($r) {
|
||||
return pg_fetch_assoc($r);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
interface iDbDriver {
|
||||
|
||||
public function connect($host, $port, $dbname, $login, $pass);
|
||||
|
||||
public function q($q);
|
||||
|
||||
public function getRows($r, $key);
|
||||
|
||||
public function getRow($r);
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user