Skip to content

Commit

Permalink
Remove deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Jan 10, 2024
1 parent aa1f0c5 commit 0380900
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 430 deletions.
106 changes: 53 additions & 53 deletions src/Condition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dayjs, { Dayjs, isDayjs } from 'dayjs';
import dayjs, { Dayjs, isDayjs } from "dayjs";

export interface Condition {
toSQL(): string;
Expand All @@ -8,21 +8,21 @@ export interface Condition {
export namespace Condition {
export function fromJSON(json: any): Condition {
switch (json.type) {
case 'BinaryCondition':
case "BinaryCondition":
return BinaryCondition.fromJSON(json);
case 'LogicalCondition':
case "LogicalCondition":
return LogicalCondition.fromJSON(json);
case 'BetweenCondition':
case "BetweenCondition":
return BetweenCondition.fromJSON(json);
case 'InCondition':
case "InCondition":
return InCondition.fromJSON(json);
case 'NotInCondition':
case "NotInCondition":
return NotInCondition.fromJSON(json);
case 'NullCondition':
case "NullCondition":
return NullCondition.fromJSON(json);
case 'LikeCondition':
case "LikeCondition":
return LikeCondition.fromJSON(json);
case 'ColumnComparisonCondition':
case "ColumnComparisonCondition":
return ColumnComparisonCondition.fromJSON(json);
default:
throw new Error(`Unknown condition type: ${json.type}`);
Expand All @@ -32,24 +32,24 @@ export namespace Condition {

type ConditionKey = string;
export type ConditionValue = string | number | boolean | null | Dayjs;
type Operator = '=' | '!=' | '>' | '<' | '>=' | '<=';
type Operator = "=" | "!=" | ">" | "<" | ">=" | "<=";

const escapeKey = (key: ConditionKey): string => {
return escapeColumn(key);
};
const escapeValue = (value: ConditionValue): string => {
if (isDayjs(value)) {
return `"${value.format('YYYY-MM-DD HH:mm:ss')}"`;
return `"${value.format("YYYY-MM-DD HH:mm:ss")}"`;
}
if (typeof value === 'string') {
if (typeof value === "string") {
return `"${value}"`;
}
return `${value}`;
};
export const escapeColumn = (name: string): string => {
const columnMatch = name.match(/^[\.a-zA-Z0-9_]+$/);
if (columnMatch) {
return `\`${name.replace(/`/g, '``').split('.').join('`.`')}\``;
return `\`${name.replace(/`/g, "``").split(".").join("`.`")}\``;
}
return name;
};
Expand All @@ -58,27 +58,27 @@ const serializeValue = (value: ConditionValue): string => {
if (isDayjs(value)) {
return value.toISOString();
}
if (typeof value === 'string') {
if (typeof value === "string") {
return value;
}
return `${value}`;
};

const deserializeValue = (value: string): ConditionValue => {
if (value === 'null') {
if (value === "null") {
return null;
}
if (value === 'true') {
if (value === "true") {
return true;
}
if (value === 'false') {
if (value === "false") {
return false;
}
const numberValue = Number(value);
if (!isNaN(numberValue)) {
return numberValue;
}
const dateValue = dayjs(value, 'YYYY-MM-DDTHH:mm:ss.SSSZ', true);
const dateValue = dayjs(value, "YYYY-MM-DDTHH:mm:ss.SSSZ", true);
if (dateValue.isValid()) {
return dateValue;
}
Expand All @@ -103,7 +103,7 @@ class BinaryCondition implements Condition {
// serialization
toJSON(): any {
return {
type: 'BinaryCondition',
type: "BinaryCondition",
key: this.key,
value: serializeValue(this.value),
operator: this.operator,
Expand All @@ -121,9 +121,9 @@ class BinaryCondition implements Condition {

class LogicalCondition implements Condition {
conditions: Condition[];
operator: 'AND' | 'OR';
operator: "AND" | "OR";

constructor(conditions: Condition[], operator: 'AND' | 'OR') {
constructor(conditions: Condition[], operator: "AND" | "OR") {
this.conditions = conditions;
this.operator = operator;
}
Expand All @@ -137,7 +137,7 @@ class LogicalCondition implements Condition {
// serialization
toJSON(): any {
return {
type: 'LogicalCondition',
type: "LogicalCondition",
conditions: this.conditions.map((condition) => condition.toJSON()),
operator: this.operator,
};
Expand Down Expand Up @@ -169,7 +169,7 @@ class BetweenCondition implements Condition {
// serialization
toJSON(): any {
return {
type: 'BetweenCondition',
type: "BetweenCondition",
key: this.key,
from: serializeValue(this.from),
to: serializeValue(this.to),
Expand Down Expand Up @@ -197,13 +197,13 @@ class InCondition implements Condition {
toSQL(): string {
return `${escapeKey(this.key)} IN (${this.values
.map(escapeValue)
.join(', ')})`;
.join(", ")})`;
}

// serialization
toJSON(): any {
return {
type: 'InCondition',
type: "InCondition",
key: this.key,
values: this.values.map(serializeValue),
};
Expand All @@ -226,13 +226,13 @@ class NotInCondition implements Condition {
toSQL(): string {
return `${escapeKey(this.key)} NOT IN (${this.values
.map(escapeValue)
.join(', ')})`;
.join(", ")})`;
}

// serialization
toJSON(): any {
return {
type: 'NotInCondition',
type: "NotInCondition",
key: this.key,
values: this.values.map(serializeValue),
};
Expand All @@ -253,13 +253,13 @@ class NullCondition implements Condition {
}

toSQL(): string {
return `${escapeKey(this.key)} IS ${this.isNull ? '' : 'NOT '}NULL`;
return `${escapeKey(this.key)} IS ${this.isNull ? "" : "NOT "}NULL`;
}

// serialization
toJSON(): any {
return {
type: 'NullCondition',
type: "NullCondition",
key: this.key,
isNull: this.isNull,
};
Expand All @@ -282,15 +282,15 @@ class LikeCondition implements Condition {
}

toSQL(): string {
return `${escapeKey(this.key)} ${this.isLike ? '' : 'NOT '}LIKE \'${
return `${escapeKey(this.key)} ${this.isLike ? "" : "NOT "}LIKE \'${
this.pattern
}\'`;
}

// serialization
toJSON(): any {
return {
type: 'LikeCondition',
type: "LikeCondition",
key: this.key,
pattern: this.pattern,
isLike: this.isLike,
Expand Down Expand Up @@ -326,7 +326,7 @@ class ColumnComparisonCondition implements Condition {
// serialization
toJSON(): any {
return {
type: 'ColumnComparisonCondition',
type: "ColumnComparisonCondition",
leftKey: this.leftKey,
rightKey: this.rightKey,
operator: this.operator,
Expand All @@ -345,70 +345,70 @@ class ColumnComparisonCondition implements Condition {
export const Conditions = {
fromString: (column: string, value: string | number): Condition => {
const str = `${value}`;
if (str.indexOf('>=') === 0) {
if (str.indexOf(">=") === 0) {
return Conditions.greaterThanOrEqual(column, str.substring(2));
}
if (str.indexOf('<=') === 0) {
if (str.indexOf("<=") === 0) {
return Conditions.lessThanOrEqual(column, str.substring(2));
}
if (str.indexOf('>') === 0) {
if (str.indexOf(">") === 0) {
return Conditions.greaterThan(column, str.substring(1));
}
if (str.indexOf('<') === 0) {
if (str.indexOf("<") === 0) {
return Conditions.lessThan(column, str.substring(1));
}
if (str.indexOf('~') === 0) {
if (str.indexOf("~") === 0) {
return Conditions.or([
Conditions.like(column, `${str.substring(1)}%`),
Conditions.like(column, `% ${str.substring(1)}%`),
]);
}
if (str.indexOf('!~') === 0) {
if (str.indexOf("!~") === 0) {
return Conditions.and([
Conditions.notLike(column, `${str.substring(2)}%`),
Conditions.notLike(column, `% ${str.substring(2)}%`),
]);
}
if (str.indexOf('!') === 0) {
if (str.indexOf("!") === 0) {
return Conditions.notEqual(column, `${str.substring(1)}%`);
}
return Conditions.equal(column, str);
},
equal: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '='),
new BinaryCondition(key, value, "="),
notEqual: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '!='),
new BinaryCondition(key, value, "!="),
greaterThan: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '>'),
new BinaryCondition(key, value, ">"),
lessThan: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '<'),
new BinaryCondition(key, value, "<"),
greaterThanOrEqual: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '>='),
new BinaryCondition(key, value, ">="),
lessThanOrEqual: (key: string, value: ConditionValue) =>
new BinaryCondition(key, value, '<='),
new BinaryCondition(key, value, "<="),
between: (key: string, values: [ConditionValue, ConditionValue]) =>
new BetweenCondition(key, values[0], values[1]),
in: (key: string, values: ConditionValue[]) => new InCondition(key, values),
notIn: (key: string, values: ConditionValue[]) =>
new NotInCondition(key, values),
and: (conditions: Condition[]) => new LogicalCondition(conditions, 'AND'),
or: (conditions: Condition[]) => new LogicalCondition(conditions, 'OR'),
and: (conditions: Condition[]) => new LogicalCondition(conditions, "AND"),
or: (conditions: Condition[]) => new LogicalCondition(conditions, "OR"),
null: (key: string) => new NullCondition(key, true),
notNull: (key: string) => new NullCondition(key, false),
like: (key: string, pattern: string) => new LikeCondition(key, pattern, true),
notLike: (key: string, pattern: string) =>
new LikeCondition(key, pattern, false),
columnEqual: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '='),
new ColumnComparisonCondition(leftKey, rightKey, "="),
columnNotEqual: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '!='),
new ColumnComparisonCondition(leftKey, rightKey, "!="),
columnGreaterThan: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '>'),
new ColumnComparisonCondition(leftKey, rightKey, ">"),
columnLessThan: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '<'),
new ColumnComparisonCondition(leftKey, rightKey, "<"),
columnGreaterThanOrEqual: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '>='),
new ColumnComparisonCondition(leftKey, rightKey, ">="),
columnLessThanOrEqual: (leftKey: string, rightKey: string) =>
new ColumnComparisonCondition(leftKey, rightKey, '<='),
new ColumnComparisonCondition(leftKey, rightKey, "<="),
};
export { Conditions as Cond };
26 changes: 18 additions & 8 deletions src/Function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dayjs, { Dayjs } from 'dayjs';
import dayjs, { Dayjs } from "dayjs";
import { Condition } from "./Condition";

const formatDayjs = (dayjs: Dayjs) => dayjs.format('YYYY-MM-DD');
const formatDayjs = (dayjs: Dayjs) => dayjs.format("YYYY-MM-DD");

export const Function = {
sum: (column: string) => {
Expand All @@ -18,14 +19,20 @@ export const Function = {
max: (column: string) => {
return `MAX(${column})`;
},
avg: (column: string) => {
return `AVG(${column})`;
},
abs: (column: string) => {
return `ABS(${column})`;
},
dateDiff: (
interval: 'year' | 'month' | 'day',
interval: "year" | "month" | "day",
date1: string,
date2: string
) => {
if (interval === 'month') {
if (interval === "month") {
return `TIMESTAMPDIFF(MONTH,${date1}, ${date2})`;
} else if (interval === 'day') {
} else if (interval === "day") {
return `DATEDIFF(${date1}, ${date2})`;
} else {
return `YEAR(${date1}) - YEAR(${date2})`;
Expand All @@ -38,7 +45,10 @@ export const Function = {
return `'${value}'`;
},
concat: (...values: string[]) => {
return `CONCAT(${values.join(',')})`;
return `CONCAT(${values.join(",")})`;
},
if: (condition: Condition, trueValue: string, falseValue: string) => {
return `IF(${condition.toSQL()},${trueValue},${falseValue})`;
},
dateRangeSumField: ({
dateColumn,
Expand All @@ -62,12 +72,12 @@ export const Function = {
thisYearColumn: string;
lastYearColumn: string;
}) =>
'CASE ' +
"CASE " +
`WHEN ${thisYearColumn} = 0 AND ${lastYearColumn} = 0 THEN 0 ` +
`WHEN ${lastYearColumn} = 0 THEN null ` +
`WHEN ${thisYearColumn} = 0 THEN -1 ` +
`ELSE (${thisYearColumn} - ${lastYearColumn}) / ${lastYearColumn} ` +
'END',
"END",
};

export { Function as Fn };
Loading

0 comments on commit 0380900

Please sign in to comment.