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
//!
//! Rust Firebird Client
//!
//! Preparation and execution of statements
//!

use crate::{
    transaction::{Transaction, TransactionData},
    Connection,
};
use rsfbclient_core::{
    Column, FbError, FirebirdClient, FreeStmtOp, FromRow, IntoParams, NamedParams, StmtType,
};

pub struct Statement<'c, 't, C: FirebirdClient> {
    pub(crate) data: StatementData<C>,
    pub(crate) tr: &'t mut Transaction<'c, C>,
}

impl<'c, 't, C> Statement<'c, 't, C>
where
    C: FirebirdClient,
{
    /// Prepare the statement that will be executed
    pub fn prepare(
        tr: &'t mut Transaction<'c, C>,
        sql: &str,
        named_params: bool,
    ) -> Result<Self, FbError> {
        let data = StatementData::prepare(tr.conn, &mut tr.data, sql, named_params)?;

        Ok(Statement { data, tr })
    }

    /// Execute the current statement, returning a
    /// count of affected rows upon success
    ///
    /// Use `()` for no parameters or a tuple of parameters
    pub fn execute<T>(&mut self, params: T) -> Result<usize, FbError>
    where
        T: IntoParams,
    {
        self.data.execute(self.tr.conn, &mut self.tr.data, params)
    }

    /// Execute the current statement
    /// and returns the lines founds
    ///
    /// Use `()` for no parameters or a tuple of parameters
    pub fn query<'s, R, P>(&'s mut self, params: P) -> Result<StatementFetch<'c, 's, R, C>, FbError>
    where
        R: FromRow,
        P: IntoParams,
    {
        self.data.query(self.tr.conn, &mut self.tr.data, params)?;

        Ok(StatementFetch {
            stmt: &mut self.data,
            tr: self.tr,
            _marker: Default::default(),
        })
    }
}

impl<C> Drop for Statement<'_, '_, C>
where
    C: FirebirdClient,
{
    fn drop(&mut self) {
        self.data.close(self.tr.conn).ok();
    }
}
/// Cursor to fetch the results of a statement
pub struct StatementFetch<'c, 's, R, C: FirebirdClient> {
    pub(crate) stmt: &'s mut StatementData<C>,
    /// Transaction needs to be alive for the fetch to work
    pub(crate) tr: &'s mut Transaction<'c, C>,
    /// Type to convert the rows
    _marker: std::marker::PhantomData<R>,
}

impl<'c, 's, R, C> StatementFetch<'c, 's, R, C>
where
    R: FromRow,
    C: FirebirdClient,
{
    /// Fetch for the next row
    pub fn fetch(&mut self) -> Result<Option<R>, FbError> {
        self.stmt
            .fetch(self.tr.conn, &mut self.tr.data)
            .and_then(|row| row.map(FromRow::try_from).transpose())
    }
}

impl<T, C> Iterator for StatementFetch<'_, '_, T, C>
where
    T: FromRow,
    C: FirebirdClient,
{
    type Item = Result<T, FbError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.fetch().transpose()
    }
}

impl<R, C> Drop for StatementFetch<'_, '_, R, C>
where
    C: FirebirdClient,
{
    fn drop(&mut self) {
        self.stmt.close_cursor(self.tr.conn).ok();
    }
}

/// Low level statement handler.
///
/// Needs to be closed calling `close` before dropping.
pub struct StatementData<C: FirebirdClient> {
    pub(crate) handle: C::StmtHandle,
    pub(crate) stmt_type: StmtType,
    named_params: NamedParams,
}

