Project Description
An object-oriented database for Swift with native queries.

** This is only a proof of concept by now **

The database uses sqlite as its backend. Any relational (SQL-capable) database can be used. The following class is used in the examples:

class Person : PersistentObject, Printable {
    var firstName:PersistentString = ""
    var lastName:PersistentString = ""
    var age:PersistentInt = 0
    required init() {}
    init(firstName fn:String, lastName ln:String, age a:Int) {
        firstName = PersistentString(fn)
        lastName = PersistentString(ln)
        age = PersistentInt(a)
    var description:String { return "'\(String(firstName)) \(String(lastName))' \(age)" }

To store an object to the database, use

let database = ObjectPile(fileName: "testdb.db")
let person = Person(firstName: "John", lastName: "Smith", age: 25)

The needed tables and columns are created/altered automatically by the library. To fetch all objects of a certain type use

let objects = database.objectsOfType(Person.self)

The result is an Array<Person> collection. The main reason for developing this proof of concept was to see how native queries could be used in Swift. A native query is a database query formulated in the programming language itself, that is, instead of using a string such as "SELECT * FROM Person" one can use the following Swift code:

let objects = database.query { (obj:Person) in obj.age <= 25 }

This looks like a regular lambda expression, but the database doesn't instantiate and evaluate every Person object. Instead it translates the Swift expression into a SELECT SQL query. That is, the underlying database can use its indexes to fetch only those objects that match the condition. The current implementation prints the translated SQL query to the console so one can see what the underlying SQL database evaluates.

Last edited Aug 31, 2015 at 4:35 PM by slonzok, version 16