akita 0.7.0

Akita - Mini orm for rust.
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
/*
 *
 *  *
 *  *      Copyright (c) 2018-2025, SnackCloud All rights reserved.
 *  *
 *  *   Redistribution and use in source and binary forms, with or without
 *  *   modification, are permitted provided that the following conditions are met:
 *  *
 *  *   Redistributions of source code must retain the above copyright notice,
 *  *   this list of conditions and the following disclaimer.
 *  *   Redistributions in binary form must reproduce the above copyright
 *  *   notice, this list of conditions and the following disclaimer in the
 *  *   documentation and/or other materials provided with the distribution.
 *  *   Neither the name of the www.snackcloud.cn developer nor the names of its
 *  *   contributors may be used to endorse or promote products derived from
 *  *   this software without specific prior written permission.
 *  *   Author: SnackCloud
 *  *
 *
 */
use crate::comm::ExecuteResult;
use crate::driver::blocking::mssql::MssqlConnection;
use crate::errors::AkitaError;
use akita_core::{
    AkitaValue, OperationType, Params, Row, Rows, SqlInjectionDetector, SqlSecurityConfig,
};
use indexmap::IndexMap;
use serde_json::Value;
use std::str::FromStr;

pub struct MssqlAdapter {
    conn: MssqlConnection,
}

impl MssqlAdapter {
    pub fn new(conn: MssqlConnection) -> Self {
        Self { conn }
    }

    /// Start the transaction
    #[track_caller]
    pub fn start_transaction(&self) -> crate::prelude::Result<()> {
        self.conn.simple_query("BEGIN TRANSACTION;")?;
        Ok(())
    }

    /// Submit transactions
    #[track_caller]
    pub fn commit_transaction(&self) -> crate::prelude::Result<()> {
        self.conn.simple_query("COMMIT TRANSACTION;")?;
        Ok(())
    }

    /// Roll back transactions
    #[track_caller]
    pub fn rollback_transaction(&self) -> crate::prelude::Result<()> {
        self.conn.simple_query("ROLLBACK TRANSACTION;")?;
        Ok(())
    }

    #[track_caller]
    pub fn query(&self, sql: &str, params: Params) -> Result<Rows, AkitaError> {
        // Convert parameters
        let mssql_params = convert_to_mssql_params(params);
        let param_refs: Vec<&dyn tiberius::ToSql> = mssql_params
            .iter()
            .map(|p| &**p as &dyn tiberius::ToSql)
            .collect();
        self.inner_query(sql, &param_refs)
    }

    #[track_caller]
    fn inner_query(
        &self,
        sql: &str,
        param_refs: &[&dyn tiberius::ToSql],
    ) -> Result<Rows, AkitaError> {
        let rows = self.conn.query(sql, &param_refs)?;
        if rows.is_empty() {
            return Ok(Rows::new());
        }
        let first_row = &rows[0];
        let column_names: Vec<String> = (0..first_row.columns().len())
            .map(|i| first_row.columns()[i].name().to_string())
            .collect();

        let mut records = Rows::new();
        for row in rows {
            let mut record = Vec::new();

            for i in 0..row.columns().len() {
                let value = get_value_from_mssql_row(&row, i)?;
                record.push(value);
            }
            records.push(Row {
                columns: column_names.clone(),
                data: record,
            });
        }

        Ok(records)
    }

    #[track_caller]
    pub fn execute(&self, sql: &str, params: Params) -> Result<ExecuteResult, AkitaError> {
        // Get statement types (query, update, etc.)
        let stmt_type = OperationType::detect_operation_type(sql);
        // Convert parameters
        let mssql_params = convert_to_mssql_params(params);
        let param_refs: Vec<&dyn tiberius::ToSql> = mssql_params
            .iter()
            .map(|p| &**p as &dyn tiberius::ToSql)
            .collect();
        match stmt_type {
            OperationType::Select => {
                let records = self.inner_query(sql, &param_refs)?;
                Ok(ExecuteResult::Rows(records))
            }
            _ => {
                let affected_rows = self.conn.execute(sql, &param_refs)?;
                Ok(ExecuteResult::AffectedRows(affected_rows))
            }
        }
    }
}

