-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On Android, allow PRAGMA statements that assign when invoked via exec
#99
Merged
craftzdog
merged 2 commits into
craftzdog:master
from
ilmoraunio:allow-all-pragma-statements
Aug 5, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -10,12 +10,23 @@ import { | |
|
||
import SQLite from 'react-native-sqlite-2' | ||
|
||
function databaseName(name) { | ||
return name + '.' + Math.floor(Math.random() * 100000) | ||
} | ||
|
||
const database_name = 'test.db' | ||
const database_version = '1.0' | ||
const database_displayname = 'SQLite Test Database' | ||
const database_size = 200000 | ||
let db | ||
|
||
function exposedPromise() { | ||
let resolveLoading | ||
const p = new Promise((resolve) => (resolveLoading = resolve)) | ||
p.resolve = resolveLoading | ||
return p | ||
} | ||
|
||
export default class ReactNativeSQLite2Test extends Component { | ||
constructor(props) { | ||
super(props) | ||
|
@@ -67,7 +78,65 @@ export default class ReactNativeSQLite2Test extends Component { | |
this.addLog('Database DELETED') | ||
} | ||
|
||
populateDatabase(db) { | ||
assigningPragma(db) { | ||
new Promise((resolve) => { | ||
let sql = "PRAGMA journal_mode = WAL" | ||
db._db.exec([{sql: sql, args: []}], false, (_, result) => { | ||
let journal_mode = result[0].rows[0].journal_mode | ||
if (journal_mode == "wal") { | ||
this.addLog("✅ " + sql) | ||
} else { | ||
this.addLog("❌ " + sql) | ||
console.log(result, journal_mode) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
queryingPragma(db, isWal) { | ||
new Promise((resolve) => { | ||
let sql = "PRAGMA journal_mode" | ||
db._db.exec([{sql: sql, args: []}], false, (_, result) => { | ||
journal_mode = result[0].rows[0].journal_mode | ||
// Default journal_modes differ on Android & iOS | ||
if (!isWal && journal_mode != "wal" || | ||
isWal && journal_mode == "wal") { | ||
this.addLog("✅ " + sql) | ||
} else { | ||
this.addLog("❌ " + sql) | ||
console.log(result, journal_mode) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
buildPragmaSchema(db) { | ||
new Promise((resolve) => { | ||
db._db.exec([{sql: "CREATE TABLE Version(version_id INTEGER PRIMARY KEY NOT NULL);", args: []}], false, (_, result) => { | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
assigningParenthesisPragma(db) { | ||
new Promise((resolve) => { | ||
let sql = "PRAGMA main.wal_checkpoint(FULL)" | ||
db._db.exec([{sql: sql, args: []}], false, (_, result) => { | ||
row = result[0].rows[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
if (row.busy == 0 && row.checkpointed != -1 && row.log != -1) { | ||
this.addLog("✅ " + sql) | ||
} else { | ||
this.addLog("❌ " + sql) | ||
console.log(result, row) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
populateDatabase(db, p) { | ||
this.addLog('Database integrity check') | ||
const prepareDB = () => { | ||
db.transaction(this.populateDB, this.errorCB, () => { | ||
|
@@ -77,6 +146,7 @@ export default class ReactNativeSQLite2Test extends Component { | |
this.addLog('Processing completed.') | ||
db.transaction(this.cleanupTables, this.errorCB, () => { | ||
this.closeDatabase() | ||
p.resolve() | ||
}) | ||
}) | ||
}) | ||
|
@@ -243,7 +313,7 @@ export default class ReactNativeSQLite2Test extends Component { | |
tx.executeSql('DROP TABLE IF EXISTS Departments;') | ||
} | ||
|
||
loadAndQueryDB() { | ||
async loadAndQueryDB() { | ||
this.addLog('Opening database ...') | ||
db = SQLite.openDatabase( | ||
database_name, | ||
|
@@ -253,7 +323,26 @@ export default class ReactNativeSQLite2Test extends Component { | |
this.openCB, | ||
this.errorCB | ||
) | ||
this.populateDatabase(db) | ||
const p = exposedPromise() | ||
this.populateDatabase(db, p) | ||
await p | ||
} | ||
|
||
async pragmaTests() { | ||
this.addLog('Open separate DB and run PRAGMA tests') | ||
db = SQLite.openDatabase( | ||
databaseName(database_name), | ||
database_version, | ||
database_displayname, | ||
database_size, | ||
this.openCB, | ||
this.errorCB | ||
) | ||
await this.queryingPragma(db, false) | ||
await this.assigningPragma(db) | ||
await this.queryingPragma(db, true) | ||
await this.buildPragmaSchema(db) | ||
await this.assigningParenthesisPragma(db) | ||
} | ||
|
||
closeDatabase = () => { | ||
|
@@ -264,11 +353,12 @@ export default class ReactNativeSQLite2Test extends Component { | |
} | ||
} | ||
|
||
runDemo() { | ||
async runDemo() { | ||
this.setState({ | ||
progress: ['Starting SQLite Callback Demo'] | ||
}) | ||
this.loadAndQueryDB() | ||
await this.loadAndQueryDB() | ||
this.pragmaTests(); | ||
} | ||
|
||
renderProgressEntry = entry => { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
const
or it causesReferenceError