mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 09:07:47 +01:00
Support regex via sqlite
This commit is contained in:
parent
733bd338e7
commit
9e86f463d8
1 changed files with 9 additions and 1 deletions
|
@ -15,7 +15,7 @@ export class Sqlite implements IDatabase {
|
||||||
// eslint-disable-next-line require-await
|
// eslint-disable-next-line require-await
|
||||||
async prepare(type: QueryType, query: string, params: any[] = []): Promise<any[]> {
|
async prepare(type: QueryType, query: string, params: any[] = []): Promise<any[]> {
|
||||||
// Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`);
|
// Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`);
|
||||||
const preparedQuery = this.db.prepare(query);
|
const preparedQuery = this.db.prepare(Sqlite.processQuery(query));
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "get": {
|
case "get": {
|
||||||
|
@ -53,6 +53,10 @@ export class Sqlite implements IDatabase {
|
||||||
Sqlite.upgradeDB(this.db, this.config.fileNamePrefix, this.config.dbSchemaFolder);
|
Sqlite.upgradeDB(this.db, this.config.fileNamePrefix, this.config.dbSchemaFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.db.function("regexp", { deterministic: true }, (regex: string, str: string) => {
|
||||||
|
return str.match(regex) ? 1 : 0;
|
||||||
|
});
|
||||||
|
|
||||||
// Enable WAL mode checkpoint number
|
// Enable WAL mode checkpoint number
|
||||||
if (this.config.enableWalCheckpointNumber) {
|
if (this.config.enableWalCheckpointNumber) {
|
||||||
this.db.exec("PRAGMA journal_mode=WAL;");
|
this.db.exec("PRAGMA journal_mode=WAL;");
|
||||||
|
@ -67,6 +71,10 @@ export class Sqlite implements IDatabase {
|
||||||
this.db.prepare(`ATTACH ? as ${attachAs}`).run(database);
|
this.db.prepare(`ATTACH ? as ${attachAs}`).run(database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static processQuery(query: string): string {
|
||||||
|
return query.replace(/ ~\* /g, " REGEXP ");
|
||||||
|
}
|
||||||
|
|
||||||
private static upgradeDB(db: Database, fileNamePrefix: string, schemaFolder: string) {
|
private static upgradeDB(db: Database, fileNamePrefix: string, schemaFolder: string) {
|
||||||
const versionCodeInfo = db.prepare("SELECT value FROM config WHERE key = ?").get("version");
|
const versionCodeInfo = db.prepare("SELECT value FROM config WHERE key = ?").get("version");
|
||||||
let versionCode = versionCodeInfo ? versionCodeInfo.value : 0;
|
let versionCode = versionCodeInfo ? versionCodeInfo.value : 0;
|
||||||
|
|
Loading…
Reference in a new issue