- 鉴于移动端通常没有过于复杂的查询, 支持的 SQL 语义为:
select
,delete
,where
,orderBy
,groupBy
,limit
,offset
,or
,and
- 绝大部分 比较运算符 (排除 between, exists 之类)
- 支持
where
条件嵌套 - v2.0 +链式语法糖
- iOS 7.0+
- ARC only
- FMDB(https://github.com/ccgus/fmdb)
- LKDBHelper-SQLite-ORM(https://github.com/li6185377/LKDBHelper-SQLite-ORM)
pod 'LKDBSearchHelper', '~> 2.0'
LKSQLCompositeCondition *cond;
// single condition
cond = LKSQLCompositeCondition.clause.where.eq(@"colA", @0.0000001);
cond = LKSQLCompositeCondition.clause.where.like(@"colA", @"0.1");
cond = LKSQLCompositeCondition.clause.where.inStrs(@"colA", @[@"str1", @"str2", @"str3"]);
cond = LKSQLCompositeCondition.clause.where.inNums(@"colA", @[@1, @2, @3]);
// use `nil` for SQLite `null`
cond = LKSQLCompositeCondition.clause.where.eq(@"colA", nil);
// multi condition ( operator `and` can be omitted
cond = LKSQLCompositeCondition.clause
.where
.eq(@"colA", nil)
.or.neq(@"colB", nil)
.lt(@"colC", nil)
.and.lte(@"colD", nil)
.gt(@"colE", nil)
.or.gte(@"colF", nil)
.like(@"colG", nil) //
.or.isNot(@"colH", nil)
.inStrs(@"colI", nil)
.or.inNums(@"colJ", nil);
// Too Long? Let's use some eye candy
#define _SQLWhere LKSQLCompositeCondition.clause
#define _SQLSelect(_arg_) LKSQLSelect.clause.from(_arg_.class)
#define _SQLDelete(_arg_) LKSQLDelete.clause.from(_arg_.class)
// Note that `.where` is a candy, it just return `self`. so we omitted it in the macro above to save a method calling
// now continue...
// match All
cond = _SQLWhere.eq(@"colA", nil).matchAll(
@[
_SQLWhere.eq(@"colA", nil).neq(@"colAA", nil),
_SQLWhere.eq(@"colB", nil).neq(@"colBA", nil),
_SQLWhere.eq(@"colC", nil).neq(@"colCA", nil),
]
);
// match Any
cond = _SQLWhere.eq(@"colA", nil).matchAny(
@[
_SQLWhere.eq(@"colA", nil).neq(@"colAA", nil),
_SQLWhere.eq(@"colB", nil).neq(@"colBA", nil),
_SQLWhere.eq(@"colC", nil).neq(@"colCA", nil),
]
);
// nested condition
cond = _SQLWhere.eq(@"colA", nil).and.expr(
_SQLWhere.eq(@"colAA", nil).or.neq(@"colAB", nil).and.expr(
_SQLWhere.eq(@"colAAA", nil).or.neq(@"colAAB", nil)
// allow endless nested conditon if you can ...
)
)
.and.lte(@"colB", nil)
// SQL: Select
// exec SQL using `LKDBHelper` according to the class passed in,
// which in this case, would be `LKTest`
_SQLSelect(LKTest).where(
_SQLWhere.eq(@"MyAge", @"16")
).orderBy(nil).groupBy(nil).limit(0).offset(0).exec();
// BTW, you can specify `LKDBHelper` if `exec()` not fitting your project scheme
_SQLSelect(LKTest).where(
_SQLWhere.eq(@"MyAge", @"16")
).orderBy(nil).groupBy(nil).limit(0).offset(0).execIn(_get_using_LKDBHelper_from_somewhere_);
// Here, orderBy, groupBy should be RAW SQL format
// SQL: Delete
_SQLDelete(LKTest).where(
_SQLWhere.eq(@"MyAge", @"16")
).exec();
LKSQLSelect
&
LKSQLDelete
&
LKSQLCondition
&
LKSQLCompositeCondition
all these object could be printed using - toString
Also, you can use FMDatabase+Debug.h
in the DEMO project which intercepting FMDB SQL execution to do same thing ( But NOT full implemented
LKDBTransaction *transaction = [LKDBSQLite transaction];
[transaction update:obj]; // mark obj as `to be update`
[transaction insert:obj]; // mark obj as `to be insert`
[transaction delete:obj]; // mark obj as `to be delete`
[transaction batchUpdate:objArr];
[transaction batchInsert:objArr];
[transaction batchDelete:objArr];
[transaction execute]; // handle marked object above in transaction
LKDB requires raw SQL should be lowerCase, you could do that by flip the macro flag SQL_KEYWORD_UPPERCASE
in LKDBSQLConstant.h
Also, you could see some convenience macro there, but use with caution though (it may cause problem for Xcode auto-indent / auto-complete, and somehow tricky :)