emmet7life / Swift-Kuery

SQL database abstraction layer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift-Kuery

SQL database abstraction layer

Build Status - Master Mac OS X Linux Apache 2

Summary

Swift-Kuery is a pluggable SQL database driver/SDK abstraction layer. Its main idea is to unify the APIs to the various relational databases, providing a Swifty yet SQL-like API.

While Swift-Kuery is not an Object-Relational Mapping (ORM), it provides a great basis to build an ORM. It is also useful if you don't want to commit to a specific database, allowing easy switch between databases.

Swift-Kuery is easy to learn, consumable framework that comes with a set of implemented plugins.

Table of Contents

Example

This example demonstrates how to execute an SQL query using Swift-Kuery and Swift-Kuery-PostgreSQL. It assumes there is a PostgreSQL server running on localhost:5432, that contains a table called Grades:

 id   |  course   | grade
------+-----------+-------
12345 | physics   |    82
12345 | chemistry |    90
12345 | history   |    98
78901 | history   |   100
78901 | chemistry |    94
78901 | physics   |    90
24680 | physics   |    74
24680 | chemistry |    92
24680 | history   |    90

First we import Swift-Kuery and Swift-Kuery-PostgreSQL:

import SwiftKuery
import SwiftKueryPostgreSQL

Now we create a Table that corresponds to our Grades table in PostgreSQL - we set the table's name and its columns:

class Grades : Table {
    let tableName = "Grades"
    let id = Column("id")
    let course = Column("course")
    let grade = Column("grade")
}
let grades = Grades()

Next we create a connection to PostgreSQL and connect to it. Note that the API is asynchronous:

