PHPackages                             kings36503/sql-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kings36503/sql-template

ActiveLibrary[Database &amp; ORM](/categories/database)

kings36503/sql-template
=======================

A powerful PHP SQL template composer library which can significantly reduce the number and complexity of the SQL statement in the project. Reference to the ibatis implementation.

630PHP

Since Dec 5Pushed 9y agoCompare

[ Source](https://github.com/kings36503/sqlTemplate)[ Packagist](https://packagist.org/packages/kings36503/sql-template)[ RSS](/packages/kings36503-sql-template/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

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
==========

[](#installing)

Preferred way to install is with [Composer](https://getcomposer.org/).

Just add

```
"require" : {
	"kings36503/sql-template" : "dev-master",
}

```

in your projects composer.json.

Example Usage
=============

[](#example-usage)

- prepare a mysql/mariaDB server.
- open [src/snow/song/db/mysql/ReportDao.php](https://github.com/kings36503/sqlTemplate/blob/master/src/snow/song/db/mysql/ReportDao.php)
- go to line 15, change the host, username, password to your own mysql/mariaDB server.
- Run [example.php](https://github.com/kings36503/sqlTemplate/blob/master/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-detail)

Configuration is JSON format, [schema.json](https://github.com/kings36503/sqlTemplate/blob/master/sqlmap/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"]
			}
		]
	}
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/7bc03f1830db12860e5594aa13eb3c59f3c4e1761ae0ef34fa6d378dd440a65d?d=identicon)[kings36503](/maintainers/kings36503)

---

Top Contributors

[![kings36503](https://avatars.githubusercontent.com/u/8828663?v=4)](https://github.com/kings36503 "kings36503 (3 commits)")

### Embed Badge

![Health badge](/badges/kings36503-sql-template/health.svg)

```
[![Health](https://phpackages.com/badges/kings36503-sql-template/health.svg)](https://phpackages.com/packages/kings36503-sql-template)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
