diff --git a/.gitignore b/.gitignore index 29d0b557..cff3dd9c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ Packages .build - -Database - .DS_Store *.xcodeproj + diff --git a/Sources/Fluent/Database/Database.swift b/Sources/Fluent/Database/Database.swift new file mode 100644 index 00000000..6c7b326c --- /dev/null +++ b/Sources/Fluent/Database/Database.swift @@ -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()) +} diff --git a/Sources/Fluent/Database/Driver.swift b/Sources/Fluent/Database/Driver.swift new file mode 100644 index 00000000..9269724f --- /dev/null +++ b/Sources/Fluent/Database/Driver.swift @@ -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(_ query: Query) throws -> [[String: Value]] + + /** + Creates the `Schema` indicated + by the `Builder`. + */ + func schema(_ schema: Schema) throws +} diff --git a/Sources/Fluent/Database/PrintDriver.swift b/Sources/Fluent/Database/PrintDriver.swift new file mode 100644 index 00000000..62a11458 --- /dev/null +++ b/Sources/Fluent/Database/PrintDriver.swift @@ -0,0 +1,28 @@ +/** + A dummy `Driver` useful for developing. +*/ +public class PrintDriver: Driver { + public var idKey: String = "foo" + + public func query(_ query: Query) 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) + } +} \ No newline at end of file diff --git a/Sources/Fluent/Query/Query.swift b/Sources/Fluent/Query/Query.swift index 59f15699..50626487 100644 --- a/Sources/Fluent/Query/Query.swift +++ b/Sources/Fluent/Query/Query.swift @@ -49,7 +49,7 @@ public class Query { Creates a new `Query` with the `Model`'s database. */ - init() { + public init() { filters = [] action = .fetch database = T.database