90 Commits

Author SHA1 Message Date
Aleh Shapo
29549b14c4
fix(mysql): validate parameter count for prepared statements (#3857)
* fix(mysql): validate parameter count for prepared statements

Add validation to ensure the number of provided parameters matches the
expected count for MySQL prepared statements.
This prevents protocol errors by returning an error if the counts do not match before sending
the statement for execution.

* refactor(mysql): use err_protocol macro for error creation

Replace direct Error::Protocol(format!()) calls with err_protocol!
macro in MySQL connection executor.

* test(mysql): add parameter count validation tests

- Add test for too few parameters provided to query
- Add test for too many parameters provided to query
- Add test for parameters provided when none expected
- All tests verify Error::Protocol is returned for mismatches

Covers cases for issue #3774 parameter validation fix.
2025-07-02 17:24:44 -07:00
JP
8ff14dc37c
feat(ok): add correct handling of ok packets in MYSQL implementation (#3910)
* feat(ok): add correct handling of ok packet

* feat(ok): add unit tests
2025-06-30 17:31:55 -07:00
Joey de Waal
8602d94c4d
Implement Decode, Encode and Type for Box, Arc, Cow and Rc (#3674)
* feat: implement Decode,Encode,Type for Box,Arc,Cow

* feat implement Encode,Type for Rc

* feat: implement Decode for Rc

* chore: make tests more concise

* chore: use macro's

* chore: remove conflicting impls

* chore: more macro's

* Relax Sized bound for Decode, Encode

* update unit tests

* fixes after review

* add comment in `Decode` impl

* add comment about `ToOwned` trait bound

* add comment explaining why decoding to `Cow::Owned` was chosen

* Remove unnecessary Decode impls
2025-06-30 17:03:48 -07:00
Austin Bonander
25cbeedab4
feat: create sqlx.toml format (#3383)
* feat: create `sqlx.toml` format

* feat: add support for ignored_chars config to sqlx_core::migrate

* chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)

https://en.wikipedia.org/wiki/Byte_order_mark

* refactor: make `Config` always compiled

simplifies usage while still making parsing optional for less generated code

* refactor: add origin information to `Column`

* feat(macros): implement `type_override` and `column_override` from `sqlx.toml`

* refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates`

* feat: make macros aware of `macros.preferred-crates`

* feat: make `sqlx-cli` aware of `database-url-var`

* feat: teach macros about `migrate.table-name`, `migrations-dir`

* feat: teach macros about `migrate.ignored-chars`

* chore: delete unused source file `sqlx-cli/src/migration.rs`

* feat: teach `sqlx-cli` about `migrate.defaults`

* feat: teach `sqlx-cli` about `migrate.migrations-dir`

* feat: teach `sqlx-cli` about `migrate.table-name`

* feat: introduce `migrate.create-schemas`

* WIP feat: create multi-tenant database example

* fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements

* feat: progress on axum-multi-tenant example

* feat(config): better errors for mislabeled fields

* WIP feat: filling out axum-multi-tenant example

* feat: multi-tenant example

No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.

* chore(ci): test multi-tenant example

* fixup after merge

* fix(ci): enable `sqlx-toml` in CLI build for examples

* fix: CI, README for `multi-tenant`

* fix: clippy warnings

* fix: multi-tenant README

* fix: sequential versioning inference for migrations

* fix: migration versioning with explicit overrides

* fix: only warn on ambiguous crates if the invocation relies on it

* fix: remove unused imports

* fix: doctest

* fix: `sqlx mig add` behavior and tests

* fix: restore original type-checking order

* fix: deprecation warning in `tests/postgres/macros.rs`

* feat: create postgres/multi-database example

* fix: examples/postgres/multi-database

* fix: cargo fmt

* chore: add tests for config `migrate.defaults`

* fix: sqlx-cli/tests/add.rs

* feat(cli): add `--config` override to all relevant commands

* chore: run `sqlx mig add` test with `RUST_BACKTRACE=1`

* fix: properly canonicalize config path for `sqlx mig add` test

* fix: get `sqlx mig add` test passing

* fix(cli): test `migrate.ignored-chars`, fix bugs

* feat: create `macros.preferred-crates` example

* fix(examples): use workspace `sqlx`

* fix: examples

* fix(sqlite): unexpected feature flags in `type_checking.rs`

* fix: run `cargo fmt`

* fix: more example fixes

* fix(ci): preferred-crates setup

* fix(examples): enable default-features for workspace `sqlx`

* fix(examples): issues in `preferred-crates`

* chore: adjust error message for missing param type in `query!()`

* doc: mention new `sqlx.toml` configuration

* chore: add `CHANGELOG` entry

Normally I generate these when cutting the release, but I wanted to take time to editorialize this one.

* doc: fix new example titles

* refactor: make `sqlx-toml` feature non-default, improve errors

* refactor: eliminate panics in `Config` read path

* chore: remove unused `axum` dependency from new examples

* fix(config): restore fallback to default config for macros

* chore(config): remove use of `once_cell` (to match `main`)
2025-06-30 16:34:46 -07:00
Paolo Barbolini
764ae2f702
chore: replace once_cell OnceCell/Lazy with std OnceLock/LazyLock (#3709) 2025-06-17 16:38:02 -07:00
Austin Bonander
90797200ee
groundwork for 0.9.0-alpha.1 (#3821)
* chore: bump version to `0.9.0-alpha.1`

* chore: delete unused `sqlx-bench` package

* chore: set `rust-version` to 1.85 for all crates

* fix: lots of new Clippy warnings

* fix: lots more Clippy warnings

* fix(cli): add `_sqlite` feature

* fix: lots, *lots* more Clippy warnings

* fix(core): warning in `tls_rustls`

* breaking: delete runtime+TLS combination features

* chore: don't re-export unstable `TransactionManager` trait

* chore: 0.9.0-alplha.1 CHANGELOG

* chore: increase MSRV further to 1.86

* fix: more clippy warnings
2025-06-01 21:09:55 -07:00
Austin Bonander
e7236881a1
Hotfix 0.8.5 (#3824)
* fix(cli): correctly handle `.env` files again

* feat(ci): add functionality tests for sqlx-cli (MySQL)

* feat(ci): add functionality tests for sqlx-cli (Postgres)

* feat(ci): add functionality tests for sqlx-cli (SQLite)

* chore: prepare 0.8.5 release

* feat(ci): run `test-attr` tests twice to catch #3825

* fix: correct bugs in MySQL implementation of `#[sqlx::test]`
2025-04-15 15:11:07 -07:00
Vladimir Petrzhikovskii
e283bf9645 mysql: Fix panic on invalid text row length field
Previously, `TextRow::decode_with` would read a length-encoded field size
and attempt to advance the buffer by that amount. If the server sent a
malformed packet containing a length value larger than the remaining data
in the buffer, the call to `buf.advance(size)` would panic.

eg: 
```
thread 'main' panicked at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bytes-1.10.1/src/bytes.rs:711:9:
cannot advance past remaining: 8590116092 <= 0
stack backtrace:
0:     0x56119b657e00 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h6d42cc84fc840290
1:     0x56119b67edd3 - core::fmt::write::h5af61a909e3ec64d
2:     0x56119b653ee3 - std::io::Write::write_fmt::h5a7b54aa6e4a315d
3:     0x56119b657c52 - std::sys::backtrace::BacktraceLock::print::h555579e7396c26ac
4:     0x56119b658cef - std::panicking::default_hook::{{closure}}::h9128866118196224
5:     0x56119b658b5a - std::panicking::default_hook::h52e9e7314e0255f6
6:     0x56119b659712 - std::panicking::rust_panic_with_hook::h541791bcc774ef34
7:     0x56119b65949a - std::panicking::begin_panic_handler::{{closure}}::h6479a2f0137c7d19
8:     0x56119b658319 - std::sys::backtrace::__rust_end_short_backtrace::ha04e7c0fc61ded91
9:     0x56119b65912d - rust_begin_unwind
10:     0x56119b67c390 - core::panicking::panic_fmt::h5764ee7030b7a73d
11:     0x56119b572b18 - <sqlx_mysql::protocol::text::row::TextRow as sqlx_core::io::decode::ProtocolDecode<&[sqlx_mysql::column::MySqlColumn]>>::decode_with::h17ac8b44140b5469
12:     0x56119b42df1f - sqlx_mysql::connection::executor::<impl sqlx_mysql::connection::MySqlConnection>::run::{{closure}}::{{closure}}::{{closure}}::h4874a0f73925d55a
```

This commit introduces a bounds check immediately after reading the field length from a packet.

This panic condition was specifically observed when executing a TiDB
`BATCH ON ... DELETE` statement via `pool.execute()`. It sends an OK packet immediately followed by
a full result set describing the batch status (column defs, row data, EOF).


 Observed TiDB response sequence for `BATCH DML` via `COM_QUERY`:
    1. OK Packet (seq=1, `SERVER_MORE_RESULTS_EXISTS` = false)
    2. Column Count Packet (seq=1, non-standard, protocol violation)
    3. Column Definition Packet (seq=2)
    4. Column Definition Packet (seq=3)
    5. Text Row Data Packet (seq=4)
    6. EOF Packet (seq=5, `SERVER_MORE_RESULTS_EXISTS` = false)

This differs from standard MySQL DML response (OK/ERR packet only) and
causes `sqlx` using `execute()` to attempt parsing the unexpected result
set packets after the initial OK packet.
2025-04-10 19:40:16 -07:00
Robin Schroer
e474be6d4b
docs: Fix a copy-paste error on get_username docs (#3786)
I suspect this is a copy-paste error, it's meant to say username, not port.
2025-03-15 23:21:56 -07:00
Austin Bonander
393b731d5e
Merge of #3427 (by @mpyw) and #3614 (by @bonsairobo) (#3765)
* feat: Implement `get_transaction_depth` for drivers

* test: Verify `get_transaction_depth()` on postgres

* Refactor: `TransactionManager` delegation without BC

SQLite implementation is currently WIP

* Fix: Avoid breaking changes on `AnyConnectionBackend`

* Refactor: Remove verbose `SqliteConnection` typing

* Feat: Implementation for SQLite

I have included `AtomicUsize` in `WorkerSharedState`. Ideally, it is not desirable to execute `load` and `fetch_add` in two separate steps, but we decided to allow it here since there is only one thread writing. To prevent writing from other threads, the field itself was made private, and a getter method was provided with `pub(crate)`.

* Refactor: Same approach for `cached_statements_size`

ref: a66787d36d62876b55475ef2326d17bade817aed

* Fix: Add missing `is_in_transaction` for backend

* Doc: Remove verbose "synchronously" word

* Fix: Remove useless `mut` qualifier

* feat: add Connection::begin_with

This patch completes the plumbing of an optional statement from these methods to
`TransactionManager::begin` without any validation of the provided statement.

There is a new `Error::InvalidSavePoint` which is triggered by any attempt to
call `Connection::begin_with` when we are already inside of a transaction.

* feat: add Pool::begin_with and Pool::try_begin_with

* feat: add Error::BeginFailed and validate that custom "begin" statements are successful

* chore: add tests of Error::BeginFailed

* chore: add tests of Error::InvalidSavePointStatement

* chore: test begin_with works for all SQLite "BEGIN" statements

* chore: improve comment on Connection::begin_with

* feat: add default impl of `Connection::begin_with`

This makes the new method a non-breaking change.

* refactor: combine if statement + unwrap_or_else into one match

* feat: use in-memory SQLite DB to avoid conflicts across tests run in parallel

* feedback: remove public wrapper for sqlite3_txn_state

Move the wrapper directly into the test that uses it instead.

* fix: cache Status on MySqlConnection

* fix: compilation errors

* fix: format

* fix: postgres test

* refactor: delete `Connection::get_transaction_depth`

* fix: tests

---------

Co-authored-by: mpyw <ryosuke_i_628@yahoo.co.jp>
Co-authored-by: Duncan Fairbanks <duncanfairbanks6@gmail.com>
2025-03-10 14:29:46 -07:00
Sam Lyon
8cdad44366
docs: add some missing backticks (#3749)
* add ending backticks to starting backticks that were missing them

* fix table alignment
2025-02-19 15:23:45 -08:00
Paolo Barbolini
65229f7ff9
Replace some futures_util APIs with std variants (#3721) 2025-02-01 16:01:56 -08:00
joeydewaal
6c2a29f67e
chore(MySql): Remove unnecessary box (#3708) 2025-01-27 13:41:07 -08:00
Andreas Liljeqvist
a83395a360
Fix: nextest cleanup race condition (#3334)
* remove unused trait fn `cleanup_test_dbs`

* *wip* solve test cleanup race condition

* check for exactly 63 chars in database name

* move base64 dependency

* change

* Use url_safe base64 encoding

* Assert quoting for database name

* refactor

* add mysql support?

* borrow

* fix borrows

* ensure quoting

* re-add trait cleanup_test_dbs

* fix mysql insert

* cargo lock

* use actual field

* cleanup converted path in sqlite

* replace dashes with underscore in db name

* refactor: remove redundant path conversion in cleanup_test and add db_name method

---------

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
2025-01-23 17:36:55 -08:00
joeydewaal
dfd0ac5974
feat: add Transaction type aliases (#3658) 2025-01-03 00:26:09 -08:00
joeydewaal
1f6ce33df4
chore: remove BoxFuture's (non-breaking) (#3629)
* chore: reduce BoxFuture's when using recursion.

* remove BoxFuture's in WithSocket

* chore: better document previous changes
2024-12-12 12:43:22 -08:00
Austin Bonander
35f3ec1944
fix(mysql): percent-decode database name (#3612)
Duplicates the fix to Postgres in #3593 to the MySQL driver.

The SQLite driver already does this: e3ef8baf23/sqlx-sqlite/src/options/parse.rs (L29-L32)
2024-11-27 14:45:46 -08:00
Lo
94607b5a10
parse timezone parameter in mysql connection url (#3418)
* feat(mysql): support configuring the timezone via url

* test: add test case for mysql with timezone

---------

Co-authored-by: lo <lo@los-MacBook-Pro.local>
2024-11-27 14:14:00 -08:00
Paolo Barbolini
3e8952b0d4
Bump thiserror to v2.0.0 (#3596) 2024-11-26 11:01:33 -08:00
Pmarquez
d4ae6ffd88
Implement AnyQueryResult for Sqlite and MySQL (#3608)
* impl AnyQueryResult for Sqlite and MySQL

* fix MySQL AnyQueryResult

* fix MySQL AnyQueryResult

* fix manifest

* rewrite `use` and address implementation concerns
2024-11-26 10:59:20 -08:00
Austin Bonander
82d332f4b4
doc(mysql): document difference between Uuid and uuid::fmt::Hyphenated (#3580) 2024-10-28 16:39:43 -07:00
Austin Bonander
6cb6fce793 fix(mysql): "exclusive range pattern is experimental" error
closes #3472
2024-09-03 14:14:56 -07:00
Austin Bonander
823261aefc fix(mysql): don't use an arbitrary cfg for one test 2024-08-23 23:39:32 -07:00
Austin Bonander
27c573083f fix(mysql): fix doctests 2024-08-23 23:39:32 -07:00
Austin Bonander
71f72e2620 fix(mysql): add sqlx as a dev-dependency for doctests 2024-08-23 23:39:32 -07:00
Austin Bonander
3a41288b84 fix(mysql): correct Capabilities assertions in unit tests 2024-08-23 23:39:32 -07:00
Austin Bonander
a6526a1cf7 fix: use same fix for the same cast in Migrate::apply() everywhere 2024-08-23 23:39:32 -07:00
Austin Bonander
2ab7565bd8 chore: configure clippy cast lints at workspace level 2024-08-23 23:39:32 -07:00
Austin Bonander
1f669ae996 fix(mysql): audit for bad casts 2024-08-23 23:39:32 -07:00
Austin Bonander
e99f0fa5b6 chore(mysql): deny bad-cast lints 2024-08-23 23:39:32 -07:00
Austin Bonander
1f3db8201d fix(mysql): fallout from ec5326e5 2024-08-23 23:39:32 -07:00
Kol Influence
fac53b05a4
correct spelling of MySqlConnectOptions::no_engine_substitution() (#3421)
* Update connect.rs

* Update mod.rs

correct naming no_engine_substitution

* Update mod.rs

* Update mod.rs

* Update mod.rs

* Update mod.rs
2024-08-12 00:13:37 -07:00
Yuta Kobayashi
f2fea27cba
Fix encoding and decoding of MySQL enums in sqlx::Type (#3371) 2024-07-25 02:47:36 -07:00
SrGesus
eaad7b2c9a
doc: Minor rust docs fixes (#3312)
* Fixed some rust docs intra-doc non functioning links

* Minor tweaks

* Added warning for MSSQL not being functional yet

* Fixed requested changes

* Readded missing time

* Aligned table
2024-07-20 12:59:52 -07:00
Eric Torreborre
b71221cd74
use the persistent query setting with the Any driver (#3297) 2024-07-15 16:57:43 -07:00
Joshua Potts
d1f180fbc5 fix: Minimally upgrade minimal dependencies to resolve build issues on declared minimum versions
Signed-off-by: Joshua Potts <8704475+iamjpotts@users.noreply.github.com>
2024-07-11 11:59:15 -07:00
alu
33aee07094 Remove compatibility check by collations. 2024-06-20 11:57:05 -07:00
alu
ce6d75208b Changed to collation which is the actual name. 2024-06-20 11:57:05 -07:00
Cristian Le
92de9d42a6 Add LICENSE-* files to crates 2024-06-20 10:56:08 -07:00
Austin Bonander
05a10de4d8 fix: suppress dead_code warnings 2024-06-13 13:11:36 -07:00
Austin Bonander
bae083cf79 fix: clippy warnings 2024-06-13 13:11:36 -07:00
Stepan Tubanov
5da0f73746
perf: box MySqlConnection to reduce sizes of futures (#3265) 2024-06-05 18:19:30 -07:00
Max Bruckner
c57b46ceb6
Make Encode return a result (#3126)
* Make encode and encode_by_ref fallible

This only changes the trait for now and makes it compile, calling .expect() on all users. Those will be removed in a later commit.

* PgNumeric: Turn TryFrom Decimal to an infallible From

* Turn panics in Encode implementations into errors

* Add Encode error analogous to the Decode error

* Propagate decode errors through Arguments::add

This pushes the panics one level further to mostly bind calls. Those will also be removed later.

* Only check argument encoding at the end

* Use Result in Query internally

* Implement query_with functions in terms of _with_result

* Surface encode errors when executing a query.

* Remove remaining panics in AnyConnectionBackend implementations

* PostgreSQL BigDecimal: Return encode error immediately

* Arguments: Add len method to report how many arguments were added

* Query::bind: Report which argument failed to encode

* IsNull: Add is_null method

* MySqlArguments: Replace manual bitmap code with NullBitMap helper type

* Roll back buffer in MySqlArguments if encoding fails

* Roll back buffer in SqliteArguments if encoding fails

* Roll back PgArgumentBuffer if encoding fails
2024-05-31 12:42:36 -07:00
Austin Bonander
60d033eda2
fix(ci): pin Rust version, ditch unmaintained actions (#3234) 2024-05-14 23:57:31 -07:00
Dan Griffin
c82bf43e98
Fix leaking connections in fetch_optional (#2647) (#3194)
When using the 'Any' driver with MySQL backend, fetch_optional
does not return the connection to the pool if no results
are returned from the query. This is due to not all of the packets
being read from the underlying stream.

This fix continues to read result packets from the stream until they
have all been exhausted (just like the normal MySql drivers
implementation of fetch_optional). In general, a better refactoring would
be to call the MySQL fetch_optional code in the Any driver, rather than
re-implementing and duplicating code.
2024-04-24 19:18:55 -07:00
Gnome!
3e7aa6bedf
Bump deps that do not need code changes (#3165) 2024-04-06 00:07:36 -07:00
Maciej Flak
45b5b61d7b
Add version information for failed cli migration (#3129) (#3130) 2024-04-04 21:45:21 -07:00
Austin Bonander
02c68a46c7
refactor: lift type mappings into driver crates (#2970)
Motivated by #2917
2024-03-30 15:52:52 -07:00
Austin Bonander
7102a7a254
feat: add MySqlTime, audit mysql::types for panics (#3154)
Also clarifies the handling of `TIME` (we never realized it's used for both time-of-day and signed intervals) and adds appropriate impls for `std::time::Duration`, `time::Duration`, `chrono::TimeDelta`
2024-03-30 11:49:12 -07:00
nitn3lav
9ba488c831
Generic Associated Types in Database, replacing HasValueRef, HasArguments, HasStatement (#2973)
* HasValueRef, HasArguments, HasStatement -> Database GATs

replace the associated types from the generic traits
`HasValueRef<'r>`, `HasArguments<'q>` and `HasStatement<'q>`
with generic associated types in `Database`

* fixup after rebase

---------

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
2024-03-14 12:35:52 -07:00