Skip to content

Commit 79d463f

Browse files
simolus3msvargas
andcommitted
Fix binding integer values
Co-authored-by: msvargas <msvargas97@gmail.com>
1 parent 8a1b3f9 commit 79d463f

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri-plugin-powersync': patch
3+
'@powersync/tauri-plugin': patch
4+
---
5+
6+
Fix integer values being bound as doubles to prepared statements.

packages/tauri/src/commands.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ impl<'de> Deserialize<'de> for SqliteValue {
138138
Ok(SqliteValue::Integer(v))
139139
}
140140

141+
fn visit_u64<E>(self, v: u64) -> std::result::Result<Self::Value, E>
142+
where
143+
E: Error,
144+
{
145+
// Wrapping cast (v as i64) would silently corrupt values > i64::MAX.
146+
i64::try_from(v)
147+
.map(SqliteValue::Integer)
148+
.map_err(E::custom)
149+
}
150+
141151
fn visit_f64<E>(self, v: f64) -> std::result::Result<Self::Value, E>
142152
where
143153
E: Error,

packages/tauri/tests/integration.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,17 @@ test('can close instances', async () => {
9292
const second = openDatabase({ schema });
9393
expect(await second.getAll('SELECT * FROM users')).toHaveLength(0);
9494
});
95+
96+
test('can bind integers', async () => {
97+
const db = openDatabase({ schema });
98+
await db.init();
99+
100+
await db.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['first']);
101+
await db.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['second']);
102+
103+
const { name } = await db.get<{ name: string }>('SELECT name FROM users ORDER BY name LIMIT ?', [1]);
104+
expect(name).toStrictEqual('first');
105+
106+
const row = await db.get<{ a: string; b: string }>('SELECT typeof(?) as a, typeof(?) as b', [123, 1.23]);
107+
expect(row).toStrictEqual({ a: 'integer', b: 'real' });
108+
});

0 commit comments

Comments
 (0)