pg-mvc-service 2.0.48 → 2.0.49

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.
@@ -78,14 +78,18 @@ class SelectExpression {
78
78
  * 指定された形式に変換されたSQLの文字列。
79
79
  */
80
80
  static createDateTime(column, to) {
81
- const columnQuery = typeof column === 'string' ? column : column.model.getColumn(column.name).expression;
81
+ const columnInfo = column.model.getColumn(column.name);
82
+ // switch (column.type) {
83
+ if (['date', 'datetime', 'timestamp'].includes(columnInfo.type) !== false) {
84
+ return '';
85
+ }
82
86
  switch (to) {
83
87
  case 'date':
84
- return `to_char(${columnQuery}, 'YYYY-MM-DD')`;
88
+ return `to_char(${columnInfo.expression}, 'YYYY-MM-DD')`;
85
89
  case 'datetime':
86
- return `to_char(${columnQuery}, 'YYYY-MM-DD HH24:mi:ss')`;
90
+ return `to_char(${columnInfo.expression}, 'YYYY-MM-DD HH24:mi:ss')`;
87
91
  case 'time':
88
- return `to_char(${columnQuery}, 'HH24:mi:ss')`;
92
+ return `to_char(${columnInfo.expression}, 'HH24:mi:ss')`;
89
93
  }
90
94
  }
91
95
  }
@@ -217,6 +217,21 @@ class TableModel {
217
217
  const column = columnInfo.model.getColumn(columnInfo.name);
218
218
  this.selectExpressions.push(`COALESCE(${column.expression}, $${this.vars.length}) as "${alias}"`);
219
219
  }
220
+ /**
221
+ * 指定されたカラムを特定のフォーマットの日付情報に変換し、SELECT句で使用します。
222
+ *
223
+ * @param column カラム情報。文字列または{name: string, model: TableModel}のオブジェクト。
224
+ * @param to 変換先のフォーマットを指定します。'date'、'time'、'datetime'のいずれか。
225
+ * @param alias 結果セットで使用するエイリアス名。
226
+ */
227
+ selectDateAsFormat(column, to, alias) {
228
+ column = typeof column === 'string' ? { name: column, model: this } : column;
229
+ const columnInfo = column.model.getColumn(column.name);
230
+ if (['date', 'datetime', 'timestamp'].includes(columnInfo.type) !== false) {
231
+ throw new Error('The first argument of the selectDateAsFormat method must specify a column of type date, datetime, or timestamp.');
232
+ }
233
+ this.selectExpressions.push(`${SelectExpression_1.default.createDateTime(column, to)} as "${alias}"`);
234
+ }
220
235
  /**
221
236
  * 指定された条件に基づいてテーブルを結合します。
222
237
  * @param joinType 結合の種類を指定します
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "2.0.48",
3
+ "version": "2.0.49",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -83,15 +83,21 @@ export default class SelectExpression {
83
83
  * @returns The SQL string converted to the specified format.
84
84
  * 指定された形式に変換されたSQLの文字列。
85
85
  */
86
- static createDateTime(column: string | TColumnInfo, to: 'date' | 'time' | 'datetime') {
87
- const columnQuery = typeof column === 'string' ? column : column.model.getColumn(column.name).expression;
86
+ public static createDateTime(column: TColumnInfo, to: 'date' | 'time' | 'datetime') {
87
+ const columnInfo = column.model.getColumn(column.name);
88
+
89
+ // switch (column.type) {
90
+ if (['date', 'datetime', 'timestamp'].includes(columnInfo.type) !== false) {
91
+ return '';
92
+ }
93
+
88
94
  switch (to) {
89
95
  case 'date':
90
- return `to_char(${columnQuery}, 'YYYY-MM-DD')`;
96
+ return `to_char(${columnInfo.expression}, 'YYYY-MM-DD')`;
91
97
  case 'datetime':
92
- return `to_char(${columnQuery}, 'YYYY-MM-DD HH24:mi:ss')`;
98
+ return `to_char(${columnInfo.expression}, 'YYYY-MM-DD HH24:mi:ss')`;
93
99
  case 'time':
94
- return `to_char(${columnQuery}, 'HH24:mi:ss')`;
100
+ return `to_char(${columnInfo.expression}, 'HH24:mi:ss')`;
95
101
  }
96
102
  }
97
103
  }
@@ -257,6 +257,24 @@ export class TableModel {
257
257
  this.selectExpressions.push(`COALESCE(${column.expression}, $${this.vars.length}) as "${alias}"`)
258
258
  }
259
259
 
260
+
261
+ /**
262
+ * 指定されたカラムを特定のフォーマットの日付情報に変換し、SELECT句で使用します。
263
+ *
264
+ * @param column カラム情報。文字列または{name: string, model: TableModel}のオブジェクト。
265
+ * @param to 変換先のフォーマットを指定します。'date'、'time'、'datetime'のいずれか。
266
+ * @param alias 結果セットで使用するエイリアス名。
267
+ */
268
+ public selectDateAsFormat(column: string | {name: string, model: TableModel}, to: 'date' | 'time' | 'datetime', alias: string) {
269
+ column = typeof column === 'string' ? {name: column, model: this} : column;
270
+ const columnInfo = column.model.getColumn(column.name);
271
+
272
+ if (['date', 'datetime', 'timestamp'].includes(columnInfo.type) !== false) {
273
+ throw new Error('The first argument of the selectDateAsFormat method must specify a column of type date, datetime, or timestamp.');
274
+ }
275
+ this.selectExpressions.push(`${SelectExpression.createDateTime(column, to)} as "${alias}"`);
276
+ }
277
+
260
278
  /**
261
279
  * 指定された条件に基づいてテーブルを結合します。
262
280
  * @param joinType 結合の種類を指定します