tank-core 0.31.0

Core of Tank: the Rust data layer. This is intended to be used by drivers to implement a backend.
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use crate::{AsValue, ColumnDef, DynQuery, TableRef, Value};
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt, quote};
use rust_decimal::prelude::ToPrimitive;
use serde_json::{Map, Number, Value as JsonValue};
use std::{
    borrow::Cow,
    cmp::min,
    collections::BTreeMap,
    ffi::{CStr, CString},
    ptr,
};
use syn::Path;

#[derive(Clone)]
/// Iterator adapter for two types.
pub enum EitherIterator<A, B>
where
    A: Iterator,
    B: Iterator<Item = A::Item>,
{
    Left(A),
    Right(B),
}

impl<A, B> Iterator for EitherIterator<A, B>
where
    A: Iterator,
    B: Iterator<Item = A::Item>,
{
    type Item = A::Item;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Left(a) => a.next(),
            Self::Right(b) => b.next(),
        }
    }
}

pub fn value_to_json(v: &Value) -> Option<JsonValue> {
    Some(match v {
        _ if v.is_null() => JsonValue::Null,
        Value::Boolean(Some(v), ..) => JsonValue::Bool(*v),
        Value::Int8(Some(v), ..) => JsonValue::Number(Number::from_i128(*v as _)?),
        Value::Int16(Some(v), ..) => JsonValue::Number(Number::from_i128(*v as _)?),
        Value::Int32(Some(v), ..) => JsonValue::Number(Number::from_i128(*v as _)?),
        Value::Int64(Some(v), ..) => JsonValue::Number(Number::from_i128(*v as _)?),
        Value::Int128(Some(v), ..) => JsonValue::Number(Number::from_i128(*v as _)?),
        Value::UInt8(Some(v), ..) => JsonValue::Number(Number::from_u128(*v as _)?),
        Value::UInt16(Some(v), ..) => JsonValue::Number(Number::from_u128(*v as _)?),
        Value::UInt32(Some(v), ..) => JsonValue::Number(Number::from_u128(*v as _)?),
        Value::UInt64(Some(v), ..) => JsonValue::Number(Number::from_u128(*v as _)?),
        Value::UInt128(Some(v), ..) => JsonValue::Number(Number::from_u128(*v as _)?),
        Value::Float32(Some(v), ..) => JsonValue::Number(Number::from_f64(*v as _)?),
        Value::Float64(Some(v), ..) => JsonValue::Number(Number::from_f64(*v as _)?),
        Value::Decimal(Some(v), ..) => JsonValue::Number(Number::from_f64(v.to_f64()?)?),
        Value::Char(Some(v), ..) => JsonValue::String(v.to_string()),
        Value::Varchar(Some(v), ..) => JsonValue::String(v.to_string()),
        Value::Blob(Some(v), ..) => JsonValue::Array(
            v.iter()
                .map(|v| Number::from_u128(*v as _).map(JsonValue::Number))
                .collect::<Option<_>>()?,
        ),
        v @ (Value::Date(Some(..), ..)
        | Value::Time(Some(..), ..)
        | Value::Timestamp(Some(..), ..)
        | Value::TimestampWithTimezone(Some(..), ..)) => JsonValue::String(v.to_string()),
        Value::Interval(Some(_v), ..) => {
            return None;
        }
        Value::Uuid(Some(v), ..) => JsonValue::String(v.to_string()),
        Value::Array(Some(v), ..) => {
            JsonValue::Array(v.iter().map(value_to_json).collect::<Option<_>>()?)
        }
        Value::List(Some(v), ..) => {
            JsonValue::Array(v.iter().map(value_to_json).collect::<Option<_>>()?)
        }
        Value::Map(Some(v), ..) => {
            let mut map = Map::new();
            for (k, v) in v.iter() {
                let Ok(k) = String::try_from_value(k.clone()) else {
                    return None;
                };
                let Some(v) = value_to_json(v) else {
                    return None;
                };
                map.insert(k, v)?;
            }
            JsonValue::Object(map)
        }
        Value::Json(Some(v), ..) => v.clone(),
        Value::Struct(Some(v), ..) => {
            let mut map = Map::new();
            for (k, v) in v.iter() {
                let Some(v) = value_to_json(v) else {
                    return None;
                };
                map.insert(k.clone(), v)?;
            }
            JsonValue::Object(map)
        }
        Value::Unknown(Some(v), ..) => JsonValue::String(v.clone()),
        _ => {
            return None;
        }
    })
}

