Skip to content

Commit a597b3e

Browse files
committed
moving regexp out of loops
1 parent ec8459a commit a597b3e

4 files changed

Lines changed: 46 additions & 43 deletions

File tree

packages/inquire/src/dialect/Mysql.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export class MysqlDialect implements Dialect {
5050
//used for json notation
5151
public separator: string = '.';
5252
public splitter: string = ':';
53+
//jsonic pattern
54+
// - ex. data:info.name
55+
// - ex. profile.data:info
56+
// - ex. profile.data:info.name
57+
public jsonic = new RegExp(
58+
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
59+
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
60+
'g'
61+
);
5362

5463
/**
5564
* Converts alter builder to query and values
@@ -519,27 +528,18 @@ export class MysqlDialect implements Dialect {
519528
if (build.where.length > 0 || build.json.length > 0) {
520529
const filters: string[] = [];
521530
if (build.where.length) {
522-
//find json phrases
523-
// - ex. data:info.name
524-
// - ex. profile.data:info
525-
// - ex. profile.data:info.name
526-
const jsonSelector = new RegExp(
527-
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
528-
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
529-
'g'
530-
);
531-
filters.push(...build.where.map(filter => {
532-
values.push(...filter.values);
533-
//then replace with XJsonDialect.parse().extract
534-
return filter.clause.replace(jsonSelector, match => {
535-
const json = MysqlJsonDialect.parse(
536-
match,
537-
this.splitter,
538-
this.separator
539-
);
540-
return json ? json.extract : match;
541-
});
542-
}));
531+
filters.push(...build.where.map(filter => {
532+
values.push(...filter.values);
533+
//then replace with XJsonDialect.parse().extract
534+
return filter.clause.replace(this.jsonic, match => {
535+
const json = MysqlJsonDialect.parse(
536+
match,
537+
this.splitter,
538+
this.separator
539+
);
540+
return json ? json.extract : match;
541+
});
542+
}));
543543
}
544544
build.json.forEach(filter => {
545545
const { query, replace } = filter;

packages/inquire/src/dialect/Pgsql.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ export class PgsqlDialect implements Dialect {
5151
//used for json notation
5252
public separator: string = '.';
5353
public splitter: string = ':';
54+
//jsonic pattern
55+
// - ex. data:info.name
56+
// - ex. profile.data:info
57+
// - ex. profile.data:info.name
58+
public jsonic = new RegExp(
59+
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
60+
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
61+
'g'
62+
);
5463

5564
/**
5665
* Converts alter builder to query and values
@@ -534,19 +543,10 @@ export class PgsqlDialect implements Dialect {
534543
if (build.where.length > 0 || build.json.length > 0) {
535544
const filters: string[] = [];
536545
if (build.where.length) {
537-
//find json phrases
538-
// - ex. data:info.name
539-
// - ex. profile.data:info
540-
// - ex. profile.data:info.name
541-
const jsonSelector = new RegExp(
542-
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
543-
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
544-
'g'
545-
);
546546
filters.push(...build.where.map(filter => {
547547
values.push(...filter.values);
548548
//then replace with XJsonDialect.parse().extract
549-
return filter.clause.replace(jsonSelector, match => {
549+
return filter.clause.replace(this.jsonic, match => {
550550
const json = PgsqlJsonDialect.parse(
551551
match,
552552
this.splitter,

packages/inquire/src/dialect/Sqlite.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ export class SqliteDialect implements Dialect {
5151
//used for json notation
5252
public separator: string = '.';
5353
public splitter: string = ':';
54+
//jsonic pattern
55+
// - ex. data:info.name
56+
// - ex. profile.data:info
57+
// - ex. profile.data:info.name
58+
public jsonic = new RegExp(
59+
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
60+
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
61+
'g'
62+
);
5463

5564
/**
5665
* Converts alter builder to query and values
@@ -477,19 +486,10 @@ export class SqliteDialect implements Dialect {
477486
if (build.where.length > 0 || build.json.length > 0) {
478487
const filters: string[] = [];
479488
if (build.where.length) {
480-
//find json phrases
481-
// - ex. data:info.name
482-
// - ex. profile.data:info
483-
// - ex. profile.data:info.name
484-
const jsonSelector = new RegExp(
485-
`([a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+){0,1}\\${this.splitter}`
486-
+ `[a-zA-Z0-9_]+(\\${this.separator}[a-zA-Z0-9_]+)*)`,
487-
'g'
488-
);
489489
filters.push(...build.where.map(filter => {
490490
values.push(...filter.values);
491491
//then replace with XJsonDialect.parse().extract
492-
return filter.clause.replace(jsonSelector, match => {
492+
return filter.clause.replace(this.jsonic, match => {
493493
const json = SqliteJsonDialect.parse(
494494
match,
495495
this.splitter,

packages/inquire/src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ export interface JsonDialect {
153153
};
154154

155155
export interface Dialect {
156-
name: string,
157-
q: string,
156+
name: string;
157+
q: string;
158+
separator: string;
159+
splitter: string;
160+
jsonic: RegExp;
158161
alter(builder: Alter): QueryObject[];
159162
create(builder: Create): QueryObject[];
160163
delete(builder: Delete): QueryObject;

0 commit comments

Comments
 (0)