This framework is beging to rewritte, with a new API more safe and more reliable.
Camembert is a toolkit written in swift, for using sqlite3 easier. Is is available for OSX and iOS.
First you need to add a bridging-header to your project. If it is already set, import Camembert.
#import "Camembert.h"
If you need to add one, follow these instructions:
- Add libsqlite3.0.tbd and libsqlite3.tbd under Linked Frameworks and Libraries in project settings.
- Create a new header file.
- Go into the settings of your project in the category build setting.
- Search Objective-C Bridging Header with the search bar.
- Add the following line : headerFileName.h
- Then import
#import "Camembert.h"
in your header file.
When the Bridging Header is created, simply copy the files *.m and *.swift from the directory sources.
First thing to do, is to initialise your sqlite3 database. To do this you must make a call before any other use. The database will be created automatically if it does not exist
Camembert.initDataBase("dataBase.sql")
And voila !, you are ready to use Camembert.
You need now to create a class, matching with your table. For doing that, you MUST inherit of CamembertModel class. And use the typealias for create your rows. You have to provide defaults values.
typealias INTEGER = Int
typealias REAL = Float
typealias TEXT = String
typealias DATE_TIME = NSDate
typealias BIT = Bool
This is an example of a table Book:
class Book :CamembertModel {
var title :TEXT = ""
var numberPage :INTEGER = 0
var currentPage :INTEGER = 0
}
The Book class corresponds to model, and will be associated with the table Book in the database.
var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()
As you can see for creating a new element, you just need to create a new object, and call the push method. If the table doest not exist yet, it will be created automatly.
var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()
//...
newBook.currentPage = 103
newBook.update()
To change something in a existing element, you just need to call the update method. To do this you need to have an object already created. Or you can use the constructor with a specific element based on the ID:
var book = Book(id: 4)
book.currentPage = 103
book.update()
var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()
//...
newBook.remove()
var book = Book(id: 4)
book.remove()
Just call the remove method, for remove the element in the Table.
var numberElement :Int = Book.numberElement()
println("number books : \(numberElement)")
For select elements in the tables, you need to perform a request. For doing this easily you can use the enum Select.
enum Select {
case SelectAll(OrderOperator, String)
case CustomRequest(String)
case Limit(Int, OrderOperator, String)
case Between(Int, Int, OrderOperator, String)
case Where(String, Operator, AnyObject, OrderOperator, String)
}
- SelectAll(OrderOperator, String): will return all element in the table
- CustomRequest(String): You can use there your own SQL request
- Limit(Int, OrderOperator, String): will return a limited number of element
- Between(Int, Int, OrderOperator, String): will return all the element between the interval
- Where (ColumnName: String, Operator: Larger,equal..etc, Value, OrderOperator, ColumnToOrderBy): will return elements that value of Column specified matches the passed value ("Value")
//display titles of the library (if order by is empty like in example below, order will be done on id column)
for currentElement in Book.select(selectRequest: Select.SelectAll, order: OrderOperator.Ascending, orderby: "") {
println("current Book's title: \((currentElement as Book).title)")
}
```Swift
//display titles of the library
for currentElement in Book.select(selectRequest: Select.SelectAll, OrderOperator.Ascending, "") {
println("current Book's title: \((currentElement as Book).title)")
}
//reset currentPage
for currentElement in Book.select(selectRequest: Select.CustomRequest("SELECT * FROM Book WHERE currentPage > 0")) {
(currentElement as Book).currentPage = 0
(currentElement as Book).update()
}
//How To us extension methods:
var myArray = Array<AnyObject>();
if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "LastName")){
myArray = m_array.Take(10);
}
if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
let myUserModel = (m_array.FirstOrDefault() as UserModel);
}
if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
let myUserModel = (m_array.LastOrDefault() as UserModel);
}
if var m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
myArray = m_array.TakeRange(1, offset: 10);
}
if var m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
myArray = m_array.Union(Array())
}
let FirstName = (myArray as Array)[0].FirstName;
You get an array of all tables present in your database:
let listTable = Camembert.getListTable()
Camembert, will improve, here's the first version.
Rémi ROBERT, [email protected] Omar Bizreh, [email protected] Camembert is available under the MIT license. See the LICENSE file for more info.