增加docke部署
This commit is contained in:
22
codes/agent/game-docker/api/lib/phprs/ezsql/DB.php
Normal file
22
codes/agent/game-docker/api/lib/phprs/ezsql/DB.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: DB.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql;
|
||||
/**
|
||||
*
|
||||
* @author caoym
|
||||
*
|
||||
*/
|
||||
class DB extends \PDO
|
||||
{
|
||||
public function __construct(
|
||||
$dsn,
|
||||
$username,
|
||||
$passwd,
|
||||
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"]){
|
||||
parent::__construct($dsn, $username, $passwd, $options);
|
||||
$this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
}
|
||||
27
codes/agent/game-docker/api/lib/phprs/ezsql/Native.php
Normal file
27
codes/agent/game-docker/api/lib/phprs/ezsql/Native.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: Native.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql;
|
||||
/**
|
||||
* 原始sql字符串, 拼接时不进行转义
|
||||
* @author caoym
|
||||
*
|
||||
*/
|
||||
class Native
|
||||
{
|
||||
/**
|
||||
* @param string $str
|
||||
*/
|
||||
function __construct($str) {
|
||||
$this->str = $str;
|
||||
}
|
||||
public function __toString(){
|
||||
return $this->str;
|
||||
}
|
||||
public function get(){
|
||||
return $this->str;
|
||||
}
|
||||
private $str;
|
||||
}
|
||||
44
codes/agent/game-docker/api/lib/phprs/ezsql/README.md
Normal file
44
codes/agent/game-docker/api/lib/phprs/ezsql/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# ezsql
|
||||
An an easy-to-use SQL builder.
|
||||
|
||||
## HOW TO USE
|
||||
|
||||
$db = new \PDO($dsn, $username, $passwd);
|
||||
|
||||
### SELECT
|
||||
|
||||
$res = Sql::select('a, b')
|
||||
->from('table')
|
||||
->leftJoin('table1')->on('table.id=table1.id')
|
||||
->where('a=?',1)
|
||||
->groupBy('b')->having('sum(b)=?', 2)
|
||||
->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
->limit(0,1)
|
||||
->forUpdate()->of('d')
|
||||
->get($db);
|
||||
### UPDATE
|
||||
|
||||
$rows = Sql::update('table')
|
||||
->set('a', 1)
|
||||
->where('b=?', 2)
|
||||
->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
->limit(1)
|
||||
->exec($db)
|
||||
->rows
|
||||
|
||||
### INSERT
|
||||
|
||||
$newId = Sql::insertInto('table')
|
||||
->values(['a'=>1])
|
||||
->exec($db)
|
||||
->lastInsertId()
|
||||
|
||||
### DELETE
|
||||
|
||||
$rows = Sql::deleteFrom('table')
|
||||
->where('b=?', 2)
|
||||
->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
->limit(1)
|
||||
->exec($db)
|
||||
->rows
|
||||
|
||||
132
codes/agent/game-docker/api/lib/phprs/ezsql/Sql.php
Normal file
132
codes/agent/game-docker/api/lib/phprs/ezsql/Sql.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: Sql.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql;
|
||||
|
||||
use phprs\ezsql\rules\select\SelectRule;
|
||||
use phprs\ezsql\rules\insert\InsertRule;
|
||||
use phprs\ezsql\rules\update\UpdateRule;
|
||||
use phprs\ezsql\rules\delete\DeleteRule;
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\rules\replace\ReplaceIntoRule;
|
||||
|
||||
require_once __DIR__.'/rules/select.php';
|
||||
require_once __DIR__.'/rules/insert.php';
|
||||
require_once __DIR__.'/rules/update.php';
|
||||
require_once __DIR__.'/rules/delete.php';
|
||||
require_once __DIR__.'/rules/replace.php';
|
||||
|
||||
/**
|
||||
* Easy SQL
|
||||
*
|
||||
* How-to-use:
|
||||
*
|
||||
* $db = new DB($dsn, $username, $passwd);
|
||||
* // 1. select
|
||||
* $res = Sql::select('a, b')
|
||||
* ->from('table')
|
||||
* ->leftJoin('table1')->on('table.id=table1.id')
|
||||
* ->where('a=?',1)
|
||||
* ->groupBy('b')->having('sum(b)=?', 2)
|
||||
* ->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
* ->limit(0,1)
|
||||
* ->forUpdate()->of('d')
|
||||
* ->get($db);
|
||||
*
|
||||
* // 2. update
|
||||
* $rows = Sql::update('table')
|
||||
* ->set('a', 1)
|
||||
* ->where('b=?', 2)
|
||||
* ->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
* ->limit(1)
|
||||
* ->exec($db)
|
||||
* ->rows
|
||||
*
|
||||
* // 3. insert
|
||||
* $newId = Sql::insertInto('table')
|
||||
* ->values(['a'=>1])
|
||||
* ->exec($db)
|
||||
* ->lastInsertId()
|
||||
*
|
||||
* //4. delete
|
||||
* $rows = Sql::deleteFrom('table')
|
||||
* ->where('b=?', 2)
|
||||
* ->orderBy('c', Sql::$ORDER_BY_ASC)
|
||||
* ->limit(1)
|
||||
* ->exec($db)
|
||||
* ->rows
|
||||
*
|
||||
* @author caoym <caoyangmin@gmail.com>
|
||||
*/
|
||||
class Sql{
|
||||
/**
|
||||
* select('column0,column1') => "SELECT column0,column1"
|
||||
*
|
||||
* select('column0', 'column1') => "SELECT column0,column1"
|
||||
*
|
||||
* @param $param0 columns
|
||||
* @return \phprs\ezsql\rules\select\FromRule
|
||||
*/
|
||||
static public function select($param0='*', $_=null){
|
||||
$obj = new SelectRule(new SqlConetxt());
|
||||
$args = func_get_args();
|
||||
if(empty($args)){
|
||||
$args = ['*'];
|
||||
}
|
||||
return $obj->select(implode(',', $args));
|
||||
}
|
||||
/**
|
||||
* insertInto('table') => "INSERT INTO table"
|
||||
*
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\insert\ValuesRule
|
||||
*/
|
||||
static public function insertInto($table) {
|
||||
$obj = new InsertRule(new SqlConetxt());
|
||||
return $obj->insertInto($table);
|
||||
}
|
||||
/**
|
||||
* update('table') => "UPDATE table"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\update\UpdateSetRule
|
||||
*/
|
||||
static public function update($table) {
|
||||
$obj = new UpdateRule(new SqlConetxt());
|
||||
return $obj->update($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* deleteFrom('table') => "DELETE FROM table"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\basic\WhereRule
|
||||
*/
|
||||
static public function deleteFrom($table){
|
||||
$obj = new DeleteRule(new SqlConetxt());
|
||||
return $obj->deleteFrom($table);
|
||||
}
|
||||
/**
|
||||
* replaceInto('table') => "REPLACE INTO table"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\replace\ValuesRule
|
||||
*/
|
||||
static public function replaceInto($table){
|
||||
$obj = new ReplaceIntoRule(new SqlConetxt());
|
||||
return $obj->replaceInto($table);
|
||||
}
|
||||
/**
|
||||
* Splice sql use native string(without escaping)
|
||||
* for example:
|
||||
* where('time>?', 'now()') => " WHERE time > 'now()' "
|
||||
* where('time>?', Sql::native('now()')) => " WHERE time > now() "
|
||||
* @param string $str
|
||||
* @return Native
|
||||
*/
|
||||
static public function native($str){
|
||||
return new Native($str);
|
||||
}
|
||||
|
||||
static public $ORDER_BY_ASC ='ASC';
|
||||
static public $ORDER_BY_DESC ='DESC';
|
||||
}
|
||||
37
codes/agent/game-docker/api/lib/phprs/ezsql/SqlConetxt.php
Normal file
37
codes/agent/game-docker/api/lib/phprs/ezsql/SqlConetxt.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: SqlConetxt.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql;
|
||||
|
||||
/**
|
||||
* @author caoym
|
||||
*/
|
||||
class SqlConetxt{
|
||||
|
||||
/**
|
||||
* 拼接sql语句,并自动插入空格
|
||||
* @param string $sql 表达式
|
||||
*/
|
||||
public function appendSql($sql, $addSpace=true){
|
||||
if($this->sql == ''){
|
||||
$this->sql = $sql;
|
||||
}else{
|
||||
if($addSpace){
|
||||
$this->sql = $this->sql.' '.$sql;
|
||||
}else{
|
||||
$this->sql = $this->sql.$sql;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 增加绑定变量值
|
||||
* @param array $params 变量
|
||||
*/
|
||||
public function appendParams($params){
|
||||
$this->params = array_merge($this->params, $params);
|
||||
}
|
||||
public $sql='';
|
||||
public $params=[];
|
||||
}
|
||||
469
codes/agent/game-docker/api/lib/phprs/ezsql/impls.php
Normal file
469
codes/agent/game-docker/api/lib/phprs/ezsql/impls.php
Normal file
@@ -0,0 +1,469 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: impls.php 401 2015-11-06 08:28:26Z dong.chen $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\impls;
|
||||
use phprs\util\NestedStringCut;
|
||||
use phprs\util\Verify;
|
||||
use phprs\ezsql\SqlConetxt;
|
||||
|
||||
class Response{
|
||||
public function __construct($success,$pdo, $st){
|
||||
$this->pdo = $pdo;
|
||||
$this->st = $st;
|
||||
$this->success = $success;
|
||||
$this->rows = $this->st->rowCount();
|
||||
}
|
||||
public function lastInsertId($name=null){
|
||||
return $this->pdo->lastInsertId($name);
|
||||
}
|
||||
/**
|
||||
* @var bool
|
||||
* true on success or false on failure.
|
||||
*/
|
||||
public $success;
|
||||
/**
|
||||
* @var int
|
||||
* the number of rows.
|
||||
*/
|
||||
public $rows;
|
||||
/**
|
||||
*
|
||||
* @var \PDO
|
||||
*/
|
||||
public $pdo;
|
||||
|
||||
/**
|
||||
* @var \PDOStatement
|
||||
*/
|
||||
public $st;
|
||||
}
|
||||
|
||||
class SelectImpl
|
||||
{
|
||||
static public function select($context, $columns){
|
||||
$context->appendSql("SELECT $columns");
|
||||
}
|
||||
}
|
||||
|
||||
class FromImpl
|
||||
{
|
||||
static public function from($context, $tables){
|
||||
$context->appendSql("FROM $tables");
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteImpl
|
||||
{
|
||||
static public function deleteFrom($context, $from)
|
||||
{
|
||||
$context->appendSql("DELETE FROM $from");
|
||||
}
|
||||
}
|
||||
|
||||
class JoinImpl
|
||||
{
|
||||
static public function join($context, $type, $table) {
|
||||
if($type){
|
||||
$context->appendSql("$type JOIN $table");
|
||||
}else{
|
||||
$context->appendSql("JOIN $table");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JoinOnImpl
|
||||
{
|
||||
static public function on($context, $condition) {
|
||||
$context->appendSql("ON $condition");
|
||||
}
|
||||
}
|
||||
|
||||
class ForUpdateImpl
|
||||
{
|
||||
static public function forUpdate($context){
|
||||
$context->appendSql("FOR UPDATE");
|
||||
}
|
||||
}
|
||||
|
||||
class ForUpdateOfImpl
|
||||
{
|
||||
static public function of($context, $column){
|
||||
$context->appendSql("OF $column");
|
||||
}
|
||||
}
|
||||
|
||||
class InsertImpl
|
||||
{
|
||||
static public function insertInto($context, $table) {
|
||||
$context->appendSql("INSERT INTO $table");
|
||||
}
|
||||
}
|
||||
class ReplaceImpl
|
||||
{
|
||||
static public function replaceInto($context, $table) {
|
||||
$context->appendSql("REPLACE INTO $table");
|
||||
}
|
||||
}
|
||||
class ValuesImpl
|
||||
{
|
||||
static public function values($context, $values){
|
||||
$params = [];
|
||||
$stubs = [];
|
||||
foreach ($values as $v){
|
||||
if(is_a($v, 'phprs\\ezsql\\Native')){//直接拼接sql,不需要转义
|
||||
$stubs[]=$v->get();
|
||||
}else{
|
||||
$stubs[]='?';
|
||||
$params[] = $v;
|
||||
}
|
||||
}
|
||||
$stubs = implode(',', $stubs);
|
||||
|
||||
if(array_keys($values) === range(0, count($values) - 1)){
|
||||
//VALUES(val0, val1, val2)
|
||||
$context->appendSql("VALUES($stubs)");
|
||||
|
||||
}else{
|
||||
//(col0, col1, col2) VALUES(val0, val1, val2)
|
||||
$columns = implode(',', array_keys($values));
|
||||
$context->appendSql("($columns) VALUES($stubs)",false);
|
||||
}
|
||||
$context->appendParams($params);
|
||||
}
|
||||
private $sql = null;
|
||||
}
|
||||
|
||||
class UpdateImpl
|
||||
{
|
||||
static public function update($context, $table){
|
||||
$context->appendSql("UPDATE $table");
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateSetImpl
|
||||
{
|
||||
public function set($context, $column, $value){
|
||||
$prefix = '';
|
||||
if($this->first){
|
||||
$this->first = false;
|
||||
$prefix = 'SET ';
|
||||
}else{
|
||||
$prefix = ',';
|
||||
}
|
||||
if(is_a($value, 'phprs\\ezsql\\Native')){
|
||||
$context->appendSql("$prefix$column=$value",$prefix == 'SET ');
|
||||
}else{
|
||||
$context->appendSql("$prefix$column=?",$prefix == 'SET ');
|
||||
$context->appendParams([$value]);
|
||||
}
|
||||
}
|
||||
public function setArgs($context, $values){
|
||||
$set = [];
|
||||
$params = [];
|
||||
foreach ($values as $k=>$v){
|
||||
if(is_a($v, 'phprs\\ezsql\\Native')){//直接拼接sql,不需要转义
|
||||
$set[]= "$k=".$v->get();
|
||||
}else{
|
||||
$set[]= "$k=?";
|
||||
$params[]=$v;
|
||||
}
|
||||
}
|
||||
if($this->first){
|
||||
$this->first = false;
|
||||
$context->appendSql('SET '.implode(',', $set));
|
||||
$context->appendParams($params);
|
||||
}else{
|
||||
$context->appendSql(','.implode(',', $set),false);
|
||||
$context->appendParams($params);
|
||||
}
|
||||
}
|
||||
private $first=true;
|
||||
}
|
||||
class OrderByImpl
|
||||
{
|
||||
public function orderByArgs($context, $orders){
|
||||
if(empty($orders)){
|
||||
return $this;
|
||||
}
|
||||
$params = array();
|
||||
foreach ($orders as $k=>$v){
|
||||
if(is_integer($k)){
|
||||
Verify::isTrue(
|
||||
preg_match('/^[a-zA-Z0-9_.]+$/', $v),
|
||||
new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")"));
|
||||
|
||||
$params[] = $v;
|
||||
}else{
|
||||
$v = strtoupper($v);
|
||||
Verify::isTrue(
|
||||
preg_match('/^[a-zA-Z0-9_.]+$/', $k) &&
|
||||
($v =='DESC' || $v =='ASC'), new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")"));
|
||||
|
||||
$params[] = "$k $v";
|
||||
}
|
||||
}
|
||||
if($this->first){
|
||||
$this->first = false;
|
||||
$context->appendSql('ORDER BY '.implode(',', $params));
|
||||
}else{
|
||||
$context->appendSql(','.implode(',', $params),false);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function orderBy($context, $column, $order=null){
|
||||
if($this->first){
|
||||
$this->first = false;
|
||||
$context->appendSql("ORDER BY $column");
|
||||
}else{
|
||||
$context->appendSql(",$column", false);
|
||||
}
|
||||
if($order){
|
||||
$context->appendSql($order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
private $first=true;
|
||||
}
|
||||
|
||||
class LimitImpl
|
||||
{
|
||||
static public function limit($context, $size){
|
||||
$intSize = intval($size);
|
||||
Verify::isTrue(strval($intSize) == $size,
|
||||
new \InvalidArgumentException("invalid params for limit($size)"));
|
||||
$context->appendSql("LIMIT $size");
|
||||
}
|
||||
static public function limitWithOffset($context,$start, $size){
|
||||
$intStart = intval($start);
|
||||
$intSize = intval($size);
|
||||
Verify::isTrue(strval($intStart) == $start && strval($intSize) == $size,
|
||||
new \InvalidArgumentException("invalid params for limit($start, $size)"));
|
||||
$context->appendSql("LIMIT $start,$size");
|
||||
}
|
||||
}
|
||||
|
||||
class WhereImpl{
|
||||
|
||||
static private function findQ($str,$offset = 0,$no=0){
|
||||
$found = strpos($str, '?', $offset);
|
||||
if($no == 0 || $found === false){
|
||||
return $found;
|
||||
}
|
||||
return self::findQ($str, $found+1, $no-1);
|
||||
}
|
||||
static public function having($context, $expr, $args){
|
||||
self::condition($context, 'HAVING', $expr, $args);
|
||||
}
|
||||
static public function where($context, $expr, $args){
|
||||
self::condition($context, 'WHERE', $expr, $args);
|
||||
}
|
||||
|
||||
static public function havingArgs($context, $args){
|
||||
self::conditionArgs($context, 'HAVING', $args);
|
||||
}
|
||||
static public function whereArgs($context, $args){
|
||||
self::conditionArgs($context, 'WHERE', $args);
|
||||
}
|
||||
/**
|
||||
* find like Mongodb query glossary
|
||||
* whereArray(
|
||||
* [
|
||||
* 'id'=>['>'=>1],
|
||||
* 'name'=>'cym',
|
||||
* ]
|
||||
* )
|
||||
* 支持的操作符有
|
||||
* = 'id'=>['=' => 1]
|
||||
* > 'id'=>['>' => 1]
|
||||
* < 'id'=>['<' => 1]
|
||||
* <> 'id'=>['<>' => 1]
|
||||
* >= 'id'=>['>=' => 1]
|
||||
* <= 'id'=>['<=' => 1]
|
||||
* BETWEEN 'id'=>['BETWEEN' => [1 ,2]]
|
||||
* LIKE 'id'=>['LIKE' => '1%']
|
||||
* IN 'id'=>['IN' => [1,2,3]]
|
||||
* NOT IN 'id'=>['NOT IN' => [1,2,3]]
|
||||
*
|
||||
* @param array $args
|
||||
*/
|
||||
static public function conditionArgs($context, $prefix, $args=[]){
|
||||
if($args ===null){
|
||||
return ;
|
||||
}
|
||||
$exprs = array();
|
||||
$params = array();
|
||||
foreach ($args as $k => $v){
|
||||
if(is_array($v)){
|
||||
$ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE'];
|
||||
$op = array_keys($v)[0];
|
||||
$op = strtoupper($op);
|
||||
|
||||
Verify::isTrue(
|
||||
false !== array_search($op, $ops),
|
||||
new \InvalidArgumentException("invalid param $op for whereArgs"));
|
||||
|
||||
$var = array_values($v)[0];
|
||||
if($op == 'IN' || $op == 'NOT IN'){
|
||||
$stubs = [];
|
||||
foreach ($var as $i){
|
||||
if(is_a($i, 'phprs\\ezsql\\Native')){
|
||||
$stubs[]=strval($i);
|
||||
}else{
|
||||
$stubs[]='?';
|
||||
$params[] = $i;
|
||||
}
|
||||
}
|
||||
$stubs = implode(',', $stubs);
|
||||
$exprs[] = "$k $op ($stubs)";
|
||||
}else if($op == 'BETWEEN'){
|
||||
$cond = "$k BETWEEN";
|
||||
if(is_a($var[0], 'phprs\\ezsql\\Native')){
|
||||
$cond = "$cond ".strval($var[0]);
|
||||
}else{
|
||||
$cond = "$cond ?";
|
||||
$params[] = $var[0];
|
||||
}
|
||||
if(is_a($var[1], 'phprs\\ezsql\\Native')){
|
||||
$cond = "$cond AND ".strval($var[1]);
|
||||
}else{
|
||||
$cond = "$cond AND ?";
|
||||
$params[] = $var[1];
|
||||
}
|
||||
$exprs[] = $cond;
|
||||
}else{
|
||||
if(is_a($var, 'phprs\\ezsql\\Native')){
|
||||
$exprs[] = "$k $op ".strval($var);
|
||||
}else{
|
||||
$exprs[] = "$k $op ?";
|
||||
$params[] = $var;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(is_a($v, 'phprs\\ezsql\\Native')){
|
||||
$exprs[] = "$k = ".strval($v);
|
||||
|
||||
}else{
|
||||
$exprs[] = "$k = ?";
|
||||
$params[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::condition($context, $prefix, implode(' AND ', $exprs), $params);
|
||||
}
|
||||
static public function condition($context, $prefix, $expr, $args){
|
||||
if(!empty($expr)){
|
||||
if($args){
|
||||
//因为PDO不支持绑定数组变量, 这里需要手动展开数组
|
||||
//也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2)
|
||||
$cutted = null;
|
||||
$cut = null;
|
||||
$toReplace = array();
|
||||
|
||||
$newArgs=array();
|
||||
//找到所有数组对应的?符位置
|
||||
foreach ($args as $k =>$arg){
|
||||
if(is_array($arg) || is_a($arg, 'phprs\\ezsql\\Native')){
|
||||
if(!$cutted){
|
||||
$cut = new NestedStringCut($expr);
|
||||
$cutted = $cut->getText();
|
||||
}
|
||||
//找到第$k个?符
|
||||
$pos = self::findQ($cutted, 0, $k);
|
||||
$pos = $cut->mapPos($pos);
|
||||
Verify::isTrue($pos !== false,
|
||||
new \InvalidArgumentException("unmatched params and ? @ $expr"));
|
||||
|
||||
if(is_array($arg)){
|
||||
$stubs = [];
|
||||
foreach ($arg as $i){
|
||||
if(is_a($i, 'phprs\\ezsql\\Native')){
|
||||
$stubs[] = strval($i);
|
||||
}else{
|
||||
$stubs[] = '?';
|
||||
$newArgs[] = $i;
|
||||
}
|
||||
}
|
||||
$stubs = implode(',', $stubs);
|
||||
}else{
|
||||
$stubs = strval($arg);
|
||||
}
|
||||
$toReplace[] = [$pos, $stubs];
|
||||
|
||||
}else{
|
||||
$newArgs[]=$arg;
|
||||
}
|
||||
}
|
||||
|
||||
if(count($toReplace)){
|
||||
$toReplace = array_reverse($toReplace);
|
||||
foreach ($toReplace as $i){
|
||||
list($pos, $v) = $i;
|
||||
$expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1);
|
||||
}
|
||||
$args = $newArgs;
|
||||
}
|
||||
}
|
||||
$context->appendSql($prefix.' '.$expr);
|
||||
if($args){
|
||||
$context->appendParams($args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GroupByImpl{
|
||||
static public function groupBy($context, $column){
|
||||
$context->appendSql("GROUP BY $column");
|
||||
}
|
||||
}
|
||||
|
||||
class ExecImpl
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param $context SqlConetxt
|
||||
* @param $db \PDO
|
||||
* @param $exceOnError boolean whether throw exceptions
|
||||
* @return Response
|
||||
*/
|
||||
static public function exec($context, $db, $exceOnError=true) {
|
||||
if($exceOnError){
|
||||
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
$st = $db->prepare($context->sql);
|
||||
$success = $st->execute($context->params);
|
||||
return new Response($success, $db,$st);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param SqlConetxt $context
|
||||
* @param PDO $db
|
||||
* @param boolean $errExce
|
||||
* @param string $asDict return as dict or array
|
||||
* @return false|array
|
||||
*/
|
||||
static public function get($context, $db, $dictAs=null ,$errExce=true){
|
||||
if($errExce){
|
||||
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
$st = $db->prepare($context->sql);
|
||||
if($st->execute($context->params)){
|
||||
$res = $st->fetchAll(\PDO::FETCH_ASSOC);
|
||||
if ($dictAs){
|
||||
$dict= [];
|
||||
foreach ($res as $i){
|
||||
$dict[$i[$dictAs]]=$i;
|
||||
}
|
||||
return $dict;
|
||||
}
|
||||
return $res;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
115
codes/agent/game-docker/api/lib/phprs/ezsql/rules/basic.php
Normal file
115
codes/agent/game-docker/api/lib/phprs/ezsql/rules/basic.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: basic.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\rules\basic;
|
||||
|
||||
use phprs\ezsql\impls\ExecImpl;
|
||||
use phprs\ezsql\impls\LimitImpl;
|
||||
use phprs\ezsql\impls\OrderByImpl;
|
||||
use phprs\ezsql\impls\WhereImpl;
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
|
||||
class BasicRule
|
||||
{
|
||||
public function __construct($context){
|
||||
$this->context = $context;
|
||||
}
|
||||
protected $context;
|
||||
}
|
||||
|
||||
class ExecRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* Execute sql
|
||||
* @param \PDO $db
|
||||
* @param boolean $errExce whether throw exceptios
|
||||
* @return Response
|
||||
*/
|
||||
public function exec($db, $errExce=true) {
|
||||
return ExecImpl::exec($this->context, $db, $errExce);
|
||||
}
|
||||
}
|
||||
|
||||
class LimitRule extends ExecRule
|
||||
{
|
||||
/**
|
||||
* limit(1) => "LIMIT 1"
|
||||
* @param int $size
|
||||
* @return \phprs\ezsql\rules\basic\ExecRule
|
||||
*/
|
||||
public function limit($size) {
|
||||
LimitImpl::limit($this->context, $size);
|
||||
return new ExecRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class OrderByRule extends LimitRule
|
||||
{
|
||||
public function __construct($context){
|
||||
parent::__construct($context);
|
||||
$this->impl = new OrderByImpl();
|
||||
}
|
||||
/**
|
||||
* orderByArgs(['column0', 'column1'=>Sql::$ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC"
|
||||
* @param array $orders
|
||||
* @return \phprs\ezsql\rules\basic\LimitRule
|
||||
*/
|
||||
public function orderByArgs($orders) {
|
||||
$this->impl->orderByArgs($this->context, $orders);
|
||||
return new LimitRule($this->context);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* orderBy('column') => "ORDER BY column"
|
||||
* orderBy('column', Sql::$ORDER_BY_ASC) => "ORDER BY column ASC"
|
||||
* orderBy('column0')->orderBy('column1') => "ORDER BY column0, column1"
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $order Sql::$ORDER_BY_ASC or Sql::$ORDER_BY_DESC
|
||||
*
|
||||
* @return \phprs\ezsql\rules\basic\LimitRule
|
||||
*/
|
||||
public function orderBy($column, $order=null) {
|
||||
$this->impl->orderBy($this->context, $column, $order);
|
||||
return new LimitRule($this->context);
|
||||
}
|
||||
private $impl;
|
||||
}
|
||||
|
||||
class WhereRule extends OrderByRule
|
||||
{
|
||||
/**
|
||||
*
|
||||
* where('a=?', 1) => "WHERE a=1"
|
||||
* where('a=?', Sql::native('now()')) => "WHERE a=now()"
|
||||
* where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)"
|
||||
*
|
||||
* @param string $expr
|
||||
* @param mixed $_
|
||||
* @return \phprs\ezsql\rules\basic\OrderByRule
|
||||
*/
|
||||
public function where($expr, $_= null) {
|
||||
WhereImpl::where($this->context, $expr, array_slice(func_get_args(), 1));
|
||||
return new OrderByRule($this->context);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* whereArgs([
|
||||
* 'a'=>1,
|
||||
* 'b'=>['IN'=>[1,2]]
|
||||
* 'c'=>['BETWEEN'=>[1,2]]
|
||||
* 'd'=>['<>'=>1]
|
||||
* ])
|
||||
*
|
||||
* =>
|
||||
* "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1"
|
||||
* @param string $args
|
||||
* @return \phprs\ezsql\rules\basic\OrderByRule
|
||||
*/
|
||||
public function whereArgs($args) {
|
||||
WhereImpl::whereArgs($this->context, $args);
|
||||
return new OrderByRule($this->context);
|
||||
}
|
||||
}
|
||||
25
codes/agent/game-docker/api/lib/phprs/ezsql/rules/delete.php
Normal file
25
codes/agent/game-docker/api/lib/phprs/ezsql/rules/delete.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: delete.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\rules\delete;
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\impls\DeleteImpl;
|
||||
use phprs\ezsql\rules\basic\WhereRule;
|
||||
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
require_once __DIR__.'/basic.php';
|
||||
|
||||
class DeleteRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* deleteFrom('table') => "DELETE FROM table"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\basic\WhereRule
|
||||
*/
|
||||
public function deleteFrom($table) {
|
||||
DeleteImpl::deleteFrom($this->context, $table);
|
||||
return new WhereRule($this->context);
|
||||
}
|
||||
}
|
||||
42
codes/agent/game-docker/api/lib/phprs/ezsql/rules/insert.php
Normal file
42
codes/agent/game-docker/api/lib/phprs/ezsql/rules/insert.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: insert.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\rules\insert;
|
||||
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\rules\basic\ExecRule;
|
||||
use phprs\ezsql\impls\InsertImpl;
|
||||
use phprs\ezsql\impls\ValuesImpl;
|
||||
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
require_once __DIR__.'/basic.php';
|
||||
|
||||
class InsertRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
*
|
||||
* insertInto('table')->values([1,2]) => "INSERT INTO table VALUES(1,2)"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\insert\ValuesRule
|
||||
*/
|
||||
public function insertInto($table) {
|
||||
InsertImpl::insertInto($this->context, $table);
|
||||
return new ValuesRule($this->context);
|
||||
}
|
||||
}
|
||||
class ValuesRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
*
|
||||
* insertInto('table')->values([1,2]) => "INSERT INTO table VALUES(1,2)"
|
||||
* insertInto('table')->values(['a'=>1, 'b'=>Sql::native('now()')]) => "INSERT INTO table(a,b) VALUES(1,now())"
|
||||
* @param unknown $values
|
||||
* @return \phprs\ezsql\rules\basic\ExecRule
|
||||
*/
|
||||
public function values($values) {
|
||||
ValuesImpl::values($this->context, $values);
|
||||
return new ExecRule($this->context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace phprs\ezsql\rules\replace;
|
||||
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\rules\basic\ExecRule;
|
||||
use phprs\ezsql\impls\ReplaceImpl;
|
||||
use phprs\ezsql\impls\ValuesImpl;
|
||||
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
require_once __DIR__.'/basic.php';
|
||||
|
||||
class ReplaceIntoRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* replaceInto('table')->values([1,2]) => "REPLACE INTO table VALUES(1,2)"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\replace\ValuesRule
|
||||
*/
|
||||
public function replaceInto($table) {
|
||||
ReplaceImpl::replaceInto($this->context, $table);
|
||||
return new ValuesRule($this->context);
|
||||
}
|
||||
}
|
||||
class ValuesRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* replaceInto('table')->values([1,2]) => "REPLACE INTO table VALUES(1,2)"
|
||||
* replaceInto('table')->values(['a'=>1, 'b'=>Sql::native('now()')]) => "REPLACE INTO table(a,b) VALUES(1,now())"
|
||||
* @param unknown $values
|
||||
* @return \phprs\ezsql\rules\basic\ExecRule
|
||||
*/
|
||||
public function values($values) {
|
||||
ValuesImpl::values($this->context, $values);
|
||||
return new ExecRule($this->context);
|
||||
}
|
||||
}
|
||||
271
codes/agent/game-docker/api/lib/phprs/ezsql/rules/select.php
Normal file
271
codes/agent/game-docker/api/lib/phprs/ezsql/rules/select.php
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: select.php 246 2015-10-21 04:48:09Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\rules\select;
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\impls\ExecImpl;
|
||||
use phprs\ezsql\impls\SelectImpl;
|
||||
use phprs\ezsql\impls\FromImpl;
|
||||
use phprs\ezsql\impls\JoinImpl;
|
||||
use phprs\ezsql\impls\JoinOnImpl;
|
||||
use phprs\ezsql\impls\WhereImpl;
|
||||
use phprs\ezsql\impls\GroupByImpl;
|
||||
use phprs\ezsql\impls\OrderByImpl;
|
||||
use phprs\ezsql\impls\LimitImpl;
|
||||
use phprs\ezsql\impls\ForUpdateOfImpl;
|
||||
use phprs\ezsql\impls\ForUpdateImpl;
|
||||
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
require_once __DIR__.'/basic.php';
|
||||
|
||||
class SelectRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* select('column0, column1') => "SELECT column0, column1"
|
||||
* select('column0', 'column1') => "SELECT column0, column1"
|
||||
* @param string $columns
|
||||
* @return \phprs\ezsql\rules\select\FromRule
|
||||
*/
|
||||
public function select($columns) {
|
||||
SelectImpl::select($this->context, $columns);
|
||||
return new FromRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class GetRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* Execute sql and get responses
|
||||
* @param \PDO $db
|
||||
* @param $errExce whether throw exceptions
|
||||
* @return array
|
||||
*/
|
||||
public function get($db, $asDict=false,$errExce=true) {
|
||||
return ExecImpl::get($this->context, $db, $asDict,$errExce);
|
||||
}
|
||||
}
|
||||
class FromRule extends GetRule
|
||||
{
|
||||
/**
|
||||
* from('table') => "FROM table"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\select\JoinRule
|
||||
*/
|
||||
public function from($table){
|
||||
FromImpl::from($this->context, $table);
|
||||
return new JoinRule($this->context);
|
||||
}
|
||||
}
|
||||
class ForUpdateOfRule extends GetRule
|
||||
{
|
||||
/**
|
||||
* forUpdate()->of('column') => 'FOR UPDATE OF column'
|
||||
* @param string $column
|
||||
* @return \phprs\ezsql\rules\select\GetRule
|
||||
*/
|
||||
public function of($column){
|
||||
ForUpdateOfImpl::of($this->context, $column);
|
||||
return new GetRule($this->context);
|
||||
}
|
||||
}
|
||||
class ForUpdateRule extends GetRule
|
||||
{
|
||||
/**
|
||||
* forUpdate() => 'FOR UPDATE'
|
||||
* @return \phprs\ezsql\rules\select\ForUpdateOfRule
|
||||
*/
|
||||
public function forUpdate(){
|
||||
ForUpdateImpl::forUpdate($this->context);
|
||||
return new ForUpdateOfRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class LimitRule extends ForUpdateRule
|
||||
{
|
||||
/**
|
||||
* limit(0,1) => "LIMIT 0,1"
|
||||
* @param int $start
|
||||
* @param int $size
|
||||
* @return \phprs\ezsql\rules\select\ForUpdateRule
|
||||
*/
|
||||
public function limit($start, $size) {
|
||||
LimitImpl::limitWithOffset($this->context, $start, $size);
|
||||
return new ForUpdateRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class OrderByRule extends LimitRule
|
||||
{
|
||||
public function __construct($context){
|
||||
parent::__construct($context);
|
||||
$this->order = new OrderByImpl();
|
||||
}
|
||||
/**
|
||||
* orderBy('column') => "ORDER BY column"
|
||||
* orderBy('column', Sql::$ORDER_BY_ASC) => "ORDER BY column ASC"
|
||||
* orderBy('column0')->orderBy('column1') => "ORDER BY column0, column1"
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $order Sql::$ORDER_BY_ASC or Sql::$ORDER_BY_DESC
|
||||
* @return \phprs\ezsql\rules\select\OrderByRule
|
||||
*/
|
||||
public function orderBy($column, $order=null) {
|
||||
$this->order->orderBy($this->context, $column, $order);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* orderByArgs(['column0', 'column1'=>Sql::$ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC"
|
||||
* @param array $args
|
||||
* @return \phprs\ezsql\rules\select\OrderByRule
|
||||
*/
|
||||
public function orderByArgs($args) {
|
||||
$this->order->orderByArgs($this->context, $args);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var OrderByImpl
|
||||
*/
|
||||
private $order;
|
||||
}
|
||||
|
||||
class HavingRule extends OrderByRule
|
||||
{
|
||||
/**
|
||||
*
|
||||
* having('SUM(a)=?', 1) => "HAVING SUM(a)=1"
|
||||
* having('a>?', Sql::native('now()')) => "HAVING a>now()"
|
||||
* having('a IN (?)', [1, 2]) => "HAVING a IN (1,2)"
|
||||
*
|
||||
* @param string $expr
|
||||
* @param string $_
|
||||
* @return \phprs\ezsql\rules\select\OrderByRule
|
||||
*/
|
||||
public function having($expr, $_=null) {
|
||||
WhereImpl::having($this->context, $expr, array_slice(func_get_args(), 1));
|
||||
return new OrderByRule($this->context);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* havingArgs([
|
||||
* 'a'=>1,
|
||||
* 'b'=>['IN'=>[1,2]]
|
||||
* 'c'=>['BETWEEN'=>[1,2]]
|
||||
* 'd'=>['<>'=>1]
|
||||
* ])
|
||||
*
|
||||
* =>
|
||||
* "HAVING a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1"
|
||||
*
|
||||
*
|
||||
* @param array $args
|
||||
* @return \phprs\ezsql\rules\select\OrderByRule
|
||||
*/
|
||||
public function havingArgs($args) {
|
||||
WhereImpl::havingArgs($this->context, $args);
|
||||
return new OrderByRule($this->context);
|
||||
}
|
||||
}
|
||||
class GroupByRule extends OrderByRule
|
||||
{
|
||||
/**
|
||||
* groupBy('column') => "GROUP BY column"
|
||||
* @param string $column
|
||||
* @return \phprs\ezsql\rules\select\HavingRule
|
||||
*/
|
||||
public function groupBy($column) {
|
||||
GroupByImpl::groupBy($this->context, $column);
|
||||
return new HavingRule($this->context);
|
||||
}
|
||||
}
|
||||
class WhereRule extends GroupByRule
|
||||
{
|
||||
/**
|
||||
*
|
||||
* where('a=?', 1) => "WHERE a=1"
|
||||
* where('a=?', Sql::native('now()')) => "WHERE a=now()"
|
||||
* where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)"
|
||||
*
|
||||
* @param string $expr
|
||||
* @param mixed $_
|
||||
* @return \phprs\ezsql\rules\select\GroupByRule
|
||||
*/
|
||||
public function where($expr, $_=null) {
|
||||
WhereImpl::where($this->context, $expr, array_slice(func_get_args(), 1));
|
||||
return new GroupByRule($this->context);
|
||||
}
|
||||
/**
|
||||
* whereArgs([
|
||||
* 'a'=>1,
|
||||
* 'b'=>['IN'=>[1,2]]
|
||||
* 'c'=>['BETWEEN'=>[1,2]]
|
||||
* 'd'=>['<>'=>1]
|
||||
* ])
|
||||
*
|
||||
* =>
|
||||
* "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1"
|
||||
* @param array $args
|
||||
* @return\phprs\ezsql\rules\select\GroupByRule
|
||||
*/
|
||||
public function whereArgs($args) {
|
||||
WhereImpl::whereArgs($this->context,$args);
|
||||
return new GroupByRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class JoinRule extends WhereRule
|
||||
{
|
||||
/**
|
||||
* join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\select\JoinOnRule
|
||||
*/
|
||||
public function join($table){
|
||||
JoinImpl::join($this->context,null, $table);
|
||||
return new JoinOnRule($this->context);
|
||||
}
|
||||
/**
|
||||
* leftJoin('table1')->on('table0.id=table1.id') => "LEFT JOIN table1 ON table0.id=table1.id"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\select\JoinOnRule
|
||||
*/
|
||||
public function leftJoin($table){
|
||||
JoinImpl::join($this->context,'LEFT', $table);
|
||||
return new JoinOnRule($this->context);
|
||||
}
|
||||
/**
|
||||
* rightJoin('table1')->on('table0.id=table1.id') => "RIGHT JOIN table1 ON table0.id=table1.id"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\select\JoinOnRule
|
||||
*/
|
||||
public function rightJoin($table) {
|
||||
JoinImpl::join($this->context,'RIGHT', $table);
|
||||
return new JoinOnRule($this->context);
|
||||
}
|
||||
/**
|
||||
* innerJoin('table1')->on('table0.id=table1.id') => "INNER JOIN table1 ON table0.id=table1.id"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\select\JoinOnRule
|
||||
*/
|
||||
public function innerJoin($table) {
|
||||
JoinImpl::join($this->context,'INNER', $table);
|
||||
return new JoinOnRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class JoinOnRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id"
|
||||
* @param string $condition
|
||||
* @return \phprs\ezsql\rules\select\WhereRule
|
||||
*/
|
||||
public function on($condition){
|
||||
JoinOnImpl::on($this->context, $condition);
|
||||
return new JoinRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
59
codes/agent/game-docker/api/lib/phprs/ezsql/rules/update.php
Normal file
59
codes/agent/game-docker/api/lib/phprs/ezsql/rules/update.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* $Id: update.php 131 2015-10-10 02:25:57Z yangmin.cao $
|
||||
* @author caoym(caoyangmin@gmail.com)
|
||||
*/
|
||||
namespace phprs\ezsql\rules\update;
|
||||
use phprs\ezsql\rules\basic\BasicRule;
|
||||
use phprs\ezsql\rules\basic\WhereRule;
|
||||
use phprs\ezsql\impls\UpdateSetImpl;
|
||||
use phprs\ezsql\impls\UpdateImpl;
|
||||
|
||||
|
||||
require_once dirname(__DIR__).'/impls.php';
|
||||
require_once __DIR__.'/basic.php';
|
||||
|
||||
class UpdateRule extends BasicRule
|
||||
{
|
||||
/**
|
||||
* update('table')->set('a', 1) => "UPDATE table SET a=1"
|
||||
* @param string $table
|
||||
* @return \phprs\ezsql\rules\update\UpdateSetRule
|
||||
*/
|
||||
public function update($table) {
|
||||
UpdateImpl::update($this->context, $table);
|
||||
return new UpdateSetRule($this->context);
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateSetRule extends WhereRule
|
||||
{
|
||||
public function __construct($context){
|
||||
parent::__construct($context);
|
||||
$this->impl = new UpdateSetImpl();
|
||||
}
|
||||
/**
|
||||
* update('table')->set('a', 1) => "UPDATE table SET a=1"
|
||||
* update('table')->set('a', 1)->set('b',Sql::native('now()')) => "UPDATE table SET a=1,b=now()"
|
||||
* @param string $column
|
||||
* @param mixed $value
|
||||
* @return \phprs\ezsql\rules\update\UpdateSetRule
|
||||
*/
|
||||
public function set($column, $value) {
|
||||
$this->impl->set($this->context, $column, $value);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* update('table')->set(['a'=>1, 'b'=>Sql::native('now()')]) => "UPDATE table SET a=1,b=now()"
|
||||
* @param array $values
|
||||
* @return \phprs\ezsql\rules\update\UpdateSetRule
|
||||
*/
|
||||
public function setArgs($values) {
|
||||
$this->impl->setArgs($this->context, $values);
|
||||
return $this;
|
||||
}
|
||||
private $impl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user