sqlx-core-oldapi 0.6.59

Core of SQLx, the rust SQL toolkit. Not intended to be used directly.
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
use crate::common::StatementCache;
use crate::connection::{Connection, LogSettings};
use crate::error::Error;
use crate::odbc::{
    Odbc, OdbcArguments, OdbcBufferSettings, OdbcColumn, OdbcConnectOptions, OdbcQueryResult,
    OdbcRow, OdbcTypeInfo,
};
use crate::transaction::Transaction;
use either::Either;
use sqlx_rt::spawn_blocking;
mod odbc_bridge;
use crate::odbc::{OdbcStatement, OdbcStatementMetadata};
use futures_core::future::BoxFuture;
use futures_util::future;
use odbc_api::{
    handles::StatementConnection, ConnectionTransitions, Prepared, ResultSetMetadata,
    SharedConnection,
};
use odbc_bridge::{establish_connection, execute_sql};
use std::borrow::Cow;
use std::sync::{Arc, Mutex};

mod executor;

type PreparedStatement = Prepared<StatementConnection<SharedConnection<'static>>>;
type SharedPreparedStatement = Arc<Mutex<PreparedStatement>>;

struct CollectedColumns {
    columns: Vec<OdbcColumn>,
    deferred: bool,
}

fn collect_columns(
    prepared: &mut PreparedStatement,
    parameter_count: usize,
    allow_deferred_result_columns: bool,
) -> Result<CollectedColumns, Error> {
    let count = match prepared.num_result_cols() {
        Ok(count) => count,
        Err(error) if allow_deferred_result_columns && parameter_count > 0 => {
            log::debug!("ODBC prepare deferred result columns until execution: {error}");
            validate_parameter_metadata(prepared, parameter_count)?;
            return Ok(CollectedColumns {
                columns: Vec::new(),
                deferred: true,
            });
        }
        Err(error) => return Err(error.into()),
    };

    let mut columns = Vec::with_capacity(count as usize);
    for i in 1..=count {
        columns.push(describe_column(prepared, i as u16)?);
    }
    Ok(CollectedColumns {
        columns,
        deferred: false,
    })
}

fn validate_parameter_metadata(
    prepared: &mut PreparedStatement,
    parameter_count: usize,
) -> Result<(), Error> {
    for index in 1..=parameter_count {
        let parameter_number = u16::try_from(index)
            .map_err(|_| Error::Protocol(format!("ODBC parameter index {index} exceeds u16")))?;
        prepared.describe_param(parameter_number)?;
    }
    Ok(())
}

fn collect_statement_metadata(
    prepared: &mut PreparedStatement,
    allow_deferred_result_columns: bool,
) -> Result<(OdbcStatementMetadata, bool), Error> {
    let parameters = usize::from(prepared.num_params()?);
    let collected = collect_columns(prepared, parameters, allow_deferred_result_columns)?;
    let metadata_complete = !(collected.deferred || parameters > 0 && collected.columns.is_empty());

    Ok((
        OdbcStatementMetadata {
            columns: collected.columns,
            parameters,
        },
        metadata_complete,
    ))
}

pub(super) fn describe_column<S>(stmt: &mut S, index: u16) -> Result<OdbcColumn, Error>
where
    S: ResultSetMetadata,
{
    let mut cd = odbc_api::ColumnDescription::default();
    stmt.describe_col(index, &mut cd)?;

    Ok(OdbcColumn {
        name: decode_column_name(cd.name, index),
        type_info: OdbcTypeInfo::new(cd.data_type),
        ordinal: usize::from(
            index
                .checked_sub(1)
                .ok_or_else(|| Error::Protocol("ODBC column indices are 1-based".into()))?,
        ),
    })
}

pub(super) trait ColumnNameDecode {
    fn decode_or_default(self, index: u16) -> String;
}

impl ColumnNameDecode for Vec<u8> {
    fn decode_or_default(self, index: u16) -> String {
        String::from_utf8(self).unwrap_or_else(|_| format!("col{}", index - 1))
    }
}

impl ColumnNameDecode for Vec<u16> {
    fn decode_or_default(self, index: u16) -> String {
        String::from_utf16(&self).unwrap_or_else(|_| format!("col{}", index - 1))
    }
}

