#77 php-pdo backend, thanks to gempse
--HG-- extra : convert_revision : svn%3Ab267cdba-c1da-11dd-874b-8bacd04a0a74/trunk%4087
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
class AbstractLayer
|
||||
{
|
||||
|
||||
public static $pdo = NULL;
|
||||
|
||||
# Generic
|
||||
public static function protect($value,$alias=NULL) {BackendPhpPdo::getException('Not Implemented');}
|
||||
|
||||
# saveloadlist
|
||||
public static function Keywords($table,$keyword='') {BackendPhpPdo::getException('Not Implemented');}
|
||||
public static function KeywordsInsert($table,$keyword,$data) {BackendPhpPdo::getException('Not Implemented');}
|
||||
public static function KeywordsUpdate($table,$keyword,$data) {BackendPhpPdo::getException('Not Implemented');}
|
||||
|
||||
# datatypes
|
||||
public static function DatatypesFile() {BackendPhpPdo::getException('Not Implemented');}
|
||||
|
||||
# model
|
||||
public static function Tables() {BackendPhpPdo::getException('Not Implemented');}
|
||||
public static function Columns($table) {BackendPhpPdo::getException('Not Implemented');}
|
||||
public static function Relations($table,$column) {BackendPhpPdo::getException('Not Implemented');}
|
||||
public static function Keys($table) {BackendPhpPdo::getException('Not Implemented');}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
class LayerMYSQL extends AbstractLayer
|
||||
{
|
||||
# Generic
|
||||
public static function protect($value,$alias=NULL)
|
||||
{
|
||||
return '`'.str_replace('.','`.`',$value).'`'.($alias?' `'.$alias.'`':'');
|
||||
}
|
||||
|
||||
# saveloadlist
|
||||
public static function Keywords($table,$keyword='')
|
||||
{
|
||||
$req = '
|
||||
SELECT
|
||||
';
|
||||
if($keyword)
|
||||
$req .= ' '.self::protect('data').' ';
|
||||
else
|
||||
$req .= ' '.self::protect('keyword').' ';
|
||||
$req .= '
|
||||
FROM '.self::protect($table).'
|
||||
';
|
||||
if($keyword)
|
||||
$req .= ' WHERE '.self::protect('keyword').' = ? ';
|
||||
else
|
||||
$req .= ' ORDER BY '.self::protect('dt').' DESC ';
|
||||
|
||||
$q = self::$pdo->prepare($req);
|
||||
if($keyword)
|
||||
$q->bindValue($keyword,'string');
|
||||
return $q->execute();
|
||||
}
|
||||
public static function KeywordsInsert($table,$keyword,$data)
|
||||
{
|
||||
$req = '
|
||||
INSERT INTO '.self::protect($table).'
|
||||
('.self::protect('keyword').', '.self::protect('data').')
|
||||
VALUES (?, ?)
|
||||
';
|
||||
return self::$pdo->prepare($req)->bindValue($keyword,'string')->bindValue($data,'string')->execute();
|
||||
}
|
||||
public static function KeywordsUpdate($table,$keyword,$data)
|
||||
{
|
||||
$req = '
|
||||
UPDATE '.self::protect($table).'
|
||||
SET '.self::protect('data').' = ?
|
||||
WHERE '.self::protect('keyword').' = ?
|
||||
';
|
||||
return self::$pdo->prepare($req)->bindValue($data,'string')->bindValue($keyword,'string')->execute();
|
||||
}
|
||||
|
||||
# datatypes
|
||||
public static function DatatypesFile()
|
||||
{
|
||||
return dirname(__FILE__).'/../../db/mysql/datatypes.xml';
|
||||
}
|
||||
|
||||
# model
|
||||
public static $schema = NULL;
|
||||
public static function Tables()
|
||||
{
|
||||
if(is_null(self::$schema))
|
||||
self::$schema = BackendPhpPdo::req('database');
|
||||
if(is_null(self::$schema))
|
||||
self::$schema = 'information_schema';
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('TABLE_NAME').' as '.self::protect('name').',
|
||||
'.self::protect('TABLE_COMMENT').' as '.self::protect('comment').',
|
||||
FROM '.self::protect('TABLES').'
|
||||
WHERE
|
||||
'.self::protect('TABLE_SCHEMA').' = ?
|
||||
';
|
||||
return self::$pdo->prepare($req)->bindValue(self::$schema,'string')->execute()->fetchAll();
|
||||
}
|
||||
private static $columns = array();
|
||||
public static function Columns($table)
|
||||
{
|
||||
if(isset(self::$columns[$table['name']]))
|
||||
return self::$columns[$table['name']];
|
||||
if(!empty(self::$columns))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('TABLE_NAME').' as '.self::protect('table').',
|
||||
'.self::protect('COLUMN_NAME').' as '.self::protect('name').',
|
||||
'.self::protect('COLUMN_TYPE').' as '.self::protect('type').',
|
||||
'.self::protect('IS_NULLABLE').' as '.self::protect('null').',
|
||||
'.self::protect('COLUMN_DEFAULT').' as '.self::protect('default').',
|
||||
'.self::protect('COLUMN_COMMENT').' as '.self::protect('comment').',
|
||||
'.self::protect('EXTRA').' as '.self::protect('autoincrement').'
|
||||
FROM '.self::protect('COLUMNS').'
|
||||
WHERE
|
||||
'.self::protect('TABLE_SCHEMA').' = ?
|
||||
';
|
||||
$columns = self::$pdo->prepare($req)->bindValue(self::$schema,'string')->execute()->fetchAll();
|
||||
foreach($columns as $k=>$column)
|
||||
{
|
||||
$column['type'] = strtoupper($column['type']);
|
||||
$column['null'] = ($column['null'] == 'YES' ? '1' : '0');
|
||||
$column['default'] = ($column['default'] == 'NULL' ? '' : $column['default']);
|
||||
$column['autoincrement'] = preg_match('/auto_increment/i',$column['autoincrement']) ? '1' : '0';
|
||||
self::$columns[$column['table']][] = $column;
|
||||
}
|
||||
return self::$columns[$table['name']];
|
||||
}
|
||||
private static $relations = array();
|
||||
public static function Relations($table,$column)
|
||||
{
|
||||
if(isset(self::$relations[$table['name']][$column['name']]))
|
||||
return self::$relations[$table['name']][$column['name']];
|
||||
if(!empty(self::$relations))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('c.TABLE_NAME').' as '.self::protect('table_src').',
|
||||
'.self::protect('k.COLUMN_NAME').' as '.self::protect('column_src').',
|
||||
'.self::protect('REFERENCED_TABLE_NAME').' as '.self::protect('table').',
|
||||
'.self::protect('REFERENCED_COLUMN_NAME').' as '.self::protect('column').'
|
||||
FROM '.self::protect('KEY_COLUMN_USAGE','k').'
|
||||
LEFT JOIN '.self::protect('TABLE_CONSTRAINTS','c').'
|
||||
ON '.self::protect('k.CONSTRAINT_NAME').' = '.self::protect('c.CONSTRAINT_NAME').'
|
||||
WHERE
|
||||
'.self::protect('c.TABLE_SCHEMA').' = ?
|
||||
';
|
||||
$relations = self::$pdo->prepare($req)->bindValue(self::$schema,'string')->execute()->fetchAll();
|
||||
foreach($relations as $relation)
|
||||
self::$relations[$relation['table_src']][$relation['column_src']][] = $relation;
|
||||
|
||||
if(isset(self::$relations[$table['name']][$column['name']]))
|
||||
return self::$relations[$table['name']][$column['name']];
|
||||
return array();
|
||||
}
|
||||
private static $keys = array();
|
||||
public static function Keys($table)
|
||||
{
|
||||
if(isset(self::$keys[$table['name']]))
|
||||
return self::$keys[$table['name']];
|
||||
if(!empty(self::$keys))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('TABLE_NAME').' as '.self::protect('table').',
|
||||
'.self::protect('INDEX_NAME').' as '.self::protect('name').',
|
||||
'.self::protect('INDEX_TYPE').' as '.self::protect('type').',
|
||||
'.self::protect('COLUMN_NAME').' as '.self::protect('column').'
|
||||
'.self::protect('NON_UNIQUE').' as '.self::protect('non_unique').'
|
||||
FROM '.self::protect('STATISTICS').'
|
||||
WHERE
|
||||
'.self::protect('TABLE_SCHEMA').' = ?
|
||||
ORDER BY '.self::protect('SEQ_IN_INDEX').'
|
||||
';
|
||||
$indexes = self::$pdo->prepare($req)->bindValue(self::$schema,'string')->execute()->fetchAll();
|
||||
|
||||
$keys = array();
|
||||
foreach($indexes as $index)
|
||||
{
|
||||
if($index['type'] != 'FULLTEXT')
|
||||
$index['type'] = 'INDEX';
|
||||
if($index['non_unique'] == '0')
|
||||
$index['type'] = 'UNIQUE';
|
||||
if($index['name'] == 'PRIMARY')
|
||||
$index['type'] = 'PRIMARY';
|
||||
self::$keys[$constraint['table']][$index['name']]['type'] = $index['type'];
|
||||
self::$keys[$constraint['table']][$index['name']]['columns'][] = $index['column'];
|
||||
}
|
||||
|
||||
return self::$keys[$table['name']];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
class LayerPGSQL extends AbstractLayer
|
||||
{
|
||||
# Generic
|
||||
public static function protect($value,$alias=NULL)
|
||||
{
|
||||
return '"'.str_replace('.','"."',$value).'"'.($alias?' "'.$alias.'"':'');
|
||||
}
|
||||
|
||||
# saveloadlist
|
||||
public static function Keywords($table,$keyword='')
|
||||
{
|
||||
$req = '
|
||||
SELECT
|
||||
';
|
||||
if($keyword)
|
||||
$req .= ' '.self::protect('xmldata').' ';
|
||||
else
|
||||
$req .= ' '.self::protect('keyword').' ';
|
||||
$req .= '
|
||||
FROM '.self::protect($table).'
|
||||
';
|
||||
if($keyword)
|
||||
$req .= ' WHERE '.self::protect('keyword').' = ? ';
|
||||
else
|
||||
$req .= ' ORDER BY '.self::protect('dt').' DESC ';
|
||||
|
||||
$q = self::$pdo->prepare($req);
|
||||
if($keyword)
|
||||
$q->bindValue($keyword,'string');
|
||||
return $q->execute();
|
||||
}
|
||||
public static function KeywordsInsert($table,$keyword,$data)
|
||||
{
|
||||
$req = '
|
||||
INSERT INTO '.self::protect($table).'
|
||||
('.self::protect('keyword').', '.self::protect('xmldata').')
|
||||
VALUES (?, ?)
|
||||
';
|
||||
return self::$pdo->prepare($req)->bindValue($keyword,'string')->bindValue($data,'string')->execute();
|
||||
}
|
||||
public static function KeywordsUpdate($table,$keyword,$data)
|
||||
{
|
||||
$req = '
|
||||
UPDATE '.self::protect($table).'
|
||||
SET '.self::protect('xmldata').' = ?
|
||||
WHERE '.self::protect('keyword').' = ?
|
||||
';
|
||||
return self::$pdo->prepare($req)->bindValue($data,'string')->bindValue($keyword,'string')->execute();
|
||||
}
|
||||
|
||||
# datatypes
|
||||
public static function DatatypesFile()
|
||||
{
|
||||
return dirname(__FILE__).'/../../db/postgresql/datatypes.xml';
|
||||
}
|
||||
|
||||
# model
|
||||
public static function Tables()
|
||||
{
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('relname').' as '.self::protect('name').',
|
||||
'.self::protect('c.oid').' as '.self::protect('oid').',
|
||||
(SELECT pg_catalog.obj_description('.self::protect('c.oid').', \'pg_class\')) as '.self::protect('comment').'
|
||||
FROM pg_catalog.pg_class '.self::protect('c').'
|
||||
WHERE
|
||||
'.self::protect('c.relname').' !~ \'^(pg_|sql_)\'
|
||||
AND '.self::protect('c.relkind').' = \'r\'
|
||||
';
|
||||
return self::$pdo->prepare($req)->execute()->fetchAll();
|
||||
}
|
||||
private static $columns = array();
|
||||
public static function Columns($table)
|
||||
{
|
||||
if(isset(self::$columns[$table['name']]))
|
||||
return self::$columns[$table['name']];
|
||||
if(!empty(self::$columns))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('table_name').' as '.self::protect('table').',
|
||||
'.self::protect('column_name').' as '.self::protect('name').',
|
||||
'.self::protect('data_type').' as '.self::protect('type').',
|
||||
'.self::protect('is_nullable').' as '.self::protect('null').',
|
||||
'.self::protect('column_default').' as '.self::protect('default')/*.',
|
||||
* col_description(? ,ordinal_position) as '.self::protect('comment').'*/.'
|
||||
FROM information_schema.columns
|
||||
ORDER BY '.self::protect('ordinal_position').'
|
||||
';
|
||||
$columns = self::$pdo->prepare($req)->execute()->fetchAll();
|
||||
foreach($columns as $k=>$column)
|
||||
{
|
||||
$column['type'] = strtoupper($column['type']);
|
||||
$column['null'] = ($column['null'] == 'YES' ? '1' : '0');
|
||||
$column['default'] = ($column['default'] == 'NULL' ? '' : $column['default']);
|
||||
$column['autoincrement'] = '0';
|
||||
self::$columns[$column['table']][] = $column;
|
||||
}
|
||||
return self::$columns[$table['name']];
|
||||
}
|
||||
private static $relations = array();
|
||||
public static function Relations($table,$column)
|
||||
{
|
||||
if(isset(self::$relations[$table['name']][$column['name']]))
|
||||
return self::$relations[$table['name']][$column['name']];
|
||||
if(!empty(self::$relations))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('kku.table_name').' as '.self::protect('table_src').',
|
||||
'.self::protect('kku.column_name').' as '.self::protect('column_src').',
|
||||
'.self::protect('ccu.table_name').' as '.self::protect('table').',
|
||||
'.self::protect('ccu.column_name').' as '.self::protect('column').'
|
||||
FROM '.self::protect('information_schema.table_constraints','tc').'
|
||||
LEFT JOIN '.self::protect('information_schema.constraint_column_usage','ccu').'
|
||||
ON '.self::protect('tc.constraint_name').' = '.self::protect('ccu.constraint_name').'
|
||||
LEFT JOIN '.self::protect('information_schema.key_column_usage','kku').'
|
||||
ON '.self::protect('kku.constraint_name').' = '.self::protect('ccu.constraint_name').'
|
||||
WHERE
|
||||
'.self::protect('constraint_type').' = ?
|
||||
';
|
||||
$relations = self::$pdo->prepare($req)->bindValue('FOREIGN KEY','string')->execute()->fetchAll();
|
||||
foreach($relations as $relation)
|
||||
self::$relations[$relation['table_src']][$relation['column_src']][] = $relation;
|
||||
|
||||
if(isset(self::$relations[$table['name']][$column['name']]))
|
||||
return self::$relations[$table['name']][$column['name']];
|
||||
return array();
|
||||
}
|
||||
private static $keys = array();
|
||||
public static function Keys($table)
|
||||
{
|
||||
if(isset(self::$keys[$table['name']]))
|
||||
return self::$keys[$table['name']];
|
||||
if(!empty(self::$keys))
|
||||
return array();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('tc.table_name').' as '.self::protect('table').',
|
||||
'.self::protect('tc.constraint_name').' as '.self::protect('name').',
|
||||
'.self::protect('tc.constraint_type').' as '.self::protect('type').',
|
||||
'.self::protect('kcu.column_name').' as '.self::protect('column').'
|
||||
FROM '.self::protect('information_schema.table_constraints','tc').'
|
||||
LEFT JOIN '.self::protect('information_schema.key_column_usage','kcu').'
|
||||
ON '.self::protect('tc.constraint_catalog').' = '.self::protect('tc.constraint_catalog').'
|
||||
AND '.self::protect('tc.constraint_schema').' = '.self::protect('kcu.constraint_schema').'
|
||||
AND '.self::protect('tc.constraint_name').' = '.self::protect('kcu.constraint_name').'
|
||||
WHERE
|
||||
'.self::protect('constraint_type').' != ?
|
||||
ORDER BY '.self::protect('tc.constraint_name').'
|
||||
';
|
||||
$contraints = self::$pdo->prepare($req)->bindValue('FOREIGN KEY','string')->execute()->fetchAll();
|
||||
|
||||
$req = '
|
||||
SELECT
|
||||
'.self::protect('pci.relname').' as '.self::protect('table').',
|
||||
'.self::protect('pcx.relname').' as '.self::protect('name').',
|
||||
'.self::protect('pa.attname').' as '.self::protect('column').',
|
||||
'.self::protect('indisunique').' as '.self::protect('unique').',
|
||||
'.self::protect('indisprimary').' as '.self::protect('primary').'
|
||||
FROM '.self::protect('pg_index','pi').'
|
||||
LEFT JOIN '.self::protect('pg_class','pcx').'
|
||||
ON '.self::protect('pi.indexrelid').' = '.self::protect('pcx.oid').'
|
||||
LEFT JOIN '.self::protect('pg_class','pci').'
|
||||
ON '.self::protect('pi.indrelid').' = '.self::protect('pci.oid').'
|
||||
LEFT JOIN '.self::protect('pg_attribute','pa').'
|
||||
ON '.self::protect('pa.attrelid').' = '.self::protect('pci.oid').'
|
||||
AND '.self::protect('pa.attnum').' = ANY('.self::protect('pi.indkey').')
|
||||
ORDER BY '.self::protect('pa.attnum').'
|
||||
';
|
||||
$indexes = self::$pdo->prepare($req)->execute()->fetchAll();
|
||||
|
||||
$keys = array();
|
||||
foreach($contraints as $constraint)
|
||||
{
|
||||
if($constraint['type'] == 'PRIMARY KEY')
|
||||
$constraint['type'] = 'PRIMARY';
|
||||
self::$keys[$constraint['table']][$constraint['name']]['type'] = $constraint['type'];
|
||||
self::$keys[$constraint['table']][$constraint['name']]['columns'][] = $constraint['column'];
|
||||
}
|
||||
foreach($indexes as $index)
|
||||
{
|
||||
if($index['unique'] == 't' || $index['primary'] == 't')
|
||||
continue;
|
||||
self::$keys[$constraint['table']][$index['name']]['type'] = 'INDEX';
|
||||
self::$keys[$constraint['table']][$index['name']]['columns'][] = $index['column'];
|
||||
}
|
||||
|
||||
return self::$keys[$table['name']];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,417 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
class BackendPhpPdo
|
||||
{
|
||||
|
||||
public static function getConfig($id)
|
||||
{
|
||||
switch($id)
|
||||
{
|
||||
case 'saveloadlist':
|
||||
$type = 'pgsql';
|
||||
$dsn = 'pgsql:dbname=wwwsqldesigner';
|
||||
$user = 'wwwsqldesigner';
|
||||
$pass = 'xxx';
|
||||
$table = 'wwwsqldesigner';
|
||||
return array($type,$dsn,$user,$pass,$table);
|
||||
case 'import':
|
||||
$type = 'pgsql';
|
||||
$dsn = 'pgsql:dbname=wwwsqldesigner;host=localhost';
|
||||
$type = 'mysql';
|
||||
$dsn = 'mysql:dbname=wwwsqldesigner;host=localhost';
|
||||
$user = 'wwwsqldesigner';
|
||||
$pass = 'xxx';
|
||||
return array($type,$dsn,$user,$pass);
|
||||
}
|
||||
}
|
||||
|
||||
public $what = '';
|
||||
# PDO
|
||||
public $type = NULL;
|
||||
public $pdo = NULL;
|
||||
# PDO STATEMENT
|
||||
public $pdo_statement = NULL;
|
||||
public $req = '';
|
||||
public $pos = 0;
|
||||
# EXCEPTION
|
||||
public $message = NULL;
|
||||
public $trace = array();
|
||||
# BENCH
|
||||
public $in = 0;
|
||||
public $out = 0;
|
||||
public static $benchs = array();
|
||||
|
||||
# Constructors
|
||||
public static function getPdo($type,$dsn,$user,$password)
|
||||
{
|
||||
$obj = new BackendPhpPdo();
|
||||
$obj->what = 'PDO';
|
||||
$obj->type = $type;
|
||||
try {
|
||||
$obj->pdo = new PDO($dsn,$user,$password);
|
||||
} catch( Exception $e ) {
|
||||
self::getException($e);
|
||||
}
|
||||
if(empty($obj->pdo))
|
||||
self::getException($obj);
|
||||
return $obj;
|
||||
}
|
||||
public static function getPdoStatement($pdo=NULL,$pdo_statement=NULL,$req='',$in=NULL)
|
||||
{
|
||||
$obj = new BackendPhpPdo();
|
||||
$obj->what = 'PDO_STATEMENT';
|
||||
$obj->pdo = $pdo;
|
||||
$obj->pdo_statement = $pdo_statement;
|
||||
$obj->req = $req;
|
||||
$obj->in = $in;
|
||||
if(empty($obj->pdo_statement))
|
||||
self::getException($obj);
|
||||
return $obj;
|
||||
}
|
||||
public static function getException($message)
|
||||
{
|
||||
$trace = debug_backtrace();
|
||||
if(is_object($message))
|
||||
{
|
||||
if($message instanceOf Exception)
|
||||
$trace = $message->getTrace();
|
||||
$message = $message->getMessage();
|
||||
}
|
||||
$obj = new BackendPhpPdo();
|
||||
$obj->what = 'EXCEPTION';
|
||||
$obj->message = $message;
|
||||
$obj->trace = $trace;
|
||||
header("HTTP/1.0 500 Internal Server Error");
|
||||
echo $obj->getMessage();
|
||||
die();
|
||||
}
|
||||
public static function getBench($in=NULL,$message='')
|
||||
{
|
||||
$t = microtime(true);
|
||||
if(is_null($in))
|
||||
return $t;
|
||||
$obj = new BackendPhpPdo();
|
||||
$obj->what = 'BENCH';
|
||||
$obj->in = $in;
|
||||
$obj->out = $t;
|
||||
$obj->message = $message;
|
||||
self::$benchs[] = $obj;
|
||||
return $t;
|
||||
}
|
||||
|
||||
# PDO
|
||||
public function prepare($req)
|
||||
{
|
||||
$in = self::getBench();
|
||||
$args = func_get_args();
|
||||
try {
|
||||
$pdo_statement = call_user_func_array(array($this->pdo,'prepare'),$args);
|
||||
} catch (Exception $e) {
|
||||
self::getException($e);
|
||||
}
|
||||
return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
|
||||
}
|
||||
public function query($req)
|
||||
{
|
||||
$in = self::getBench();
|
||||
$args = func_get_args();
|
||||
try {
|
||||
$pdo_statement = call_user_func_array(array($this->pdo,'query'),$args);
|
||||
} catch (Exception $e) {
|
||||
self::getException($e);
|
||||
}
|
||||
return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
|
||||
}
|
||||
public static $dones = array();
|
||||
public function getClass()
|
||||
{
|
||||
if(empty(self::$dones))
|
||||
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'AbstractLayer.php');
|
||||
AbstractLayer::$pdo = $this;
|
||||
|
||||
$class = 'Layer'.strtoupper($this->type);
|
||||
if(isset(self::$dones[$class]))
|
||||
return $class;
|
||||
|
||||
$file = dirname(__FILE__).DIRECTORY_SEPARATOR.$class.'.php';
|
||||
if(!file_exists($file))
|
||||
self::getException('Layer not implemented : '.$this->type);
|
||||
require_once($file);
|
||||
|
||||
self::$dones[$class] = 1;
|
||||
return $class;
|
||||
}
|
||||
public function getLayer()
|
||||
{
|
||||
$class = $this->getClass();
|
||||
$args = func_get_args();
|
||||
$method = str_replace(' ','',ucwords(str_replace('-',' ',array_shift($args))));
|
||||
if(!method_exists($class,$method))
|
||||
self::getException($class.'::'.$method.' not implemented');
|
||||
return call_user_func_array(array($class,$method),$args);
|
||||
}
|
||||
|
||||
# PDO STATEMENT
|
||||
public function execute()
|
||||
{
|
||||
$count = count(explode('?',$this->req))-1;
|
||||
if($count != $this->pos)
|
||||
self::getException('Not enough or too much bindValue in regards to the number of "?" : '.$this->req.' ('.$this->pos.';'.$count.')');
|
||||
$this->pos = 0;
|
||||
$args = func_get_args();
|
||||
$status = call_user_func_array(array($this->pdo_statement,'execute'),$args);
|
||||
self::getBench($this->in,'execute');
|
||||
if(!$status)
|
||||
self::getException($this);
|
||||
return $this;
|
||||
}
|
||||
public function fetchName()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$args[] = PDO::FETCH_ASSOC;
|
||||
$results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
|
||||
return $results;
|
||||
}
|
||||
public function fetchNum()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$args[] = PDO::FETCH_NUM;
|
||||
$results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
|
||||
return $results;
|
||||
}
|
||||
public function fetchOne()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
|
||||
foreach($results as $k=>$result)
|
||||
if(count($result) == 2)
|
||||
$results[$k] = $result[0];
|
||||
return $results;
|
||||
}
|
||||
public function fetchAll()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
|
||||
self::getBench($this->in,'fetch');
|
||||
return $results;
|
||||
}
|
||||
public function bindValue($value,$type)
|
||||
{
|
||||
switch($type)
|
||||
{
|
||||
case 'int': $type = PDO::PARAM_INT; break;
|
||||
case 'string': $type = PDO::PARAM_STR; break;
|
||||
case 'resource': $type = PDO::PARAM_LOB; break;
|
||||
default: self::getException('Unknown type : '.$type); break;
|
||||
}
|
||||
$this->pdo_statement->bindValue(++$this->pos,$value,$type);
|
||||
return $this;
|
||||
}
|
||||
|
||||
# General
|
||||
public function getMessage()
|
||||
{
|
||||
switch($this->what)
|
||||
{
|
||||
case 'EXCEPTION':
|
||||
$trace = array();
|
||||
foreach($this->trace as $e)
|
||||
{
|
||||
if(!isset($e['file'])) $e['file'] = '<i>user_func</i>';
|
||||
if(!isset($e['line'])) $e['line'] = '0';
|
||||
if(!isset($e['class'])) $e['class'] = '';
|
||||
if(!isset($e['type'])) $e['type'] = '';
|
||||
$trace[] = '<small>'.$e['file'].':'.$e['line'].'</small> '.$e['class'].$e['type'].$e['function'];
|
||||
}
|
||||
return '<strong>'.$this->message.'</strong>'."\n".'<hr />'."\n".implode('<br />'."\n",$trace);
|
||||
case 'PDO':
|
||||
if(empty($this->pdo))
|
||||
return 'Fail to connect.';
|
||||
$error = $this->pdo->errorInfo();
|
||||
return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
|
||||
case 'PDO_STATEMENT':
|
||||
if(empty($this->pdo_statement))
|
||||
$error = $this->pdo->errorInfo();
|
||||
else
|
||||
$error = $this->pdo_statement->errorInfo();
|
||||
return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
|
||||
case 'BENCH':
|
||||
return (round(($this->out-$this->in)*1000)/1000).'s '.$this->message;
|
||||
}
|
||||
}
|
||||
public function getTrace()
|
||||
{
|
||||
switch($this->what)
|
||||
{
|
||||
case 'EXCEPTION':
|
||||
return $this->trace;
|
||||
default:
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
public static function fetchDatatypes($file)
|
||||
{
|
||||
if(!file_exists($file))
|
||||
{
|
||||
$header = '<'.'?xml version="1.0" ?'.'>';
|
||||
$datatypes = '';
|
||||
}
|
||||
else
|
||||
{
|
||||
$datatypes = file($file);
|
||||
$header = array_shift($datatypes);
|
||||
$datatypes = implode('',$datatypes);
|
||||
}
|
||||
return array($header,$datatypes);
|
||||
}
|
||||
public static function buildXML($pdo)
|
||||
{
|
||||
list($header,$datatypes) = self::fetchDatatypes($pdo->getLayer('datatypes-file'));
|
||||
|
||||
$xml = array();
|
||||
$xml[] = $header;
|
||||
$xml[] = '<sql db="'.$pdo->type.'">';
|
||||
$xml[] = $datatypes;
|
||||
|
||||
$tables = $pdo->getLayer('tables');
|
||||
foreach($tables as $table)
|
||||
{
|
||||
$xmltable = '<table name="'.$table['name'].'">';
|
||||
if(!empty($table['comment']))
|
||||
$xmltable .= '<comment>'.$table['comment'].'</comment>';
|
||||
|
||||
$columns = $pdo->getLayer('columns',$table);
|
||||
foreach($columns as $column)
|
||||
{
|
||||
$xmlcolumn = '<row name="'.$column['name'].'" null="'.$column['null'].'" autoincrement="'.$column['autoincrement'].'">';
|
||||
$xmlcolumn .= '<datatype>'.strtoupper($column['type']).'</datatype>';
|
||||
$xmlcolumn .= '<default>'.$column['default'].'</default>';
|
||||
if(!empty($column['comment']))
|
||||
$xmlcolumn .= '<comment>'.$column['comment'].'</comment>';
|
||||
|
||||
$relations = $pdo->getLayer('relations',$table,$column);
|
||||
foreach($relations as $relation)
|
||||
$xmlcolumn .= '<relation table="'.$relation['table'].'" row="'.$relation['column'].'" />';
|
||||
|
||||
$xmlcolumn .= '</row>';
|
||||
$xmltable .= $xmlcolumn;
|
||||
}
|
||||
|
||||
$keys = $pdo->getLayer('keys',$table);
|
||||
foreach($keys as $name=>$key)
|
||||
{
|
||||
$xmlkey = '<key name="'.$name.'" type="'.$key['type'].'">';
|
||||
|
||||
foreach($key['columns'] as $column)
|
||||
if(!empty($column))
|
||||
$xmlkey .= '<part>'.$column.'</part>';
|
||||
|
||||
$xmlkey .= '</key>';
|
||||
$xmltable .= $xmlkey;
|
||||
}
|
||||
|
||||
$xmltable .= '</table>';
|
||||
$xml[] = $xmltable;
|
||||
}
|
||||
|
||||
$xml[] = '</sql>';
|
||||
|
||||
return implode("\n",$xml);
|
||||
}
|
||||
|
||||
public static function actionList()
|
||||
{
|
||||
list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
|
||||
$pdo = self::getPdo($type,$dsn,$user,$pass);
|
||||
$r = $pdo->getLayer('keywords',$table)->fetchOne();
|
||||
foreach($r as $keyword)
|
||||
echo $keyword."\n";
|
||||
}
|
||||
public static function actionSave($key='keyword')
|
||||
{
|
||||
$keyword = self::req($key);
|
||||
$data = file_get_contents("php://input");
|
||||
if (get_magic_quotes_gpc() || get_magic_quotes_runtime())
|
||||
$data = stripslashes($data);
|
||||
|
||||
list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
|
||||
$pdo = self::getPdo($type,$dsn,$user,$pass);
|
||||
|
||||
$r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
|
||||
if(count($r) > 0)
|
||||
$pdo->getLayer('keywords-update',$table,$keyword,$data);
|
||||
else
|
||||
$pdo->getLayer('keywords-insert',$table,$keyword,$data);
|
||||
|
||||
header("HTTP/1.0 201 Created");
|
||||
}
|
||||
public static function actionLoad($key='keyword')
|
||||
{
|
||||
$keyword = self::req($key);
|
||||
|
||||
list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
|
||||
$pdo = self::getPdo($type,$dsn,$user,$pass);
|
||||
|
||||
$r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
|
||||
if(count($r) > 0)
|
||||
{
|
||||
header("Content-type: text/xml");
|
||||
echo $r[0];
|
||||
}
|
||||
else
|
||||
header("HTTP/1.0 404 Not Found");
|
||||
die();
|
||||
}
|
||||
public static function actionImport()
|
||||
{
|
||||
ob_start();
|
||||
|
||||
list($type,$dsn,$user,$pass) = self::getConfig('import');
|
||||
$pdo = self::getPdo($type,$dsn,$user,$pass);
|
||||
|
||||
$xml = self::buildXML($pdo);
|
||||
|
||||
if(!ob_get_contents())
|
||||
header("Content-type: text/xml");
|
||||
echo $xml;
|
||||
die();
|
||||
}
|
||||
|
||||
public static function req($key)
|
||||
{
|
||||
return isset($_REQUEST[$key])?$_REQUEST[$key]:NULL;
|
||||
}
|
||||
public static function controler($key='action')
|
||||
{
|
||||
$action = self::req($key);
|
||||
switch(strtolower($action))
|
||||
{
|
||||
case 'list':
|
||||
return self::actionList();
|
||||
case 'save':
|
||||
return self::actionSave();
|
||||
case 'load':
|
||||
return self::actionLoad();
|
||||
case 'import':
|
||||
return self::actionImport();
|
||||
default:
|
||||
header("HTTP/1.0 501 Not Implemented");
|
||||
echo 'Action not implemented';
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function bench()
|
||||
{
|
||||
foreach(BackendPhpPdo::$benchs as $bench)
|
||||
echo $bench->getMessage().'<br />';
|
||||
}
|
||||
//register_shutdown_function('bench');
|
||||
|
||||
BackendPhpPdo::controler();
|
||||
|
||||
?>
|
||||
+1
-1
@@ -5,7 +5,7 @@ var CONFIG = {
|
||||
AVAILABLE_LOCALES: ["en", "fr", "de", "cs", "pl", "pt_BR", "es", "ru", "eo", "ja"],
|
||||
DEFAULT_LOCALE: "en",
|
||||
|
||||
AVAILABLE_BACKENDS: ["php-mysql","php-blank","php-file","php-sqlite"],
|
||||
AVAILABLE_BACKENDS: ["php-mysql", "php-blank", "php-file", "php-sqlite", "php-mysql+file", "php-postgresql", "php-pdo", "perl-file"],
|
||||
DEFAULT_BACKEND: ["php-mysql"],
|
||||
|
||||
RELATION_THICKNESS: 2,
|
||||
|
||||
Reference in New Issue
Block a user