简单的 SQL 构建器
生成可供执行的查询字符串, 内置参数从引用中跳出, 以及帮助的条款构建器 .
公共方法
- 选择
- 插入
- 删除
- 更新
- 加入
- 顺序
- 组
- 地点
- 拥有
- 限制
- 段落
- 从杰森
- 构建
选择
需要一个 {对象} 带有下列密钥:
| 段落 | 类型 | 注释 |
|---|---|---|
| 图表 | {字符串/对象} | 'table_name' 编号 : {'table_name': 'alias'} ^ |
| 栏 | {字符串} [ | 要获取的列列表 |
| 查询 | {对象} | [可选 预建内置查询. ^table 此选项不需要 . {'alias': 'pre-built query'} |
回归自我,可连锁.
// Simple query example
let query = new SimpleSqlBuilder()
.select({
'table': {'users': 'user'},
'columns': [
'user.first_name',
'user.last_name',
'YEAR(user.date_added) AS alumni'
]
})
.build();
/** Output:
SELECT `user`.`first_name`, `user`.`last_name`, YEAR(`user`.`date_added`) AS `alumni`
FROM `users` AS `user`
*/
---
// Inner query example
let inner = new SimpleSqlBuilder()
.select({
'table': {'orders': 't'},
'columns': [
'MAX(t.date_added) AS date_added',
]
})
.where([
['t.type', '=', '?'],
])
.group([
't.type',
])
.params([
'Fish',
])
.build();
// Main/outer query
let query = new SimpleSqlBuilder()
.select({
'query': {'joiner': inner},
'columns': [
'order.*',
]
})
.joins([
{
'type': 'inner',
'table': { 'orders': 'order' },
'using': [
'date_added'
]
}
])
.where([
['order.type', '=', '?'],
])
.group([
'order.type'
])
.params([
'Fish'
])
.build();
/* Output:
SELECT `order`.*
FROM (
SELECT MAX(`t`.`date_added`) AS `date_added`
FROM `orders` AS `t`
WHERE `t`.`type` = 'Fish'
GROUP BY `t`.`type`
) AS `joiner`
INNER JOIN `orders` AS `order` USING (`date_added`)
WHERE `order`.`type` = 'Fish'
GROUP BY `order`.`type`
*/
插入
需要一个 {对象} 带有下列密钥:
| 段落 | 类型 | 注释 |
|---|---|---|
| 图表 | {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 | 'table_name' |
| 栏 | {对象} | {column: value} 双对 |
| 复制 | {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 | [可选 更新重复条款 . 注意: 字符串未脱出 |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.insert({
'table': 'users',
'columns': {
'first_name', '?',
'last_name', '?',
'age': '?'
},
'duplicates': 'age = age + 1'
})
.params([
'John',
'Doe',
30
]);
删除
需要一个 {对象} 带有下列密钥:
| 段落 | 类型 | 注释 |
|---|---|---|
| 图表 | {字符串/对象} | 'table_name' 编号 : {'table_name': 'alias'} |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.delete({
'table': {'users': 'user'}
})
.where([
['user.id','=','?'],
])
.params([
47,
]);
更新
需要一个 {对象} 带有下列密钥:
| 段落 | 类型 | 注释 |
|---|---|---|
| 图表 | {字符串/对象} | 'table_name' 编号 : {'table_name': 'alias'} |
| 栏 | {对象} | {column: value} 双对 |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.update({
'table': {'users': 'user'},
'columns': {
'age': '?'
}
})
.where([
['user.id','=','?'],
])
.params([
47,
30,
]);
加入
需要一个 {对象 []} 带有下列密钥:
| 段落 | 类型 | 注释 |
|---|---|---|
| 类型 | {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 | inner 编号 : outer 编号 : left 编号 : right 编号 : cross |
| 图表 | {字符串/对象} | 'table_name' 编号 : {'table_name': 'alias'} |
| 条件 | [阵列] | @ 见 where 需要更多信息 ^ |
| 使用 | {字符串} [ | [可选 内部查询的列列表 。 ^conditions 此选项不需要 . |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.select({
'table': {'users': 'user'},
'columns': ['user.*']
})
.joins([
{
'type': 'left',
'table': {'orders': 'order'},
'conditions': [
['order.user_id', '=', 'user.id']
]
},
// Repeat for each join...
]);
顺序
需要一个 [阵列] 联 合 国 strings 或者说 objects数字 :
| 类型 | 注释 |
|---|---|
| {字符串 [] / 对象 []} | 'alias.column' 编号 : {'alias.column': 'asc' / 'desc'} |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.order([
'user.id',
{'user.first_name': 'desc'},
]);
组
需要一个 [阵列] 联 合 国 strings数字 :
| 类型 | 注释 |
|---|---|
| {字符串} [ | 'alias.column' |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.group([
'user.age',
'user.last_name',
]);
地点
需要一个 [阵列] 下列任何条件:
| 类型 | 注释 |
|---|---|
| [阵列] | [column, operator, value] 所有运算符见下文. |
| {对象} | {or / and: []} 有关键词的关闭 or 或者说 and 以组合各种条件. |
| {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 | 允许高级 SQL 函数 . 注意: 字符串没有逃脱 . |
回归自我,可连锁.
let query = new SimpleSqlBuilder()
.where([
['user.active', '=', true],
{
'or': [
['user.first_name', '=', '?'],
['user.last_name', '=', '?'],
]
},
])
.params([
'John',
'Doe'
]);
// Output: WHERE `user`.`active` = 1 AND (`user`.`first_name` = 'John' OR `user`.`last_name` = 'Doe')
运算符 :
=平等无相.['user.age', '=', '?']。 。 。 .<=少或等为相.['user.age', '<=', '?']。 。 。 .>=超过或等相.['user.age', '>=', '?']。 。 。 .<少过.['user.age', '<', '?']。 。 。 .>超过.['user.age', '>', '?']。 。 。 .!=不相等义.['user.age', '!=', '?']。 。 。 .<>不相等义.['user.age', '<>', '?']。 。 。 .is通常使用值为null。 。 。 .['user.age', 'is', null]。 。 。 .is not通常使用值为null。 。 。 .['user.age', 'is not', null]。 。 。 .between价值必须是 数组 联 合 国["start", "end"]数值.['user.age', 'between', ['?','?']]。 。 。 .in价值必须是 [阵列]['user.age', 'in', ['?','?','?']]。 。 。 .not in价值必须是 [阵列]['user.age', 'not in', ['?','?','?']]。 。 。 .contains执行 aLIKE列值的条件 包含 给定值value。 。 。 .['user.age', 'contains', '?']。 。 。 .begins执行 aLIKE列值的条件 开始 给定value。 。 。 .['user.age', 'beings', '?']。 。 。 .ends执行 aLIKE列值的条件 结束 给定value。 。 。 .['user.age', 'ends', '?']。 。 。 .
拥有
需要一个 [阵列] 条件 @ 见 where 更多信息.
回归自我,可连锁.
限制
需要一个 {数字} /{阵列}数字 : 10 编号 : [offset, limit]
回归自我,可连锁.
// Limit
let query = new SimpleSqlBuilder()
.limit(10);
---
// Offset, limit
let query = new SimpleSqlBuilder()
.limit([100, 15]);
段落
需要两个 [阵列] 价值,或 {对象} 联 合 国 key:value 双人.
回归自我,可连锁.
// Numeric array of params
let query = new SimpleSqlBuilder()
.where([
['user.first_name', '=', '?'],
['user.age', '=', '?'],
['user.nickname', '=', '?'],
])
.params([
'John',
30,
'John',
]);
---
// Placeholder key-value params
let query = new SimpleSqlBuilder()
.where([
['user.first_name', '=', '?:name'],
['user.age', '=', '?:age'],
['user.nickname', '=', '?:name'],
])
.params({
'name': 'John',
'age': 30
});
从杰森
静态方法 {\FN黑体\FS22\BORD1\SHAD0\3AHBE\4AH00\FSCX67\FSCY66\2CHFFFFFF\3CH808080}你觉得呢 与任何 高于公共方法 作为密钥并返回输出 build 方法.
回返 {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 完成的SQL语句.
let query = SimpleSqlBuilder.fromJson({
'select': {
'table': {'users': 'user'},
'columns': ['user.*']
},
'where': [
['user.first_name', '=', '?'],
],
'params': [
'John'
],
'limit': 10
});
console.log(query);
// Output: SELECT `user`.* FROM `users` AS `user` WHERE `user`.`first_name` = 'John' LIMIT 10
构建
回返 {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你来干什么 完成的SQL语句.
let query = new SimpleSqlBuilder()
.select({
'table': {'users': 'user'},
'columns': ['user.*']
})
.where([
['user.first_name', '=', '?'],
])
.limit(10)
.params([
'John',
])
.build();
console.log(query);
// Output: SELECT `user`.* FROM `users` AS `user` WHERE `user`.`first_name` = 'John' LIMIT 10