sqlx 0.3.4

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.3.4 - 2020-04-10

### Fixed

 - [[#241]] Type name for custom enum is not always attached to TypeInfo in PostgreSQL
 
 - [[#237]] [[#238]] User-defined type name matching is now case-insensitive in PostgreSQL [[@qtbeee]]
 
 - [[#231]] Handle empty queries (and those with comments) in SQLite
 
 - [[#228]] Provide `MapRow` implementations for functions (enables `.map(|row| ...)` over `.try_map(|row| ...)`)

### Added

 - [[#234]] Add support for `NUMERIC` in MySQL with the `bigdecimal` crate [[@xiaopengli89]]
 
 - [[#227]] Support `#[sqlx(rename = "new_name")]` on struct fields within a `FromRow` derive [[@sidred]]

[#228]: https://github.com/launchbadge/sqlx/issues/228
[#231]: https://github.com/launchbadge/sqlx/issues/231 
[#237]: https://github.com/launchbadge/sqlx/issues/237
[#241]: https://github.com/launchbadge/sqlx/issues/241

[#227]: https://github.com/launchbadge/sqlx/pull/227
[#234]: https://github.com/launchbadge/sqlx/pull/234
[#238]: https://github.com/launchbadge/sqlx/pull/238

## 0.3.3 - 2020-04-01

### Fixed

 - [[#214]] Handle percent-encoded usernames in a database URL [[@jamwaffles]]

### Changed

 - [[#216]] Mark `Cursor`, `Query`, `QueryAs`, `query::Map`, and `Transaction` as `#[must_use]` [[@Ace4896]]
 
 - [[#213]] Remove matches dependency and use matches macro from std [[@nrjais]]

[#216]: https://github.com/launchbadge/sqlx/pull/216
[#214]: https://github.com/launchbadge/sqlx/pull/214
[#213]: https://github.com/launchbadge/sqlx/pull/213

## 0.3.2 - 2020-03-31

### Fixed

 - [[#212]] Removed sneaky `println!` in `MySqlCursor`
 
[#212]: https://github.com/launchbadge/sqlx/issues/212

## 0.3.1 - 2020-03-30

### Fixed

 - [[#203]] Allow an empty password for MySQL
 
 - [[#204]] Regression in error reporting for invalid SQL statements on PostgreSQL
 
 - [[#200]] Fixes the incorrect handling of raw (`r#...`) fields of a struct in the `FromRow` derive [[@sidred]]

[#200]: https://github.com/launchbadge/sqlx/pull/200 
[#203]: https://github.com/launchbadge/sqlx/issues/203
[#204]: https://github.com/launchbadge/sqlx/issues/204

## 0.3.0 - 2020-03-29

### Breaking Changes

 - `sqlx::Row` now has a lifetime (`'c`) tied to the database connection. In effect, this means that you cannot store `Row`s or collect
   them into a collection. `Query` (returned from `sqlx::query()`) has `map()` which takes a function to map from the `Row` to
   another type to make this transition easier.

   In 0.2.x

   ```rust
   let rows = sqlx::query("SELECT 1")
       .fetch_all(&mut conn).await?;
   ```

   In 0.3.x

   ```rust
   let values: Vec<i32> = sqlx::query("SELECT 1")
       .map(|row: PgRow| row.get(0))
       .fetch_all(&mut conn).await?;
   ```

   To assist with the above, `sqlx::query_as()` now supports querying directly into tuples (up to 9 elements) or 
   struct types with a `#[derive(FromRow)]`.

   ```rust
   // This extension trait is needed until a rust bug is fixed
   use sqlx::postgres::PgQueryAs;
   
   let values: Vec<(i32, bool)> = sqlx::query_as("SELECT 1, false")
       .fetch_all(&mut conn).await?;
   ```

 - `HasSqlType<T>: Database` is now `T: Type<Database>` to mirror `Encode` and `Decode`

 - `Query::fetch` (returned from `query()`) now returns a new `Cursor` type. `Cursor` is a Stream-like type where the
   item type borrows into the stream (which itself borrows from connection). This means that using `query().fetch()` you can now
   stream directly from the database with **zero-copy** and **zero-allocation**.
   
 - Remove `PgTypeInfo::with_oid` and replace with `PgTypeInfo::with_name`

### Added

 - Results from the database are now zero-copy and no allocation beyond a shared read buffer
   for the TCP stream ( in other words, almost no per-query allocation ). Bind arguments still 
   do allocate a buffer per query.

 - [[#129]] Add support for [SQLite]https://sqlite.org/index.html. Generated code should be very close to normal use of the C API.

      * Adds `Sqlite`, `SqliteConnection`, `SqlitePool`, and other supporting types

 - [[#97]] [[#134]] Add support for user-defined types. [[@Freax13]]

      * Rust-only domain types or transparent wrappers around SQL types. These may be used _transparently_ inplace of
        the SQL type.

          ```rust
          #[derive(sqlx::Type)]
          #[repr(transparent)]
          struct Meters(i32);
          ```

      * Enumerations may be defined in Rust and can match SQL by integer discriminant or variant name.

          ```rust
          #[derive(sqlx::Type)]
          #[repr(i32)] // Expects a INT in SQL
          enum Color { Red = 1, Green = 2, Blue = 3 }
          ```

          ```rust
          #[derive(sqlx::Type)]
          #[sqlx(rename = "TEXT")] // May also be the name of a user defined enum type
          #[sqlx(rename_all = "lowercase")] // similar to serde rename_all
          enum Color { Red, Green, Blue } // expects 'red', 'green', or 'blue'
          ```

      * **Postgres** further supports user-defined composite types.

          ```rust
          #[derive(sqlx::Type)]
          #[sqlx(rename = "interface_type")]
          struct InterfaceType {
              name: String,
              supplier_id: i32,
              price: f64
          }
          ```

 - [[#98]] [[#131]] Add support for asynchronous notifications in Postgres (`LISTEN` / `NOTIFY`). [[@thedodd]]

      * Supports automatic reconnection on connection failure.

      * `PgListener` implements `Executor` and may be used to execute queries. Be careful however as if the
        intent is to handle and process messages rapidly you don't want to be tying up the connection
        for too long. Messages received during queries are buffered and will be delivered on the next call
        to `recv()`.

   ```rust
   let mut listener = PgListener::new(DATABASE_URL).await?;

   listener.listen("topic").await?;

   loop {
       let message = listener.recv().await?;

       println!("payload = {}", message.payload);
   }
   ```
   
 - Add _unchecked_ variants of the query macros. These will still verify the SQL for syntactic and 
   semantic correctness with the current database but they will not check the input or output types.
   
   This is intended as a temporary solution until `query_as!` is able to support user defined types.
   
     * `query_as_unchecked!`
     * `query_file_as_unchecked!`
     
 - Add support for many more types in Postgres
 
   - `JSON`, `JSONB` [[@oeb25]]
   - `INET`, `CIDR` [[@PoiScript]]
   - Arrays [[@oeb25]]
   - Composites ( Rust tuples or structs with a `#[derive(Type)]` )
   - `NUMERIC` [[@abonander]]
   - `OID` (`u32`)
   - `"CHAR"` (`i8`)
   - `TIMESTAMP`, `TIMESTAMPTZ`, etc. with the `time` crate [[@utter-step]]
   - Enumerations ( Rust enums with a `#[derive(Type)]` ) [[@Freax13]]

### Changed

 - `Query` (and `QueryAs`; returned from `query()`, `query_as()`, `query!()`, and `query_as!()`) now will accept both `&mut Connection` or
   `&Pool` where as in 0.2.x they required `&mut &Pool`.

 - `Executor` now takes any value that implements `Execute` as a query. `Execute` is implemented for `Query` and `QueryAs` to mean
   exactly what they've meant so far, a prepared SQL query. However, `Execute` is also implemented for just `&str` which now performs
   a raw or unprepared SQL query. You can further use this to fetch `Row`s from the database though it is not as efficient as the
   prepared API (notably Postgres and MySQL send data back in TEXT mode as opposed to in BINARY mode).

   ```rust
   use sqlx::Executor;
   
   // Set the time zone parameter
   conn.execute("SET TIME ZONE LOCAL;").await

   // Demonstrate two queries at once with the raw API
   let mut cursor = conn.fetch("SELECT 1; SELECT 2");
   let row = cursor.next().await?.unwrap();
   let value: i32 = row.get(0); // 1
   let row = cursor.next().await?.unwrap();
   let value: i32 = row.get(0); // 2
   ```

### Removed

 - `Query` (returned from `query()`) no longer has `fetch_one`, `fetch_optional`, or `fetch_all`. You _must_ map the row using `map()` and then
   you will have a `query::Map` value that has the former methods available.

   ```rust
   let values: Vec<i32> = sqlx::query("SELECT 1")
       .map(|row: PgRow| row.get(0))
       .fetch_all(&mut conn).await?;
   ```

### Fixed

 - [[#62]] [[#130]] [[#135]] Remove explicit set of `IntervalStyle`. Allow usage of SQLx for CockroachDB and potentially PgBouncer. [[@bmisiak]]

 - [[#108]] Allow nullable and borrowed values to be used as arguments in `query!` and `query_as!`. For example, where the column would
   resolve to `String` in Rust (TEXT, VARCHAR, etc.), you may now use `Option<String>`, `Option<&str>`, or `&str` instead. [[@abonander]]

 - [[#108]] Make unknown type errors far more informative. As an example, trying to `SELECT` a `DATE` column will now try and tell you about the
   `chrono` feature. [[@abonander]]

   ```
   optional feature `chrono` required for type DATE of column #1 ("now")
   ```

[#62]: https://github.com/launchbadge/sqlx/issues/62
[#130]: https://github.com/launchbadge/sqlx/issues/130

[#98]: https://github.com/launchbadge/sqlx/pull/98
[#97]: https://github.com/launchbadge/sqlx/pull/97
[#134]: https://github.com/launchbadge/sqlx/pull/134
[#129]: https://github.com/launchbadge/sqlx/pull/129
[#131]: https://github.com/launchbadge/sqlx/pull/131
[#135]: https://github.com/launchbadge/sqlx/pull/135
[#108]: https://github.com/launchbadge/sqlx/pull/108

## 0.2.6 - 2020-03-10

### Added

 - [[#114]] Export `sqlx_core::Transaction` [[@thedodd]]

### Fixed

 - [[#125]] [[#126]] Fix statement execution in MySQL if it contains NULL statement values [[@repnop]]

 - [[#105]] [[#109]] Allow trailing commas in query macros [[@timmythetiny]]

[#105]: https://github.com/launchbadge/sqlx/pull/105
[#109]: https://github.com/launchbadge/sqlx/pull/109
[#114]: https://github.com/launchbadge/sqlx/pull/114
[#125]: https://github.com/launchbadge/sqlx/pull/125
[#126]: https://github.com/launchbadge/sqlx/pull/126

[@timmythetiny]: https://github.com/timmythetiny
[@thedodd]: https://github.com/thedodd

## 0.2.5 - 2020-02-01

### Fixed

 - Fix decoding of Rows containing NULLs in Postgres [#104]

 - After a large review and some battle testing by [@ianthetechie]https://github.com/ianthetechie
   of the `Pool`, a live leaking issue was found. This has now been fixed by [@abonander] in [#84] which
   included refactoring to make the pool internals less brittle (using RAII instead of manual
   work is one example) and to help any future contributors when changing the pool internals.

 - Passwords are now being precent decoding before being presented to the server [[@repnop]]

 - [@100] Fix `FLOAT` and `DOUBLE` decoding in MySQL

[#84]: https://github.com/launchbadge/sqlx/issues/84
[#100]: https://github.com/launchbadge/sqlx/issues/100
[#104]: https://github.com/launchbadge/sqlx/issues/104

### Added

 - [[#72]] Add `PgTypeInfo::with_oid` to allow simple construction of `PgTypeInfo` which enables `HasSqlType`
   to be implemented by downstream consumers of SQLx [[@jplatte]]

 - [[#96]] Add support for returning columns from `query!` with a name of a rust keyword by
   using raw identifiers [[@yaahc]]

 - [[#71]] Implement derives for `Encode` and `Decode`. This is the first step to supporting custom types in SQLx. [[@Freax13]]

[#72]: https://github.com/launchbadge/sqlx/issues/72
[#96]: https://github.com/launchbadge/sqlx/issues/96
[#71]: https://github.com/launchbadge/sqlx/issues/71

## 0.2.4 - 2020-01-18

### Fixed

 - Fix decoding of Rows containing NULLs in MySQL (and add an integration test so this doesn't break again)

## 0.2.3 - 2020-01-18

### Fixed

 - Fix `query!` when used on a query that does not return results

## 0.2.2 - 2020-01-16

### Added

 - [[#57]] Add support for unsigned integers and binary types in `query!` for MySQL [[@mehcode]]

[#57]: https://github.com/launchbadge/sqlx/issues/57

### Fixed

 - Fix stall when requesting TLS from a Postgres server that explicitly does not support TLS (such as postgres running inside docker) [[@abonander]]

 - [[#66]] Declare used features for `tokio` in `sqlx-macros` explicitly

[#66]: https://github.com/launchbadge/sqlx/issues/66

## 0.2.1 - 2020-01-16

### Fixed

 - [[#64], [#65]] Fix decoding of Rows containing NULLs in MySQL [[@danielakhterov]]

[#64]: https://github.com/launchbadge/sqlx/pull/64
[#65]: https://github.com/launchbadge/sqlx/pull/65

 - [[#55]] Use a shared tokio runtime for the `query!` macro compile-time execution (under the `runtime-tokio` feature) [[@udoprog]]

[#55]: https://github.com/launchbadge/sqlx/pull/55

## 0.2.0 - 2020-01-15

### Fixed

 - https://github.com/launchbadge/sqlx/issues/47

### Added

 - Support Tokio through an optional `runtime-tokio` feature.

 - Support SQL transactions. You may now use the `begin()` function on `Pool` or `Connection` to
   start a new SQL transaction. This returns `sqlx::Transaction` which will `ROLLBACK` on `Drop`
   or can be explicitly `COMMIT` using `commit()`.

 - Support TLS connections.

## 0.1.4 - 2020-01-11

### Fixed

 - https://github.com/launchbadge/sqlx/issues/43

 - https://github.com/launchbadge/sqlx/issues/40

### Added

 - Support for `SCRAM-SHA-256` authentication in Postgres [#37]https://github.com/launchbadge/sqlx/pull/37 [@danielakhterov]https://github.com/danielakhterov

 - Implement `Debug` for Pool [#42]https://github.com/launchbadge/sqlx/pull/42 [@prettynatty]https://github.com/prettynatty

## 0.1.3 - 2020-01-06

### Fixed

 - https://github.com/launchbadge/sqlx/issues/30

## 0.1.2 - 2020-01-03

### Added

 - Support for Authentication in MySQL 5+ including the newer authentication schemes now default in MySQL 8: `mysql_native_password`, `sha256_password`, and `caching_sha2_password`.

 - [`Chrono`]https://github.com/chronotope/chrono support for MySQL was only partially implemented (was missing `NaiveTime` and `DateTime<Utc>`).

 - `Vec<u8>` (and `[u8]`) support for MySQL (`BLOB`) and Postgres (`BYTEA`).

[@abonander]: https://github.com/abonander
[@danielakhterov]: https://github.com/danielakhterov
[@mehcode]: https://github.com/mehcode
[@udoprog]: https://github.com/udoprog
[@jplatte]: https://github.com/jplatte
[@yaahc]: https://github.com/yaahc
[@Freax13]: https://github.com/Freax13
[@repnop]: https://github.com/repnop
[@bmisiak]: https://github.com/bmisiak
[@oeb25]: https://github.com/oeb25
[@PoiScript]: https://github.com/PoiScript
[@utter-step]: https://github.com/utter-step
[@sidred]: https://github.com/sidred
[@Ace4896]: https://github.com/Ace4896
[@jamwaffles]: https://github.com/jamwaffles
[@nrjais]: https://github.com/nrjais
[@qtbeee]: https://github.com/qtbeee
[@xiaopengli89]: https://github.com/xiaopengli89