Skip to content

Commit f58174a

Browse files
committed
Upgrade mithril-common to new sqlite crate api
1 parent f4270c0 commit f58174a

File tree

5 files changed

+67
-120
lines changed

5 files changed

+67
-120
lines changed

mithril-common/src/database/db_version.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ pub struct DatabaseVersion {
5858

5959
impl SqLiteEntity for DatabaseVersion {
6060
fn hydrate(row: Row) -> Result<Self, HydrationError> {
61-
let version = row.get::<i64, _>(0);
62-
let application_type = &row.get::<String, _>(1);
63-
let updated_at = &row.get::<String, _>(2);
61+
let version = row.read::<i64, _>(0);
62+
let application_type = row.read::<&str, _>(1);
63+
let updated_at = row.read::<&str, _>(2);
6464

6565
Ok(Self {
6666
version,
@@ -126,11 +126,11 @@ impl<'conn> DatabaseVersionProvider<'conn> {
126126
let sql = "select exists(select name from sqlite_master where type='table' and name='db_version') as table_exists";
127127
let table_exists = connection
128128
.prepare(sql)?
129-
.into_cursor()
130-
.bind(&[])?
129+
.iter()
131130
.next()
132-
.unwrap()?
133-
.get::<i64, _>(0)
131+
.unwrap()
132+
.unwrap()
133+
.read::<i64, _>(0)
134134
== 1;
135135

136136
if !table_exists {

mithril-common/src/database/version_checker.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,11 @@ mod tests {
205205
let column_count = connection
206206
.prepare(sql)
207207
.unwrap()
208-
.into_cursor()
209-
.bind(&[])
210-
.unwrap()
208+
.iter()
211209
.next()
212210
.unwrap()
213211
.unwrap()
214-
.get::<i64, _>(0);
212+
.read::<i64, _>(0);
215213

216214
column_count
217215
}

mithril-common/src/sqlite/cursor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use std::marker::PhantomData;
22

3-
use sqlite::Cursor;
3+
use sqlite::CursorWithOwnership;
44

55
use super::SqLiteEntity;
66

77
/// Database query result Iterator wrapper. This wrapper allows to call entity
88
/// hydration for each extracted result.
99
pub struct EntityCursor<'a, T> {
10-
cursor: Cursor<'a>,
10+
cursor: CursorWithOwnership<'a>,
1111
phantom: PhantomData<T>,
1212
}
1313

1414
impl<'a, T> EntityCursor<'a, T> {
1515
/// [EntityCursor] constructor.
16-
pub fn new(cursor: Cursor<'a>) -> Self {
16+
pub fn new(cursor: CursorWithOwnership<'a>) -> Self {
1717
Self {
1818
cursor,
1919
phantom: PhantomData,

mithril-common/src/sqlite/provider.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ pub trait Provider<'conn> {
2424
.get_connection()
2525
.prepare(&sql)
2626
.map_err(|e| format!("error=`{e}, SQL=`{}`", &sql.replace('\n', " ").trim()))?
27-
.into_cursor()
28-
.bind(&params)?;
27+
.into_iter()
28+
.bind(&params[..])?;
29+
2930
let iterator = EntityCursor::new(cursor);
3031

3132
Ok(iterator)
@@ -56,10 +57,10 @@ mod tests {
5657
impl SqLiteEntity for TestEntity {
5758
fn hydrate(row: sqlite::Row) -> Result<Self, HydrationError> {
5859
Ok(TestEntity {
59-
text_data: row.get::<String, _>(0),
60-
real_data: row.get::<f64, _>(1),
61-
integer_data: row.get::<i64, _>(2),
62-
maybe_null: row.get::<Option<i64>, _>(3),
60+
text_data: row.read::<&str, _>(0).to_string(),
61+
real_data: row.read::<f64, _>(1),
62+
integer_data: row.read::<i64, _>(2),
63+
maybe_null: row.read::<Option<i64>, _>(3),
6364
})
6465
}
6566

mithril-common/src/store/adapter/sqlite_adapter.rs

Lines changed: 48 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use sha2::{Digest, Sha256};
44
use sqlite::{Connection, State, Statement};
55
use tokio::sync::Mutex;
66

7-
use std::{marker::PhantomData, ops::Deref, sync::Arc, thread::sleep, time::Duration};
7+
use lazy_static::__Deref;
8+
use std::{marker::PhantomData, sync::Arc, thread::sleep, time::Duration};
89

910
use super::{AdapterError, StoreAdapter};
1011

@@ -54,7 +55,7 @@ where
5455
.next()
5556
.map_err(|e| AdapterError::QueryError(e.into()))?;
5657
let table_exists = statement
57-
.read::<i64>(0)
58+
.read::<i64, _>(0)
5859
.map_err(|e| AdapterError::ParsingDataError(e.into()))?;
5960

6061
if table_exists != 1 {
@@ -103,7 +104,7 @@ where
103104
.prepare(sql)
104105
.map_err(|e| AdapterError::InitializationError(e.into()))?;
105106
statement
106-
.bind::<&str>(1, self.get_hash_from_key(key)?.as_str())
107+
.bind((1, self.get_hash_from_key(key)?.as_str()))
107108
.map_err(|e| AdapterError::InitializationError(e.into()))?;
108109

109110
Ok(statement)
@@ -131,7 +132,7 @@ where
131132
return Ok(None);
132133
}
133134
let maybe_value: Option<V> = statement
134-
.read::<String>(0)
135+
.read::<String, _>(0)
135136
.map_err(|e| AdapterError::QueryError(e.into()))
136137
.and_then(|v| {
137138
serde_json::from_str(&v).map_err(|e| AdapterError::ParsingDataError(e.into()))
@@ -166,13 +167,13 @@ where
166167
.prepare(sql)
167168
.map_err(|e| AdapterError::InitializationError(e.into()))?;
168169
statement
169-
.bind::<&str>(1, self.get_hash_from_key(key)?.as_str())
170+
.bind((1, self.get_hash_from_key(key)?.as_str()))
170171
.map_err(|e| AdapterError::InitializationError(e.into()))?;
171172
statement
172-
.bind::<&str>(2, self.serialize_key(key)?.as_str())
173+
.bind((2, self.serialize_key(key)?.as_str()))
173174
.map_err(|e| AdapterError::InitializationError(e.into()))?;
174175
statement
175-
.bind::<&str>(3, value.as_str())
176+
.bind((3, value.as_str()))
176177
.map_err(|e| AdapterError::InitializationError(e.into()))?;
177178
let _ = statement
178179
.next()
@@ -203,7 +204,7 @@ where
203204
.map_err(|e| AdapterError::QueryError(e.into()))?;
204205

205206
statement
206-
.read::<i64>(0)
207+
.read::<i64, _>(0)
207208
.map_err(|e| {
208209
AdapterError::GeneralError(format!("There should be a result in this case ! {e:?}"))
209210
})
@@ -221,15 +222,15 @@ where
221222
.prepare(sql)
222223
.map_err(|e| AdapterError::InitializationError(e.into()))?;
223224
statement
224-
.bind::<i64>(1, how_many as i64)
225+
.bind((1, how_many as i64))
225226
.map_err(|e| AdapterError::InitializationError(e.into()))?;
226-
let cursor = statement.into_cursor();
227+
let cursor = statement.iter();
227228

228229
let results = cursor
229230
.map(|row| {
230231
let row = row.unwrap();
231-
let key: K = serde_json::from_str(&row.get::<String, _>(0)).unwrap();
232-
let value: V = serde_json::from_str(&row.get::<String, _>(1)).unwrap();
232+
let key: K = serde_json::from_str(row.read::<&str, _>(0)).unwrap();
233+
let value: V = serde_json::from_str(row.read::<&str, _>(1)).unwrap();
233234

234235
(key, value)
235236
})
@@ -278,12 +279,12 @@ where
278279
let cursor = connection
279280
.prepare(sql)
280281
.map_err(|e| AdapterError::QueryError(e.into()))?
281-
.into_cursor();
282+
.into_iter();
282283

283284
let results = cursor
284285
.map(|row| {
285286
let row = row.unwrap();
286-
let res: V = serde_json::from_str(&row.get::<String, _>(0)).unwrap();
287+
let res: V = serde_json::from_str(row.read::<&str, _>(0)).unwrap();
287288

288289
res
289290
})
@@ -304,8 +305,8 @@ impl<V> Iterator for SQLiteResultIterator<V> {
304305
#[cfg(test)]
305306
mod tests {
306307

308+
use sqlite::Value;
307309
use std::{
308-
borrow::Borrow,
309310
fs::{create_dir_all, remove_file},
310311
path::PathBuf,
311312
};
@@ -351,71 +352,48 @@ mod tests {
351352
async fn test_store_record() {
352353
let test_name = "test_store_record";
353354
let mut adapter = init_db(test_name, None);
354-
adapter
355-
.store_record(&1, "one".to_string().borrow())
356-
.await
357-
.unwrap();
355+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
358356
let filepath = get_file_path(test_name);
359357
let connection = Connection::open(&filepath).unwrap_or_else(|_| {
360358
panic!(
361359
"Expecting to be able to open SQLite file '{}'.",
362360
filepath.display()
363361
)
364362
});
365-
let mut statement = connection
363+
let mut cursor = connection
366364
.prepare(format!("select key_hash, key, value from {TABLE_NAME}"))
367365
.unwrap()
368-
.into_cursor();
369-
let row = statement
366+
.into_iter();
367+
let row = cursor
370368
.try_next()
371369
.unwrap()
372370
.expect("Expecting at least one row in the result set.");
373-
assert_eq!(
374-
1,
375-
row[1]
376-
.as_integer()
377-
.expect("expecting field 1 to be an integer")
378-
);
379-
assert_eq!(
380-
"\"one\"".to_string(),
381-
row[2]
382-
.as_string()
383-
.expect("expecting field 2 to be a string")
384-
);
385-
adapter
386-
.store_record(&1, "zwei".to_string().borrow())
387-
.await
388-
.unwrap();
371+
assert_eq!(Value::Integer(1), row[1]);
372+
assert_eq!(Value::String("\"one\"".to_string()), row[2]);
373+
374+
// We must drop the cursor else the db will be locked
375+
drop(cursor);
376+
377+
adapter.store_record(&1, &"zwei".to_string()).await.unwrap();
389378
let mut statement = connection
390379
.prepare(format!("select key_hash, key, value from {TABLE_NAME}"))
391-
.unwrap()
392-
.into_cursor();
393-
let row = statement
380+
.unwrap();
381+
let mut cursor = statement.iter();
382+
let row = cursor
394383
.try_next()
395384
.unwrap()
396385
.expect("Expecting at least one row in the result set.");
397-
assert_eq!(
398-
"\"zwei\"".to_string(),
399-
row[2]
400-
.as_string()
401-
.expect("expecting field 2 to be a string")
402-
);
386+
assert_eq!(Value::String("\"zwei\"".to_string()), row[2]);
403387
}
404388

405389
#[tokio::test]
406390
async fn test_get_record() {
407391
let test_name = "test_get_record";
408392
let mut adapter = init_db(test_name, None);
393+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
394+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
409395
adapter
410-
.store_record(&1, "one".to_string().borrow())
411-
.await
412-
.unwrap();
413-
adapter
414-
.store_record(&2, "two".to_string().borrow())
415-
.await
416-
.unwrap();
417-
adapter
418-
.store_record(&3, "three".to_string().borrow())
396+
.store_record(&3, &"three".to_string())
419397
.await
420398
.unwrap();
421399
assert_eq!(
@@ -437,16 +415,10 @@ mod tests {
437415
async fn test_get_iterator() {
438416
let test_name = "test_get_iterator";
439417
let mut adapter = init_db(test_name, None);
418+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
419+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
440420
adapter
441-
.store_record(&1, "one".to_string().borrow())
442-
.await
443-
.unwrap();
444-
adapter
445-
.store_record(&2, "two".to_string().borrow())
446-
.await
447-
.unwrap();
448-
adapter
449-
.store_record(&3, "three".to_string().borrow())
421+
.store_record(&3, &"three".to_string())
450422
.await
451423
.unwrap();
452424
let collection: Vec<(usize, String)> =
@@ -465,14 +437,8 @@ mod tests {
465437
async fn test_record_exists() {
466438
let test_name = "test_record_exists";
467439
let mut adapter = init_db(test_name, None);
468-
adapter
469-
.store_record(&1, "one".to_string().borrow())
470-
.await
471-
.unwrap();
472-
adapter
473-
.store_record(&2, "two".to_string().borrow())
474-
.await
475-
.unwrap();
440+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
441+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
476442

477443
assert!(adapter.record_exists(&1).await.unwrap());
478444
assert!(adapter.record_exists(&2).await.unwrap());
@@ -483,14 +449,8 @@ mod tests {
483449
async fn test_remove() {
484450
let test_name = "test_remove";
485451
let mut adapter = init_db(test_name, None);
486-
adapter
487-
.store_record(&1, "one".to_string().borrow())
488-
.await
489-
.unwrap();
490-
adapter
491-
.store_record(&2, "two".to_string().borrow())
492-
.await
493-
.unwrap();
452+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
453+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
494454
let record = adapter
495455
.remove(&1)
496456
.await
@@ -508,16 +468,10 @@ mod tests {
508468
async fn test_get_last_n_records() {
509469
let test_name = "test_get_last_n_records";
510470
let mut adapter = init_db(test_name, None);
471+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
472+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
511473
adapter
512-
.store_record(&1, "one".to_string().borrow())
513-
.await
514-
.unwrap();
515-
adapter
516-
.store_record(&2, "two".to_string().borrow())
517-
.await
518-
.unwrap();
519-
adapter
520-
.store_record(&3, "three".to_string().borrow())
474+
.store_record(&3, &"three".to_string())
521475
.await
522476
.unwrap();
523477
assert_eq!(
@@ -544,20 +498,14 @@ mod tests {
544498
async fn check_get_last_n_modified_records() {
545499
let test_name = "check_get_last_n_modified_records";
546500
let mut adapter = init_db(test_name, None);
501+
adapter.store_record(&1, &"one".to_string()).await.unwrap();
502+
adapter.store_record(&2, &"two".to_string()).await.unwrap();
547503
adapter
548-
.store_record(&1, "one".to_string().borrow())
549-
.await
550-
.unwrap();
551-
adapter
552-
.store_record(&2, "two".to_string().borrow())
553-
.await
554-
.unwrap();
555-
adapter
556-
.store_record(&3, "three".to_string().borrow())
504+
.store_record(&3, &"three".to_string())
557505
.await
558506
.unwrap();
559507
adapter
560-
.store_record(&1, "updated record".to_string().borrow())
508+
.store_record(&1, &"updated record".to_string())
561509
.await
562510
.unwrap();
563511
let values = adapter.get_last_n_records(2).await.unwrap();

0 commit comments

Comments
 (0)