Skip to content

Commit

Permalink
Merge pull request #19 from vapor/sql
Browse files Browse the repository at this point in the history
sql protocols
  • Loading branch information
tanner0101 authored Jun 19, 2018
2 parents 8c5c44a + 380f7e9 commit 3fd2c7f
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 800 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
.package(url: "https://github.com/vapor/sqlite.git", from: "3.0.0-rc"),
],
targets: [
.target(name: "FluentSQLite", dependencies: ["Async", "Fluent", "Service", "SQLite"]),
.target(name: "FluentSQLite", dependencies: ["Async", "FluentSQL", "Service", "SQLite"]),
.testTarget(name: "FluentSQLiteTests", dependencies: ["FluentBenchmark", "FluentSQLite", "SQLite"]),
]
)
149 changes: 1 addition & 148 deletions Sources/FluentSQLite/Exports.swift
Original file line number Diff line number Diff line change
@@ -1,149 +1,2 @@
@_exported import Fluent
@_exported import FluentSQL
@_exported import SQLite


//extension SQLiteDatabase: QuerySupporting {
// /// See `SQLDatabase`.
// public typealias QueryJoin = SQLQuery.DML.Join
//
// /// See `SQLDatabase`.
// public typealias QueryJoinMethod = SQLQuery.DML.Join.Method
//
// /// See `SQLDatabase`.
// public typealias Query = SQLQuery.DML
//
// /// See `SQLDatabase`.
// public typealias Output = [SQLiteColumn: SQLiteData]
//
// /// See `SQLDatabase`.
// public typealias QueryAction = SQLQuery.DML.Statement
//
// /// See `SQLDatabase`.
// public typealias QueryAggregate = String
//
// /// See `SQLDatabase`.
// public typealias QueryData = [SQLQuery.DML.Column: SQLQuery.DML.Value]
//
// /// See `SQLDatabase`.
// public typealias QueryField = SQLQuery.DML.Column
//
// /// See `SQLDatabase`.
// public typealias QueryFilterMethod = SQLQuery.DML.Predicate.Comparison
//
// /// See `SQLDatabase`.
// public typealias QueryFilterValue = SQLQuery.DML.Value
//
// /// See `SQLDatabase`.
// public typealias QueryFilter = SQLQuery.DML.Predicate
//
// /// See `SQLDatabase`.
// public typealias QueryFilterRelation = SQLQuery.DML.Predicate.Relation
//
// /// See `SQLDatabase`.
// public typealias QueryKey = SQLQuery.DML.Key
//
// /// See `SQLDatabase`.
// public typealias QuerySort = SQLQuery.DML.OrderBy
//
// /// See `SQLDatabase`.
// public typealias QuerySortDirection = SQLQuery.DML.OrderBy.Direction
//
// /// See `SQLDatabase`.
// public static func queryExecute(_ dml: SQLQuery.DML, on conn: SQLiteConnection, into handler: @escaping ([SQLiteColumn: SQLiteData], SQLiteConnection) throws -> ()) -> Future<Void> {
// // always cache the names first
// return conn.query(.init(.dml(dml))) { row in
// try handler(row, conn)
// }
// }
//
// /// See `SQLDatabase`.
// public static func queryDecode<D>(_ data: [SQLiteColumn: SQLiteData], entity: String, as decodable: D.Type, on conn: SQLiteConnection) -> Future<D>
// where D: Decodable
// {
// do {
// let decoded = try SQLiteRowDecoder().decode(D.self, from: data, table: entity)
// return conn.future(decoded)
// } catch {
// return conn.future(error: error)
// }
// }
//
// /// See `SQLDatabase`.
// public static func schemaColumnType(for type: Any.Type, primaryKey: Bool) -> SQLQuery.DDL.ColumnDefinition.ColumnType {
// var sqliteType: SQLiteFieldTypeStaticRepresentable.Type?
//
// if let optionalType = type as? AnyOptionalType.Type {
// sqliteType = optionalType.anyWrappedType as? SQLiteFieldTypeStaticRepresentable.Type
// } else {
// sqliteType = type as? SQLiteFieldTypeStaticRepresentable.Type
// }
//
// if let type = sqliteType {
// var name: String
// var attributes: [String] = []
//
// switch type.sqliteFieldType {
// case .blob: name = "BLOB"
// case .integer: name = "INTEGER"
// case .null: name = "NULL"
// case .real: name = "REAL"
// case .text: name = "TEXT"
// }
//
// if primaryKey {
// attributes.append("PRIMARY KEY")
// }
//
// return .init(name: name, attributes: attributes)
// } else {
// fatalError("Unsupported SQLite type: \(type).")
// }
// }
//
// /// See `SQLDatabase`.
// public static func schemaExecute(_ ddl: SQLQuery.DDL, on conn: SQLiteConnection) -> EventLoopFuture<Void> {
// // always cache the names first
// return conn.query(.init(.ddl(ddl))).transform(to: ())
// }
//
// /// See `SQLSupporting`.
// public static func enableForeignKeys(on conn: SQLiteConnection) -> Future<Void> {
// return conn.query("PRAGMA foreign_keys = ON;").transform(to: ())
// }
//
// /// See `SQLSupporting`.
// public static func disableForeignKeys(on conn: SQLiteConnection) -> Future<Void> {
// return conn.query("PRAGMA foreign_keys = OFF;").transform(to: ())
// }
//
// /// See `SQLSupporting`.
// public static func modelEvent<M>(event: ModelEvent, model: M, on conn: SQLiteConnection) -> Future<M> where SQLiteDatabase == M.Database, M: Model {
// var copy = model
// switch event {
// case .willCreate:
// if M.ID.self is UUID.Type {
// copy.fluentID = UUID() as? M.ID
// }
// case .didCreate:
// if M.ID.self is Int.Type {
// copy.fluentID = conn.lastAutoincrementID as? M.ID
// }
// default: break
// }
// return conn.future(copy)
// }
//
// /// See `SQLSupporting`.
// public static func transactionExecute<T>(_ transaction: @escaping (SQLiteConnection) throws -> Future<T>, on conn: SQLiteConnection) -> Future<T> {
// return conn.query("BEGIN TRANSACTION").flatMap { _ -> Future<T> in
// return try transaction(conn).flatMap { res -> Future<T> in
// return conn.query("COMMIT TRANSACTION").transform(to: res)
// }.catchFlatMap { err -> Future<T> in
// return conn.query("ROLLBACK TRANSACTION").map { query -> T in
// // still fail even tho rollback succeeded
// throw err
// }
// }
// }
// }
//}
57 changes: 57 additions & 0 deletions Sources/FluentSQLite/FluentSQLiteQuery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public enum FluentSQLiteQueryStatement: FluentSQLQueryStatement {
public static var insert: FluentSQLiteQueryStatement { return ._insert }
public static var select: FluentSQLiteQueryStatement { return ._select }
public static var update: FluentSQLiteQueryStatement { return ._update }
public static var delete: FluentSQLiteQueryStatement { return ._delete }

public var isInsert: Bool {
switch self {
case ._insert: return true
default: return false
}
}

case _insert
case _select
case _update
case _delete
}

