-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinsert.rs
104 lines (87 loc) · 2.7 KB
/
insert.rs
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
//!
//! Rust Firebird Client
//!
//! Example of insert
//!
//! You need create a database with this table:
//! create table test (col_a int generated by default as identity, col_b float, col_c varchar(10));
//!
#![allow(unused_variables, unused_mut)]
use rsfbclient::{prelude::*, FbError};
const SQL_INSERT: &str = "insert into test (col_b, col_c) values (?, ?)";
const SQL_INSERT_NAMED: &str = "insert into test (col_b, col_c) values (:colb, :colc)";
const SQL_INSERT_PURE: &str = "insert into test (col_b, col_c) values (32, 'Leite')";
#[derive(Clone, IntoParams)]
struct ParamTest {
colb: f32,
colc: String,
}
#[derive(Clone, IntoParams)]
struct ParamTest2 {
colb: f32,
colc: String,
}
fn main() -> Result<(), FbError> {
#[cfg(feature = "linking")]
let mut conn = rsfbclient::builder_native()
.with_dyn_link()
.with_remote()
.host("localhost")
.db_name("examples.fdb")
.user("SYSDBA")
.pass("masterkey")
.connect()?;
#[cfg(feature = "dynamic_loading")]
let mut conn = rsfbclient::builder_native()
.with_dyn_load("./fbclient.lib")
.with_remote()
.host("localhost")
.db_name("examples.fdb")
.user("SYSDBA")
.pass("masterkey")
.connect()?;
#[cfg(feature = "pure_rust")]
let mut conn = rsfbclient::builder_pure_rust()
.host("localhost")
.db_name("examples.fdb")
.user("SYSDBA")
.pass("masterkey")
.connect()?;
let p1 = ParamTest {
colb: 150.0,
colc: "Café".to_string(),
};
let p2 = ParamTest {
colb: 132.0,
colc: "Arroz".to_string(),
};
let p3 = ParamTest2 {
colb: 150.0,
colc: "Feijão".to_string(),
};
conn.with_transaction(|tr| {
// First alternative (Recommended) (Prepares if needed and executes automatically)
tr.execute(SQL_INSERT, (94, "Banana"))?; // with position params
tr.execute(SQL_INSERT_NAMED, p1.clone())?; // with named params
tr.execute(SQL_INSERT_NAMED, p3.clone())?; // with named params, again
// Second alternative
tr.execute_immediate(SQL_INSERT_PURE)?;
// Third alternative, with position params
{
let mut stmt = tr.prepare(SQL_INSERT, false)?;
stmt.execute((-39, "test"))?;
stmt.execute((12, "test 2"))?;
}
// Fourth alternative, with named params
{
let mut stmt = tr.prepare(SQL_INSERT_NAMED, true)?;
stmt.execute(p1.clone())?;
stmt.execute(p2.clone())?;
stmt.execute(p3.clone())?;
}
Ok(())
})?;
// Explicit close is optional
conn.close()?;
Ok(())
}