Skip to content

Commit

Permalink
add database files
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Jun 20, 2016
1 parent 129fe21 commit 319012f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Packages
.build

Database

.DS_Store
*.xcodeproj

34 changes: 34 additions & 0 deletions Sources/Fluent/Database/Database.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation

/**
References a database with a single `Driver`.
Statically maps `Model`s to `Database`s.
*/
public class Database {
/**
The `Driver` powering this database.
Responsible for executing queries.
*/
public let driver: Driver

/**
Creates a `Database` with the supplied
`Driver`. This cannot be changed later.
*/
public init(driver: Driver) {
self.driver = driver
}

/**
Maps `Model` names to their respective
`Database`. This allows multiple models
in the same application to use different
methods of data persistence.
*/
public static var map: [String: Database] = [:]

/**
The default database for all `Model` types.
*/
public static var `default`: Database = Database(driver: PrintDriver())
}
31 changes: 31 additions & 0 deletions Sources/Fluent/Database/Driver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
A `Driver` execute queries
and returns an array of results.
It is responsible for interfacing
with the data store powering Fluent.
*/
public protocol Driver {
/**
The string value for the
default identifier key.
The `idKey` will be used when
`Model.find(_:)` or other find
by identifier methods are used.
*/
var idKey: String { get }

/**
Executes a `Query` from and
returns an array of results fetched,
created, or updated by the action.
*/
@discardableResult
func query<T: Model>(_ query: Query<T>) throws -> [[String: Value]]

/**
Creates the `Schema` indicated
by the `Builder`.
*/
func schema(_ schema: Schema) throws
}
28 changes: 28 additions & 0 deletions Sources/Fluent/Database/PrintDriver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
A dummy `Driver` useful for developing.
*/
public class PrintDriver: Driver {
public var idKey: String = "foo"

public func query<T: Model>(_ query: Query<T>) throws -> [[String : Value]] {

let sql = SQL(query: query)
let serializer = GeneralSQLSerializer(sql: sql)

let (statement, values) = serializer.serialize()
print("[Print driver]")

print("Statement: \(statement) Values: \(values)")

print("Table \(query.entity)")
print("Action \(query.action)")
print("Filters \(query.filters)")
print()

return []
}

public func schema(_ schema: Schema) throws {
//let sql = SQL(builder: builder)
}
}
2 changes: 1 addition & 1 deletion Sources/Fluent/Query/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Query<T: Model> {
Creates a new `Query` with the
`Model`'s database.
*/
init() {
public init() {
filters = []
action = .fetch
database = T.database
Expand Down

0 comments on commit 319012f

Please sign in to comment.