/// Quote a `BTreeMap<K, V>` into tokens.
pub fn quote_btree_map<K: ToTokens, V: ToTokens>(value: &BTreeMap<K, V>) -> TokenStream {
    let mut tokens = TokenStream::new();
    for (k, v) in value {
        let ks = k.to_token_stream();
        let vs = v.to_token_stream();
        tokens.append_all(quote! {
            (#ks, #vs),
        });
    }
    quote! {
        ::std::collections::BTreeMap::from([
            #tokens
        ])
    }
}

/// Quote a `Cow<T>` preserving borrowed vs owned status for generated code.
pub fn quote_cow<T: ToOwned + ToTokens + ?Sized>(value: &Cow<T>) -> TokenStream
where
    <T as ToOwned>::Owned: ToTokens,
{
    match value {
        Cow::Borrowed(v) => quote! { ::std::borrow::Cow::Borrowed(#v) },
        Cow::Owned(v) => quote! { ::std::borrow::Cow::Owned(#v) },
    }
}

/// Quote an `Option<T>` into tokens.
pub fn quote_option<T: ToTokens>(value: &Option<T>) -> TokenStream {
    match value {
        None => quote! { None },
        Some(v) => quote! { Some(#v) },
    }
}

/// Determine if the trailing segments of a `syn::Path` match the expected identifiers.
pub fn matches_path(path: &Path, expect: &[&str]) -> bool {
    let len = min(path.segments.len(), expect.len());
    path.segments
        .iter()
        .rev()
        .take(len)
        .map(|v| &v.ident)
        .eq(expect.iter().rev().take(len))
}

/// Write an iterator of items separated by a delimiter into a string.
pub fn separated_by<T, F>(
    out: &mut DynQuery,
    values: impl IntoIterator<Item = T>,
    mut f: F,
    separator: &str,
) where
    F: FnMut(&mut DynQuery, T),
{
    let mut len = out.len();
    for v in values {
        if out.len() > len {
            out.push_str(separator);
        }
        len = out.len();
        f(out, v);
    }
}

/// Write, escaping occurrences of `search` char with `replace` while copying into buffer.
pub fn write_escaped(out: &mut DynQuery, value: &str, search: char, replace: &str) {
    let mut position = 0;
    for (i, c) in value.char_indices() {
        if c == search {
            out.push_str(&value[position..i]);
            out.push_str(replace);
            position = i + c.len_utf8();
        }
    }
    out.push_str(&value[position..]);
}

/// Convenience wrapper converting into a `CString`.
pub fn as_c_string(str: impl Into<Vec<u8>>) -> CString {
    CString::new(
        str.into()
            .into_iter()
            .map(|b| if b == 0 { b'?' } else { b })
            .collect::<Vec<u8>>(),
    )
    .unwrap_or_default()
}

pub fn error_message_from_ptr<'a>(ptr: &'a *const i8) -> Cow<'a, str> {
    unsafe {
        if *ptr != ptr::null() {
            CStr::from_ptr(*ptr).to_string_lossy()
        } else {
            Cow::Borrowed("Unknown error: could not extract the error message")
        }
    }
}

/// Consume a prefix of `input` while the predicate returns true, returning that slice.
pub fn consume_while<'s>(input: &mut &'s str, predicate: impl FnMut(&char) -> bool) -> &'s str {
    let len = input
        .chars()
        .take_while(predicate)
        .map(char::len_utf8)
        .sum();
    if len == 0 {
        return "";
    }
    let result = &input[..len];
    *input = &input[len..];
    result
}

pub fn extract_number<'s, const SIGNED: bool>(input: &mut &'s str) -> &'s str {
    let mut end = 0;
    let mut chars = input.chars().peekable();
    if SIGNED && matches!(chars.peek(), Some('+') | Some('-')) {
        chars.next();
        end += 1;
    }
    for _ in chars.take_while(char::is_ascii_digit) {
        end += 1;
    }
    let result = &input[..end];
    *input = &input[end..];
    result
}

pub fn column_def(name: &str, table: &TableRef) -> Option<&'static ColumnDef> {
    table.columns.iter().find(|v| v.name() == name)
}

