forked from vlcn-io/cr-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce wa-sqlite for music-store example
- Loading branch information
Showing
13 changed files
with
1,527 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html lang="en"> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="apple-mobile-web-app-capable" content="yes" /> | ||
<meta name="mobile-web-app-capable" content="yes" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, | ||
user-scalable=no"> | ||
<title>CF Music</title> | ||
</head> | ||
|
||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="./src/main.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import SQLiteAsyncESMFactory from "wa-sqlite/dist/wa-sqlite-async.mjs"; | ||
import * as SQLite from "wa-sqlite"; | ||
import { IDBBatchAtomicVFS } from "./wa-sqlite/vfs/IDBBatchAtomicVFS.js"; | ||
|
||
async function hello() { | ||
const module = await SQLiteAsyncESMFactory(); | ||
const sqlite3 = SQLite.Factory(module); | ||
sqlite3.vfs_register( | ||
new IDBBatchAtomicVFS("idb-batch-atomic", { durability: "relaxed" }) | ||
); | ||
|
||
const db = await sqlite3.open_v2( | ||
"music-store", | ||
SQLite.SQLITE_OPEN_CREATE | | ||
SQLite.SQLITE_OPEN_READWRITE | | ||
SQLite.SQLITE_OPEN_URI, | ||
"idb-batch-atomic" | ||
); | ||
|
||
const sql = tag(sqlite3, db); | ||
|
||
console.log(await sql`SELECT 'Hello, world!'`); | ||
// await sqlite3.exec(db, `SELECT 'Hello, world!'`, (row, columns) => { | ||
// console.log(row); | ||
// }); | ||
await sqlite3.close(db); | ||
} | ||
|
||
// from wa-sqlite demo -- https://github.com/rhashimoto/wa-sqlite/blob/66bc483115d8c5bb37abd5939cd51dd71f973998/src/examples/tag.js#L21 | ||
export function tag(sqlite3: SQLiteAPI, db: number) { | ||
return async function (strings: TemplateStringsArray, ...values) { | ||
// Assemble the template string components. | ||
const interleaved: string[] = []; | ||
strings.forEach((s, i) => { | ||
interleaved.push(s, values[i]); | ||
}); | ||
const sql = interleaved.join(""); | ||
|
||
// Loop over the SQL statements. sqlite3.statements is an API | ||
// convenience function (not in the C API) that iterates over | ||
// compiled statements, automatically managing resources. | ||
const results: { columns: string[]; rows: any[] }[] = []; | ||
for await (const stmt of sqlite3.statements(db, sql)) { | ||
const rows: any[] = []; | ||
const columns = sqlite3.column_names(stmt); | ||
while ((await sqlite3.step(stmt)) === SQLite.SQLITE_ROW) { | ||
// Collect row elements. sqlite3.row is an API convenience | ||
// function (not in the C API) that extracts values for all | ||
// the columns of the row. | ||
const row = sqlite3.row(stmt); | ||
rows.push(row); | ||
} | ||
if (columns.length) { | ||
results.push({ columns, rows }); | ||
} | ||
} | ||
return results; | ||
}; | ||
} | ||
|
||
hello(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright 2022 Roy T. Hashimoto. All Rights Reserved. | ||
import * as VFS from "./sqlite-constants.js"; | ||
export * from "./sqlite-constants.js"; | ||
|
||
// Base class for a VFS. | ||
export class Base { | ||
mxPathName = 64; | ||
|
||
/** | ||
* @param {number} fileId | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xClose(fileId) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {{ size: number, value: Int8Array }} pData | ||
* @param {number} iOffset | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xRead(fileId, pData, iOffset) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {{ size: number, value: Int8Array }} pData | ||
* @param {number} iOffset | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xWrite(fileId, pData, iOffset) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {number} iSize | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xTruncate(fileId, iSize) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {*} flags | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xSync(fileId, flags) { | ||
return VFS.SQLITE_OK; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {{ set: function(number): void }} pSize64 | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xFileSize(fileId, pSize64) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {number} flags | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xLock(fileId, flags) { | ||
return VFS.SQLITE_OK; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {number} flags | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xUnlock(fileId, flags) { | ||
return VFS.SQLITE_OK; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {{ set: function(number): void }} pResOut | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xCheckReservedLock(fileId, pResOut) { | ||
pResOut.set(0); | ||
return VFS.SQLITE_OK; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @param {number} flags | ||
* @param {{ value: Int8Array }} pOut | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xFileControl(fileId, flags, pOut) { | ||
return VFS.SQLITE_NOTFOUND; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xSectorSize(fileId) { | ||
return 512; | ||
} | ||
|
||
/** | ||
* @param {number} fileId | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xDeviceCharacteristics(fileId) { | ||
return 0; | ||
} | ||
|
||
/** | ||
* @param {string?} name | ||
* @param {number} fileId | ||
* @param {number} flags | ||
* @param {{ set: function(number): void }} pOutFlags | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xOpen(name, fileId, flags, pOutFlags) { | ||
return VFS.SQLITE_CANTOPEN; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} name | ||
* @param {number} syncDir | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xDelete(name, syncDir) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
* @param {number} flags | ||
* @param {{ set: function(number): void }} pResOut | ||
* @returns {number|Promise<number>} | ||
*/ | ||
xAccess(name, flags, pResOut) { | ||
return VFS.SQLITE_IOERR; | ||
} | ||
|
||
/** | ||
* Handle asynchronous operation. This implementation will be overriden on | ||
* registration by an Asyncify build. | ||
* @param {function(): Promise<number>} f | ||
* @returns {Promise<number>} | ||
*/ | ||
handleAsync(f) { | ||
return f(); | ||
} | ||
} | ||
|
||
export const FILE_TYPE_MASK = [ | ||
VFS.SQLITE_OPEN_MAIN_DB, | ||
VFS.SQLITE_OPEN_MAIN_JOURNAL, | ||
VFS.SQLITE_OPEN_TEMP_DB, | ||
VFS.SQLITE_OPEN_TEMP_JOURNAL, | ||
VFS.SQLITE_OPEN_TRANSIENT_DB, | ||
VFS.SQLITE_OPEN_SUBJOURNAL, | ||
VFS.SQLITE_OPEN_SUPER_JOURNAL, | ||
].reduce((mask, element) => mask | element); |
Oops, something went wrong.