pg-mvc-service 1.0.23 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ExpressionClient {
|
|
4
|
+
constructor(model) {
|
|
5
|
+
this.model = model;
|
|
6
|
+
}
|
|
7
|
+
toSqlStringValue(value) {
|
|
8
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
9
|
+
return `"${value}"`;
|
|
10
|
+
}
|
|
11
|
+
throw new Error('Please enter a value of type string or number.');
|
|
12
|
+
}
|
|
13
|
+
toSqlNumberValue(value) {
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
return value.toString();
|
|
16
|
+
}
|
|
17
|
+
else if (typeof value === 'string') {
|
|
18
|
+
if (value.trim() !== "" || isNaN(Number(value)) === false) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
23
|
+
}
|
|
24
|
+
createCaseFromObject(column, obj, elseValue) {
|
|
25
|
+
const columnInfo = typeof column === 'string' ? this.model.getColumn(column) : column.model.getColumn(column.name);
|
|
26
|
+
if (columnInfo.type !== 'string' && columnInfo.type !== 'integer') {
|
|
27
|
+
throw new Error('This method cannot be used for columns other than integer, real, or string.');
|
|
28
|
+
}
|
|
29
|
+
const whenExpression = Object.entries(obj).map(([key, value]) => {
|
|
30
|
+
let expression = `WHEN ${columnInfo.expression} = `;
|
|
31
|
+
// 今は文字列と数値だけだが、他のも対応しないといけないかも
|
|
32
|
+
if (columnInfo.type === 'string') {
|
|
33
|
+
expression += this.toSqlStringValue(key);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
expression += this.toSqlNumberValue(key);
|
|
37
|
+
}
|
|
38
|
+
expression += ` THEN `;
|
|
39
|
+
if (value === null) {
|
|
40
|
+
expression += 'null';
|
|
41
|
+
}
|
|
42
|
+
else if (typeof value === 'string') {
|
|
43
|
+
expression += `"${value}"`;
|
|
44
|
+
}
|
|
45
|
+
else if (typeof value === 'boolean') {
|
|
46
|
+
expression += `${value}`;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
expression += `${value}`;
|
|
50
|
+
}
|
|
51
|
+
return expression;
|
|
52
|
+
});
|
|
53
|
+
return `CASE ${whenExpression.join(' ')} END`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.default = ExpressionClient;
|
|
@@ -18,6 +18,7 @@ const SelectExpression_1 = __importDefault(require("./SqlUtils/SelectExpression"
|
|
|
18
18
|
const WhereExpression_1 = __importDefault(require("./SqlUtils/WhereExpression"));
|
|
19
19
|
const ValidateClient_1 = __importDefault(require("./ValidateClient"));
|
|
20
20
|
const Exception_1 = require("../exceptions/Exception");
|
|
21
|
+
const ExpressionClient_1 = __importDefault(require("./ExpressionClient"));
|
|
21
22
|
class TableModel {
|
|
22
23
|
get DbName() { return this.dbName; }
|
|
23
24
|
get TableName() {
|
|
@@ -677,5 +678,11 @@ class TableModel {
|
|
|
677
678
|
}
|
|
678
679
|
return this.validateClient;
|
|
679
680
|
}
|
|
681
|
+
get ExpressionClient() {
|
|
682
|
+
if (this.expressionClient === undefined) {
|
|
683
|
+
this.expressionClient = new ExpressionClient_1.default(this);
|
|
684
|
+
}
|
|
685
|
+
return this.expressionClient;
|
|
686
|
+
}
|
|
680
687
|
}
|
|
681
688
|
exports.TableModel = TableModel;
|
package/package.json
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TableModel } from "./TableModel";
|
|
2
|
+
import { TColumnInfo } from "./Type";
|
|
3
|
+
|
|
4
|
+
export default class ExpressionClient {
|
|
5
|
+
private model: TableModel;
|
|
6
|
+
constructor(model: TableModel) {
|
|
7
|
+
this.model = model;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public toSqlStringValue(value: any): string {
|
|
11
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
12
|
+
return `"${value}"`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw new Error('Please enter a value of type string or number.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public toSqlNumberValue(value: any): string {
|
|
19
|
+
if (typeof value === 'number') {
|
|
20
|
+
return value.toString();
|
|
21
|
+
} else if (typeof value === 'string') {
|
|
22
|
+
if (value.trim() !== "" || isNaN(Number(value)) === false) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public createCaseFromObject(column: string | TColumnInfo, obj: {[key: string | number]: string | number | boolean | null}, elseValue: string | number | boolean | null): string {
|
|
31
|
+
const columnInfo = typeof column === 'string' ? this.model.getColumn(column) : column.model.getColumn(column.name);
|
|
32
|
+
if (columnInfo.type !== 'string' && columnInfo.type !== 'integer') {
|
|
33
|
+
throw new Error('This method cannot be used for columns other than integer, real, or string.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const whenExpression = Object.entries(obj).map(([key, value]) => {
|
|
37
|
+
let expression = `WHEN ${columnInfo.expression} = `;
|
|
38
|
+
// 今は文字列と数値だけだが、他のも対応しないといけないかも
|
|
39
|
+
if (columnInfo.type === 'string') {
|
|
40
|
+
expression += this.toSqlStringValue(key);
|
|
41
|
+
} else {
|
|
42
|
+
expression += this.toSqlNumberValue(key);
|
|
43
|
+
}
|
|
44
|
+
expression += ` THEN `;
|
|
45
|
+
|
|
46
|
+
if (value === null) {
|
|
47
|
+
expression += 'null';
|
|
48
|
+
} else if (typeof value === 'string') {
|
|
49
|
+
expression += `"${value}"`;
|
|
50
|
+
} else if (typeof value === 'boolean') {
|
|
51
|
+
expression += `${value}`;
|
|
52
|
+
} else {
|
|
53
|
+
expression += `${value}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return expression;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return `CASE ${whenExpression.join(' ')} END`;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/models/TableModel.ts
CHANGED
|
@@ -5,6 +5,7 @@ import SelectExpression from './SqlUtils/SelectExpression';
|
|
|
5
5
|
import WhereExpression from './SqlUtils/WhereExpression';
|
|
6
6
|
import ValidateClient from './ValidateClient';
|
|
7
7
|
import { DbConflictException, UnprocessableException } from '../exceptions/Exception';
|
|
8
|
+
import ExpressionClient from './ExpressionClient';
|
|
8
9
|
|
|
9
10
|
export class TableModel {
|
|
10
11
|
|
|
@@ -760,4 +761,13 @@ export class TableModel {
|
|
|
760
761
|
|
|
761
762
|
return this.validateClient;
|
|
762
763
|
}
|
|
764
|
+
|
|
765
|
+
private expressionClient?: ExpressionClient;
|
|
766
|
+
get ExpressionClient(): ExpressionClient {
|
|
767
|
+
if (this.expressionClient === undefined) {
|
|
768
|
+
this.expressionClient = new ExpressionClient(this);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return this.expressionClient;
|
|
772
|
+
}
|
|
763
773
|
}
|