php sql基本语言,sqlTemplate

SqlTemplate

ibatis-like php based sqltemplate library, it can be used as a sql template, support mysql now, and will support more db type later. PHP version 5.5.10 or later.

Installing

Preferred way to install is with Composer.

Just add

"require" : {

"kings36503/sql-template" : "dev-master",

}

in your projects composer.json.

Example Usage

prepare a mysql/mariaDB server.

go to line 15, change the host, username, password to your own mysql/mariaDB server.

Run example.php in CLI mode: php example.php

// load configuration file

$sqlComposer = new SqlComposer(__DIR__ . '/sqlmap/sqlmapACC.json', true);

// create some test data

$sqlComposer->execute('report.createDB');

$sqlComposer->execute('report.createTable1');

$sqlComposer->execute('report.createTable2');

$sqlComposer->execute('report.truncateTable1');

$sqlComposer->execute('report.truncateTable2');

// begin a transcation

try{

$sqlComposer->beginTranscation('report');

$sqlComposer->execute('report.addTable1Data', ['count' => [1,2,3,4,5,6,7,8,9,10]]);

$sqlComposer->execute('report.addTable2Data', ['count' => [1,2,3,4,5,6,7,8,9,10]]);

$sqlComposer->execute('report.useReport');

}catch(\Exception $e){

// error accured, roll back.

$sqlComposer->rollBack('report');

}

// commit

$sqlComposer->endTranscation('report');

// query data from database

$result = $sqlComposer->query('report.getReport', [

'tableNames' => [

'table_1',

'table_2'

],

'ip' => [

'hasDstIp' => true,

'srcIp' => 0,

'dstIp' => [

1,

2,

3,

4,

5

]

],

'alertName' => '%alert name%',

'limit' => [

'one' => 0,

'two' => 10

]

]);

print_r($result);

Configuration detail

Configuration is JSON format, schema.json is the json schema of the file. if you are familiar with

ibatis sqlMap config, it will be easy for you to use. If you never heard of ibatis sqlMap config,

that all right, you can see the comments as follows:

{

/**

* MUST

* namespace of the config file , one configuration file prefer only one namespace.

*/

"report" : {

/**

* MUST

* Class path of the dbdao, it will be initialized in a reflection way. this dao must

* implement interface db\IDBDAO.

*/

"daoName" : "snow\\song\\db\\mysql\\ReportDao",

/**

* MUST ==== SQL ID which value can not be literal 'daoName' stand for a sql statement.

* SQL ID consist of many elements, such as 'iterate', 'dynamic', 'isEqual' etc.

*/

"getReport" : [

/**

* String type element

*/

"SELECT * FROM",

{

/**

* MUST

* Type of the element, can be [iterate, dynamic, include, isEqual, isNotEqual,

* isGreaterThan, isGreaterEqual, isLessThan, isLessEqual, isPropertyAvailable,

* isNotPropertyAvailable, isNull, isNotNull, isEmpty, isNotEmpty]. iterate

* stand for a loop.

*/

"type" : "iterate",

/**

* MUST

* Perperty name that use to loop, must be an array. dot chains is supported.

*/

"property" : "tableNames",

/**

* OPTIONAL

* Put its value at the begining of the loop.

*/

"open" : "(",

/**

* OPTIONAL

* Put its value at the end of the loop.

*/

"close" : ") AS t1",

/**

* OPTIONAL

* Conjunction of the loop. used for 'AND' or 'OR' or 'UNION ALL'

*/

"conjunction" : "UNION ALL",

/**

* OPTIONAL

* A string that can be over write. put it at the front of the sql.

*/

"prepend" : "",

/**

* OPTIONAL

* Contents of the loop, consist of some elements which can be [iterate, dynamic,

* include, isEqual, isNotEqual, isGreaterThan, isGreaterEqual, isLessThan,

* isLessEqual, isPropertyAvailable, isNotPropertyAvailable, isNull, isNotNull,

* isEmpty, isNotEmpty]

*/

"contents" : [

/**

* String type element

*/

"SELECT sip, dip FROM $tableNames[]$",

{

/**

* dynamic means that its contents can only contains conditional element,

* such as : [isEqual, isNotEqual, isGreaterThan, isGreaterEqual, isLessThan,

* isLessEqual, isPropertyAvailable, isNotPropertyAvailable, isNull, isNotNull,

* isEmpty, isNotEmpty]

*/

"type" : "dynamic",

"prepend" : "WHERE",

"contents" : [

{

"type" : "isEqual",

/**

* dot chains example. Asssume you have a parameter: ['ip' => ['dstIp' => 1]],

* then you could type a dot in the middle of the properties.

*/

"property" : "ip.dstIp",

"compareValue" : "1",

"prepend" : "AND",

"contents" : [

/**

* 'dstIp' is a property name of the param, property between '##' means

* it will be treated as a prepared statement. It will be parsed to

* " dip <> ? ". character 'i' means 'dstIp' has type integer. 's' means

* type string, 'd' means type float number, and 'b' means type blob.

* s,i,d,b is optional, default value is 's'.

* see http://php.net/manual/en/mysqli-stmt.bind-param.php for details.

*/

"dip <> #ip.dstIp#i"

]

},

{

"type" : "isPropertyAvailable",

"property" : "ip.dstIp",

"prepend" : "AND",

"contents" : [

/**

* Property name between '$$' will be replaced by the property value.

* in this case, if srcIp is 0, it will be parsed to " sip >= 0 ".

*/

"sip >= $ip.dstIp$"

]

},

{

"type" : "isNotNull",

"property" : "alertName",

"prepend" : "AND",

"contents" : ["alert like #alertName#s"]

}

]

},

"GROUP BY sip"

]

},

{

/**

* A 'include' type means it is a reference of other SQL ID.

*/

"type" : "include",

/**

* MUST

* Name of other SQL ID in this namespace.

*/

"refid" : "orderBy"

}

],

"orderBy" : [

"GROUP BY sip ORDER BY sip",

{

"type" : "isNotEmpty",

"property" : "limit",

"contents" : ["LIMIT #limit#i"]

}

]

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值