-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
129fe21
commit 319012f
Showing
5 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
Packages | ||
.build | ||
|
||
Database | ||
|
||
.DS_Store | ||
*.xcodeproj | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters