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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! `FirebirdConnection` implementation for the native fbclient

use crate::{
    ibase::{self, IBase},
    params::Params,
    row::ColumnBuffer,
    status::Status,
    xsqlda::XSqlDa,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use rsfbclient_core::*;
use std::ffi::CString;
use std::os::raw::c_char;
use std::{convert::TryFrom, io::Cursor, ptr, str};

type NativeDbHandle = ibase::isc_db_handle;
type NativeTrHandle = ibase::isc_tr_handle;
type NativeStmtHandle = ibase::isc_stmt_handle;

/// Client that wraps the native fbclient library
pub struct NativeFbClient<T: LinkageMarker> {
    ibase: T::L,
    status: Status,
    charset: Charset,
}

/// The remote part of native client configuration
#[derive(Clone, Default)]
pub struct RemoteConfig {
    pub host: String,
    pub port: u16,
    pub pass: String,
}

/// Data associated with a prepared statement
pub struct StmtHandleData {
    /// Statement handle
    handle: NativeStmtHandle,
    /// Output xsqlda
    xsqlda: XSqlDa,
    /// Buffers for the output xsqlda
    col_buffers: Vec<ColumnBuffer>,
}

///The common part of native client configuration (for both embedded/remote)
#[derive(Clone, Default)]
pub struct NativeFbAttachmentConfig {
    pub db_name: String,
    pub user: String,
    pub role_name: Option<String>,
    pub remote: Option<RemoteConfig>,
}

/// A marker trait which can be used to
/// obtain the associated client instance
pub trait LinkageMarker: Send + Sync {
    type L: IBase + Send;
}

/// Configuration details for dynamic linking
#[derive(Clone)]
pub struct DynLink(pub Charset);

#[cfg(feature = "linking")]
impl LinkageMarker for DynLink {
    type L = ibase::IBaseLinking;
}

#[cfg(feature = "linking")]
impl DynLink {
    pub fn to_client(&self) -> NativeFbClient<DynLink> {
        let result: NativeFbClient<DynLink> = NativeFbClient {
            ibase: ibase::IBaseLinking,
            status: Default::default(),
            charset: self.0.clone(),
        };
        result
    }
}

/// Configuration details for dynamic loading
#[derive(Clone)]
pub struct DynLoad {
    pub charset: Charset,
    pub lib_path: String,
}

#[cfg(feature = "dynamic_loading")]
impl LinkageMarker for DynLoad {
    type L = ibase::IBaseDynLoading;
}

#[cfg(feature = "dynamic_loading")]
impl DynLoad {
    pub fn try_to_client(&self) -> Result<NativeFbClient<Self>, FbError> {
        let load_result = ibase::IBaseDynLoading::with_client(self.lib_path.as_ref())
            .map_err(|e| FbError::from(e.to_string()))?;

        let result: NativeFbClient<DynLoad> = NativeFbClient {
            ibase: load_result,
            status: Default::default(),
            charset: self.charset.clone(),
        };

        Ok(result)
    }
}

impl<T: LinkageMarker> FirebirdClientDbOps for NativeFbClient<T> {
    type DbHandle = NativeDbHandle;
    type AttachmentConfig = NativeFbAttachmentConfig;

    fn attach_database(
        &mut self,
        config: &Self::AttachmentConfig,
    ) -> Result<NativeDbHandle, FbError> {
        let (dpb, conn_string) = self.build_dpb(config);
        let mut handle = 0;

        unsafe {
            if self.ibase.isc_attach_database()(
                &mut self.status[0],
                conn_string.len() as i16,
                conn_string.as_ptr() as *const _,
                &mut handle,
                dpb.len() as i16,
                dpb.as_ptr() as *const _,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        // Assert that the handle is valid
        debug_assert_ne!(handle, 0);

        Ok(handle)
    }

    fn detach_database(&mut self, db_handle: &mut NativeDbHandle) -> Result<(), FbError> {
        unsafe {
            // Close the connection, if the handle is valid
            if *db_handle != 0
                && self.ibase.isc_detach_database()(&mut self.status[0], db_handle) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }
        Ok(())
    }

    fn drop_database(&mut self, db_handle: &mut NativeDbHandle) -> Result<(), FbError> {
        unsafe {
            if self.ibase.isc_drop_database()(&mut self.status[0], db_handle) != 0 {
                return Err(self.status.as_error(&self.ibase));
            }
        }
        Ok(())
    }

    fn create_database(
        &mut self,
        config: &Self::AttachmentConfig,
        page_size: Option<u32>,
    ) -> Result<NativeDbHandle, FbError> {
        let (mut dpb, conn_string) = self.build_dpb(config);
        let mut handle = 0;

        if let Some(ps) = page_size {
            dpb.extend(&[ibase::isc_dpb_page_size as u8, 4]);
            dpb.write_u32::<LittleEndian>(ps)?;
        }

        unsafe {
            if self.ibase.isc_create_database()(
                &mut self.status[0],
                conn_string.len() as i16,
                conn_string.as_ptr() as *const _,
                &mut handle,
                dpb.len() as i16,
                dpb.as_ptr() as *const _,
                0,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        // Assert that the handle is valid
        debug_assert_ne!(handle, 0);

        Ok(handle)
    }
}

impl<T: LinkageMarker> FirebirdClientSqlOps for NativeFbClient<T> {
    type DbHandle = NativeDbHandle;
    type TrHandle = NativeTrHandle;
    type StmtHandle = StmtHandleData;

    fn begin_transaction(
        &mut self,
        db_handle: &mut Self::DbHandle,
        confs: TransactionConfiguration,
    ) -> Result<Self::TrHandle, FbError> {
        let mut handle = 0;

        // Transaction parameter buffer
        let mut tpb = vec![
            ibase::isc_tpb_version3 as u8,
            confs.isolation.into(),
            confs.data_access as u8,
            confs.lock_resolution.into(),
        ];
        if let TrLockResolution::Wait(Some(time)) = confs.lock_resolution {
            tpb.push(ibase::isc_tpb_lock_timeout as u8);
            tpb.push(4 as u8);
            tpb.extend_from_slice(&time.to_le_bytes());
        }

        if let TrIsolationLevel::ReadCommited(rec) = confs.isolation {
            tpb.push(rec as u8);
        }

        #[repr(C)]
        struct IscTeb {
            db_handle: *mut ibase::isc_db_handle,
            tpb_len: usize,
            tpb_ptr: *const u8,
        }

        unsafe {
            if self.ibase.isc_start_multiple()(
                &mut self.status[0],
                &mut handle,
                1,
                &mut IscTeb {
                    db_handle,
                    tpb_len: tpb.len(),
                    tpb_ptr: &tpb[0],
                } as *mut _ as _,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        // Assert that the handle is valid
        debug_assert_ne!(handle, 0);

        Ok(handle)
    }

    fn transaction_operation(
        &mut self,
        tr_handle: &mut Self::TrHandle,
        op: TrOp,
    ) -> Result<(), FbError> {
        let handle = tr_handle;
        unsafe {
            if match op {
                TrOp::Commit => self.ibase.isc_commit_transaction()(&mut self.status[0], handle),
                TrOp::CommitRetaining => {
                    self.ibase.isc_commit_retaining()(&mut self.status[0], handle)
                }
                TrOp::Rollback => {
                    self.ibase.isc_rollback_transaction()(&mut self.status[0], handle)
                }
                TrOp::RollbackRetaining => {
                    self.ibase.isc_rollback_retaining()(&mut self.status[0], handle)
                }
            } != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }
        Ok(())
    }

    fn exec_immediate(
        &mut self,
        db_handle: &mut Self::DbHandle,
        tr_handle: &mut Self::TrHandle,
        dialect: Dialect,
        sql: &str,
    ) -> Result<(), FbError> {
        let sql = self.charset.encode(sql)?;

        unsafe {
            if self.ibase.isc_dsql_execute_immediate()(
                &mut self.status[0],
                db_handle,
                tr_handle,
                sql.len() as u16,
                sql.as_ptr() as *const _,
                dialect as u16,
                ptr::null(),
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }
        Ok(())
    }

    fn prepare_statement(
        &mut self,
        db_handle: &mut Self::DbHandle,
        tr_handle: &mut Self::TrHandle,
        dialect: Dialect,
        sql: &str,
    ) -> Result<(StmtType, Self::StmtHandle), FbError> {
        let sql = self.charset.encode(sql)?;

        let mut handle = 0;

        let mut xsqlda = XSqlDa::new(1);

        let mut stmt_type = 0;

        unsafe {
            if self.ibase.isc_dsql_allocate_statement()(&mut self.status[0], db_handle, &mut handle)
                != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }

            if self.ibase.isc_dsql_prepare()(
                &mut self.status[0],
                tr_handle,
                &mut handle,
                sql.len() as u16,
                sql.as_ptr() as *const _,
                dialect as u16,
                &mut *xsqlda,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }

            let row_count = xsqlda.sqld;

            if row_count > xsqlda.sqln {
                // Need more XSQLVARs
                xsqlda = XSqlDa::new(row_count);

                if self.ibase.isc_dsql_describe()(&mut self.status[0], &mut handle, 1, &mut *xsqlda)
                    != 0
                {
                    return Err(self.status.as_error(&self.ibase));
                }
            }

            // Get the statement type
            let info_req = [ibase::isc_info_sql_stmt_type as std::os::raw::c_char];
            let mut info_buf = [0; 10];

            if self.ibase.isc_dsql_sql_info()(
                &mut self.status[0],
                &mut handle,
                info_req.len() as i16,
                &info_req[0],
                info_buf.len() as i16,
                &mut info_buf[0],
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }

            for &v in &info_buf[3..] {
                // Search for the data
                if v != 0 {
                    stmt_type = v;
                    break;
                }
            }
        }

        let stmt_type = StmtType::try_from(stmt_type as u8)
            .map_err(|_| FbError::from(format!("Invalid statement type: {}", stmt_type)))?;

        // Create the column buffers and set the xsqlda conercions
        let col_buffers = (0..xsqlda.sqld)
            .map(|col| {
                let xcol = xsqlda
                    .get_xsqlvar_mut(col as usize)
                    .ok_or_else(|| FbError::from("Error getting the xsqlvar"))?;

                ColumnBuffer::from_xsqlvar(xcol)
            })
            .collect::<Result<_, _>>()?;

        Ok((
            stmt_type,
            StmtHandleData {
                handle,
                xsqlda,
                col_buffers,
            },
        ))
    }

    fn free_statement(
        &mut self,
        stmt_handle: &mut Self::StmtHandle,
        op: FreeStmtOp,
    ) -> Result<(), FbError> {
        unsafe {
            if self.ibase.isc_dsql_free_statement()(
                &mut self.status[0],
                &mut stmt_handle.handle,
                op as u16,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        Ok(())
    }

    fn execute(
        &mut self,
        db_handle: &mut Self::DbHandle,
        tr_handle: &mut Self::TrHandle,
        stmt_handle: &mut Self::StmtHandle,
        params: Vec<SqlType>,
    ) -> Result<usize, FbError> {
        let params = Params::new(
            db_handle,
            tr_handle,
            &self.ibase,
            &mut self.status,
            &mut stmt_handle.handle,
            params,
            &self.charset,
        )?;

        unsafe {
            if self.ibase.isc_dsql_execute()(
                &mut self.status[0],
                tr_handle,
                &mut stmt_handle.handle,
                1,
                if let Some(xsqlda) = &params.xsqlda {
                    &**xsqlda
                } else {
                    ptr::null()
                },
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        // Just to make sure the params are not dropped too soon
        drop(params);

        // Get the affected rows count
        let info_req = [ibase::isc_info_sql_records as std::os::raw::c_char];
        let mut info_buf = [0u8; 64];

        unsafe {
            if self.ibase.isc_dsql_sql_info()(
                &mut self.status[0],
                &mut stmt_handle.handle,
                info_req.len() as i16,
                &info_req[0],
                info_buf.len() as i16,
                info_buf.as_mut_ptr() as _,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        let mut affected = 0;

        let mut data = Cursor::new(info_buf);

        if data.read_u8()? == ibase::isc_info_sql_records as u8 {
            let _info_buf_size = data.read_u16::<LittleEndian>()?;

            loop {
                match data.read_u8()? as u32 {
                    ibase::isc_info_req_select_count => {
                        // Not interested in the selected count
                        let len = data.read_u16::<LittleEndian>()? as usize;
                        let _selected = data.read_uint::<LittleEndian>(len)?;
                    }

                    ibase::isc_info_req_insert_count
                    | ibase::isc_info_req_update_count
                    | ibase::isc_info_req_delete_count => {
                        let len = data.read_u16::<LittleEndian>()? as usize;

                        affected += data.read_uint::<LittleEndian>(len)? as usize;
                    }

                    ibase::isc_info_end => {
                        break;
                    }

                    _ => return Err(FbError::from("Invalid affected rows response")),
                }
            }
        }

        Ok(affected as usize)
    }

    fn fetch(
        &mut self,
        db_handle: &mut Self::DbHandle,
        tr_handle: &mut Self::TrHandle,
        stmt_handle: &mut Self::StmtHandle,
    ) -> Result<Option<Vec<Column>>, FbError> {
        unsafe {
            let fetch_status = self.ibase.isc_dsql_fetch()(
                &mut self.status[0],
                &mut stmt_handle.handle,
                1,
                &*stmt_handle.xsqlda,
            );

            // 100 indicates that no more rows: http://docwiki.embarcadero.com/InterBase/2020/en/Isc_dsql_fetch()
            if fetch_status == 100 {
                return Ok(None);
            }

            if fetch_status != 0 {
                return Err(self.status.as_error(&self.ibase));
            };
        }

        let cols = stmt_handle
            .col_buffers
            .iter()
            .map(|cb| cb.to_column(db_handle, tr_handle, &self.ibase, &self.charset))
            .collect::<Result<_, _>>()?;

        Ok(Some(cols))
    }

    fn execute2(
        &mut self,
        db_handle: &mut Self::DbHandle,
        tr_handle: &mut Self::TrHandle,
        stmt_handle: &mut Self::StmtHandle,
        params: Vec<SqlType>,
    ) -> Result<Vec<Column>, FbError> {
        let params = Params::new(
            db_handle,
            tr_handle,
            &self.ibase,
            &mut self.status,
            &mut stmt_handle.handle,
            params,
            &self.charset,
        )?;

        unsafe {
            if self.ibase.isc_dsql_execute2()(
                &mut self.status[0],
                tr_handle,
                &mut stmt_handle.handle,
                1,
                if let Some(xsqlda) = &params.xsqlda {
                    &**xsqlda
                } else {
                    ptr::null()
                },
                &*stmt_handle.xsqlda,
            ) != 0
            {
                return Err(self.status.as_error(&self.ibase));
            }
        }

        // Just to make sure the params are not dropped too soon
        drop(params);

        let rcol = stmt_handle
            .col_buffers
            .iter()
            .map(|cb| cb.to_column(db_handle, tr_handle, &self.ibase, &self.charset))
            .collect::<Result<_, _>>()?;

        Ok(rcol)
    }
}

impl<T: LinkageMarker> FirebirdClientDbEvents for NativeFbClient<T> {
    fn wait_for_event(
        &mut self,
        db_handle: &mut Self::DbHandle,
        name: String,
    ) -> Result<(), FbError> {
        let mut event_buffer = ptr::null_mut();
        let mut result_buffer = ptr::null_mut();

        let name = CString::new(name.clone()).unwrap();
        let len = unsafe {
            self.ibase.isc_event_block()(
                &mut event_buffer,
                &mut result_buffer,
                1,
                name.as_ptr() as *mut c_char,
            )
        };
        debug_assert!(!event_buffer.is_null() && !result_buffer.is_null());

        // Preparing the event_buffer. Yes, I need call the isc_wait_for_event
        // before call isc_wait_for_event.
        //
        // PHP example: https://github.com/FirebirdSQL/php-firebird/blob/b6c288326678f7b8613f5a7d0648ad010c67674c/ibase_events.c#L126
        {
            unsafe {
                if self.ibase.isc_wait_for_event()(
                    &mut self.status[0],
                    db_handle,
                    len as i16,
                    event_buffer,
                    result_buffer,
                ) != 0
                {
                    self.ibase.isc_free()(event_buffer);
                    self.ibase.isc_free()(result_buffer);

                    return Err(self.status.as_error(&self.ibase));
                }
            }

            unsafe {
                self.ibase.isc_event_counts()(
                    &mut self.status[0],
                    len as i16,
                    event_buffer,
                    result_buffer,
                );
            }
        }

        unsafe {
            if self.ibase.isc_wait_for_event()(
                &mut self.status[0],
                db_handle,
                len as i16,
                event_buffer,
                result_buffer,
            ) != 0
            {
                self.ibase.isc_free()(event_buffer);
                self.ibase.isc_free()(result_buffer);

                return Err(self.status.as_error(&self.ibase));
            }

            self.ibase.isc_free()(event_buffer);
            self.ibase.isc_free()(result_buffer);
        }

        Ok(())
    }
}

impl<T: LinkageMarker> NativeFbClient<T> {
    /// Build the dpb and the connection string
    ///
    /// Used by attach database operations
    fn build_dpb(&mut self, config: &NativeFbAttachmentConfig) -> (Vec<u8>, String) {
        let user = &config.user;
        let mut password = None;
        let db_name = &config.db_name;

        let conn_string = match &config.remote {
            None => db_name.clone(),
            Some(remote_conf) => {
                password = Some(remote_conf.pass.as_str());
                format!(
                    "{}/{}:{}",
                    remote_conf.host.as_str(),
                    remote_conf.port,
                    db_name.as_str()
                )
            }
        };

        let dpb = {
            let mut dpb: Vec<u8> = Vec::with_capacity(64);

            dpb.extend(&[ibase::isc_dpb_version1 as u8]);

            dpb.extend(&[ibase::isc_dpb_user_name as u8, user.len() as u8]);
            dpb.extend(user.bytes());

            if let Some(pass_str) = password {
                dpb.extend(&[ibase::isc_dpb_password as u8, pass_str.len() as u8]);
                dpb.extend(pass_str.bytes());
            };

            let charset = self.charset.on_firebird.bytes();

            dpb.extend(&[ibase::isc_dpb_lc_ctype as u8, charset.len() as u8]);
            dpb.extend(charset);

            if let Some(role) = &config.role_name {
                dpb.extend(&[ibase::isc_dpb_sql_role_name as u8, role.len() as u8]);
                dpb.extend(role.bytes());
            }

            dpb
        };

        (dpb, conn_string)
    }
}