-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nicer separation depending on DB type; implement support for Postgres…
… and (hopefully) MySQL
- Loading branch information
1 parent
4d043a2
commit 30ca2d7
Showing
3 changed files
with
75 additions
and
59 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
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 | ||
import SQLKit | ||
import Fluent | ||
import Queues | ||
|
||
struct MySQLPop : PopQueryProtocol { | ||
func pop(db: Database, select: SQLExpression) -> EventLoopFuture<UUID?> { | ||
db.transaction { transaction in | ||
let database = transaction as! SQLDatabase | ||
|
||
var id: UUID? | ||
return database.execute(sql: select) { (row) -> Void in | ||
print("••• columns: \(row.allColumns)") | ||
id = try? row.decode(column: "\(FluentQueue.model.$id.key)", as: UUID.self) | ||
print("••• returned id \(id)") | ||
} | ||
.flatMap { | ||
if (id != nil) { | ||
let updateQuery = database | ||
.update(JobModel.schema) | ||
.set(SQLColumn.init("\(FluentQueue.model.$state.key)"), to: SQLBind.init(JobState.processing)) | ||
.set(SQLColumn.init("\(FluentQueue.model.$updatedAt.path.first!)"), to: SQLBind.init(Date())) | ||
.where(SQLColumn.init("\(FluentQueue.model.$id.key)"), .equal, SQLBind.init(id!)) | ||
.query | ||
return database.execute(sql: updateQuery) { (row) in } | ||
.map { id } | ||
} | ||
return database.eventLoop.makeSucceededFuture(nil) | ||
} | ||
|
||
|
||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Sources/QueuesFluentDriver/PopQueries/PostgresPopQuery.swift
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,29 @@ | ||
import Foundation | ||
import SQLKit | ||
import Fluent | ||
import Queues | ||
|
||
struct PostgresPop : PopQueryProtocol { | ||
func pop(db: Database, select: SQLExpression) -> EventLoopFuture<UUID?> { | ||
let database = db as! SQLDatabase | ||
let subQueryGroup = SQLGroupExpression.init(select) | ||
let query = database | ||
.update(JobModel.schema) | ||
.set(SQLColumn.init("\(FluentQueue.model.$state.key)"), to: SQLBind.init(JobState.processing)) | ||
.set(SQLColumn.init("\(FluentQueue.model.$updatedAt.path.first!)"), to: SQLBind.init(Date())) | ||
.where( | ||
SQLBinaryExpression(left: SQLColumn("\(FluentQueue.model.$id.key)"), op: SQLBinaryOperator.equal , right: subQueryGroup) | ||
) | ||
// Gross abuse | ||
.orWhere(SQLReturning.returning(column: FluentQueue.model.$id.key)) | ||
.query | ||
|
||
var id: UUID? | ||
return database.execute(sql: query) { (row) -> Void in | ||
id = try? row.decode(column: "\(FluentQueue.model.$id.key)", as: UUID.self) | ||
} | ||
.map { | ||
return id | ||
} | ||
} | ||
} |