impl<C: FirebirdClient> StatementData<C>
where
    C::StmtHandle: Send,
{
    /// Prepare the statement that will be executed
    pub fn prepare(
        conn: &mut Connection<C>,
        tr: &mut TransactionData<C>,
        raw_sql: &str,
        named_params: bool,
    ) -> Result<Self, FbError> {
        let named_params = if named_params {
            NamedParams::parse(raw_sql)?
        } else {
            NamedParams::empty(raw_sql)
        };
        let sql = &named_params.sql;

        let (stmt_type, handle) =
            conn.cli
                .prepare_statement(&mut conn.handle, &mut tr.handle, conn.dialect, sql)?;

        Ok(Self {
            stmt_type,
            handle,
            named_params,
        })
    }

    /// Execute the current statement without returnig any row
    ///
    /// Use `()` for no parameters or a tuple of parameters
    pub fn execute<T>(
        &mut self,
        conn: &mut Connection<C>,
        tr: &mut TransactionData<C>,
        params: T,
    ) -> Result<usize, FbError>
    where
        T: IntoParams,
    {
        let rows_count = conn.cli.execute(
            &mut conn.handle,
            &mut tr.handle,
            &mut self.handle,
            self.named_params.convert(params)?,
        )?;

        if self.stmt_type == StmtType::Select {
            // Close the cursor, as it will not be used
            self.close_cursor(conn)?;
        }

        Ok(rows_count)
    }

    /// Execute the current statement with input and returns a single row
    ///
    /// Use `()` for no parameters or a tuple of parameters
    pub fn execute2<T>(
        &mut self,
        conn: &mut Connection<C>,
        tr: &mut TransactionData<C>,
        params: T,
    ) -> Result<Vec<Column>, FbError>
    where
        T: IntoParams,
    {
        conn.cli.execute2(
            &mut conn.handle,
            &mut tr.handle,
            &mut self.handle,
            self.named_params.convert(params)?,
        )
    }

    /// Execute the current statement
    /// and returns the affected rows count
    ///
    /// Use `()` for no parameters or a tuple of parameters
    pub fn query<'s, T>(
        &'s mut self,
        conn: &'s mut Connection<C>,
        tr: &mut TransactionData<C>,
        params: T,
    ) -> Result<usize, FbError>
    where
        T: IntoParams,
    {
        conn.cli.execute(
            &mut conn.handle,
            &mut tr.handle,
            &mut self.handle,
            self.named_params.convert(params)?,
        )
    }

    /// Fetch for the next row, needs to be called after `query`
    pub fn fetch(
        &mut self,
        conn: &mut Connection<C>,
        tr: &mut TransactionData<C>,
    ) -> Result<Option<Vec<Column>>, FbError> {
        conn.cli
            .fetch(&mut conn.handle, &mut tr.handle, &mut self.handle)
    }

    /// Closes the statement cursor, if it was open
    pub fn close_cursor(&mut self, conn: &mut Connection<C>) -> Result<(), FbError> {
        conn.cli.free_statement(&mut self.handle, FreeStmtOp::Close)
    }

    /// Closes the statement
    pub fn close(&mut self, conn: &mut Connection<C>) -> Result<(), FbError> {
        conn.cli.free_statement(&mut self.handle, FreeStmtOp::Drop)
    }
}

#[cfg(test)]
/// Counter to allow the tests to be run in parallel without interfering in each other
static TABLE_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