let connection = PostgreSQLConnection(host: "localhost", port: 5432, options: [.userName("username"), .password("password")])
connection.connect() { error: QueryError in
  if let error = error {
    print(error)
  }
  else {
    // Build and execute your query here.
  }

Now lets build the query. Suppose we want to retrieve the average grades for courses with average greater than 90, and sort the results by the average ascending. Here is the SQL query we need to build:

SELECT course, ROUND(AVG(grade), 1) AS "average" FROM grades
GROUP BY course
HAVING AVG(grade) > 90
ORDER BY AVG(grade) ASC

Note, that we also round the average grades and alias this column in the result as "average".

Here is how to create such query using Swift-Kuery:

let query = Select(grades.course, round(avg(grades.grade), to: 1).as("average"), from: grades)
            .group(by: grades.course)
            .having(avg(grades.grade) > 90)
            .order(by: .ASC(avg(grades.grade)))

As you can see, it is very similar to the SQL query syntax.

Now we execute the created query on our PostgreSQL connection:

connection.execute(query: query) { result: QueryResult in
  if let resultSet = queryResult.asResultSet {
    for title in resultSet.titles {
      // The column names of the result.
    }
    for row in resultSet.rows {
      for value in row {
        ...
      }
    }
  }
  else if let queryError = result.asError {
      // Something went wrong.
  }
}

The expected result is:

course     average
chemistry  92.0
history    96.0

Query Examples

Lets see more examples of how to build and execute SQL queries using Swift-Kuery.

Classes used in the examples:

class T1 {
  let tableName = "t1"
  let a = Column("a")
  let b = Column("b")
}

class T2 {
  let tableName = "t2"
  let c = Column("c")
  let b = Column("b")
}

__SELECT * FROM t1;__
let t1 = T1()

let s = Select(from: t1)

s.execute(connection) { queryResult in
  if let resultSet = queryResult.asResultSet {
    for title in resultSet.titles {
      ...
    }
    for row in resultSet.rows {
      for value in row {
        ...
      }
    }
  }
  else if let queryError = result.asError {
    ...
  }
}

__SELECT a, b FROM t1 WHERE (a LIKE '%b' OR a = 'apple') AND b > 5 ORDER BY b ASC, a DESC OFFSET 5;__
...
let s = Select(t1.a, t1.b, from: t1)
  .where((t1.a.like("b%") || t1.a == "apple") && t1.b > 5)
  .order(by: .ASC(t1.b), .DESC(t1.a))
  .offset(5)

connection.execute(query: s) { queryResult in
   ...
}

__SELECT UCASE(a) AS name FROM t1 WHERE b >= 0 GROUP BY a HAVING SUM(b) > 3 ORDER BY a DESC;__
...
let s = Select(ucase(t1.a).as("name"), from: t1)
  .where(t1.b >= 0)
  .group(by: t1.a)
  .having(sum(t1.b) > 3)
  .order(by: .DESC(t1.a))
...

__INSERT INTO t1 VALUES ('apple', 10), ('apricot', 3), ('banana', 17);__
...
let i = Insert(into: t1, rows: [["apple", 10], ["apricot", 3], ["banana", 17]])

connection.execute(query: i) { queryResult in
  if queryResult.success  {
    ...
  }
  else if let queryError = result.asError {
    ...
  }
}

__INSERT INTO t1 VALUES ('apple', 10);__
...
let i = Insert(into: t1, values: "apple", 10)
...

__INSERT INTO t1 (a, b) VALUES ('apricot', '3');__
...
let i = Insert(into: t1, valueTuples: (t1.a, "apricot"), (t1.b, "3"))
...

__INSERT INTO t1 (a, b) VALUES ('apricot', '3');__
...
let i = Insert(into: t1, columns: [t1.a, t1.b], values: ["apricot", 3])
...

__UPDATE t1 SET a = 'peach', b = 2 WHERE a = 'banana' RETURNING b;__
...
let u = Update(t1, set: [(t1.a, "peach"), (t1.b, 2)])
  .where(t1.a == "banana")
  .returning(t1.b)
...

__SELECT * FROM t1 AS left LEFT JOIN t2 AS right ON left.b = right.b;__
let t1 = T1()
let t2 = T2()

let leftTable = t1.as("left")
let rightTable = t2.as("right")
let s2 = Select(from: leftTable)
  .leftJoin(rightTable)
  .on(leftTable.b == rightTable.b)
...

__SELECT * FROM t1 JOIN t2 USING (b);__
...
let s2 = Select(from: t1)
  .join(t2)
  .using(t1.b)
...

__INSERT INTO t1 VALUES (@0,@1);__
let i = Insert(into: t1, values: Parameter(), Parameter())

connection.execute(query: i1, parameters: "banana", 28) { queryResult in
  ...
}

__INSERT INTO t1 VALUES (@fruit,@number);__
let i = Insert(into: t1, values: Parameter("fruit"), Parameter("number"))

connection.execute(query: i1, parameters: ["number" : 28, "fruit" : "banana"]) { queryResult in
  ...
}

__Raw query:__
connection.execute("CREATE TABLE myTable (a varchar(40), b integer)") {  queryResult in
  ...
}

__SELECT LEFT(a, 2) as raw FROM t1 WHERE b >= 0 GROUP BY a HAVING sum(b) > 3 ORDER BY a DESC;__
...
let s = Select(RawField("LEFT(a, 2) as raw"), from: t1)
  .where("b >= 0")
  .group(by: t1.a)
  .having("sum(b) > 3")
  .order(by: .DESC(t1.a))
...

__SELECT * FROM t1 WHERE b >= ANY (SELECT b FROM t2);__
...
let s = Select(from: t1)
  .where(t1.b >= any(Select(t2.b, from: t2)))
...

__SELECT * FROM t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE b < 8);__
...
let s = Select(from: t1)
  .where(notExists(Select(from: t2).where(t2.b < 8)))
...

__SELECT c FROM t2 GROUP BY c HAVING SUM(b) NOT IN (SELECT b FROM t1 WHERE a = 'apple');__
...
let s = Select(t2.c, from: t2)
    .group(by: t2.c)
    .having(sum(t2.b).notIn(Select(t1.b, from: t1).where(t1.a == "apple")))
...

List of plugins:

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

About

SQL database abstraction layer

License:Apache License 2.0


Languages

Language:Swift 95.1%Language:Shell 4.9%