geekorm_core/backends/
mod.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
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
//! # Backend Module for GeekORM
//!
//! **Example:**
//!
//! Here is an example of how to use GeekORM with a mock connection.
//!
//! ```no_run
//! # #[cfg(feature = "backends")] {
//! # use anyhow::Result;
//! use geekorm::prelude::*;
//!
//! # #[derive(Debug, Clone)]
//! # struct Connection;
//! # impl GeekConnection for Connection {
//! #     type Connection = Self;
//! # }
//!
//! #[derive(Table, Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
//! pub struct Users {
//!     #[geekorm(primary_key, auto_increment)]
//!     pub id: PrimaryKey<i32>,
//!     #[geekorm(unique)]
//!     pub username: String,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!     // Create a new connection (this is a mock connection)
//!     let connection = Connection {};
//!
//!     Users::create_table(&connection).await?;
//!
//!     let users = vec!["geekmasher", "bob", "alice", "eve", "mallory", "trent"];
//!     for user in users {
//!         let mut new_user = Users::new(user);
//!         new_user.save(&connection).await?;
//!     }
//!
//!     // Fetch or create a user
//!     let mut geek = Users::new("geekmasher");
//!     geek.fetch_or_create(&connection).await?;
//!     
//!     // Fetch a user by their username (exact match)
//!     let geekmasher = Users::fetch_by_username(&connection, "geekmasher").await?;
//!
//!     // Search for a user (partial match)
//!     let search = Users::search(&connection, "geek").await?;
//!     # assert_eq!(search.len(), 1);
//!
//!     // Fetch first and last user
//!     let first_user = Users::first(&connection).await?;
//!     # assert_eq!(first_user.username, "geekmasher");
//!     let last_user = Users::last(&connection).await?;
//!     # assert_eq!(last_user.username, "trent");
//!
//!     // Delete the user
//!     geek.delete(&connection).await?;
//!
//!     Ok(())
//! }
//! # }
//! ```

use std::collections::HashMap;

use crate::{Query, QueryBuilder, QueryBuilderTrait, TableBuilder, TablePrimaryKey, Value};

#[cfg(feature = "libsql")]
pub mod libsql;
#[cfg(feature = "rusqlite")]
pub mod rusqlite;

