Initial commit,修正

This commit is contained in:
2025-07-13 19:37:38 +08:00
parent 38ce288e27
commit 65bbd06981
12384 changed files with 3985464 additions and 8 deletions
+358
View File
@@ -0,0 +1,358 @@
<?php
/** File PDO.class.php *
* Porting of native PHP 5.1 PDO *
* object usable with PHP 4.X.X *
* and PHP 5.0.X version. *
* ------------------------------------ *
*(C) Andrea Giammarchi [2005/10/19] *
* ____________________________________ */
// check and preserve native PDO driver, for PHP4 or PHP 5.0 users
if(!class_exists('PDO')) {
// SUPPORTED STATIC ENVIROMENT VARIABLES
define('PDO_ATTR_SERVER_VERSION', 4); // server version
define('PDO_ATTR_CLIENT_VERSION', 5); // client version
define('PDO_ATTR_SERVER_INFO', 6); // server informations
define('PDO_ATTR_PERSISTENT', 12); // connection mode, persistent or normal
// SUPPORTED STATIC PDO FETCH MODE VARIABLES
define('PDO_FETCH_ASSOC', 2); // such mysql_fetch_assoc
define('PDO_FETCH_NUM', 3); // such mysql_fetch_row
define('PDO_FETCH_BOTH', 4); // such mysql_fetch_array
define('PDO_FETCH_OBJ', 5); // such mysql_fetch_object
// UNSUPPORTED STATIC PDO FETCH MODE VARIABLES
define('PDO_FETCH_LAZY', 1); // usable but not supported, default is PDO_FETCH_BOTH and will be used
define('PDO_FETCH_BOUND', 6); // usable but not supported, default is PDO_FETCH_BOTH and will be used
/**
* Class PDO
* PostgreSQL, SQLITE and MYSQL PDO support for PHP 4.X.X or PHP 5.0.X users, compatible with PHP 5.1.0 (RC1).
*
* DESCRIPTION [directly from http://us2.php.net/manual/en/ref.pdo.php]
* The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.
* Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.
* Note that you cannot perform any database functions using the PDO extension by itself;
* you must use a database-specific PDO driver to access a database server.
*
* HOW TO USE
* To know how to use PDO driver and all its methods visit php.net wonderful documentation.
* http://us2.php.net/manual/en/ref.pdo.php
* In this class some methods are not available and actually this porting is only for MySQL, SQLITE and PostgreSQL.
*
* LIMITS
* For some reasons ( time and php used version with this class ) some PDO methods are not availables and
* someother are not totally supported.
*
* PDO :: UNSUPPORTED METHODS:
* - beginTransaction [ mysql 3 has not transaction and manage them is possible only with a direct BEGIN
* or COMMIT query ]
* - commit
* - rollback
*
* PDO :: NOT TOTALLY SUPPORTED METHODS:
* - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
* - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
* - lastInsertId [ only fo PostgreSQL , returns only pg_last_oid ]
*
* - - - - - - - - - - - - - - - - - - - -
*
* PDOStatement :: UNSUPPORTED METHODS:
* - bindColumn [ is not possible to undeclare a variable and using global scope is not
* really a good idea ]
*
* PDOStatement :: NOT TOTALLY SUPPORTED METHODS:
* - getAttribute [ accepts only PDO_ATTR_SERVER_INFO, PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION and PDO_ATTR_PERSISTENT attributes ]
* - setAttribute [ supports only PDO_ATTR_PERSISTENT modification ]
* - setFetchMode [ supports only PDO_FETCH_NUM, PDO_FETCH_ASSOC, PDO_FETCH_OBJ and
* PDO_FETCH_BOTH database reading mode ]
* ---------------------------------------------
* @Compatibility >= PHP 4
* @Dependencies PDO_mysql.class.php
* PDO_sqlite.class.php
* PDOStatement_mysql.class.php
* PDOStatement_sqlite.class.php
* @Author Andrea Giammarchi
* @Site http://www.devpro.it/
* @Mail andrea [ at ] 3site [ dot ] it
* @Date 2005/10/13
* @LastModified 2005/12/01 21:40
* @Version 0.1b - tested, supports only PostgreSQL, MySQL or SQLITE databases
*/
class PDO {
/** Modified on 2005/12/01 to support new PDO constants on PHP 5.1.X */
const FETCH_ASSOC = PDO_FETCH_ASSOC;
const FETCH_NUM = PDO_FETCH_NUM;
const FETCH_BOTH = PDO_FETCH_BOTH;
const FETCH_OBJ = PDO_FETCH_OBJ;
const FETCH_LAZY = PDO_FETCH_LAZY;
const FETCH_BOUND = PDO_FETCH_BOUND;
const ATTR_SERVER_VERSION = PDO_ATTR_SERVER_VERSION;
const ATTR_CLIENT_VERSION = PDO_ATTR_CLIENT_VERSION;
const ATTR_SERVER_INFO = PDO_ATTR_SERVER_INFO;
const ATTR_PERSISTENT = PDO_ATTR_PERSISTENT;
/**
* 'Private' variables:
* __driver:PDO_* Dedicated PDO database class
*/
var $__driver;
/**
* Public constructor
* http://us2.php.net/manual/en/function.pdo-construct.php
*/
function PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
$con = &$this->__getDNS($string_dsn);
if($con['dbtype'] === 'mysql') {
require_once('PDO_mysql.class.php');
if(isset($con['port']))
$con['host'] .= ':'.$con['port'];
$this->__driver = new PDO_mysql(
$con['host'],
$con['dbname'],
$string_username,
$string_password
);
}
elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
require_once('PDO_sqlite.class.php');
$this->__driver = new PDO_sqlite($con['dbname']);
}
elseif($con['dbtype'] === 'pgsql') {
require_once('PDO_pgsql.class.php');
$string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
if(isset($con['port']))
$string_dsn .= " port={$con['port']}";
$this->__driver = new PDO_pgsql($string_dsn);
}
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-begintransaction.php
*/
function beginTransaction() {
$this->__driver->beginTransaction();
}
/** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER
* Public method
* Calls database_close function.
* this->close( Void ):Boolean
* @Return Boolean True on success, false otherwise
*/
function close() {
return $this->__driver->close();
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-commit.php
*/
function commit() {
$this->__driver->commit();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-exec.php
*/
function exec($query) {
return $this->__driver->exec($query);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-errorcode.php
*/
function errorCode() {
return $this->__driver->errorCode();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-errorinfo.php
*/
function errorInfo() {
return $this->__driver->errorInfo();
}
/** NOT TOTALLY UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-getattribute.php
*/
function getAttribute($attribute) {
return $this->__driver->getAttribute($attribute);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-lastinsertid.php
*/
function lastInsertId() {
return $this->__driver->lastInsertId();
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-prepare.php
*/
function prepare($query, $array = Array()) {
return $this->__driver->prepare($query, $array = Array());
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-query.php
*/
function query($query) {
return $this->__driver->query($query);
}
/**
* Public method
* http://us2.php.net/manual/en/function.pdo-quote.php
*/
function quote($string) {
return $this->__driver->quote($string);
}
/** UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-rollback.php
*/
function rollBack() {
$this->__driver->rollBack();
}
/** NOT TOTALLY UNSUPPORTED
* Public method
* http://us2.php.net/manual/en/function.pdo-setattribute.php
*/
function setAttribute($attribute, $mixed) {
return $this->__driver->setAttribute($attribute, $mixed);
}
// PRIVATE METHOD [uncommented]
function __getDNS(&$string) {
$result = array();
$pos = strpos($string, ':');
$parameters = explode(';', substr($string, ($pos + 1)));
$result['dbtype'] = strtolower(substr($string, 0, $pos));
for($a = 0, $b = count($parameters); $a < $b; $a++) {
$tmp = explode('=', $parameters[$a]);
if(count($tmp) == 2)
$result[$tmp[0]] = $tmp[1];
else
$result['dbname'] = $parameters[$a];
}
return $result;
}
}
}
// If you have PHP 5.1 but want to test this class, declare PDO variables as _PDO variables
else {
/**
* Class _PDO
* (C) Andrea Giammarchi
* Please read PDO class comments to know more
*/
class _PDO {
const FETCH_ASSOC = PDO::FETCH_ASSOC;
const FETCH_NUM = PDO::FETCH_NUM;
const FETCH_BOTH = PDO::FETCH_BOTH;
const FETCH_OBJ = PDO::FETCH_OBJ;
const FETCH_LAZY = PDO::FETCH_LAZY;
const FETCH_BOUND = PDO::FETCH_BOUND;
const ATTR_SERVER_VERSION = PDO::ATTR_SERVER_VERSION;
const ATTR_CLIENT_VERSION = PDO::ATTR_CLIENT_VERSION;
const ATTR_SERVER_INFO = PDO::ATTR_SERVER_INFO;
const ATTR_PERSISTENT = PDO::ATTR_PERSISTENT;
var $__driver;
function _PDO($string_dsn, $string_username = '', $string_password = '', $array_driver_options = null) {
$con = &$this->__getDNS($string_dsn);
if($con['dbtype'] === 'mysql') {
require_once('PDO_mysql.class.php');
if(isset($con['port']))
$con['host'] .= ':'.$con['port'];
$this->__driver = new PDO_mysql(
$con['host'],
$con['dbname'],
$string_username,
$string_password
);
}
elseif($con['dbtype'] === 'sqlite2' || $con['dbtype'] === 'sqlite') {
require_once('PDO_sqlite.class.php');
$this->__driver = new PDO_sqlite($con['dbname']);
}
elseif($con['dbtype'] === 'pgsql') {
require_once('PDO_pgsql.class.php');
$string_dsn = "host={$con['host']} dbname={$con['dbname']} user={$string_username} password={$string_password}";
if(isset($con['port']))
$string_dsn .= " port={$con['port']}";
$this->__driver = new PDO_pgsql($string_dsn);
}
}
function beginTransaction() {
$this->__driver->beginTransaction();
}
function close() {
return $this->__driver->close();
}
function commit() {
$this->__driver->commit();
}
function exec($query) {
return $this->__driver->exec($query);
}
function errorCode() {
return $this->__driver->errorCode();
}
function errorInfo() {
return $this->__driver->errorInfo();
}
function getAttribute($attribute) {
return $this->__driver->getAttribute($attribute);
}
function lastInsertId() {
return $this->__driver->lastInsertId();
}
function prepare($query, $array = Array()) {
return $this->__driver->prepare($query, $array = Array());
}
function query($query) {
return $this->__driver->query($query);
}
function quote($string) {
return $this->__driver->quote($string);
}
function rollBack() {
$this->__driver->rollBack();
}
function setAttribute($attribute, $mixed) {
return $this->__driver->setAttribute($attribute, $mixed);
}
function __getDNS(&$string) {
$result = array();
$pos = strpos($string, ':');
$parameters = explode(';', substr($string, ($pos + 1)));
$result['dbtype'] = strtolower(substr($string, 0, $pos));
for($a = 0, $b = count($parameters); $a < $b; $a++) {
$tmp = explode('=', $parameters[$a]);
if(count($tmp) == 2)
$result[$tmp[0]] = $tmp[1];
else
$result['dbname'] = $parameters[$a];
}
return $result;
}
}
}
?>
+362
View File
@@ -0,0 +1,362 @@
<?php
/** File PDOStatement_mysql.class.php *
*(C) Andrea Giammarchi [2005/10/13] */
/**
* Class PDOStatement_mysql
* This class is used from class PDO_mysql to manage a MySQL database.
* Look at PDO.clas.php file comments to know more about MySQL connection.
* ---------------------------------------------
* @Compatibility >= PHP 4
* @Dependencies PDO.class.php
* PDO_mysql.class.php
* @Author Andrea Giammarchi
* @Site http://www.devpro.it/
* @Mail andrea [ at ] 3site [ dot ] it
* @Date 2005/10/13
* @LastModified 2006/01/29 09:30 [fixed execute bug]
* @Version 0.1b - tested
*/
class PDOStatement_mysql {
/**
* 'Private' variables:
* __connection:Resource Database connection
* __dbinfo:Array Array with 4 elements used to manage connection
* __persistent:Boolean Connection mode, is true on persistent, false on normal (deafult) connection
* __query:String Last query used
* __result:Resource Last result from last query
* __fetchmode:Integer constant PDO_FETCH_* result mode
* __errorCode:String Last error string code
* __errorInfo:Array Last error informations, code, number, details
* __boundParams:Array Stored bindParam
*/
var $__connection;
var $__dbinfo;
var $__persistent = false;
var $__query = '';
var $__result = null;
var $__fetchmode = PDO::FETCH_BOTH;
var $__errorCode = '';
var $__errorInfo = Array('');
var $__boundParams = Array();
/**
* Public constructor:
* Called from PDO to create a PDOStatement for this database
* new PDOStatement_sqlite( &$__query:String, &$__connection:Resource, $__dbinfo:Array )
* @Param String query to prepare
* @Param Resource database connection
* @Param Array 4 elements array to manage connection
*/
function PDOStatement_mysql(&$__query, &$__connection, &$__dbinfo) {
$this->__query = &$__query;
$this->__connection = &$__connection;
$this->__dbinfo = &$__dbinfo;
}
/**
* Public method:
* Replace ? or :named values to execute prepared query
* this->bindParam( $mixed:Mixed, &$variable:Mixed, $type:Integer, $lenght:Integer ):Void
* @Param Mixed Integer or String to replace prepared value
* @Param Mixed variable to replace
* @Param Integer this variable is not used but respects PDO original accepted parameters
* @Param Integer this variable is not used but respects PDO original accepted parameters
*/
function bindParam($mixed, &$variable, $type = null, $lenght = null) {
if(is_string($mixed))
$this->__boundParams[$mixed] = $variable;
else
array_push($this->__boundParams, $variable);
}
/**
* Public method:
* Checks if query was valid and returns how may fields returns
* this->columnCount( void ):Void
*/
function columnCount() {
$result = 0;
if(!is_null($this->__result))
$result = mysql_num_fields($this->__result);
return $result;
}
/**
* Public method:
* Returns a code rappresentation of an error
* this->errorCode( void ):String
* @Return String String rappresentation of the error
*/
function errorCode() {
return $this->__errorCode;
}
/**
* Public method:
* Returns an array with error informations
* this->errorInfo( void ):Array
* @Return Array Array with 3 keys:
* 0 => error code
* 1 => error number
* 2 => error string
*/
function errorInfo() {
return $this->__errorInfo;
}
/**
* Public method:
* Excecutes a query and returns true on success or false.
* this->exec( $array:Array ):Boolean
* @Param Array If present, it should contain all replacements for prepared query
* @Return Boolean true if query has been done without errors, false otherwise
*/
function execute($array = Array()) {
if(count($this->__boundParams) > 0)
$array = &$this->__boundParams;
$__query = $this->__query;
if(count($array) > 0) {
foreach($array as $k => $v) {
if(!is_int($k) || substr($k, 0, 1) === ':') {
if(!isset($tempf))
$tempf = $tempr = array();
array_push($tempf, $k);
array_push($tempr, '"'.mysql_escape_string($v).'"');
}
else {
$parse = create_function('$v', 'return \'"\'.mysql_escape_string($v).\'"\';');
$__query = preg_replace("/(\?)/e", '$parse($array[$k++]);', $__query);
break;
}
}
if(isset($tempf)) {
foreach ($tempf as $k=>$v) {
$search[$k] = '/' . preg_quote($tempf[$k],'`') . '\b/';
}
$__query = preg_replace($search, $tempr, $__query);
//$__query = str_replace($tempf, $tempr, $__query);
}
}
if(is_null($this->__result = &$this->__uquery($__query)))
$keyvars = false;
else
$keyvars = true;
$this->__boundParams = array();
return $keyvars;
}
/**
* Public method:
* Returns, if present, next row of executed query or false.
* this->fetch( $mode:Integer, $cursor:Integer, $offset:Integer ):Mixed
* @Param Integer PDO_FETCH_* constant to know how to read next row, default PDO_FETCH_BOTH
* NOTE: if $mode is omitted is used default setted mode, PDO_FETCH_BOTH
* @Param Integer this variable is not used but respects PDO original accepted parameters
* @Param Integer this variable is not used but respects PDO original accepted parameters
* @Return Mixed Next row of executed query or false if there is nomore.
*/
function fetch($mode = PDO_FETCH_ASSOC, $cursor = null, $offset = null) {
if(func_num_args() == 0)
$mode = &$this->__fetchmode;
$result = false;
if(!is_null($this->__result)) {
switch($mode) {
case PDO::FETCH_NUM:
$result = mysql_fetch_row($this->__result);
break;
case PDO::FETCH_ASSOC:
$result = mysql_fetch_assoc($this->__result);
break;
case PDO::FETCH_OBJ:
$result = mysql_fetch_object($this->__result);
break;
case PDO::FETCH_BOTH:
default:
$result = mysql_fetch_array($this->__result);
break;
}
}
if(!$result)
$this->__result = null;
return $result;
}
/**
* Public method:
* Returns an array with all rows of executed query.
* this->fetchAll( $mode:Integer ):Array
* @Param Integer PDO_FETCH_* constant to know how to read all rows, default PDO_FETCH_BOTH
* NOTE: this doesn't work as fetch method, then it will use always PDO_FETCH_BOTH
* if this param is omitted
* @Return Array An array with all fetched rows
*/
function fetchAll($mode = PDO_FETCH_ASSOC) {
$result = array();
if(!is_null($this->__result)) {
switch($mode) {
case PDO::FETCH_NUM:
while($r = mysql_fetch_row($this->__result))
array_push($result, $r);
break;
case PDO::FETCH_ASSOC:
while($r = mysql_fetch_assoc($this->__result))
array_push($result, $r);
break;
case PDO::FETCH_OBJ:
while($r = mysql_fetch_object($this->__result))
array_push($result, $r);
break;
case PDO::FETCH_BOTH:
default:
while($r = mysql_fetch_array($this->__result))
array_push($result, $r);
break;
}
}
$this->__result = null;
return $result;
}
/**
* Public method:
* Returns, if present, first column of next row of executed query
* this->fetchSingle( void ):Mixed
* @Return Mixed Null or next row's first column
*/
function fetchSingle() {
$result = null;
if(!is_null($this->__result)) {
$result = @mysql_fetch_row($this->__result);
if($result)
$result = $result[0];
else
$this->__result = null;
}
return $result;
}
function fetchColumn($column=0) {
$row = mysql_fetch_row($this->__result);
return $row[$column];
}
/**
* Public method:
* Returns number of last affected database rows
* this->rowCount( void ):Integer
* @Return Integer number of last affected rows
* NOTE: works with INSERT, UPDATE and DELETE query type
*/
function rowCount() {
return mysql_affected_rows($this->__connection);
}
// NOT TOTALLY SUPPORTED PUBLIC METHODS
/**
* Public method:
* Quotes correctly a string for this database
* this->getAttribute( $attribute:Integer ):Mixed
* @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
* PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION,
* PDO_ATTR_PERSISTENT ]
* @Return Mixed correct information or false
*/
function getAttribute($attribute) {
$result = false;
switch($attribute) {
case PDO_ATTR_SERVER_INFO:
$result = mysql_get_host_info($this->__connection);
break;
case PDO_ATTR_SERVER_VERSION:
$result = mysql_get_server_info($this->__connection);
break;
case PDO_ATTR_CLIENT_VERSION:
$result = mysql_get_client_info();
break;
case PDO_ATTR_PERSISTENT:
$result = $this->__persistent;
break;
}
return $result;
}
/**
* Public method:
* Sets database attributes, in this version only connection mode.
* this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
* @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
* @Param Mixed value for PDO_* constant, in this case a Boolean value
* true for permanent connection, false for default not permament connection
* @Return Boolean true on change, false otherwise
*/
function setAttribute($attribute, $mixed) {
$result = false;
if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
$result = true;
$this->__persistent = (boolean) $mixed;
mysql_close($this->__connection);
if($this->__persistent === true)
$this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
else
$this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
mysql_select_db($this->__dbinfo[3], $this->__connection);
}
return $result;
}
/**
* Public method:
* Sets default fetch mode to use with this->fetch() method.
* this->setFetchMode( $mode:Integer ):Boolean
* @Param Integer PDO_FETCH_* constant to use while reading an execute query with fetch() method.
* NOTE: PDO_FETCH_LAZY and PDO_FETCH_BOUND are not supported
* @Return Boolean true on change, false otherwise
*/
function setFetchMode($mode) {
$result = false;
switch($mode) {
case PDO_FETCH_NUM:
case PDO_FETCH_ASSOC:
case PDO_FETCH_OBJ:
case PDO_FETCH_BOTH:
$result = true;
$this->__fetchmode = &$mode;
break;
}
return $result;
}
// UNSUPPORTED PUBLIC METHODS
function bindColumn($mixewd, &$param, $type = null, $max_length = null, $driver_option = null) {
return false;
}
function __setErrors($er) {
if(!is_resource($this->__connection)) {
$errno = mysql_errno();
$errst = mysql_error();
}
else {
$errno = mysql_errno($this->__connection);
$errst = mysql_error($this->__connection);
}
$this->__errorCode = &$er;
$this->__errorInfo = Array($this->__errorCode, $errno, $errst);
$this->__result = null;
}
function __uquery(&$query) {
if(!@$query = mysql_query($query, $this->__connection)) {
$this->__setErrors('SQLER');
$query = null;
}
return $query;
}
}
?>
+265
View File
@@ -0,0 +1,265 @@
<?php
/** File PDO_mysql.class.php *
*(C) Andrea Giammarchi [2005/10/13] */
// Requires PDOStatement_mysql.class.php , drived by PDO.class.php file
require_once('PDOStatement_mysql.class.php');
/**
* Class PDO_mysql
* This class is used from class PDO to manage a MySQL database.
* Look at PDO.clas.php file comments to know more about MySQL connection.
* ---------------------------------------------
* @Compatibility >= PHP 4
* @Dependencies PDO.class.php
* PDOStatement_mysql.class.php
* @Author Andrea Giammarchi
* @Site http://www.devpro.it/
* @Mail andrea [ at ] 3site [ dot ] it
* @Date 2005/10/13
* @LastModified 2005/18/14 12:30
* @Version 0.1 - tested
*/
class PDO_mysql {
/**
* 'Private' variables:
* __connection:Resource Database connection
* __dbinfo:Array Array with 4 elements used to manage connection
* __persistent:Boolean Connection mode, is true on persistent, false on normal (deafult) connection
* __errorCode:String Last error code
* __errorInfo:Array Detailed errors
*/
var $__connection;
var $__dbinfo;
var $__persistent = false;
var $__errorCode = '';
var $__errorInfo = Array('');
/**
* Public constructor:
* Checks connection and database selection
* new PDO_mysql( &$host:String, &$db:String, &$user:String, &$pass:String )
* @Param String host with or without port info
* @Param String database name
* @Param String database user
* @Param String database password
*/
function PDO_mysql(&$host, &$db, &$user, &$pass) {
if(!@$this->__connection = &mysql_connect($host, $user, $pass))
$this->__setErrors('DBCON');
else {
if(!@mysql_select_db($db, $this->__connection))
$this->__setErrors('DBER');
else
$this->__dbinfo = Array($host, $user, $pass, $db);
}
}
/** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER
* Public method
* Calls mysql_close function.
* this->close( Void ):Boolean
* @Return Boolean True on success, false otherwise
*/
function close() {
$result = is_resource($this->__connection);
if($result) {
mysql_close($this->__connection);
}
return $result;
}
/**
* Public method:
* Returns a code rappresentation of an error
* this->errorCode( void ):String
* @Return String String rappresentation of the error
*/
function errorCode() {
return $this->__errorCode;
}
/**
* Public method:
* Returns an array with error informations
* this->errorInfo( void ):Array
* @Return Array Array with 3 keys:
* 0 => error code
* 1 => error number
* 2 => error string
*/
function errorInfo() {
return $this->__errorInfo;
}
/**
* Public method:
* Excecutes a query and returns affected rows
* this->exec( $query:String ):Mixed
* @Param String query to execute
* @Return Mixed Number of affected rows or false on bad query.
*/
function exec($query) {
$result = 0;
if(!is_null($this->__uquery($query)))
$result = mysql_affected_rows($this->__connection);
if(is_null($result))
$result = false;
return $result;
}
/**
* Public method:
* Returns last inserted id
* this->lastInsertId( void ):Number
* @Return Number Last inserted id
*/
function lastInsertId() {
$id = mysql_insert_id($this->__connection);
if ($id > 0) {
return $id;
} else {
$query = $this->prepare('SELECT last_insert_id()');
$query->execute();
return $query->fetchColumn();
}
}
/**
* Public method:
* Returns a new PDOStatement
* this->prepare( $query:String, $array:Array ):PDOStatement
* @Param String query to prepare
* @Param Array this variable is not used but respects PDO original accepted parameters
* @Return PDOStatement new PDOStatement to manage
*/
function prepare($query, $array = Array()) {
return new PDOStatement_mysql($query, $this->__connection, $this->__dbinfo);
}
/**
* Public method:
* Executes directly a query and returns an array with result or false on bad query
* this->query( $query:String ):Mixed
* @Param String query to execute
* @Return Mixed false on error, array with all info on success
*/
function query($query) {
$query = @mysql_unbuffered_query($query, $this->__connection);
if($query) {
$result = Array();
while($r = mysql_fetch_assoc($query))
array_push($result, $r);
}
else {
$result = false;
$this->__setErrors('SQLER');
}
return $result;
}
/**
* Public method:
* Quotes correctly a string for this database
* this->quote( $string:String ):String
* @Param String string to quote
* @Return String a correctly quoted string
*/
function quote($string) {
return ('"'.mysql_escape_string($string).'"');
}
// NOT TOTALLY SUPPORTED PUBLIC METHODS
/**
* Public method:
* Quotes correctly a string for this database
* this->getAttribute( $attribute:Integer ):Mixed
* @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
* PDO_ATTR_SERVER_VERSION,
* PDO_ATTR_CLIENT_VERSION,
* PDO_ATTR_PERSISTENT ]
* @Return Mixed correct information or false
*/
function getAttribute($attribute) {
$result = false;
switch($attribute) {
case PDO_ATTR_SERVER_INFO:
$result = mysql_get_host_info($this->__connection);
break;
case PDO_ATTR_SERVER_VERSION:
$result = mysql_get_server_info($this->__connection);
break;
case PDO_ATTR_CLIENT_VERSION:
$result = mysql_get_client_info();
break;
case PDO_ATTR_PERSISTENT:
$result = $this->__persistent;
break;
}
return $result;
}
/**
* Public method:
* Sets database attributes, in this version only connection mode.
* this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
* @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
* @Param Mixed value for PDO_* constant, in this case a Boolean value
* true for permanent connection, false for default not permament connection
* @Return Boolean true on change, false otherwise
*/
function setAttribute($attribute, $mixed) {
$result = false;
if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
$result = true;
$this->__persistent = (boolean) $mixed;
mysql_close($this->__connection);
if($this->__persistent === true)
$this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
else
$this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
mysql_select_db($this->__dbinfo[3], $this->__connection);
}
return $result;
}
// UNSUPPORTED PUBLIC METHODS
function beginTransaction() {
return false;
}
function commit() {
return false;
}
function rollBack() {
return false;
}
// PRIVATE METHODS [ UNCOMMENTED ]
function __setErrors($er) {
if(!is_resource($this->__connection)) {
$errno = mysql_errno();
$errst = mysql_error();
}
else {
$errno = mysql_errno($this->__connection);
$errst = mysql_error($this->__connection);
}
$this->__errorCode = &$er;
$this->__errorInfo = Array($this->__errorCode, $errno, $errst);
}
function __uquery(&$query) {
if(!@$query = mysql_query($query, $this->__connection)) {
$this->__setErrors('SQLER');
$query = null;
}
return $query;
}
}
?>
+864
View File
@@ -0,0 +1,864 @@
<?php
/**
*
* @author Fm453
* @remark 数据模型批处理类
*/
defined('HI_FM') or exit("Access Denied");
global $_FM;
global $_FM_CORE;
$_FM_CORE = dirname(__FILE__).'/';
class dataModel
{
protected $siteid;
protected $name; //表名(并非完整的数据表名)
protected $sourceTable; //源表
//表命名规范:主系统前缀_本商城程序前缀_功能类_具体表(表名_区分字符)_表类型后缀
protected $self; //模型主表;仅允许系统级的生成(最低到shop级别)
protected $form; //模型实例表(根据模型生成不同的实例)
protected $param; //模型关联参数(同一模型,对应不同站点、公号、实例时,参数可分别设置)
protected $order; //实例产生的数据提交表单
protected $addon; //模型表单附加参数表(附加参数与主表关联参数冲突时,附加参数优先)
protected $data; //收集的数据表
protected $attr; //数据属性表(用于对数据进行细分场景隔离的规则微调——因为该规则默认会在关联参数表或其他设置中事先声明;同一份数据,不同场景下可见性、读写权限等可不同-极少用到)
protected $pre; //表预编辑操作记录
protected $dbPrefix;//系统统一规定的表前缀
protected $tablePrefix;//内部规则约定的表功能前缀(用于对数据表文件进行文件夹归档保存等场景使用)
protected $tableType;//表类型(用于规定表后缀)
protected $tableName;//综合prefix\name\type拼接格式化的表名称
protected $tableTitle; //表备注(comment)
protected $sql = array();
protected $tables = array();//表SQL的配置集
protected $type = array();//表归属类(用于后台一些统计时作归纳之用)
/*
数据库前缀
*/
function db_prefix($prefix)
{
return $prefix;
}
/*
数据表后缀集
$comment 是否带注释,默认否
*/
function table_suffix($comment=null)
{
if(!$comment){
return ['self','form','param','order','addon','data','attr','pre'];
}else{
return [
'self'=>'主表',
'form'=>'实例表',
'param'=>'主参数表',
'order'=>'数据提交/收集表单表',
'addon'=>'附加参数表',
'data'=>'具体数据表',
'attr'=>'数据属性微调表',
'pre'=>'数据表预编辑操作记录',
];
}
}
/*
数据表过滤校验字段集
$suffix 表后缀类型
*/
function table_fileds($suffix)
{
switch($suffix) {
case 'self':
$fields = ['title','keywords','displayorder','status','deleted','statuscode'];
break;
case 'form':
$fields = ['s_sn','uid','title','value','displayorder','status','deleted','statuscode'];
break;
case 'param':
$fields = ['s_sn','f_sn','uid','setfor','title','key','value','displayorder','status','deleted','statuscode'];
break;
case 'order':
$fields = ['s_sn','f_sn','mid','fid','title','value','displayorder','status','deleted','statuscode'];
break;
case 'addon':
$fields = ['s_sn','f_sn','o_sn','mid','fid','setfor','title','key','value','displayorder','status','deleted','statuscode'];
break;
case 'data':
$fields = ['s_sn','f_sn','o_sn','mid','fid','setfor','title','key','value','displayorder','status','deleted','statuscode'];
break;
case 'attr':
$fields = ['t_sn','uid','mid','fid','suffix','setfor','title','key','value','displayorder','status','deleted','statuscode'];
break;
case 'pre':
$fields = ['tid','uid','mid','fid','suffix','setfor','value','displayorder','status','deleted','statuscode'];
break;
default:
$fields = ['id','sn','siteid','platid','uniacid','shopid','createtime','updatetime','update_at'];
break;
}
return $fields;
}
/*
数据过滤校验
*/
function data_filter($dataPost)
{
$newData = array();
//需要直接过滤掉的部分
$keys = ['id','siteid','createtime','updatetime','update_at'];
foreach($keys as $key){
if(isset($dataPost[$key])){
unset($dataPost[$key]);
}
}
//需要从GET中取得的字段,此处也需过滤掉
$keys = ['uniacid','platid','shopid'];
foreach($keys as $key){
if(isset($dataPost[$key])){
unset($dataPost[$key]);
}
}
//格式化为整型数字并保留的字段
$keys = ['displayorder','status','statuscode','deleted'];
foreach($keys as $key){
if(isset($dataPost[$key])){
$dataPost[$key] = intval($dataPost[$key]);
$newData[$key] = intval($dataPost[$key]);
}
}
//格式化为整型数字但不保留的字段
$keys = ['partnerid','uid','mid','fid'];
foreach($keys as $key){
if(isset($dataPost[$key])){
$newData[$key] = intval($dataPost[$key]);
unset($dataPost[$key]);
}
}
//约定SQL命令辅助(sql_开头)
$keys = ['orders','limits'];
foreach($keys as $key){
if(isset($dataPost['sql_'.$key]) && is_array($dataPost['sql_'.$key])) {
$newData['sqls'][$key] = $dataPost['sql_'.$key];
unset($dataPost['sql_'.$key]);
}else{
$newData['sqls'][$key] = array();
}
}
//约定搜索条件辅助(searchings数组)
/*
@status 指定状态(数据是否启用)(0,1)
@deleted 指定状态(数据是否删除)(0,1)
@keyword 指定要匹配的关键词(模糊检索)
@filter 用于设定数据过滤条件(数组集,要匹配的字段=》字段值)($col=>$val)
@plat 指定跨平台搜索
*/
$keys = ['status','deleted','keyword','filter','plat'];
$newData['searching'] = array();
foreach($keys as $key){
if(isset($dataPost['searching'][$key])) {
$newData['searching'][$key] = $dataPost['searching'][$key];
unset($dataPost['searching'][$key]);
}
}
$hasSearchAddons = (isset($dataPost['searching']) && count($dataPost['searching'])>0) ? true : false;
if($hasSearchAddons){
$newData['searching']['addons'] = array();
foreach($dataPost['searching'] as $key => $value){
$newData['searching']['addons'][$key] = $value;
unset($dataPost['searching'][$key]);
}
}
unset($dataPost['searching']);
//初始化赋值 TBD
$keys = ['statuscode'=>64];
//剩余部分
$newData['addons'] = array();
foreach($dataPost as $key => $v){
$newData['addons'][$key] = $v;
}
return $newData;
}
/*
根据表功能类型进行数据格式化
*/
function data_format($data,$suffix)
{
global $_FM;
$newData = array();
switch($suffix){
case 'self':
//取各类型表字段
$columns1 = $this->table_fileds($suffix);
$columns2 = $this->table_fileds('');
//去除非字段集内的部分
foreach($data as $key => $v){
if(in_array($key,$columns1) || in_array($key,$columns2)){
$newData[$key] = $data[$key];
}
}
//进一步整理
$newData['siteid'] = $_FM['apiCert']['siteId'];
$newData['platid'] = $_FM['platid'];
$newData['uniacid'] = $_FM['uniacid'];
$newData['shopid'] = $_FM['shopid'];
break;
case 'form':
//取各类型表字段
$columns1 = $this->table_fileds($suffix);
$columns2 = $this->table_fileds('');
//去除非字段集内的部分
foreach($data as $key => $v){
if(in_array($key,$columns1) || in_array($key,$columns2)){
$newData[$key] = $data[$key];
}
}
//进一步整理
$newData['siteid'] = $_FM['apiCert']['siteId'];
$newData['platid'] = $_FM['platid'];
$newData['uniacid'] = $_FM['uniacid'];
$newData['shopid'] = $_FM['shopid'];
break;
case 'order':
//取各类型表字段
$columns1 = $this->table_fileds($suffix);
$columns2 = $this->table_fileds('');
//去除非字段集内的部分
foreach($data as $key => $v){
if(in_array($key,$columns1) || in_array($key,$columns2)){
$newData[$key] = $data[$key];
}
}
//进一步整理
$newData['siteid'] = $_FM['apiCert']['siteId'];
$newData['platid'] = $_FM['platid'];
$newData['uniacid'] = $_FM['uniacid'];
$newData['shopid'] = $_FM['shopid'];
break;
case 'param':
//要在此处格式化掉sn等数据
$reset = ['sn','s_sn','f_sn','o_sn'];
foreach($data as $key => $value)
{
if(in_array($key,$reset)) {
unset($data[$key]);
}
}
foreach($data as $key => $value)
{
$temp = array();
$setfor = $key; //析取setfor
$temp['setfor'] = $setfor;
if(is_array($value))
{
//仍是数组,继续
foreach($value as $k => $v)
{
$title = $k; //析取title
$temp['title'] = $k;
if(is_array($v))
{
//仍是数组,继续
foreach($v as $kk => $vv){
$temp['key'] = $kk; //析取key
$temp['value'] = serialize($vv); //不再往下构造了——三级数组保存式(setfor-title-key=>value)
$newData[] = $temp;
}
}else{
$temp['key'] = $k;
$temp['value'] = serialize($v);
$newData[] = $temp;
}
}
}else{
$temp['title'] = $setfor;
$temp['key'] = $setfor;
$temp['value'] = serialize($value);
$newData[] = $temp;
}
}
break;
case 'addon':
//要在此处格式化掉sn等数据
$reset = ['sn','s_sn','f_sn','o_sn','partnerid','mid','fid'];
foreach($data as $key => $value)
{
if(in_array($key,$reset)) {
unset($data[$key]);
}
}
foreach($data as $key => $value)
{
$temp = array();
$setfor = $key; //析取setfor
$temp['setfor'] = $setfor;
if(is_array($value))
{
//仍是数组,继续
foreach($value as $k => $v)
{
$title = $k; //析取title
$temp['title'] = $k;
if(is_array($v))
{
//仍是数组,继续
foreach($v as $kk => $vv){
$temp['key'] = $kk; //析取key
$temp['value'] = serialize($vv); //不再往下构造了——三级数组保存式(setfor-title-key=>value)
$newData[] = $temp;
}
}else{
$temp['key'] = $k;
$temp['value'] = serialize($v);
$newData[] = $temp;
}
}
}else{
$temp['title'] = $setfor;
$temp['key'] = $setfor;
$temp['value'] = serialize($value);
$newData[] = $temp;
}
}
break;
case 'data':
//要在此处格式化掉sn等数据
$reset = ['sn'];
foreach($data as $key => $value)
{
if(in_array($key,$reset)) {
unset($data[$key]);
}
}
foreach($data as $key => $value)
{
$temp = array();
$setfor = $key; //析取setfor
$temp['setfor'] = $setfor;
if(is_array($value))
{
//仍是数组,继续
foreach($value as $k => $v)
{
$title = $k; //析取title
$temp['title'] = $k;
if(is_array($v))
{
//仍是数组,继续
foreach($v as $kk => $vv){
$temp['key'] = $kk; //析取key
$temp['value'] = serialize($vv); //不再往下构造了——三级数组保存式(setfor-title-key=>value)
$newData[] = $temp;
}
}else{
$temp['key'] = $k;
$temp['value'] = serialize($v);
$newData[] = $temp;
}
}
}else{
$temp['title'] = $setfor;
$temp['key'] = $setfor;
$temp['value'] = serialize($value);
$newData[] = $temp;
}
}
break;
case 'attr':
break;
default:
break;
}
return $newData;
}
/*
SQL命令生成
根据参数生成某类表的SQL命令并返回
*/
function get_sql_all($params)
{
global $_FM;
$sqls = array();
$dbPrefix = $params['dbPrefix'];
$tablePrefix = $params['tablePrefix'];
$name = $params['name'];
$tableTitle = $params['tableTitle'];
$type = $params['type'];
$tableTypes = $this->table_suffix();
foreach($tableTypes as $k){
$sqls[$k] = $this->get_sql($dbPrefix,$tablePrefix,$name,$k,$tableTitle,$type);
}
$result = '';
foreach($sqls as $k => $sql){
$columns = $sqls[$k]['columns'];
unset($columns['id']); //将id字段排除(新表创建时自动添加了)
$indexes = $sqls[$k]['indexes'];
unset($indexes['id']); //将id字段排除(新表创建时自动添加了)
$result .= '<br><br>';
$result .= $sql = $sqls[$k]['sql'];
//1st 创建字段
foreach($columns as $column){
$result .= '<br>';
$result .= $column;
}
//2nd 创建索引
foreach($indexes as $index){
$result .= '<br>';
$result .= $index;
}
}
$sqls = array($result);
return $result;
}
/*
SQL命令生成-返回单表命令集:[sql]-表创建命令(带id字段);[columns]-表字段创建命令集(适用于新建);[indexes]-表索引创建命令集(适用于新建);[changec]-表字段调整命令集(适用于后期维护表-TBD)
@db 目标数据库
@dbPrefix 数据库系统前缀
@tablePrefix 数据表类型前缀
@name 类表名称
@tableType 目标数据表后缀(self,param,form,addon,order,data,attr,pre)
@tableTitle 类表备注/注释
@type 表功能类约定(数组)
*/
function get_sql($dbPrefix,$tablePrefix,$name,$tableType,$tableTitle,$type)
{
$dbPrefix = isset($dbPrefix) ? $dbPrefix : false;
$dbPrefix = ($dbPrefix) ? $dbPrefix.'_' : '';
$tablePrefix = isset($tablePrefix) ? $tablePrefix : false;
$tablePrefix = ($tablePrefix) ? $tablePrefix.'_' : '';
$type = (!is_array($type) || empty($type)) ? array("all") : $type;
if(!isset($name)){
if(FM_DEBUG){
trigger_error('未传入name参数', E_USER_ERROR);
}
return false;
}
if(!isset($tableType)){
if(FM_DEBUG){
trigger_error('未传入tableType参数', E_USER_ERROR);
}
return false;
}
$tableName = $dbPrefix.$tablePrefix.$name.'_'.$tableType;
$tables[$tableName] = array();
$tables[$tableName]['name'] = $tableTitle;
$key = "\$table['".$tableName."']['name']"; //原生成到php文件以便动态调用的方法,带了$table变量名的
$key = "name"; //直接生成固定表名的方法
$sql[$key] = $tables[$tableName]['name'];
$tables[$tableName]['type'] = $type;
$key = "\$table\[\'".$tableName."\'\]\[\'type\'\]";
$key = "type";
$sql[$key] = $tables[$tableName]['type'];
switch($tableType) {
case 'self':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."主表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '标题' ;";
$columns['keywords'] = "ALTER TABLE ".$tableName." ADD `keywords` VARCHAR(255) NOT NULL COMMENT '供检索的关键词串' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`siteid` (`siteid`)";
$indexes['siteid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`uniacid` (`uniacid`)";
$indexes['uniacid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`shopid` (`shopid`)";
$indexes['shopid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`keywords` (`keywords`)";
$indexes['keywords'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'form':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."实例表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['s_sn'] = "ALTER TABLE ".$tableName." ADD `s_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应主表的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['uid'] = "ALTER TABLE ".$tableName." ADD `uid` INT(11) UNSIGNED NOT NULL COMMENT '所属后台用户编码' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '标题' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '具体内容或描述' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`s_sn` (`s_sn`)";
$indexes['s_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`siteid` (`siteid`)";
$indexes['siteid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`uniacid` (`uniacid`)";
$indexes['uniacid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`title` (`title`)";
$indexes['title'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'param':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."主参数表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['s_sn'] = "ALTER TABLE ".$tableName." ADD `s_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应主表的记录编码' ;";
$columns['f_sn'] = "ALTER TABLE ".$tableName." ADD `f_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应实例表的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['setfor'] = "ALTER TABLE ".$tableName." ADD `setfor` VARCHAR(50) NOT NULL COMMENT '参数归属类别' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '参数标题' ;";
$columns['key'] = "ALTER TABLE ".$tableName." ADD `key` VARCHAR(255) NOT NULL COMMENT '参数键值' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '参数具体值' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`s_sn` (`s_sn`)";
$indexes['s_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`setfor` (`setfor`)";
$indexes['setfor'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`title` (`title`)";
$indexes['title'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`key` (`key`)";
$indexes['key'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'order':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."数据提交/收集表单表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['s_sn'] = "ALTER TABLE ".$tableName." ADD `s_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应主表的记录编码' ;";
$columns['f_sn'] = "ALTER TABLE ".$tableName." ADD `f_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应实例表的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['mid'] = "ALTER TABLE ".$tableName." ADD `mid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台会员编码' ;";
$columns['fid'] = "ALTER TABLE ".$tableName." ADD `fid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台粉丝(非注册用户)编码' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '标题' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '具体内容或描述' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$indexes['siteid']="`siteid` (`siteid`)";
$indexes['uniacid']="`uniacid` (`uniacid`)";
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`s_sn` (`s_sn`)";
$indexes['s_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`f_sn` (`f_sn`)";
$indexes['f_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`siteid` (`siteid`)";
$indexes['siteid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`uniacid` (`uniacid`)";
$indexes['uniacid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'addon':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."附加参数表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['s_sn'] = "ALTER TABLE ".$tableName." ADD `s_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应主表的记录编码' ;";
$columns['f_sn'] = "ALTER TABLE ".$tableName." ADD `f_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应实例表的记录编码' ;";
$columns['o_sn'] = "ALTER TABLE ".$tableName." ADD `o_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应数据提交表单的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['mid'] = "ALTER TABLE ".$tableName." ADD `mid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台会员编码' ;";
$columns['fid'] = "ALTER TABLE ".$tableName." ADD `fid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台粉丝(非注册用户)编码' ;";
$columns['setfor'] = "ALTER TABLE ".$tableName." ADD `setfor` VARCHAR(50) NOT NULL COMMENT '参数归属类别' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '参数标题' ;";
$columns['key'] = "ALTER TABLE ".$tableName." ADD `key` VARCHAR(255) NOT NULL COMMENT '参数键值' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '参数具体值' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`o_sn` (`o_sn`)";
$indexes['o_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`setfor` (`setfor`)";
$indexes['setfor'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`title` (`title`)";
$indexes['title'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`key` (`key`)";
$indexes['key'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'data':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."具体数据表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['s_sn'] = "ALTER TABLE ".$tableName." ADD `s_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应主表的记录编码' ;";
$columns['f_sn'] = "ALTER TABLE ".$tableName." ADD `f_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应实例表的记录编码' ;";
$columns['o_sn'] = "ALTER TABLE ".$tableName." ADD `o_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应数据提交表单的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['mid'] = "ALTER TABLE ".$tableName." ADD `mid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台会员编码' ;";
$columns['fid'] = "ALTER TABLE ".$tableName." ADD `fid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台粉丝(非注册用户)编码' ;";
$columns['setfor'] = "ALTER TABLE ".$tableName." ADD `setfor` VARCHAR(50) NOT NULL COMMENT '参数归属类别' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '参数标题' ;";
$columns['key'] = "ALTER TABLE ".$tableName." ADD `key` VARCHAR(255) NOT NULL COMMENT '参数键值' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '参数具体值' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`o_sn` (`o_sn`)";
$indexes['o_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`mid` (`mid`)";
$indexes['mid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`setfor` (`setfor`)";
$indexes['setfor'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`title` (`title`)";
$indexes['title'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`key` (`key`)";
$indexes['key'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'attr':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."数据属性微调表' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['sn'] = "ALTER TABLE ".$tableName." ADD `sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '与其他表进行关联的唯一记录编码' ;";
$columns['t_sn'] = "ALTER TABLE ".$tableName." ADD `t_sn` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应数据表的记录编码' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['platid'] = "ALTER TABLE ".$tableName." ADD `platid` INT(11) UNSIGNED NOT NULL COMMENT '所属平台编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['shopid'] = "ALTER TABLE ".$tableName." ADD `shopid` INT(11) UNSIGNED NOT NULL COMMENT '所属商铺编码' ;";
$columns['partnerid'] = "ALTER TABLE ".$tableName." ADD `partnerid` INT(11) UNSIGNED NOT NULL COMMENT '所属合作商户编码' ;";
$columns['uid'] = "ALTER TABLE ".$tableName." ADD `uid` INT(11) UNSIGNED NOT NULL COMMENT '所属后台会员编码' ;";
$columns['mid'] = "ALTER TABLE ".$tableName." ADD `mid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台会员编码' ;";
$columns['fid'] = "ALTER TABLE ".$tableName." ADD `fid` INT(11) UNSIGNED NOT NULL COMMENT '所属前台粉丝(非注册用户)编码' ;";
$columns['suffix'] = "ALTER TABLE ".$tableName." ADD `suffix` VARCHAR(50) NOT NULL COMMENT '对应表后缀(用于区分表)' ;";
$columns['setfor'] = "ALTER TABLE ".$tableName." ADD `setfor` VARCHAR(50) NOT NULL COMMENT '参数归属类别' ;";
$columns['title'] = "ALTER TABLE ".$tableName." ADD `title` VARCHAR(255) NOT NULL COMMENT '参数标题' ;";
$columns['key'] = "ALTER TABLE ".$tableName." ADD `key` VARCHAR(255) NOT NULL COMMENT '参数键值' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '参数具体值' ;";
$columns['displayorder'] = "ALTER TABLE ".$tableName." ADD `displayorder` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序权重' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['updatetime'] = "ALTER TABLE ".$tableName." ADD `updatetime` INT(11) UNSIGNED NOT NULL COMMENT '记录更新时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['update_at'] = "ALTER TABLE ".$tableName." ADD `update_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`sn` (`sn`)";
$indexes['sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`t_sn` (`t_sn`)";
$indexes['t_sn'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`uid` (`uid`)";
$indexes['uid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`mid` (`mid`)";
$indexes['mid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`setfor` (`setfor`)";
$indexes['setfor'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
case 'pre':
$createsql = "CREATE TABLE IF NOT EXISTS ".$tableName." (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID', PRIMARY KEY (`id`) USING BTREE) ENGINE=MyISAM CHARSET=utf8 COLLATE utf8_general_ci COMMENT='".$tableTitle."数据表预编辑操作记录' ;";
$columns = array();
$columns['id'] = "ALTER TABLE ".$tableName." ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表记录序号ID' ;";
$columns['tid'] = "ALTER TABLE ".$tableName." ADD `tid` BIGINT(20) UNSIGNED NOT NULL COMMENT '对应数据表的记录id' ;";
$columns['siteid'] = "ALTER TABLE ".$tableName." ADD `siteid` INT(11) UNSIGNED NOT NULL COMMENT '所属安装站点编码' ;";
$columns['uniacid'] = "ALTER TABLE ".$tableName." ADD `uniacid` INT(11) UNSIGNED NOT NULL COMMENT '所属公众号编码' ;";
$columns['uid'] = "ALTER TABLE ".$tableName." ADD `uid` INT(11) UNSIGNED NOT NULL COMMENT '操作者所属后台会员编码' ;";
$columns['mid'] = "ALTER TABLE ".$tableName." ADD `mid` INT(11) UNSIGNED NOT NULL COMMENT '操作者所属前台会员编码' ;";
$columns['fid'] = "ALTER TABLE ".$tableName." ADD `fid` INT(11) UNSIGNED NOT NULL COMMENT '操作者所属前台粉丝(非注册用户)编码' ;";
$columns['suffix'] = "ALTER TABLE ".$tableName." ADD `suffix` VARCHAR(50) NOT NULL COMMENT '对应表后缀(用于区分表)' ;";
$columns['setfor'] = "ALTER TABLE ".$tableName." ADD `setfor` VARCHAR(50) NOT NULL COMMENT '对应字段' ;";
$columns['value'] = "ALTER TABLE ".$tableName." ADD `value` TEXT NOT NULL COMMENT '参数具体值' ;";
$columns['status'] = "ALTER TABLE ".$tableName." ADD `status` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '站内状态代码' ;";
$columns['deleted'] = "ALTER TABLE ".$tableName." ADD `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否删除(1是0否)' ;";
$columns['createtime'] = "ALTER TABLE ".$tableName." ADD `createtime` INT(11) UNSIGNED NOT NULL COMMENT '记录创建时间' ;";
$columns['statuscode'] = "ALTER TABLE ".$tableName." ADD `statuscode` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '全站状态代码(-128~127)' ;";
$columns['create_at'] = "ALTER TABLE ".$tableName." ADD `create_at`TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录更新时间' ;";
$indexes = array();
$index = "`id` (`id`)";
$indexes['id'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`tid` (`tid`)";
$indexes['tid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`uid` (`uid`)";
$indexes['uid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`mid` (`mid`)";
$indexes['mid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`siteid` (`siteid`)";
$indexes['siteid'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
$index = "`setfor` (`setfor`)";
$indexes['setfor'] = "ALTER TABLE ".$tableName." ADD INDEX".$index.";";
//$indexes['\/\/'] = "为了提高效率,根据建议,优选6个索引序列,前两个为常用;使用顺序不可更改";
break;
default:
break;
}
$tables[$tableName]['sql'] = $createsql;
$key = "\$table\[\'".$tableName."\'\]\[\'sql\'\]";
$key = "sql";
$sql[$key] = $tables[$tableName]['sql'];
$key = "\$table\[\'".$tableName."\'\]\[\'columns\'\]";
$key = "columns";
$sql[$key] = $columns;
$key = "\$table\[\'".$tableName."\'\]\[\'indexes\'\]";
$key = "indexes";
$sql[$key] = $indexes;
return $sql;
}
}
+491
View File
@@ -0,0 +1,491 @@
<?php
/**
* [WeEngine System] Copyright (c) 2014 WE7.CC
* WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
*/
defined('HI_FM') or exit("Access Denied");
define('PDO_DEBUG', true);
global $_FM;
$_FM_CORE = dirname(__FILE__).'/';
class FMDB {
protected $pdo;
protected $cfg;
protected $tablepre;
protected $result;
protected $statement;
protected $errors = array();
protected $link = array();
public function getPDO() {
return $this->pdo;
}
public function __construct($data = array()) {
$this->cfg = $data;
$cfg = $data;
$this->tablepre = $cfg['tablepre'];
$this->connect($cfg);
}
public function connect() {
global $_FM_CORE;
$cfg = $this->cfg;
if(empty($cfg)) {
exit("数据库连接配置不当,请检查!'");
}
$dsn = "mysql:dbname={$cfg['database']};host={$cfg['host']};port={$cfg['port']}";
$dbclass = '';
$options = array();
if (class_exists('PDO')) {
if (extension_loaded("pdo_mysql") && in_array('mysql', PDO::getAvailableDrivers())) {
$dbclass = 'PDO';
$options = array(PDO::ATTR_PERSISTENT => $cfg['pconnect']);
} else {
if(!class_exists('_PDO')) {
fm_load()->fm_class('PDO');
}
$dbclass = '_PDO';
}
} else {
fm_load()->fm_class('PDO');
$dbclass = 'PDO';
}
$this->pdo = new $dbclass($dsn, $cfg['username'], $cfg['password'], $options);
$sql = "SET NAMES '{$cfg['charset']}';";
$this->pdo->exec($sql);
$this->pdo->exec("SET sql_mode='';");
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['error'] = $this->pdo->errorInfo();
$this->debug(false, $info);
}
}
public function prepare($sql) {
$statement = $this->pdo->prepare($sql);
return $statement;
}
public function query($sql, $params = array()) {
$starttime = microtime();
if (empty($params)) {
$result = $this->pdo->exec($sql);
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['error'] = $this->pdo->errorInfo();
$this->debug(false, $info);
}
return $result;
}
$statement = $this->prepare($sql);
$result = $statement->execute($params);
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['params'] = $params;
$info['error'] = $statement->errorInfo();
$this->debug(false, $info);
}
$endtime = microtime();
//$this->performance($sql, $endtime - $starttime);
if (!$result) {
return false;
} else {
return $statement->rowCount();
}
}
public function fetchcolumn($sql, $params = array(), $column = 0) {
$starttime = microtime();
$statement = $this->prepare($sql);
$result = $statement->execute($params);
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['params'] = $params;
$info['error'] = $statement->errorInfo();
$this->debug(false, $info);
}
$endtime = microtime();
$this->performance($sql, $endtime - $starttime);
if (!$result) {
return false;
} else {
return $statement->fetchColumn($column);
}
}
public function fetch($sql, $params = array()) {
$starttime = microtime();
$statement = $this->prepare($sql);
$result = $statement->execute($params);
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['params'] = $params;
$info['error'] = $statement->errorInfo();
$this->debug(false, $info);
}
$endtime = microtime();
$this->performance($sql, $endtime - $starttime);
if (!$result) {
return false;
} else {
return $statement->fetch(pdo::FETCH_ASSOC);
}
}
public function fetchall($sql, $params = array(), $keyfield = '') {
$starttime = microtime();
$statement = $this->prepare($sql);
$result = $statement->execute($params);
if(PDO_DEBUG) {
$info = array();
$info['sql'] = $sql;
$info['params'] = $params;
$info['error'] = $statement->errorInfo();
$this->debug(false, $info);
}
$endtime = microtime();
$this->performance($sql, $endtime - $starttime);
if (!$result) {
return false;
} else {
if (empty($keyfield)) {
return $statement->fetchAll(pdo::FETCH_ASSOC);
} else {
$temp = $statement->fetchAll(pdo::FETCH_ASSOC);
$rs = array();
if (!empty($temp)) {
foreach ($temp as $key => &$row) {
if (isset($row[$keyfield])) {
$rs[$row[$keyfield]] = $row;
} else {
$rs[] = $row;
}
}
}
return $rs;
}
}
}
public function get($tablename, $params = array(), $fields = array()) {
$select = '*';
if (!empty($fields)){
if (is_array($fields)) {
$select = '`'.implode('`,`', $fields).'`';
} else {
$select = $fields;
}
}
$condition = $this->implode($params, 'AND');
$sql = "SELECT {$select} FROM " . $this->tablename($tablename) . (!empty($condition['fields']) ? " WHERE {$condition['fields']}" : '') . " LIMIT 1";
return $this->fetch($sql, $condition['params']);
}
public function getall($tablename, $params = array(), $fields = array(), $keyfield = '', $orderby = array(), $limit = array()) {
$select = '*';
if (!empty($fields)){
if (is_array($fields)) {
$select = '`'.implode('`,`', $fields).'`';
} else {
$select = $fields;
}
}
$condition = $this->implode($params, 'AND');
if (!empty($limit)) {
if (is_array($limit)) {
if (count($limit) == 1) {
$limitsql = " LIMIT " . $limit[0];
} else {
$limitsql = " LIMIT " . ($limit[0] - 1) * $limit[1] . ', ' . $limit[1];
}
} else {
$limitsql = strexists(strtoupper($limit), 'LIMIT') ? " $limit " : " LIMIT $limit";
}
}
if (!empty($orderby)) {
if (is_array($orderby)) {
$orderbysql = implode(',', $orderbysql);
} else {
$orderbysql = $orderby;
}
}
$sql = "SELECT {$select} FROM " .$this->tablename($tablename) . (!empty($condition['fields']) ? " WHERE {$condition['fields']}" : '') . (!empty($orderbysql) ? " ORDER BY $orderbysql " : '') . $limitsql;
return $this->fetchall($sql, $condition['params'], $keyfield);
}
public function getslice($tablename, $params = array(), $limit = array(), &$total = null, $fields = array(), $keyfield = '', $orderby = array()) {
$select = '*';
if (!empty($fields)){
if (is_array($fields)) {
$select = '`'.implode('`,`', $fields).'`';
} else {
$select = $fields;
}
}
$condition = $this->implode($params, 'AND');
if (!empty($limit)) {
if (is_array($limit)) {
$limitsql = " LIMIT " . ($limit[0] - 1) * $limit[1] . ', ' . $limit[1];
} else {
$limitsql = strexists(strtoupper($limit), 'LIMIT') ? " $limit " : " LIMIT $limit";
}
}
if (!empty($orderby)) {
if (is_array($orderby)) {
$orderbysql = implode(',', $orderbysql);
} else {
$orderbysql = $orderby;
}
}
$sql = "SELECT {$select} FROM " . $this->tablename($tablename) . (!empty($condition['fields']) ? " WHERE {$condition['fields']}" : '') . (!empty($orderbysql) ? " ORDER BY $orderbysql " : '') . $limitsql;
$total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($tablename) . (!empty($condition['fields']) ? " WHERE {$condition['fields']}" : ''), $condition['params']);
return $this->fetchall($sql, $condition['params'], $keyfield);
}
public function getcolumn($tablename, $params = array(), $field) {
$result = $this->get($tablename, $params, array($field));
if (!empty($result)) {
return $result[$field];
} else {
return false;
}
}
public function update($table, $data = array(), $params = array(), $glue = 'AND') {
$fields = $this->implode($data, ',');
$condition = $this->implode($params, $glue);
$params = array_merge($fields['params'], $condition['params']);
$sql = "UPDATE " . $this->tablename($table) . " SET {$fields['fields']}";
$sql .= $condition['fields'] ? ' WHERE '.$condition['fields'] : '';
return $this->query($sql, $params);
}
public function insert($table, $data = array(), $replace = FALSE) {
$cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
$condition = $this->implode($data, ',');
return $this->query("$cmd " . $this->tablename($table) . " SET {$condition['fields']}", $condition['params']);
}
public function insertid() {
return $this->pdo->lastInsertId();
}
public function delete($table, $params = array(), $glue = 'AND') {
$condition = $this->implode($params, $glue);
$sql = "DELETE FROM " . $this->tablename($table);
$sql .= $condition['fields'] ? ' WHERE '.$condition['fields'] : '';
return $this->query($sql, $condition['params']);
}
public function begin() {
$this->pdo->beginTransaction();
}
public function commit() {
$this->pdo->commit();
}
public function rollback() {
$this->pdo->rollBack();
}
private function implode($params, $glue = ',') {
$result = array('fields' => ' 1 ', 'params' => array());
$split = '';
$suffix = '';
$allow_operator = array('>', '<', '<>', '!=', '>=', '<=', '+=', '-=', 'LIKE', 'like');
if (in_array(strtolower($glue), array('and', 'or'))) {
$suffix = '__';
}
if (!is_array($params)) {
$result['fields'] = $params;
return $result;
}
if (is_array($params)) {
$result['fields'] = '';
foreach ($params as $fields => $value) {
$operator = '';
if (strpos($fields, ' ') !== FALSE) {
list($fields, $operator) = explode(' ', $fields, 2);
if (!in_array($operator, $allow_operator)) {
$operator = '';
}
}
if (empty($operator)) {
$fields = trim($fields);
if (is_array($value)) {
$operator = 'IN';
} else {
$operator = '=';
}
} elseif ($operator == '+=') {
$operator = " = `$fields` + ";
} elseif ($operator == '-=') {
$operator = " = `$fields` - ";
}
if (is_array($value)) {
$insql = array();
foreach ($value as $k => $v) {
$insql[] = ":{$suffix}{$fields}_{$k}";
$result['params'][":{$suffix}{$fields}_{$k}"] = is_null($v) ? '' : $v;
}
$result['fields'] .= $split . "`$fields` {$operator} (".implode(",", $insql).")";
$split = ' ' . $glue . ' ';
} else {
$result['fields'] .= $split . "`$fields` {$operator} :{$suffix}$fields";
$split = ' ' . $glue . ' ';
$result['params'][":{$suffix}$fields"] = is_null($value) ? '' : $value;
}
}
}
return $result;
}
public function run($sql, $stuff = 'ims_') {
if(!isset($sql) || empty($sql)) return;
$sql = str_replace("\r", "\n", str_replace(' ' . $stuff, ' ' . $this->tablepre, $sql));
$sql = str_replace("\r", "\n", str_replace(' `' . $stuff, ' `' . $this->tablepre, $sql));
$ret = array();
$num = 0;
$sql = preg_replace("/\;[ \f\t\v]+/", ';', $sql);
foreach(explode(";\n", trim($sql)) as $query) {
$ret[$num] = '';
$queries = explode("\n", trim($query));
foreach($queries as $query) {
$ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $query[0].$query[1] == '--') ? '' : $query;
}
$num++;
}
unset($sql);
foreach($ret as $query) {
$query = trim($query);
if($query) {
$this->query($query, array());
}
}
}
public function fieldexists($tablename, $fieldname) {
$isexists = $this->fetch("DESCRIBE " . $this->tablename($tablename) . " `{$fieldname}`", array());
return !empty($isexists) ? true : false;
}
public function indexexists($tablename, $indexname) {
if (!empty($indexname)) {
$indexs = $this->fetchall("SHOW INDEX FROM " . $this->tablename($tablename), array(), '');
if (!empty($indexs) && is_array($indexs)) {
foreach ($indexs as $row) {
if ($row['Key_name'] == $indexname) {
return true;
}
}
}
}
return false;
}
public function tablename($table) {
return "`{$this->tablepre}{$table}`";
}
public function debug($output = true, $append = array()) {
if(!empty($append)) {
$output = false;
array_push($this->errors, $append);
}
if($output) {
print_r($this->errors);
} else {
if (!empty($append['error'][1])) {
$traces = debug_backtrace();
$ts = '';
foreach($traces as $trace) {
$trace['file'] = str_replace('\\', '/', $trace['file']);
$trace['file'] = str_replace(IA_ROOT, '', $trace['file']);
$ts .= "file: {$trace['file']}; line: {$trace['line']}; <br />";
}
$params = var_export($append['params'], true);
if (!function_exists('message')) {
load()->web('common');
load()->web('template');
}
message("SQL: <br/>{$append['sql']}<hr/>Params: <br/>{$params}<hr/>SQL Error: <br/>{$append['error'][2]}<hr/>Traces: <br/>{$ts}");
}
}
return $this->errors;
}
public function tableexists($table) {
if(!empty($table)) {
$data = $this->fetch("SHOW TABLES LIKE '{$this->tablepre}{$table}'", array());
if(!empty($data)) {
$data = array_values($data);
$tablename = $this->tablepre . $table;
if(in_array($tablename, $data)) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
private function performance($sql, $runtime = 0) {
global $_FM;
if ($runtime == 0) {
return false;
}
if (strexists($sql, 'c_performance')) {
return false;
}
if (empty($_W['config']['setting']['maxtimesql'])) {
$_W['config']['setting']['maxtimesql'] = 5;
}
if ($runtime > $_W['config']['setting']['maxtimesql']) {
$sqldata = array(
'type' => '2',
'runtime' => $runtime,
'runurl' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
'runsql' => $sql,
'createtime' => time()
);
$this->insert('c_performance', $sqldata);
}
return true;
}
}