-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enum (take 2) (#83) * add enum type * Add test, fix bug with using raw rather than string literal for enum values. * annotation in test case * take 2. protocol based from the start. no direct support on SQLDatabaseType. * enum updates * enum support + benchmark * fix tests * as sqlexpression * fail with warning if enum types not supported when using builders * test enum branches * revert branch Co-authored-by: Mathew Polzin <[email protected]>
- Loading branch information
1 parent
d09b552
commit 371bdf2
Showing
22 changed files
with
651 additions
and
109 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
extension SQLDatabase { | ||
public func alter(enum name: String) -> SQLAlterEnumBuilder { | ||
self.alter(enum: SQLIdentifier(name)) | ||
} | ||
|
||
public func alter(enum name: SQLExpression) -> SQLAlterEnumBuilder { | ||
.init(database: self, name: name) | ||
} | ||
} | ||
|
||
public final class SQLAlterEnumBuilder: SQLQueryBuilder { | ||
public var database: SQLDatabase | ||
public var alterEnum: SQLAlterEnum | ||
public var query: SQLExpression { | ||
self.alterEnum | ||
} | ||
|
||
init(database: SQLDatabase, name: SQLExpression) { | ||
self.database = database | ||
self.alterEnum = .init(name: name, value: nil) | ||
} | ||
|
||
public func add(value: String) -> Self { | ||
self.add(value: SQLLiteral.string(value)) | ||
} | ||
|
||
public func add(value: SQLExpression) -> Self { | ||
self.alterEnum.value = value | ||
return self | ||
} | ||
|
||
public func run() -> EventLoopFuture<Void> { | ||
guard self.database.dialect.enumSyntax == .typeName else { | ||
self.database.logger.warning("Database does not support enum types.") | ||
return self.database.eventLoop.makeSucceededFuture(()) | ||
} | ||
return self.database.execute(sql: self.query) { _ in } | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,69 @@ | ||
// MARK: Connection | ||
|
||
extension SQLDatabase { | ||
/// Creates a new `SQLCreateEnumBuilder`. | ||
/// | ||
/// conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner")... | ||
/// | ||
/// - parameters: | ||
/// - name: Name of ENUM type to create. | ||
/// - cases: The cases of the ENUM type. | ||
/// - returns: `SQLCreateEnumBuilder`. | ||
public func create(enum name: String) -> SQLCreateEnumBuilder { | ||
return self.create(enum: SQLIdentifier(name)) | ||
} | ||
|
||
/// Creates a new `SQLCreateEnumBuilder`. | ||
/// | ||
/// conn.create(enum: SQLIdentifier("meal"), cases: "breakfast", "lunch", "dinner")... | ||
/// | ||
/// - parameters: | ||
/// - name: Name of ENUM type to create. | ||
/// - cases: The cases of the ENUM type. | ||
/// - returns: `SQLCreateEnumBuilder`. | ||
public func create(enum name: SQLExpression) -> SQLCreateEnumBuilder { | ||
return .init(name: name, on: self) | ||
} | ||
} | ||
|
||
/// Builds `SQLCreateEnum` queries. | ||
/// | ||
/// conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner") | ||
/// .run() | ||
/// | ||
/// See `SQLColumnBuilder` and `SQLQueryBuilder` for more information. | ||
public final class SQLCreateEnumBuilder: SQLQueryBuilder { | ||
/// `CreateType` query being built. | ||
public var createEnum: SQLCreateEnum | ||
|
||
/// See `SQLQueryBuilder`. | ||
public var database: SQLDatabase | ||
|
||
/// See `SQLQueryBuilder`. | ||
public var query: SQLExpression { | ||
return self.createEnum | ||
} | ||
|
||
/// Creates a new `SQLCreateEnumBuilder`. | ||
init(name: SQLExpression, on database: SQLDatabase) { | ||
self.createEnum = .init(name: name, values: []) | ||
self.database = database | ||
} | ||
|
||
public func value(_ value: String) -> Self { | ||
self.value(SQLLiteral.string(value)) | ||
} | ||
|
||
public func value(_ value: SQLExpression) -> Self { | ||
self.createEnum.values.append(value) | ||
return self | ||
} | ||
|
||
public func run() -> EventLoopFuture<Void> { | ||
guard self.database.dialect.enumSyntax == .typeName else { | ||
self.database.logger.warning("Database does not support enum types.") | ||
return self.database.eventLoop.makeSucceededFuture(()) | ||
} | ||
return self.database.execute(sql: self.query) { _ in } | ||
} | ||
} |
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
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,70 @@ | ||
extension SQLDatabase { | ||
/// Creates a new `SQLDropEnumBuilder`. | ||
/// | ||
/// sql.drop(enum: "meal").run() | ||
/// | ||
/// - parameters: | ||
/// - type: Name of type to drop. | ||
/// - returns: `SQLDropEnumBuilder`. | ||
public func drop(enum name: String) -> SQLDropEnumBuilder { | ||
self.drop(enum: SQLIdentifier(name)) | ||
} | ||
|
||
/// Creates a new `SQLDropEnumBuilder`. | ||
/// | ||
/// sql.drop(enum: "meal").run() | ||
/// | ||
/// - parameters: | ||
/// - type: Name of type to drop. | ||
/// - returns: `SQLDropEnumBuilder`. | ||
public func drop(enum name: SQLExpression) -> SQLDropEnumBuilder { | ||
.init(name: name, on: self) | ||
} | ||
} | ||
|
||
/// Builds `SQLDropEnumBuilder` queries. | ||
/// | ||
/// conn.drop(type: "meal").run() | ||
/// | ||
/// See `SQLQueryBuilder` for more information. | ||
public final class SQLDropEnumBuilder: SQLQueryBuilder { | ||
/// `DropType` query being built. | ||
public var dropEnum: SQLDropEnum | ||
|
||
/// See `SQLQueryBuilder`. | ||
public var database: SQLDatabase | ||
|
||
/// See `SQLQueryBuilder`. | ||
public var query: SQLExpression { | ||
return self.dropEnum | ||
} | ||
|
||
/// Creates a new `SQLDropEnumBuilder`. | ||
init(name: SQLExpression, on database: SQLDatabase) { | ||
self.dropEnum = .init(name: name) | ||
self.database = database | ||
} | ||
|
||
/// The optional `IF EXISTS` clause suppresses the error that would normally | ||
/// result if the type does not exist. | ||
public func ifExists() -> Self { | ||
self.dropEnum.ifExists = true | ||
return self | ||
} | ||
|
||
/// The optional `CASCADE` clause drops other objects that depend on this type | ||
/// (such as table columns, functions, and operators), and in turn all objects | ||
/// that depend on those objects. | ||
public func cascade() -> Self { | ||
self.dropEnum.cascade = true | ||
return self | ||
} | ||
|
||
public func run() -> EventLoopFuture<Void> { | ||
guard self.database.dialect.enumSyntax == .typeName else { | ||
self.database.logger.warning("Database does not support enum types.") | ||
return self.database.eventLoop.makeSucceededFuture(()) | ||
} | ||
return self.database.execute(sql: self.query) { _ in } | ||
} | ||
} |
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
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
Oops, something went wrong.