pub(super) fn decode_column_name<T: ColumnNameDecode>(name: T, index: u16) -> String {
    name.decode_or_default(index)
}

/// A connection to an ODBC-accessible database.
///
/// ODBC uses a blocking C API, so we offload blocking calls to the runtime's blocking
/// thread-pool via `spawn_blocking` and synchronize access with a mutex.
pub struct OdbcConnection {
    pub(crate) conn: SharedConnection<'static>,
    pub(crate) stmt_cache: StatementCache<SharedPreparedStatement>,
    pub(crate) buffer_settings: OdbcBufferSettings,
    pub(crate) log_settings: LogSettings,
}

impl std::fmt::Debug for OdbcConnection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OdbcConnection")
            .field("conn", &self.conn)
            .field("buffer_settings", &self.buffer_settings)
            .finish()
    }
}

impl OdbcConnection {
    pub(crate) async fn with_conn<R, F, S>(&mut self, operation: S, f: F) -> Result<R, Error>
    where
        R: Send + 'static,
        F: FnOnce(&mut odbc_api::Connection<'static>) -> Result<R, Error> + Send + 'static,
        S: std::fmt::Display + Send + 'static,
    {
        let conn = Arc::clone(&self.conn);
        spawn_blocking(move || {
            let mut conn_guard = conn.lock().map_err(|_| {
                Error::Protocol(format!("ODBC {}: failed to lock connection", operation))
            })?;
            f(&mut conn_guard)
        })
        .await
    }

    pub(crate) async fn establish(options: &OdbcConnectOptions) -> Result<Self, Error> {
        let shared_conn = spawn_blocking({
            let options = options.clone();
            move || {
                let conn = establish_connection(&options)?;
                let shared_conn = odbc_api::SharedConnection::new(std::sync::Mutex::new(conn));
                Ok::<_, Error>(shared_conn)
            }
        })
        .await?;

        Ok(Self {
            conn: shared_conn,
            stmt_cache: StatementCache::new(options.statement_cache_capacity),
            buffer_settings: options.buffer_settings,
            log_settings: options.log_settings.clone(),
        })
    }

    pub(crate) async fn ping_blocking(&mut self) -> Result<(), Error> {
        self.with_conn("ping", move |conn| {
            conn.execute("SELECT 1", (), None)?;
            Ok(())
        })
        .await
    }

    pub(crate) async fn begin_blocking(&mut self) -> Result<(), Error> {
        self.with_conn("begin", move |conn| {
            conn.set_autocommit(false)?;
            Ok(())
        })
        .await
    }

    pub(crate) async fn commit_blocking(&mut self) -> Result<(), Error> {
        self.with_conn("commit", move |conn| {
            conn.commit()?;
            conn.set_autocommit(true)?;
            Ok(())
        })
        .await
    }

    pub(crate) async fn rollback_blocking(&mut self) -> Result<(), Error> {
        self.with_conn("rollback", move |conn| {
            conn.rollback()?;
            conn.set_autocommit(true)?;
            Ok(())
        })
        .await
    }

    /// Launches a background task to execute the SQL statement and send the results to the returned channel.
    pub(crate) async fn execute_stream(
        &mut self,
        sql: &str,
        args: Option<OdbcArguments>,
        persistent: bool,
    ) -> Result<flume::Receiver<Result<Either<OdbcQueryResult, OdbcRow>, Error>>, Error> {
        let statement = if args.is_some() {
            MaybePrepared::Prepared(self.prepared_statement(sql, persistent).await?)
        } else {
            MaybePrepared::NotPrepared(sql.to_string())
        };

        let (tx, rx) = flume::bounded(64);

        let sql_owned = sql.to_string();
        let conn = Arc::clone(&self.conn);
        let buffer_settings = self.buffer_settings;
        let log_settings = self.log_settings.clone();
        sqlx_rt::spawn(sqlx_rt::spawn_blocking(move || {
            let mut logger = crate::logger::QueryLogger::new(&sql_owned, log_settings);
            let result = conn
                .lock()
                .map_err(|_| Error::Protocol("ODBC execute: failed to lock connection".into()))
                .and_then(|mut conn| {
                    execute_sql(
                        &mut conn,
                        statement,
                        args,
                        &tx,
                        buffer_settings,
                        &mut logger,
                    )
                });

            if let Err(e) = result {
                let _ = tx.send(Err(e));
            }
        }));

        Ok(rx)
    }

    async fn prepared_statement(
        &mut self,
        sql: &str,
        store_to_cache: bool,
    ) -> Result<SharedPreparedStatement, Error> {
        if let Some(cached) = self.stmt_cache.get_mut(sql) {
            return Ok(Arc::clone(cached));
        }

        let conn = Arc::clone(&self.conn);
        let sql_owned = sql.to_string();
        let prepared =
            spawn_blocking(move || conn.into_prepared(&sql_owned).map_err(Error::from)).await?;
        let prepared = Arc::new(Mutex::new(prepared));

        if store_to_cache && self.stmt_cache.is_enabled() {
            self.stmt_cache.insert(sql, Arc::clone(&prepared));
        }

        Ok(prepared)
    }

    pub(crate) async fn clear_cached_statements(&mut self) -> Result<(), Error> {
        while self.stmt_cache.remove_lru().is_some() {}
        Ok(())
    }

    async fn prepare_with_metadata_policy<'a>(
        &mut self,
        sql: &'a str,
        store_to_cache: bool,
        allow_deferred_result_columns: bool,
    ) -> Result<OdbcStatement<'a>, Error> {
        let prepared = self.prepared_statement(sql, false).await?;