#[macro_export]
macro_rules! number_to_month {
    ($month:expr, $throw:expr $(,)?) => {
        match $month {
            1 => Month::January,
            2 => Month::February,
            3 => Month::March,
            4 => Month::April,
            5 => Month::May,
            6 => Month::June,
            7 => Month::July,
            8 => Month::August,
            9 => Month::September,
            10 => Month::October,
            11 => Month::November,
            12 => Month::December,
            _ => $throw,
        }
    };
}

#[macro_export]
macro_rules! month_to_number {
    ($month:expr $(,)?) => {
        match $month {
            Month::January => 1,
            Month::February => 2,
            Month::March => 3,
            Month::April => 4,
            Month::May => 5,
            Month::June => 6,
            Month::July => 7,
            Month::August => 8,
            Month::September => 9,
            Month::October => 10,
            Month::November => 11,
            Month::December => 12,
        }
    };
}

#[macro_export]
/// Conditionally wrap a generated fragment in parentheses.
macro_rules! possibly_parenthesized {
    ($out:ident, $cond:expr, $v:expr) => {
        if $cond {
            $out.push('(');
            $v;
            $out.push(')');
        } else {
            $v;
        }
    };
}

pub const TRUNCATE_LONG_LIMIT: usize = {
    #[cfg(debug_assertions)]
    {
        2000
    }

    #[cfg(not(debug_assertions))]
    {
        497
    }
};

#[macro_export]
/// Truncate long strings for logging and error messages purpose.
///
/// Returns a `format_args!` that yields at most 497 characters from the start
/// of the input followed by `...` when truncation occurred. Minimal overhead.
///
/// If true is the second argument, it evaluates the first argument just once.
///
/// # Examples
/// ```ignore
/// use tank_core::truncate_long;
/// let short = "SELECT 1";
/// assert_eq!(format!("{}", truncate_long!(short)), "SELECT 1\n");
/// let long = format!("SELECT {}", "X".repeat(600));
/// let logged = format!("{}", truncate_long!(long));
/// assert!(logged.starts_with("SELECT XXXXXX"));
/// assert!(logged.ends_with("...\n"));
/// ```
macro_rules! truncate_long {
    ($query:expr) => {
        format_args!(
            "{}{}",
            &$query[..::std::cmp::min($query.len(), $crate::TRUNCATE_LONG_LIMIT)].trim(),
            if $query.len() > $crate::TRUNCATE_LONG_LIMIT {
                "...\n"
            } else {
                ""
            },
        )
    };
    ($query:expr,true) => {{
        let query = $query;
        format!(
            "{}{}",
            &query[..::std::cmp::min(query.len(), $crate::TRUNCATE_LONG_LIMIT)].trim(),
            if query.len() > $crate::TRUNCATE_LONG_LIMIT {
                "...\n"
            } else {
                ""
            },
        )
    }};
}

/// Sends the value through the channel and logs in case of error.
///
/// Parameters:
/// * `$tx`: sender channel
/// * `$value`: value to be sent
///
/// *Example*:
/// ```ignore
/// send_value!(tx, Ok(QueryResult::Row(row)));
/// ```

#[macro_export]
macro_rules! send_value {
    ($tx:ident, $value:expr) => {{
        if let Err(e) = $tx.send($value) {
            log::error!("{e:#}");
        }
    }};
}

