Skip to content

Commit 503a231

Browse files
committed
HACKS
1 parent 297d7da commit 503a231

2 files changed

Lines changed: 49 additions & 30 deletions

File tree

index.d.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/* auto-generated by NAPI-RS */
55

6-
/** Database connection options. */
6+
/** SQLite connection options. */
77
export interface Options {
88
timeout?: number
99
authToken?: string
@@ -12,26 +12,27 @@ export interface Options {
1212
encryptionCipher?: string
1313
encryptionKey?: string
1414
}
15+
export interface SyncResult {
16+
framesSynced: number
17+
replicationIndex: number
18+
}
1519
export declare function databaseSyncSync(db: Database): SyncResult
1620
export declare function databasePrepareSync(db: Database, sql: string): Statement
21+
/** SQLite `run()` result object */
1722
export interface RunResult {
1823
changes: number
1924
duration: number
2025
lastInsertRowid: number
2126
}
2227
/** Retrieve next row from an iterator synchronously. Needed for better-sqlite3 API compatibility. */
2328
export declare function iteratorNextSync(iter: RowsIterator): Record
24-
export interface SyncResult {
25-
framesSynced: number
26-
replicationIndex: number
27-
}
2829
/** SQLite error object. */
2930
export declare class SqliteError {
3031
message: string
3132
code: string
3233
rawCode: number
3334
}
34-
/** Database connection. */
35+
/** SQLite database connection. */
3536
export declare class Database {
3637
constructor(path: string, opts?: Options | undefined | null)
3738
get memory(): boolean
@@ -44,9 +45,9 @@ export declare class Database {
4445
interrupt(): void
4546
close(): void
4647
defaultSafeIntegers(toggle?: boolean | undefined | null): void
47-
unsafeMode(): void
4848
sync(): Promise<SyncResult>
4949
}
50+
/** SQLite statement object. */
5051
export declare class Statement {
5152
columns(): unknown[]
5253
iterate(params?: unknown | undefined | null): object

src/lib.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,6 @@ impl Statement {
642642
rt.block_on(async move {
643643
let conn = self.conn.lock().await;
644644
let total_changes_before = conn.total_changes();
645-
// Get start time
646645
let start = std::time::Instant::now();
647646

648647
let mut stmt = self.stmt.lock().await;
@@ -697,38 +696,57 @@ impl Statement {
697696
let start = std::time::Instant::now();
698697
rt.block_on(async move {
699698
let mut stmt = self.stmt.lock().await;
700-
stmt.reset();
701699
let params = map_params(&stmt, params)?;
702700
let mut rows = stmt.query(params).await.map_err(Error::from)?;
703701
let column_names = Self::get_column_names(&rows)?;
704702
let row = rows.next().await.map_err(Error::from)?;
705703
let duration = start.elapsed().as_secs_f64();
706-
let result = match row {
707-
Some(row) => {
708-
if raw {
709-
let js_array = map_row_raw(&env, &column_names, &row, safe_ints, pluck)?;
710-
Ok(js_array.into_unknown())
711-
} else {
712-
let mut js_object =
713-
map_row_object(&env, &column_names, &row, safe_ints, pluck)?
714-
.coerce_to_object()?;
715-
let mut metadata = env.create_object()?;
716-
let js_duration = env.create_double(duration)?;
717-
metadata.set_named_property("duration", js_duration)?;
718-
js_object.set_named_property("_metadata", metadata)?;
719-
Ok(js_object.into_unknown())
720-
}
721-
}
722-
None => {
723-
let undefined = env.get_undefined()?;
724-
Ok(undefined.into_unknown())
725-
}
726-
};
704+
let result = Self::get_internal(
705+
&env,
706+
&row,
707+
&column_names,
708+
safe_ints,
709+
raw,
710+
pluck,
711+
duration,
712+
);
727713
stmt.reset();
728714
result
729715
})
730716
}
731717

718+
fn get_internal(
719+
env: &Env,
720+
row: &Option<libsql::Row>,
721+
column_names: &[std::ffi::CString],
722+
safe_ints: bool,
723+
raw: bool,
724+
pluck: bool,
725+
duration: f64,
726+
) -> Result<napi::JsUnknown> {
727+
match row {
728+
Some(row) => {
729+
if raw {
730+
let js_array = map_row_raw(&env, &column_names, &row, safe_ints, pluck)?;
731+
Ok(js_array.into_unknown())
732+
} else {
733+
let mut js_object =
734+
map_row_object(&env, &column_names, &row, safe_ints, pluck)?
735+
.coerce_to_object()?;
736+
let mut metadata = env.create_object()?;
737+
let js_duration = env.create_double(duration)?;
738+
metadata.set_named_property("duration", js_duration)?;
739+
js_object.set_named_property("_metadata", metadata)?;
740+
Ok(js_object.into_unknown())
741+
}
742+
}
743+
None => {
744+
let undefined = env.get_undefined()?;
745+
Ok(undefined.into_unknown())
746+
}
747+
}
748+
}
749+
732750
#[napi]
733751
pub fn safeIntegers(&self, toggle: Option<bool>) -> Result<&Self> {
734752
self.safe_ints

0 commit comments

Comments
 (0)