fn convert_to_mssql_params(params: Params) -> Vec<Box<dyn tiberius::ToSql>> {
    match params {
        Params::None => vec![],
        Params::Positional(param_values) => param_values
            .into_iter()
            .map(|v| convert_akita_value_to_mssql(v))
            .collect::<Vec<_>>(),
        Params::Named(named_params) => named_params
            .values()
            .cloned()
            .into_iter()
            .map(|v| convert_akita_value_to_mssql(v))
            .collect::<Vec<_>>(),
    }
}

fn convert_akita_value_to_mssql(val: AkitaValue) -> Box<dyn tiberius::ToSql> {
    use tiberius::numeric::BigDecimal;
    match val {
        AkitaValue::Text(v) => Box::new(v),
        AkitaValue::Bool(v) => Box::new(v),
        AkitaValue::Tinyint(v) => {
            let unsigned = if v >= 0 {
                v as u8
            } else {
                (v as i16 + 256) as u8
            };
            Box::new(unsigned)
        }
        AkitaValue::Smallint(v) => Box::new(v),
        AkitaValue::Int(v) => Box::new(v),
        AkitaValue::Bigint(v) => Box::new(v),
        AkitaValue::Float(v) => Box::new(v),
        AkitaValue::Double(v) => Box::new(v),
        AkitaValue::BigDecimal(v) => {
            let bd = BigDecimal::from_str(v.to_string().as_str()).unwrap_or(BigDecimal::from(0));
            Box::new(bd)
        }
        AkitaValue::Blob(v) => Box::new(v),
        AkitaValue::Char(v) => Box::new(format!("{}", v)),
        AkitaValue::Json(v) => Box::new(serde_json::to_string(&v).unwrap_or_default()),
        AkitaValue::Json(j) => match j {
            Value::Bool(v) => Box::new(v),
            Value::Number(v) => {
                if let Some(n) = v.as_u64() {
                    Box::new(n as i64)
                } else if let Some(n) = v.as_f64() {
                    Box::new(n)
                } else if let Some(n) = v.as_i64() {
                    Box::new(n)
                } else {
                    Box::new(v.to_string())
                }
            }
            Value::String(v) => Box::new(v),
            _ => Box::new(serde_json::to_string(&j).unwrap_or_default()),
        },
        AkitaValue::Uuid(v) => Box::new(v),
        AkitaValue::Date(v) => Box::new(v),
        AkitaValue::DateTime(v) => Box::new(v),
        AkitaValue::Null => Box::new(Option::<String>::None),
        _ => Box::new(val.to_string()),
    }
}