#[cfg(test)]
mk_tests_default! {
    use crate::{prelude::*, Connection, Row};
    use rsfbclient_core::FirebirdClient;

    #[test]
    fn new_api_select() {
        let (mut conn, table) = setup();

        let vals = vec![
            (Some(2), "coffee".to_string()),
            (Some(3), "milk".to_string()),
            (None, "fail coffee".to_string()),
        ];

        conn.with_transaction(|tr| {
            for val in &vals {
                tr.execute(&format!("insert into {} (id, name) values (?, ?)", table), val.clone())
                    .expect("Error on insert");
            }

            Ok(())
        })
        .expect("Error commiting the transaction");

        let rows = conn
            .query(&format!("select id, name from {}", table), ())
            .expect("Error executing query");

        // Asserts that all values are equal
        assert_eq!(vals, rows);
    }

    #[test]
    fn old_api_select() {
        let (mut conn, table) = setup();

        let vals = vec![
            (Some(2), "coffee".to_string()),
            (Some(3), "milk".to_string()),
            (None, "fail coffee".to_string()),
        ];

        conn.with_transaction(|tr| {
            let mut stmt = tr
                .prepare(&format!("insert into {} (id, name) values (?, ?)", table), false)
                .expect("Error preparing the insert statement");

            for val in &vals {
                stmt.execute(val.clone()).expect("Error on insert");
            }

            Ok(())
        })
        .expect("Error commiting the transaction");

        conn.with_transaction(|tr| {
            let mut stmt = tr
                .prepare(&format!("select id, name from {}", table), false)
                .expect("Error on prepare the select");

            let rows: Vec<(Option<i32>, String)> = stmt
                .query(())
                .expect("Error on query")
                .collect::<Result<_, _>>()
                .expect("Error on fetch");

            // Asserts that all values are equal
            assert_eq!(vals, rows);

            let mut rows = stmt.query(()).expect("Error on query");

            let row1: Row = rows
                .fetch()
                .expect("Error on fetch the next row")
                .expect("No more rows");

            assert_eq!(
                2,
                row1.get::<i32>(0)
                    .expect("Error on get the first column value")
            );
            assert_eq!(
                "coffee".to_string(),
                row1.get::<String>(1)
                    .expect("Error on get the second column value")
            );

            let row = rows
                .fetch()
                .expect("Error on fetch the next row")
                .expect("No more rows");

            assert_eq!(
                3,
                row.get::<i32>(0)
                    .expect("Error on get the first column value")
            );
            assert_eq!(
                "milk".to_string(),
                row.get::<String>(1)
                    .expect("Error on get the second column value")
            );

            let row = rows
                .fetch()
                .expect("Error on fetch the next row")
                .expect("No more rows");

            assert!(
                row.get::<i32>(0).is_err(),
                "The 3° row have a null value, then should return an error"
            ); // null value
            assert!(
                row.get::<Option<i32>>(0)
                    .expect("Error on get the first column value")
                    .is_none(),
                "The 3° row have a null value, then should return a None"
            ); // null value
            assert_eq!(
                "fail coffee".to_string(),
                row.get::<String>(1)
                    .expect("Error on get the second column value")
            );

            let row = rows.fetch().expect("Error on fetch the next row");

            assert!(
                row.is_none(),
                "The 4° row dont exists, then should return a None"
            ); // null value

            Ok(())
        })
        .expect("Error commiting the transaction");

        conn.close().expect("error on close the connection");
    }

    #[test]
    fn prepared_insert() {
        let (mut conn, table) = setup();

        let vals = vec![(Some(9), "apple"), (Some(12), "jack"), (None, "coffee")];

        conn.with_transaction(|tr| {
            for val in vals.into_iter() {
                tr.execute(&format!("insert into {} (id, name) values (?, ?)", table), val)
                    .expect("Error on insert");
            }

            Ok(())
        })
        .expect("Error in the transaction");

        conn.close().expect("error on close the connection");
    }

    // #[test]
    // fn immediate_insert() {
    //     let (mut conn, table) = setup();

    //     conn.with_transaction(|tr| {
    //         tr.execute_immediate(&format!("insert into {} (id, name) values (?, ?)", (1, "apple", table)))
    //             .expect("Error on 1° insert");

    //         tr.execute_immediate(&format!("insert into {} (id, name) values (?, ?)", (2, "coffe", table)))
    //             .expect("Error on 2° insert");

    //         Ok(())
    //     })
    //     .expect("Error in the transaction");

    //     conn.close().expect("error on close the connection");
    // }

    fn setup() -> (Connection<impl FirebirdClient>, String) {
        let mut conn = cbuilder().connect()
            .expect("Error on connect in the test database");

        let table_num = super::TABLE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let table = format!("product{}", table_num);

        conn.with_transaction(|tr| {
            tr.execute_immediate(&format!("DROP TABLE {}", table)).ok();

            tr.execute_immediate(&format!("CREATE TABLE {} (id int, name varchar(60), quantity int)", table))
                .expect("Error on create the table product");

            Ok(())
        })
        .expect("Error in the transaction");

        (conn, table)
    }
}