TrimQuery引擎是TrimPath开源项目的一个轻量组件,遵守GPL & APL开源协议。 它可以为你的客户端程序提供使用sql的能力。
TrimQuery引擎是使用javascript编写的,源代码大约900多行。最新版本号为1.1.14
项目托管在google code。地址:
http://code.google.com/p/trimpath/
TrimQuery引擎支持类似于SQL的语法有:
INSERT, UPDATE, DELETE
SELECT ... FROM
WHERE ...
LIKE ...
ORDER BY ... ASC/DESC
AS ...
GROUP BY ... HAVING ...
SUM, COUNT, AVG 聚合函数
自联接(self join)
LIMIT和偏移量
“?”占位符(类似JAVA JDBC的中的方式)
不支持的SQL语法:
BETWEEN
IN (for lists and for sub-queries or nested SELECT's)
ANY
EXISTS
DISTINCT
AVG DISTINCT, SUM DISTINCT, COUNT DISTINCT
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
UNION, EXCEPT, INTERSECT
下面通过一个简单的例子了解TrimQuery(代码在附件中文件 TrimQueryDemo.html)
<!DOCTYPE >
<html>
<head>
<title>TrimQuery Demo</title>
</head>
<script type="text/javascript" src="trimpath-query-1.1.14.js"></script>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
//定义表结构
var columnDefs = {
Invoice : {
id : {
type: "String"
},
total : {
type: "Number"
},
custId : {
type: "String"
}
},
Customer : {
id : {
type: "String"
},
acctBalance : {
type: "Number"
}
}
};
//表数据
var tableData = {
Invoice : [{
id: 1,
total: 100,
custId: 10
},{
id: 2,
total: 200,
custId: 10
},{
id: 3,
total: 300,
custId: 10
},{
id: 4,
total: 400,
custId: 20
} ],
Customer : [{
id: 10,
acctBalance: 1000
},{
id: 20,
acctBalance: 2000
},{
id: 30,
acctBalance: 3000
} ]
};
//首先我们预编译查询表。
var queryLang = TrimPath.makeQueryLang(columnDefs);
// 下面我们做一个查询。
var statement = queryLang.parseSQL(
"SELECT Customer.id, Customer.acctBalance, Invoice.total " +
"FROM Customer, Invoice " +
"WHERE Customer.id = Invoice.custId " +
"ORDER BY Customer.id ASC");
// 运行查询语句在指定的数据集中查询结果集。
var results = statement.filter(tableData);
//输出查询结果
alert(results.toJSONString());
// 此外我们可可以获取查询语句。
alert(statement.toString());
</script>
<body>
</body>
</html>
通过上例相信大家已经看出了里面的道道,使用TrimQuery首先要有表的定义,然后要有表数据,最后通过查询语句获得结果集。
下面详细介绍下TrimQuery中提供的方法(API)
TrimPath是一个全局对象,在这个对象中包含有TrimQuery方法。
TrimPath.makeQueryLang(表定义) :用于预声明 要操作的表结构。也就是确定操作的范围。该函数返回一个queryLang 对象,这个对象下包括一系列的SQL操作方法。如果多次使用相同的参数调用TrimPath.makeQueryLang方法,将会生成多个不同的queryLang 对象。参数格式如例子中columnDefs定义的格式
queryLang.parseSQL(sqlString,optionalParamsArray):方法用于声明执行的SQL语句。第一个参数是SQL语句字符串,第二个参数为可选参数,支持"?"替换,防止脚本注入。如果SQL语句不符合语法则会抛出异常。方法会返回一个selectStatement 对象,通过这个对象就可以查询了。
包括第二个参数的例子:
selectStatement = queryLang.parseSQL(
"SELECT Customer.* FROM Customer " +
"WHERE Customer.acctBalance > ? " +
"AND Customer.acctBalance < ?"
, [ minBalance, maxBalance ]);
selectStatement.filter( tableData, options ):方法用于执行SQL语句。第一个参数是作用的表数据集,格式如例子中的tableData ,第二个参数可选,我这里就不介绍了。返回结果集是一个JSON对象。
selectStatement.toString():方法返回一个String表示selectStatement对象的SQL。
再说说TrimQuery的SQL语法需要注意的点
TrimQuery的所有关键字必须大写。
列必须使用TableName.columnName点缀语法指定
TrimQuery不支持复杂的函数。
下面列出TrimQuery SQL的示例:
//查询全表
SELECT * FROM Invoice
//查询全表
SELECT Invoice.* FROM Invoice
//查询指定列
SELECT Invoice.id, Invoice.total FROM Invoice
//两表联合查询
SELECT Invoice.id, Invoice.total, Customer.acctBalance FROM Invoice, Customer
//量表联合查询使用 *
SELECT Invoice.*, Customer.* FROM Invoice, Customer
//使用AS 为表重命名
SELECT i.* FROM Invoice AS i
//使用AS为列重命名
SELECT i.id AS ID_NUM FROM Invoice AS i
//量表联合查询中使用AS
SELECT Invoice.*, i2.* FROM Invoice, Invoice AS i2
//两表联合查询使用 关联条件 过滤
SELECT Customer.id, Invoice.custId, Invoice.total, Customer.acctBalance
FROM Invoice, Customer
WHERE Invoice.custId = Customer.id
//两表联合查询使用 关联条件 过滤条件 过滤
SELECT Customer.id, Invoice.custId, Invoice.total, Customer.acctBalance
FROM Invoice, Customer
WHERE Invoice.total < 250 AND Invoice.custId = Customer.id
// 使用ORDER BY 语句为结果集排序
SELECT Invoice.id FROM Invoice ORDER BY Invoice.id
//指定排序规则 为升序
SELECT Invoice.id FROM Invoice ORDER BY Invoice.id ASC
//指定排序规则为降序
SELECT Invoice.id FROM Invoice ORDER BY Invoice.id DESC
//使用多列作为排序列
SELECT Invoice.custId, Invoice.id FROM Invoice ORDER BY Invoice.custId DESC, Invoice.id
//获取前100条数据
SELECT Refund.* FROM Refund LIMIT 100
//获取从100到 100 + 50的数据
SELECT Refund.* FROM Refund LIMIT 100, 50
//使用集合函数
SELECT Invoice.total,
SUM (Invoice.total) AS SUM_total,
COUNT (Invoice.total) AS COUNT_total,
AVG (Invoice.total) AS AVG_total
FROM Invoice
//使用GROUP BY 语法
SELECT Invoice.total,
SUM (Invoice.total) AS SUM_total,
COUNT (Invoice.total) AS COUNT_total,
AVG (Invoice.total) AS AVG_total
FROM Invoice
GROUP BY Invoice.custId
//使用 GROUP BY 与 ORDER BY
SELECT Invoice.total,
SUM (Invoice.total) AS SUM_total
FROM Invoice GROUP BY Invoice.custId ORDER BY SUM_total ASC
//使用 HAVING
SELECT Invoice.total AS TOT,
SUM (Invoice.total) AS SUM_total
FROM Invoice
GROUP BY Invoice.custId
HAVING (TOT) > 15000
ORDER BY SUM_total ASC
//使用LIKE
SELECT * FROM User WHERE User.name LIKE 'P%'
//插入数据
INSERT INTO Event (id, date, invoice_id) VALUES (1, '2003-01-31', 43)
//更新数据
UPDATE User SET name='Frank Thomas' WHERE User.id = '3'
//删除数据
DELETE Thing FROM Thing WHERE Thing.id = '1'
好啦就些这么多。