You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
interface LtDbConnectionAdapter
|
||||
{
|
||||
/**
|
||||
* @todo 兼容使用Unix Domain Socket方式连接数据库(即:可以不指定port)
|
||||
*/
|
||||
public function connect($connConf);
|
||||
public function exec($sql, $connResource);
|
||||
public function query($sql, $connResource);
|
||||
public function lastInsertId($connResource);
|
||||
public function escape($sql, $connResource);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
class LtDbConnectionAdapterMysql implements LtDbConnectionAdapter
|
||||
{
|
||||
public function connect($connConf)
|
||||
{
|
||||
return mysql_connect($connConf["host"] . ":" . $connConf["port"], $connConf["username"], $connConf["password"]);
|
||||
}
|
||||
|
||||
public function exec($sql, $connResource)
|
||||
{
|
||||
return mysql_query($sql, $connResource) ? mysql_affected_rows($connResource) : false;
|
||||
}
|
||||
|
||||
public function query($sql, $connResource)
|
||||
{
|
||||
$result = mysql_query($sql, $connResource);
|
||||
$rows = array();
|
||||
while($row = mysql_fetch_assoc($result))
|
||||
{
|
||||
$rows[] = $row;
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function lastInsertId($connResource)
|
||||
{
|
||||
return mysql_insert_id($connResource);
|
||||
}
|
||||
|
||||
public function escape($sql, $connResource)
|
||||
{
|
||||
return mysql_real_escape_string($sql, $connResource);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
class LtDbConnectionAdapterMysqli implements LtDbConnectionAdapter
|
||||
{
|
||||
public function connect($connConf)
|
||||
{
|
||||
return new mysqli($connConf["host"], $connConf["username"], $connConf["password"], $connConf["dbname"], $connConf["port"]);
|
||||
}
|
||||
|
||||
public function exec($sql, $connResource)
|
||||
{
|
||||
$connResource->query($sql);
|
||||
return $connResource->affected_rows;
|
||||
}
|
||||
|
||||
public function query($sql, $connResource)
|
||||
{
|
||||
$rows = array();
|
||||
$result = $connResource->query($sql);
|
||||
while($row = $result->fetch_assoc())
|
||||
{
|
||||
$rows[] = $row;
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function lastInsertId($connResource)
|
||||
{
|
||||
return $connResource->insert_id;
|
||||
}
|
||||
|
||||
public function escape($sql, $connResource)
|
||||
{
|
||||
return mysqli_real_escape_string($connResource, $sql);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
class LtDbConnectionAdapterPdo implements LtDbConnectionAdapter
|
||||
{
|
||||
public function connect($connConf)
|
||||
{
|
||||
// $option = array(PDO::ATTR_PERSISTENT => true);
|
||||
if (isset($connConf['pconnect']) && true == $connConf['pconnect'])
|
||||
{
|
||||
$option[PDO::ATTR_PERSISTENT] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
$option[PDO::ATTR_PERSISTENT] = false;
|
||||
}
|
||||
switch ($connConf['adapter'])
|
||||
{
|
||||
case "pdo_mysql":
|
||||
$dsn = "mysql:host={$connConf['host']};dbname={$connConf['dbname']}";
|
||||
break;
|
||||
case "pdo_sqlite":
|
||||
$connConf["host"] = rtrim($connConf["host"], '\\/') . DIRECTORY_SEPARATOR;
|
||||
if (!is_dir($connConf["host"]))
|
||||
{
|
||||
if (!@mkdir($connConf["host"], 0777, true))
|
||||
{
|
||||
trigger_error("Can not create {$connConf['host']}");
|
||||
}
|
||||
}
|
||||
$dsn = "{$connConf['sqlite_version']}:{$connConf['host']}{$connConf['dbname']}";
|
||||
break;
|
||||
case "pdo_pgsql":
|
||||
$dsn = "pgsql:host={$connConf['host']} port={$connConf['port']} dbname={$connConf['dbname']} user={$connConf['username']} password={$connConf['password']}";
|
||||
break;
|
||||
case "odbc":
|
||||
$dsn = "odbc:" . $connConf["host"];
|
||||
break;
|
||||
}
|
||||
return new PDO($dsn, $connConf['username'], $connConf['password'], $option);
|
||||
}
|
||||
|
||||
public function exec($sql, $connResource)
|
||||
{
|
||||
return $connResource->exec($sql);
|
||||
}
|
||||
|
||||
public function query($sql, $connResource)
|
||||
{
|
||||
return $connResource->query($sql)->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @todo pgsql support
|
||||
*/
|
||||
public function lastInsertId($connResource)
|
||||
{
|
||||
return $connResource->lastInsertId();
|
||||
}
|
||||
|
||||
public function escape($sql, $connResource)
|
||||
{
|
||||
// quote返回值带最前面和最后面的单引号, 这里去掉, DbHandler中加
|
||||
return trim($connResource->quote($sql), "'");
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
class LtDbConnectionAdapterPgsql implements LtDbConnectionAdapter
|
||||
{
|
||||
public function connect($connConf)
|
||||
{
|
||||
if (isset($connConf['pconnect']) && true == $connConf['pconnect'])
|
||||
{
|
||||
$func = 'pg_pconnect';
|
||||
}
|
||||
else
|
||||
{
|
||||
$func = 'pg_connect';
|
||||
}
|
||||
return $func("host={$connConf['host']} port={$connConf['port']} user={$connConf['username']} password={$connConf['password']}");
|
||||
}
|
||||
|
||||
public function exec($sql, $connResource)
|
||||
{
|
||||
$result = pg_query($connResource, $sql);
|
||||
return pg_affected_rows($result);
|
||||
}
|
||||
|
||||
public function query($sql, $connResource)
|
||||
{
|
||||
$result = pg_query($connResource, $sql);
|
||||
return pg_fetch_all($result);
|
||||
}
|
||||
|
||||
// SELECT CURRVAL(
|
||||
// pg_get_serial_sequence('my_tbl_name','id_col_name'));"
|
||||
// ------------------------------------------------------
|
||||
// CREATE FUNCTION last_insert_id() RETURNS bigint AS $$
|
||||
// SELECT lastval();
|
||||
// $$ LANGUAGE SQL VOLATILE;
|
||||
public function lastInsertId($connResource)
|
||||
{
|
||||
$result = pg_query($connResource, "SELECT lastval()");
|
||||
$row = pg_fetch_array($result, 0, PGSQL_NUM);
|
||||
return $row[0];
|
||||
}
|
||||
|
||||
public function escape($sql, $connResource)
|
||||
{
|
||||
return pg_escape_string($sql);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Sqlite 预定义了类 SQLiteDatabase 本实现没有使用。
|
||||
* 这里使用的全部是过程函数。
|
||||
* 无论是函数还是类,本实现只支持sqlite的2.x系列版本。
|
||||
* php5.3新增扩展sqlite3用来支持3.x版本。
|
||||
* PDO则同时支持2.x和3.x版本。
|
||||
*/
|
||||
class LtDbConnectionAdapterSqlite implements LtDbConnectionAdapter
|
||||
{
|
||||
public function connect($connConf)
|
||||
{
|
||||
if (isset($connConf['pconnect']) && true == $connConf['pconnect'])
|
||||
{
|
||||
$func = 'sqlite_popen';
|
||||
}
|
||||
else
|
||||
{
|
||||
$func = 'sqlite_open';
|
||||
}
|
||||
$connConf["host"] = rtrim($connConf["host"], '\\/') . DIRECTORY_SEPARATOR;
|
||||
if(!is_dir($connConf["host"]))
|
||||
{
|
||||
if(!@mkdir($connConf["host"], 0777, true))
|
||||
{
|
||||
trigger_error("Can not create {$connConf['host']}");
|
||||
}
|
||||
}
|
||||
$error = '';
|
||||
$connResource = $func($connConf["host"] . $connConf["dbname"], 0666, $error);
|
||||
if (!$connResource)
|
||||
{
|
||||
trigger_error($error, E_USER_ERROR);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $connResource;
|
||||
}
|
||||
}
|
||||
|
||||
public function exec($sql, $connResource)
|
||||
{
|
||||
if(empty($sql))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
sqlite_exec($connResource, $sql);
|
||||
// echo '<pre>';
|
||||
// print_r(debug_backtrace());
|
||||
// debug_print_backtrace();
|
||||
// echo '</pre>';
|
||||
// delete from table 结果为0,原因未知。
|
||||
// 使用 delete from table where 1 能返回正确结果
|
||||
return sqlite_changes($connResource);
|
||||
}
|
||||
|
||||
public function query($sql, $connResource)
|
||||
{
|
||||
$result = sqlite_query($connResource, $sql, SQLITE_ASSOC);
|
||||
return sqlite_fetch_all($result, SQLITE_ASSOC);
|
||||
}
|
||||
|
||||
public function lastInsertId($connResource)
|
||||
{
|
||||
return sqlite_last_insert_rowid($connResource);
|
||||
}
|
||||
|
||||
public function escape($sql, $connResource)
|
||||
{
|
||||
return sqlite_escape_string($sql);
|
||||
}
|
||||
}
|
31
extend/app_alipay/lotusphp_runtime/DB/Adapter/SqlAdapter/DbSqlAdapter.php
Executable file
31
extend/app_alipay/lotusphp_runtime/DB/Adapter/SqlAdapter/DbSqlAdapter.php
Executable file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
interface LtDbSqlAdapter
|
||||
{
|
||||
/**
|
||||
* Return SQL statements
|
||||
*/
|
||||
public function setCharset($charset);
|
||||
public function setSchema($schema);
|
||||
|
||||
public function showSchemas($database);
|
||||
public function showTables($schema);
|
||||
public function showFields($table);
|
||||
|
||||
public function beginTransaction();
|
||||
public function commit();
|
||||
public function rollBack();
|
||||
|
||||
public function limit($limit, $offset);
|
||||
|
||||
/**
|
||||
* Retrive recordset
|
||||
*/
|
||||
public function getSchemas($queryResult);
|
||||
public function getTables($queryResult);
|
||||
public function getFields($queryResult);
|
||||
|
||||
/**
|
||||
* Parse SQL
|
||||
*/
|
||||
public function detectQueryType($sql);
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
class LtDbSqlAdapterMysql implements LtDbSqlAdapter
|
||||
{
|
||||
public function setCharset($charset)
|
||||
{
|
||||
return "SET NAMES " . str_replace('-', '', $charset);
|
||||
}
|
||||
public function setSchema($schema)
|
||||
{
|
||||
return "USE $schema";
|
||||
}
|
||||
|
||||
public function showSchemas($database)
|
||||
{
|
||||
return "SHOW DATABASES";
|
||||
}
|
||||
public function showTables($schema)
|
||||
{
|
||||
return "SHOW TABLES";
|
||||
}
|
||||
public function showFields($table)
|
||||
{
|
||||
return "DESCRIBE $table";
|
||||
}
|
||||
|
||||
public function beginTransaction()
|
||||
{
|
||||
return "START TRANSACTION";
|
||||
}
|
||||
public function commit()
|
||||
{
|
||||
return "COMMIT";
|
||||
}
|
||||
public function rollBack()
|
||||
{
|
||||
return "ROLLBACK";
|
||||
}
|
||||
|
||||
public function limit($limit, $offset)
|
||||
{
|
||||
return " LIMIT $limit OFFSET $offset";
|
||||
}
|
||||
public function getSchemas($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function getTables($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function getFields($queryResult)
|
||||
{
|
||||
foreach ($queryResult as $key => $value)
|
||||
{
|
||||
$fields[$value['Field']]['name'] = $value['Field'];
|
||||
$fields[$value['Field']]['type'] = $value['Type'];
|
||||
/*
|
||||
* not null is NO or empty, null is YES
|
||||
*/
|
||||
$fields[$value['Field']]['notnull'] = (bool) ($value['Null'] != 'YES');
|
||||
$fields[$value['Field']]['default'] = $value['Default'];
|
||||
$fields[$value['Field']]['primary'] = (strtolower($value['Key']) == 'pri');
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
public function detectQueryType($sql)
|
||||
{
|
||||
if (preg_match("/^\s*SELECT|^\s*EXPLAIN|^\s*SHOW|^\s*DESCRIBE/i", $sql))
|
||||
{
|
||||
$ret = 'SELECT';
|
||||
}
|
||||
else if (preg_match("/^\s*INSERT/i", $sql))
|
||||
{
|
||||
$ret = 'INSERT';
|
||||
}
|
||||
else if (preg_match("/^\s*UPDATE|^\s*DELETE|^\s*REPLACE/i", $sql))
|
||||
{
|
||||
$ret = 'CHANGE_ROWS';
|
||||
}
|
||||
else if (preg_match("/^\s*USE|^\s*SET/i", $sql))
|
||||
{
|
||||
$ret = 'SET_SESSION_VAR';
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret = 'OTHER';
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
class LtDbSqlAdapterPgsql implements LtDbSqlAdapter
|
||||
{
|
||||
public function setCharset($charset)
|
||||
{
|
||||
return "SET client_encoding TO '$charset'";
|
||||
}
|
||||
public function setSchema($schema)
|
||||
{
|
||||
return "SET search_path TO $schema";
|
||||
}
|
||||
|
||||
public function beginTransaction()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
public function commit()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
public function rollBack()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public function showSchemas($database)
|
||||
{
|
||||
|
||||
}
|
||||
public function showTables($schema)
|
||||
{
|
||||
return "SELECT case when n.nspname='public' then c.relname else n.nspname||'.'||c.relname end as relname
|
||||
FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)
|
||||
WHERE c.relkind = 'r'
|
||||
AND n.nspname NOT IN ('information_schema','pg_catalog')
|
||||
AND n.nspname NOT LIKE 'pg_temp%'
|
||||
AND n.nspname NOT LIKE 'pg_toast%'
|
||||
ORDER BY relname";
|
||||
}
|
||||
public function showFields($table)
|
||||
{
|
||||
return "SELECT a.attnum, a.attname AS field, t.typname AS type,
|
||||
format_type(a.atttypid, a.atttypmod) AS complete_type,
|
||||
a.attnotnull AS isnotnull,
|
||||
( SELECT 't' FROM pg_index
|
||||
WHERE c.oid = pg_index.indrelid
|
||||
AND pg_index.indkey[0] = a.attnum
|
||||
AND pg_index.indisprimary = 't') AS pri,
|
||||
(SELECT pg_attrdef.adsrc FROM pg_attrdef
|
||||
WHERE c.oid = pg_attrdef.adrelid
|
||||
AND pg_attrdef.adnum=a.attnum) AS default
|
||||
FROM pg_attribute a, pg_class c, pg_type t
|
||||
WHERE c.relname = '$table'
|
||||
AND a.attnum > 0
|
||||
AND a.attrelid = c.oid
|
||||
AND a.atttypid = t.oid
|
||||
ORDER BY a.attnum";
|
||||
}
|
||||
|
||||
public function limit($limit, $offset)
|
||||
{
|
||||
return " LIMIT $limit OFFSET $offset";
|
||||
}
|
||||
|
||||
public function getSchemas($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function getTables($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function getFields($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function detectQueryType($sql)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
120
extend/app_alipay/lotusphp_runtime/DB/Adapter/SqlAdapter/DbSqlAdapterSqlite.php
Executable file
120
extend/app_alipay/lotusphp_runtime/DB/Adapter/SqlAdapter/DbSqlAdapterSqlite.php
Executable file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
class LtDbSqlAdapterSqlite implements LtDbSqlAdapter
|
||||
{
|
||||
public function setCharset($charset)
|
||||
{
|
||||
// return 'PRAGMA encoding = "' . $charset . '"';
|
||||
return '';
|
||||
}
|
||||
public function setSchema($schema)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function beginTransaction()
|
||||
{
|
||||
return 'BEGIN TRANSACTION';
|
||||
}
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return 'COMMIT TRANSACTION';
|
||||
}
|
||||
|
||||
public function rollBack()
|
||||
{
|
||||
return 'ROLLBACK TRANSACTION';
|
||||
}
|
||||
|
||||
public function showSchemas($database)
|
||||
{
|
||||
//return "SHOW DATABASES";
|
||||
return '';
|
||||
}
|
||||
public function showTables($schema)
|
||||
{
|
||||
// 临时表及其索引不在 SQLITE_MASTER 表中而在 SQLITE_TEMP_MASTER 中出现
|
||||
return "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name";
|
||||
}
|
||||
public function showFields($table)
|
||||
{
|
||||
return "PRAGMA table_info('" . $table . "')";
|
||||
|
||||
}
|
||||
public function limit($limit, $offset)
|
||||
{
|
||||
return " LIMIT $limit OFFSET $offset";
|
||||
}
|
||||
|
||||
public function getSchemas($queryResult)
|
||||
{
|
||||
|
||||
}
|
||||
public function getTables($queryResult)
|
||||
{
|
||||
return $queryResult;
|
||||
}
|
||||
public function getFields($queryResult)
|
||||
{
|
||||
$fields = array();
|
||||
foreach ($queryResult as $key => $value)
|
||||
{
|
||||
// 字段名
|
||||
$fields[$value['name']]['name'] = $value['name'];
|
||||
// 字段类型
|
||||
$fulltype = $value['type'];
|
||||
$size = null;
|
||||
$precision = null;
|
||||
$scale = null;
|
||||
|
||||
if (preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/',$fulltype, $matches))
|
||||
{
|
||||
$type = $matches[1];
|
||||
$precision = $matches[2];
|
||||
$scale = $matches[3]; // aka precision
|
||||
}
|
||||
elseif (preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/',$fulltype, $matches))
|
||||
{
|
||||
$type = $matches[1];
|
||||
$size = $matches[2];
|
||||
}
|
||||
else
|
||||
{
|
||||
$type = $fulltype;
|
||||
}
|
||||
|
||||
$fields[$value['name']]['type'] = $type;
|
||||
/**
|
||||
* not null is 99, null is 0
|
||||
*/
|
||||
$fields[$value['name']]['notnull'] = (bool) ($value['notnull'] != 0);
|
||||
$fields[$value['name']]['default'] = $value['dflt_value'];
|
||||
$fields[$value['name']]['primary'] = (bool) ($value['pk'] == 1 && strtoupper($fulltype) == 'INTEGER');
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
public function detectQueryType($sql)
|
||||
{
|
||||
if (preg_match("/^\s*SELECT|^\s*PRAGMA/i", $sql))
|
||||
{
|
||||
$ret = 'SELECT';
|
||||
}
|
||||
else if (preg_match("/^\s*INSERT/i", $sql))
|
||||
{
|
||||
$ret = 'INSERT';
|
||||
}
|
||||
else if (preg_match("/^\s*UPDATE|^\s*DELETE|^\s*REPLACE/i", $sql))
|
||||
{
|
||||
$ret = 'CHANGE_ROWS';
|
||||
}
|
||||
else if (preg_match("/^\s*USE|^\s*SET/i", $sql))
|
||||
{
|
||||
$ret = 'SET_SESSION_VAR';
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret = 'OTHER';
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user