public struct FluentSQLiteQuery: FluentSQLQuery {
public typealias Statement = FluentSQLiteQueryStatement
public typealias TableIdentifier = SQLiteTableIdentifier
public typealias Expression = SQLiteExpression
public typealias SelectExpression = SQLiteSelectExpression
public typealias Join = SQLiteJoin
public typealias OrderBy = SQLiteOrderBy
public typealias GroupBy = SQLiteGroupBy
public typealias RowDecoder = SQLiteRowDecoder

public var statement: Statement
public var table: TableIdentifier
public var keys: [SelectExpression]
public var values: [String : Expression]
public var joins: [Join]
public var predicate: Expression?
public var orderBy: [OrderBy]
public var groupBy: [GroupBy]
public var limit: Int?
public var offset: Int?
public var defaultBinaryOperator: GenericSQLBinaryOperator

public static func query(_ statement: Statement, _ table: TableIdentifier) -> FluentSQLiteQuery {
return .init(
statement: statement,
table: table,
keys: [],
values: [:],
joins: [],
predicate: nil,
orderBy: [],
groupBy: [],
limit: nil,
offset: nil,
defaultBinaryOperator: .and
)
}
}
34 changes: 34 additions & 0 deletions Sources/FluentSQLite/FluentSQLiteSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public enum FluentSQLiteSchemaStatement: FluentSQLSchemaStatement {
public static var createTable: FluentSQLiteSchemaStatement { return ._createTable }
public static var alterTable: FluentSQLiteSchemaStatement { return ._alterTable }
public static var dropTable: FluentSQLiteSchemaStatement { return ._dropTable }

case _createTable
case _alterTable
case _dropTable
}

public struct FluentSQLiteSchema: FluentSQLSchema {
public typealias Statement = FluentSQLiteSchemaStatement
public typealias TableIdentifier = SQLiteTableIdentifier
public typealias ColumnDefinition = SQLiteColumnDefinition
public typealias TableConstraint = SQLiteTableConstraint

public var statement: Statement
public var table: TableIdentifier
public var columns: [SQLiteColumnDefinition]
public var deleteColumns: [SQLiteColumnIdentifier]
public var constraints: [SQLiteTableConstraint]
public var deleteConstraints: [SQLiteTableConstraint]

public static func schema(_ statement: Statement, _ table: TableIdentifier) -> FluentSQLiteSchema {
return .init(
statement: statement,
table: table,
columns: [],
deleteColumns: [],
constraints: [],
deleteConstraints: []
)
}
}
29 changes: 0 additions & 29 deletions Sources/FluentSQLite/SQLiteDatabase+Contains.swift

This file was deleted.

24 changes: 2 additions & 22 deletions Sources/FluentSQLite/SQLiteDatabase+JoinSupporting.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
extension SQLiteDatabase: JoinSupporting {
/// See `JoinSupporting`.
public typealias QueryJoin = SQLiteQuery.JoinClause.Join
public typealias QueryJoin = SQLiteJoin

/// See `JoinSupporting`.
public typealias QueryJoinMethod = SQLiteQuery.JoinClause.Join.Operator

/// See `JoinSupporting`.
public static var queryJoinMethodDefault: SQLiteQuery.JoinClause.Join.Operator {
return .inner
}

/// See `JoinSupporting`.
public static func queryJoin(_ method: SQLiteQuery.JoinClause.Join.Operator, base: SQLiteQuery.QualifiedColumnName, joined: SQLiteQuery.QualifiedColumnName) -> SQLiteQuery.JoinClause.Join {
return .init(
natural: false,
method,
table: .table(.init(table: .init(table: .init(name: joined.table!)))),
constraint: .condition(.binary(.column(base), .equal, .column(joined)))
)
}

/// See `JoinSupporting`.
public static func queryJoinApply(_ join: SQLiteQuery.JoinClause.Join, to query: inout SQLiteQuery.FluentQuery) {
query.joins.append(join)
}
public typealias QueryJoinMethod = SQLiteJoinMethod
}
Loading

0 comments on commit 3fd2c7f

Please sign in to comment.