/// GeekConnection is the trait used for models to interact with the database.
///
/// This trait is used to define the methods that are used to interact with the database.
pub trait GeekConnector<'a, C>
where
    C: GeekConnection<Connection = C> + 'a,
    Self: Sized + TableBuilder + QueryBuilderTrait + serde::Serialize + serde::de::DeserializeOwned,
{
    /// Query the database with an active Connection and Query
    #[allow(async_fn_in_trait, unused_variables)]
    async fn query(connection: &'a C, query: Query) -> Result<Vec<Self>, crate::Error> {
        C::query::<Self>(connection, query).await
    }

    /// Query the first row from the database with an active Connection and Query
    #[allow(async_fn_in_trait, unused_variables)]
    async fn query_first(connection: &'a C, query: Query) -> Result<Self, crate::Error> {
        C::query_first::<Self>(connection, query).await
    }

    /// Execute a query on the database and do not return any rows
    #[allow(async_fn_in_trait, unused_variables)]
    async fn execute(connection: &'a C, query: Query) -> Result<(), crate::Error> {
        C::execute(connection, query).await
    }

    /// Create a table in the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn create_table(connection: &'a C) -> Result<(), crate::Error> {
        C::create_table::<Self>(connection).await
    }

    /// Count the number of rows based on a Query
    #[allow(async_fn_in_trait, unused_variables)]
    async fn row_count(connection: &'a C, query: Query) -> Result<i64, crate::Error> {
        C::row_count(connection, query).await
    }

    /// Count the total number of rows in the table
    #[allow(async_fn_in_trait, unused_variables)]
    async fn total(connection: &'a C) -> Result<i64, crate::Error> {
        C::row_count(
            connection,
            Self::query_count().table(Self::table()).build()?,
        )
        .await
    }

    /// Fetch all rows from the table
    #[allow(async_fn_in_trait, unused_variables)]
    async fn all(connection: &'a C) -> Result<Vec<Self>, crate::Error> {
        C::query::<Self>(
            connection,
            Self::query_select().table(Self::table()).build()?,
        )
        .await
    }

    /// Fetch by Page
    #[cfg(feature = "pagination")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn page(connection: &'a C, page: &crate::Page) -> Result<Vec<Self>, crate::Error> {
        C::query::<Self>(
            connection,
            QueryBuilder::select()
                .table(Self::table())
                .page(page)
                .build()?,
        )
        .await
    }

    /// Create a new Pagination instance with the current table and fetch
    /// total number of rows
    #[cfg(feature = "pagination")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn paginate(connection: &'a C) -> Result<crate::Pagination<Self>, crate::Error> {
        let mut page = crate::Pagination::new();
        page.set_total(Self::total(connection).await? as u32);
        Ok(page)
    }

    /// Update the current object in the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn update(&mut self, connection: &'a C) -> Result<(), crate::Error> {
        C::execute(connection, Self::query_update(self)).await
    }

    /// Save the current object to the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn save(&mut self, connection: &'a C) -> Result<(), crate::Error>;

    /// Delete the current object from the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn delete(&self, connection: &'a C) -> Result<(), crate::Error> {
        C::execute(connection, Self::query_delete(self)).await
    }

    /// Fetches all of the foreign key values for the current object
    #[allow(async_fn_in_trait, unused_variables)]
    async fn fetch(&mut self, connection: &'a C) -> Result<(), crate::Error>;

    /// Filter the rows in the table based on specific criteria passed as a tuple of (&str, Value).
    ///
    /// You can use prefix operators to define the type of comparison to use:
    ///
    /// - `=`: Equal
    /// - `~`: Like
    /// - `!`: Not equal
    ///
    /// If no prefix is used, the default comparison is equal.
    #[allow(async_fn_in_trait, unused_variables)]
    async fn filter(
        connection: &'a C,
        fields: Vec<(&str, impl Into<Value>)>,
    ) -> Result<Vec<Self>, crate::Error> {
        Self::query(
            connection,
            Self::query_select()
                .table(Self::table())
                .filter(fields)
                .build()?,
        )
        .await
    }

    /// Filter with Pagination
    #[cfg(feature = "pagination")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn filter_page(
        connection: &'a C,
        fields: Vec<(&str, impl Into<Value>)>,
        page: &crate::Page,
    ) -> Result<Vec<Self>, crate::Error> {
        Self::query(
            connection,
            Self::query_select()
                .table(Self::table())
                .filter(fields)
                .page(page)
                .build()?,
        )
        .await
    }

    /// Fetch all rows from the database
    #[deprecated(
        since = "0.8.4",
        note = "Please use the `all` method instead of `fetch_all`"
    )]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn fetch_all(connection: &'a C) -> Result<Vec<Self>, crate::Error> {
        C::query::<Self>(
            connection,
            QueryBuilder::select().table(Self::table()).build()?,
        )
        .await
    }

    /// Fetch or create a row in the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn fetch_or_create(&mut self, connection: &'a C) -> Result<(), crate::Error>;

    /// Search for a row in the database based on specific criteria
    #[cfg(feature = "search")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn search(
        connection: &'a C,
        search: impl Into<String>,
    ) -> Result<Vec<Self>, crate::Error>;

    /// Fetch the first row from the database (based on the primary key)
    #[allow(async_fn_in_trait, unused_variables)]
    async fn first(connection: &'a C) -> Result<Self, crate::Error>
    where
        Self: TablePrimaryKey,
    {
        C::query_first::<Self>(
            connection,
            Self::query_select()
                .table(Self::table())
                .order_by(
                    &Self::primary_key(),
                    crate::builder::models::QueryOrder::Asc,
                )
                .limit(1)
                .build()?,
        )
        .await
    }

    /// Fetch last row from the database (based on the primary key)
    #[allow(async_fn_in_trait, unused_variables)]
    async fn last(connection: &'a C) -> Result<Self, crate::Error>
    where
        Self: TablePrimaryKey,
    {
        C::query_first::<Self>(
            connection,
            Self::query_select()
                .table(Self::table())
                .order_by(
                    &Self::primary_key(),
                    crate::builder::models::QueryOrder::Desc,
                )
                .limit(1)
                .build()?,
        )
        .await
    }
}

/// GeekConnection is the trait that all backends must implement to be able
/// to interact with the database.
pub trait GeekConnection {
    /// Native Connection
    type Connection;

    /// Create a table in the database
    #[allow(async_fn_in_trait, unused_variables)]
    async fn create_table<T>(connection: &Self::Connection) -> Result<(), crate::Error>
    where
        T: TableBuilder
            + QueryBuilderTrait
            + Sized
            + serde::Serialize
            + serde::de::DeserializeOwned,
    {
        Err(crate::Error::NotImplemented)
    }

    /// Run a SELECT Count query on the database and return the number of rows
    #[allow(async_fn_in_trait, unused_variables)]
    async fn row_count(connection: &Self::Connection, query: Query) -> Result<i64, crate::Error> {
        Err(crate::Error::NotImplemented)
    }

    /// Execute a query on the database and do not return any rows
    #[allow(async_fn_in_trait, unused_variables)]
    async fn execute(connection: &Self::Connection, query: Query) -> Result<(), crate::Error> {
        Err(crate::Error::NotImplemented)
    }

    /// Execute a batch query on the database and do not return any rows
    #[allow(async_fn_in_trait, unused_variables)]
    async fn batch(connection: &Self::Connection, query: Query) -> Result<(), crate::Error> {
        Err(crate::Error::NotImplemented)
    }

    /// Query the database with an active Connection and Query
    #[allow(async_fn_in_trait, unused_variables)]
    async fn query<T>(connection: &Self::Connection, query: Query) -> Result<Vec<T>, crate::Error>
    where
        T: serde::de::DeserializeOwned,
    {
        Err(crate::Error::NotImplemented)
    }

    /// Query the database with an active Connection and Query and return the first row.
    ///
    /// Note: Make sure the query is limited to 1 row to avoid retrieving multiple rows
    /// and only using the first one.
    #[allow(async_fn_in_trait, unused_variables)]
    async fn query_first<T>(connection: &Self::Connection, query: Query) -> Result<T, crate::Error>
    where
        T: serde::de::DeserializeOwned,
    {
        Err(crate::Error::NotImplemented)
    }

    /// Query the database with an active Connection and Query and return a list of GeekORM Values.
    #[allow(async_fn_in_trait, unused_variables)]
    async fn query_raw(
        connection: &Self::Connection,
        query: Query,
    ) -> Result<Vec<HashMap<String, Value>>, crate::Error> {
        Err(crate::Error::NotImplemented)
    }

    /// Get Table Names
    #[cfg(feature = "migrations")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn table_names(connection: &Self::Connection) -> Result<Vec<String>, crate::Error> {
        // TODO: This only works for SQLite
        let results: Vec<TableNames> = Self::query(
            connection,
            Query {
                query: format!("SELECT name FROM sqlite_master WHERE type='table'"),
                query_type: crate::builder::models::QueryType::Select,
                ..Default::default()
            },
        )
        .await?;

        // Make sure to not include `sqlite_sequence` table
        Ok(results
            .iter()
            .filter_map(|table| {
                if table.name != "sqlite_sequence" {
                    Some(table.name.clone())
                } else {
                    None
                }
            })
            .collect())
    }

    /// Pragma table info
    #[cfg(feature = "migrations")]
    #[allow(async_fn_in_trait, unused_variables)]
    async fn pragma_info(
        connection: &Self::Connection,
        table: &str,
    ) -> Result<Vec<TableInfo>, crate::Error> {
        Self::query(
            connection,
            Query {
                query: format!("PRAGMA table_info({})", table),
                query_type: crate::builder::models::QueryType::Select,
                ..Default::default()
            },
        )
        .await
    }
}

/// Table Info
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct TableInfo {
    /// The column ID
    pub cid: i32,
    /// The column name
    pub name: String,
    /// The column type
    #[serde(rename = "type")]
    pub coltype: String,
    /// The column notnull value
    pub notnull: i32,
    /// The column default value
    pub dflt_value: Option<String>,
    /// The column primary key value
    pub pk: i32,
}

/// Table Names
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
struct TableNames {
    /// The table name
    pub name: String,
}