-
Notifications
You must be signed in to change notification settings - Fork 14
Database
A generic type to represent any database you might be interacting
with. Currently, the only two implementations are
PostgresDatabase
and MySQLDatabase
. The QueryBuilder and Rune
ORM are built on top of this abstraction.
public protocol Database
Any migrations associated with this database, whether applied yet or not.
var migrations: [Migration]
Functions around compiling SQL statments for this database's SQL dialect when using the QueryBuilder or Rune.
var grammar: Grammar
Start a QueryBuilder query on this database. See Query
or
QueryBuilder guides.
func query() -> Query
Usage:
database.query()
.from(table: "users")
.where("id" == 1)
.first()
.whenSuccess { row in
guard let row = row else {
return print("No row found :(")
}
print("Got a row with fields: \(row.allColumns)")
}
The start of a QueryBuilder Query
.
Run a parameterized query on the database. Parameterization helps protect against SQL injection.
func runRawQuery(_ sql: String, values: [DatabaseValue]) -> EventLoopFuture<[DatabaseRow]>
Usage:
// No bindings
db.runRawQuery("SELECT * FROM users where id = 1")
.whenSuccess { rows
guard let first = rows.first else {
return print("No rows found :(")
}
print("Got a user row with columns \(rows.allColumns)!")
}
// Bindings, to protect against SQL injection.
db.runRawQuery("SELECT * FROM users where id = ?", values = [.int(1)])
.whenSuccess { rows
...
}
- sql: The SQL string with '?'s denoting variables that should be parameterized.
- values: An array,
[DatabaseValue]
, that will replace the '?'s insql
. Ensure there are the same amnount of values as there are '?'s insql
.
An EventLoopFuture
of the rows returned by the query.
Called when the database connection will shut down.
func shutdown() throws
Any error that occurred when shutting down.
Generated at 2021-01-13T22:24:59-0800 using swift-doc 1.0.0-beta.5.
Alchemy
Types
- AlterTableBuilder
- BCryptDigest
- BasicAuthMiddleware
- BcryptError
- BelongsToRelationship
- CORSMiddleware
- CORSMiddleware.AllowOriginSetting
- CORSMiddleware.Configuration
- ColumnType
- CreateColumn
- CreateColumnBuilder
- CreateIndex
- CreateTableBuilder
- DatabaseConfig
- DatabaseError
- DatabaseField
- DatabaseKeyMappingStrategy
- DatabaseValue
- DayUnit
- Env
- FrequencyTyped
- Grammar
- HTTPAuth
- HTTPAuth.Basic
- HTTPAuth.Bearer
- HTTPBody
- HTTPError
- HasManyRelationship
- HasOneRelationship
- HasRelationship
- HourUnit
- JoinClause
- JoinType
- Launch
- Log
- MIMEType
- MinuteUnit
- ModelQuery
- MySQLDatabase
- Operator
- OrderClause
- OrderClause.Sort
- OrderedDictionary
- PapyrusClientError
- PathParameter
- PathParameter.DecodingError
- PostgresDatabase
- Query
- Request
- Response
- Router
- RuneError
- SQL
- SQLJSON
- Scheduler
- Schema
- SecondUnit
- Services
- Socket
- StaticFileMiddleware
- StringLength
- Thread
- TokenAuthMiddleware
- WeekUnit
- Weekday
- WhereBoolean
- WhereColumn
- WhereIn
- WhereIn.InType
- WhereNested
- WhereRaw
- WhereValue