/// Incrementally accumulates tokens from a speculative parse stream until one
/// of the supplied parsers succeeds.
///
/// Returns `(accumulated_tokens, (parser1_option, parser2_option, ...))` with
/// exactly one `Some(T)`: the first successful parser.
#[doc(hidden)]
#[macro_export]
macro_rules! take_until {
    ($original:expr, $($parser:expr),+ $(,)?) => {{
        let macro_local_input = $original.fork();
        let mut macro_local_result = (
            TokenStream::new(),
            ($({
                let _ = $parser;
                None
            }),+),
        );
        loop {
            if macro_local_input.is_empty() {
                break;
            }
            let mut parsed = false;
            let produced = ($({
                let attempt = macro_local_input.fork();
                if let Ok(content) = ($parser)(&attempt) {
                    macro_local_input.advance_to(&attempt);
                    parsed = true;
                    Some(content)
                } else {
                    None
                }
            }),+);
            if parsed {
                macro_local_result.1 = produced;
                break;
            }
            macro_local_result.0.append(macro_local_input.parse::<TokenTree>()?);
        }
        $original.advance_to(&macro_local_input);
        macro_local_result
    }};
}

#[macro_export]
/// Implement the `Executor` trait for a transaction wrapper type by
/// delegating each operation to an underlying connection object.
///
/// This reduces boilerplate across driver implementations. The macro expands
/// into an `impl Executor for $transaction<'c>` with forwarding methods for
/// `prepare`, `run`, `fetch`, `execute`, and `append`.
///
/// Parameters:
/// * `$driver`: concrete driver type.
/// * `$transaction`: transaction wrapper type (generic over lifetime `'c`).
/// * `$connection`: field name on the transaction pointing to the connection.
///
/// # Examples
/// ```ignore
/// use crate::{YourDBConnection, YourDBDriver};
/// use tank_core::{Error, Result, Transaction, impl_executor_transaction};
///
/// pub struct YourDBTransaction<'c> {
///     connection: &'c mut YourDBConnection,
/// }
///
/// impl_executor_transaction!(YourDBDriver, YourDBTransaction<'c>, connection);
///
/// impl<'c> Transaction<'c> for YourDBTransaction<'c> { ... }
/// ```
macro_rules! impl_executor_transaction {
    // Case 1: Lifetime is present (necessary for transactions)
    ($driver:ty, $transaction:ident $(< $lt:lifetime >)?, $connection:ident) => {
       impl $(<$lt>)? ::tank_core::Executor for $transaction $(<$lt>)? {
            type Driver = $driver;

            fn accepts_multiple_statements(&self) -> bool {
                self.$connection.accepts_multiple_statements()
            }

            fn do_prepare(
                &mut self,
                sql: String,
            ) -> impl Future<Output = ::tank_core::Result<::tank_core::Query<Self::Driver>>> + Send
            {
                self.$connection.do_prepare(sql)
            }

            fn run<'s>(
                &'s mut self,
                query: impl ::tank_core::AsQuery<Self::Driver> + 's,
            ) -> impl ::tank_core::stream::Stream<
                Item = ::tank_core::Result<::tank_core::QueryResult>,
            > + Send {
                self.$connection.run(query)
            }

            fn fetch<'s>(
                &'s mut self,
                query: impl ::tank_core::AsQuery<Self::Driver> + 's,
            ) -> impl ::tank_core::stream::Stream<
                Item = ::tank_core::Result<::tank_core::Row>,
            > + Send
            + 's {
                self.$connection.fetch(query)
            }

            fn execute<'s>(
                &'s mut self,
                query: impl ::tank_core::AsQuery<Self::Driver> + 's,
            ) -> impl Future<Output = ::tank_core::Result<::tank_core::RowsAffected>> + Send {
                self.$connection.execute(query)
            }

            fn append<'a, E, It>(
                &mut self,
                entities: It,
            ) -> impl Future<Output = ::tank_core::Result<::tank_core::RowsAffected>> + Send
            where
                E: ::tank_core::Entity + 'a,
                It: IntoIterator<Item = &'a E> + Send,
                <It as IntoIterator>::IntoIter: Send,
            {
                self.$connection.append(entities)
            }
        }
    }
}

#[macro_export]
macro_rules! current_timestamp_ms {
    () => {{}};
}