        let (metadata, metadata_complete) = spawn_blocking({
            let prepared = Arc::clone(&prepared);
            move || {
                let mut prepared = prepared.lock().map_err(|_| {
                    Error::Protocol("ODBC prepare: failed to lock prepared statement".into())
                })?;
                collect_statement_metadata(&mut prepared, allow_deferred_result_columns)
            }
        })
        .await?;

        if !allow_deferred_result_columns && !metadata_complete {
            return Err(Error::Protocol(
                "ODBC driver did not provide result-column metadata before execution".into(),
            ));
        }

        if store_to_cache && metadata_complete && self.stmt_cache.is_enabled() {
            self.stmt_cache.insert(sql, prepared);
        }

        Ok(OdbcStatement {
            sql: Cow::Borrowed(sql),
            metadata,
        })
    }

    pub async fn prepare<'a>(&mut self, sql: &'a str) -> Result<OdbcStatement<'a>, Error> {
        self.prepare_with_metadata_policy(sql, true, true).await
    }

    pub(crate) async fn describe_statement<'a>(
        &mut self,
        sql: &'a str,
    ) -> Result<OdbcStatement<'a>, Error> {
        self.prepare_with_metadata_policy(sql, false, false).await
    }
}

pub(crate) enum MaybePrepared {
    Prepared(SharedPreparedStatement),
    NotPrepared(String),
}

impl std::fmt::Debug for MaybePrepared {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MaybePrepared::Prepared(_) => f.debug_tuple("Prepared").finish(),
            MaybePrepared::NotPrepared(sql) => f.debug_tuple("NotPrepared").field(sql).finish(),
        }
    }
}

impl Connection for OdbcConnection {
    type Database = Odbc;

    type Options = OdbcConnectOptions;

    fn close(self) -> BoxFuture<'static, Result<(), Error>> {
        Box::pin(async move {
            // Drop connection by moving Arc and letting it fall out of scope.
            drop(self);
            Ok(())
        })
    }

    fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
        Box::pin(async move { Ok(()) })
    }

    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(self.ping_blocking())
    }

    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
    where
        Self: Sized,
    {
        Transaction::begin(self)
    }

    fn cached_statements_size(&self) -> usize {
        self.stmt_cache.len()
    }

    #[doc(hidden)]
    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(future::ok(()))
    }

    #[doc(hidden)]
    fn should_flush(&self) -> bool {
        false
    }

    fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(self.clear_cached_statements())
    }

    fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
        Box::pin(async move {
            self.with_conn("dbms_name", move |conn| {
                Ok(conn.database_management_system_name()?)
            })
            .await
        })
    }
}