Skip to content

Commit 89a12b1

Browse files
fix: maintain the support for v4 quick-sqlite to avoind breaking change
1 parent 3e8123c commit 89a12b1

File tree

2 files changed

+147
-4
lines changed

2 files changed

+147
-4
lines changed

package/src/store/QuickSqliteClient.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ try {
1111
}
1212

1313
import { DB_LOCATION, DB_NAME } from './constants';
14+
import { QuickSqliteClient_v4 } from './QuickSqliteClient_v4';
1415
import { tables } from './schema';
1516
import { createCreateTableQuery } from './sqlite-utils/createCreateTableQuery';
1617
import type { PreparedQueries, Table } from './types';
1718

1819
/**
1920
* QuickSqliteClient takes care of any direct interaction with sqlite.
2021
* This way usage react-native-quick-sqlite package is scoped to a single class/file.
22+
*
23+
* TODO: Drop the support for v4 of react-native-quick-sqlite in the next major release.
2124
*/
2225
export class QuickSqliteClient {
2326
static dbVersion = 3;
@@ -29,7 +32,16 @@ export class QuickSqliteClient {
2932
// Force a specific db version. This is mainly useful for testsuit.
3033
static setDbVersion = (version: number) => (this.dbVersion = version);
3134

35+
// @ts-ignore
36+
static isQuickSqliteV4 = sqlite.executeSql ? true : false;
37+
38+
// print if legacy version
3239
static openDB = () => {
40+
if (this.isQuickSqliteV4) {
41+
QuickSqliteClient_v4.openDB();
42+
return;
43+
}
44+
3345
try {
3446
sqlite.open(this.dbName, this.dbLocation);
3547
sqlite.execute(this.dbName, `PRAGMA foreign_keys = ON`, []);
@@ -39,6 +51,11 @@ export class QuickSqliteClient {
3951
};
4052

4153
static closeDB = () => {
54+
if (this.isQuickSqliteV4) {
55+
QuickSqliteClient_v4.closeDB();
56+
return;
57+
}
58+
4259
try {
4360
sqlite.close(this.dbName);
4461
} catch (e) {
@@ -48,10 +65,16 @@ export class QuickSqliteClient {
4865

4966
static executeSqlBatch = (queries: PreparedQueries[]) => {
5067
if (!queries || !queries.length) return;
51-
this.openDB();
5268

69+
if (this.isQuickSqliteV4) {
70+
QuickSqliteClient_v4.executeSqlBatch(queries);
71+
return;
72+
}
73+
74+
this.openDB();
5375
try {
5476
sqlite.executeBatch(DB_NAME, queries);
77+
5578
this.closeDB();
5679
} catch (e) {
5780
this.closeDB();
@@ -62,6 +85,10 @@ export class QuickSqliteClient {
6285
static executeSql = (query: string, params?: string[]) => {
6386
this.openDB();
6487

88+
if (this.isQuickSqliteV4) {
89+
return QuickSqliteClient_v4.executeSql(query, params);
90+
}
91+
6592
try {
6693
const { rows } = sqlite.execute(DB_NAME, query, params);
6794
this.closeDB();
@@ -83,6 +110,10 @@ export class QuickSqliteClient {
83110
};
84111

85112
static deleteDatabase = () => {
113+
if (this.isQuickSqliteV4) {
114+
return QuickSqliteClient_v4.deleteDatabase();
115+
}
116+
86117
try {
87118
sqlite.delete(this.dbName, this.dbLocation);
88119
} catch (e) {
@@ -100,6 +131,14 @@ export class QuickSqliteClient {
100131
);
101132
}
102133

134+
if (this.isQuickSqliteV4) {
135+
console.warn(
136+
'You seem to be using an older version of "react-native-quick-sqlite" dependency,',
137+
'and we are going to drop support for it in the next major release.',
138+
'Please upgrade to the version v5 of "react-native-quick-sqlite" to avoid any issues.',
139+
);
140+
}
141+
103142
const version = this.getUserPragmaVersion();
104143

105144
if (version !== this.dbVersion) {
@@ -118,16 +157,22 @@ export class QuickSqliteClient {
118157
};
119158

120159
static updateUserPragmaVersion = (version: number) => {
121-
this.openDB();
160+
if (this.isQuickSqliteV4) {
161+
QuickSqliteClient_v4.updateUserPragmaVersion(version);
162+
return;
163+
}
122164

165+
this.openDB();
123166
sqlite.execute(DB_NAME, `PRAGMA user_version = ${version}`, []);
124-
125167
this.closeDB();
126168
};
127169

128170
static getUserPragmaVersion = () => {
129-
this.openDB();
171+
if (this.isQuickSqliteV4) {
172+
return QuickSqliteClient_v4.getUserPragmaVersion();
173+
}
130174

175+
this.openDB();
131176
try {
132177
const { rows } = sqlite.execute(DB_NAME, `PRAGMA user_version`, []);
133178
const result = rows ? rows._array : [];
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// We are going to disable type checks for this file, since this QuickSqliteClient is for legacy version of sqlite but
2+
// dev-dependency "react-native-quick-sqlite" is not available for legacy version of sqlite.
3+
/* eslint-disable no-underscore-dangle */
4+
// @ts-nocheck
5+
6+
import type { QuickSQLite } from 'react-native-quick-sqlite';
7+
let sqlite: typeof QuickSQLite;
8+
9+
try {
10+
sqlite = require('react-native-quick-sqlite').QuickSQLite;
11+
} catch (e) {
12+
// Failed for one of the reason
13+
// 1. Running on expo, where we don't support offline storage yet.
14+
// 2. Offline support is disabled, in which case this library is not installed.
15+
}
16+
import { DB_LOCATION, DB_NAME, DB_STATUS_ERROR } from './constants';
17+
import type { PreparedQueries } from './types';
18+
19+
// QuickSqliteClient takes care of any direct interaction with sqlite for v4 of react-native-quick-sqlite.
20+
// This is to avoid any breaking changes for users using v4 of react-native-quick-sqlite.
21+
export class QuickSqliteClient_v4 {
22+
static dbName = DB_NAME;
23+
static dbLocation = DB_LOCATION;
24+
25+
static openDB = () => {
26+
const { message, status } = sqlite.open(this.dbName, this.dbLocation);
27+
sqlite.executeSql(this.dbName, `PRAGMA foreign_keys = ON`, []);
28+
29+
if (status === DB_STATUS_ERROR) {
30+
console.error(`Error opening database ${this.dbName}: ${message}`);
31+
}
32+
};
33+
34+
static closeDB = () => {
35+
const { message, status } = sqlite.close(this.dbName);
36+
37+
if (status === DB_STATUS_ERROR) {
38+
console.error(`Error closing database ${this.dbName}: ${message}`);
39+
}
40+
};
41+
42+
static executeSqlBatch = (queries: PreparedQueries[]) => {
43+
this.openDB();
44+
45+
const res = sqlite.executeSqlBatch(DB_NAME, queries);
46+
47+
if (res.status === 1) {
48+
console.error(`Query/queries failed: ${res.message} ${JSON.stringify(res)}`);
49+
}
50+
51+
this.closeDB();
52+
};
53+
54+
static executeSql = (query: string, params?: string[]) => {
55+
this.openDB();
56+
57+
const { message, rows, status } = sqlite.executeSql(DB_NAME, query, params);
58+
59+
this.closeDB();
60+
61+
if (status === 1) {
62+
console.error(`Query/queries failed: ${message}: `, query);
63+
}
64+
65+
return rows ? rows._array : [];
66+
};
67+
68+
static deleteDatabase = () => {
69+
const { message, status } = sqlite.delete(this.dbName, this.dbLocation);
70+
if (status === DB_STATUS_ERROR) {
71+
throw new Error(`Error deleting DB: ${message}`);
72+
}
73+
74+
return true;
75+
};
76+
77+
static updateUserPragmaVersion = (version: number) => {
78+
this.openDB();
79+
80+
sqlite.executeSql(DB_NAME, `PRAGMA user_version = ${version}`, []);
81+
82+
this.closeDB();
83+
};
84+
85+
static getUserPragmaVersion = () => {
86+
this.openDB();
87+
88+
const { message, rows, status } = sqlite.executeSql(DB_NAME, `PRAGMA user_version`, []);
89+
90+
this.closeDB();
91+
if (status === 1) {
92+
console.error(`Querying for user_version failed: ${message}`);
93+
}
94+
95+
const result = rows ? rows._array : [];
96+
return result[0].user_version as number;
97+
};
98+
}

0 commit comments

Comments
 (0)