fn get_value_from_mssql_row(row: &tiberius::Row, index: usize) -> Result<AkitaValue, AkitaError> {
    use chrono::{NaiveDate, NaiveDateTime};
    use tiberius::numeric::BigDecimal;

    let column = &row.columns()[index];

    match column.column_type() {
        // Bit / Boolean Type
        tiberius::ColumnType::Bit | tiberius::ColumnType::Bitn => {
            let val: Option<bool> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Bool(v),
                None => AkitaValue::Null,
            })
        }

        // Integer types
        tiberius::ColumnType::Int1 => {
            let val: Option<u8> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Tinyint(v as i8),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Int2 => {
            let val: Option<i16> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Smallint(v),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Int4 => {
            let val: Option<i32> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Int(v),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Int8 => {
            let val: Option<i64> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Bigint(v),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Intn => {
            // Intn may be an integer of 1, 2, 4, 8 bytes, try to get i64
            let val: Option<i64> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Bigint(v),
                None => AkitaValue::Null,
            })
        }

        // Floating-point types
        tiberius::ColumnType::Float4 => {
            let val: Option<f32> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Float(v),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Float8 => {
            let val: Option<f64> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Double(v),
                None => AkitaValue::Null,
            })
        }
        tiberius::ColumnType::Floatn => {
            //Floatn may be a 4 or 8 byte floating-point number; try to get it as f64
            let val: Option<f64> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Double(v),
                None => AkitaValue::Null,
            })
        }

        // Numeric types(Decimal/Numeric)
        tiberius::ColumnType::Decimaln | tiberius::ColumnType::Numericn => {
            let val: Option<BigDecimal> = row.get(index);
            Ok(match val {
                Some(v) => {
                    // Convert to a string, and then create BigDecimal
                    let decimal_str = v.to_string();
                    match decimal_str.parse() {
                        Ok(bd) => AkitaValue::BigDecimal(bd),
                        Err(_) => AkitaValue::Text(decimal_str),
                    }
                }
                None => AkitaValue::Null,
            })
        }

        // String Types
        tiberius::ColumnType::BigVarChar
        | tiberius::ColumnType::BigChar
        | tiberius::ColumnType::NVarchar
        | tiberius::ColumnType::NChar
        | tiberius::ColumnType::Text
        | tiberius::ColumnType::NText => {
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Text(v.to_string()),
                None => AkitaValue::Null,
            })
        }

        // XML type
        tiberius::ColumnType::Xml => {
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Text(v.to_string()), // 或者 AkitaValue::Xml 如果你有这种类型
                None => AkitaValue::Null,
            })
        }

        // Date-time types
        tiberius::ColumnType::Daten => {
            let val: Option<NaiveDate> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Date(v),
                None => AkitaValue::Null,
            })
        }

        tiberius::ColumnType::Datetime
        | tiberius::ColumnType::Datetime4
        | tiberius::ColumnType::Datetimen
        | tiberius::ColumnType::Datetime2 => {
            if let Ok(val) = row.try_get::<NaiveDateTime, _>(index) {
                return Ok(match val {
                    Some(v) => AkitaValue::DateTime(v),
                    None => AkitaValue::Null,
                });
            }
            // If you can't get NaiveDateTime directly, try getting it as a string
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::DateTime(
                    NaiveDateTime::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.f").unwrap_or_else(|_| {
                        NaiveDate::from_ymd_opt(1970, 1, 1)
                            .unwrap()
                            .and_hms_opt(0, 0, 0)
                            .unwrap()
                    }),
                ),
                None => AkitaValue::Null,
            })
        }

        tiberius::ColumnType::Timen => {
            //The Time type, possibly treated as a string
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Text(v.to_string()), // 或者创建专门的 Time 类型
                None => AkitaValue::Null,
            })
        }

        tiberius::ColumnType::DatetimeOffsetn => {
            // Date and time with a time zone, treated as a string
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Text(v.to_string()),
                None => AkitaValue::Null,
            })
        }

        // Binary type
        tiberius::ColumnType::BigVarBin
        | tiberius::ColumnType::BigBinary
        | tiberius::ColumnType::Image => {
            let val: Option<&[u8]> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Blob(v.to_vec()),
                None => AkitaValue::Null,
            })
        }

        // GUID/UUID type
        tiberius::ColumnType::Guid => {
            let val = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Uuid(v),
                None => AkitaValue::Null,
            })
        }

        // Type of currency
        tiberius::ColumnType::Money | tiberius::ColumnType::Money4 => {
            // Currency types can be treated as floating-point numbers or strings
            if let Ok(val) = row.try_get::<f64, _>(index) {
                return Ok(match val {
                    Some(v) => AkitaValue::Double(v),
                    None => AkitaValue::Null,
                });
            }
            let val: Option<&str> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Text(v.to_string()),
                None => AkitaValue::Null,
            })
        }

        // Variant type
        tiberius::ColumnType::SSVariant => {
            // SQL Variant type.Try every possible type
            if let Ok(val) = row.try_get::<&str, _>(index) {
                if let Some(v) = val {
                    return Ok(AkitaValue::Text(v.to_string()));
                }
            }
            if let Ok(val) = row.try_get::<i64, _>(index) {
                if let Some(v) = val {
                    return Ok(AkitaValue::Bigint(v));
                }
            }
            if let Ok(val) = row.try_get::<f64, _>(index) {
                if let Some(v) = val {
                    return Ok(AkitaValue::Double(v));
                }
            }
            Ok(AkitaValue::Null)
        }

        // Other types
        tiberius::ColumnType::Udt => {
            // User-defined types, treated as binary
            let val: Option<&[u8]> = row.get(index);
            Ok(match val {
                Some(v) => AkitaValue::Blob(v.to_vec()),
                None => AkitaValue::Null,
            })
        }

        // Null Type or unknown type
        tiberius::ColumnType::Null => Ok(AkitaValue::Null),
    }
}