@dascompany/database 2.1.4 → 2.1.5

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.
package/dist/Table.d.ts CHANGED
@@ -4,5 +4,5 @@ import QueryOptionsI from './types/QueryOptionsI';
4
4
  export default abstract class Table {
5
5
  private connection;
6
6
  constructor(connection: Connection);
7
- protected createQuery(queryString: string, options?: QueryOptionsI): Query;
7
+ createQuery(queryString: string, options?: QueryOptionsI): Query;
8
8
  }
@@ -5,5 +5,4 @@ export default class BindParams {
5
5
  private static bindWhereParams;
6
6
  private static bindWhereParam;
7
7
  private static bindLimitParams;
8
- private static bindValuesParams;
9
8
  }
@@ -6,9 +6,6 @@ export default class BindParams {
6
6
  BindParams.bindWhereParams(query, options.where.parameters);
7
7
  if (options?.limit)
8
8
  BindParams.bindLimitParams(query, options.limit);
9
- if (options?.values) {
10
- BindParams.bindValuesParams(query, options.values);
11
- }
12
9
  }
13
10
  static bindWhereParams(query, parameters) {
14
11
  parameters.forEach((parameter) => {
@@ -37,24 +34,4 @@ export default class BindParams {
37
34
  query.bindParameter("limit", resultLimitation.limit);
38
35
  query.bindParameter("offset", resultLimitation.offset);
39
36
  }
40
- static bindValuesParams(query, values) {
41
- if (Array.isArray(values[0].value)) {
42
- const quantity = values[0].value.length;
43
- for (let i = 0; i < quantity; i++) {
44
- values.forEach((value) => {
45
- if (Array.isArray(value.value)) {
46
- query.bindParameter(`${value.name}_${i}`, value.value[i]);
47
- }
48
- else {
49
- query.bindParameter(`${value.name}_${i}`, value.value);
50
- }
51
- });
52
- }
53
- }
54
- else {
55
- values.forEach((value) => {
56
- query.bindParameter(`${value.name}_0`, value.value);
57
- });
58
- }
59
- }
60
37
  }
@@ -1,20 +1,7 @@
1
- import ParameterI from '../types/ParameterI';
2
1
  import QueryOptionsI from '../types/QueryOptionsI';
3
2
  export default class QueryParams {
4
- private static where;
5
- private static params;
6
- private static orderBy;
7
- private static limit;
8
- private static values;
9
- private static paramConnector;
10
3
  static createParams(queryString: string, options: QueryOptionsI): string;
11
- static createValuesParams(sql: string, values: ParameterI[]): string;
12
- private static createValueParams;
13
4
  private static createWhereParams;
14
- private static createOrderParams;
15
- private static createLimitParams;
16
5
  private static createParamsSql;
17
- private static removeWhereVariable;
18
- private static splitSql;
19
- private static getWhereConnector;
6
+ private static createOrderParams;
20
7
  }
@@ -1,88 +1,20 @@
1
- import QueryParamCreator from './QueryParamCreator';
2
1
  import QueryParamsCreator from './QueryParamCreator';
3
2
  export default class QueryParams {
4
- static where = '$WHERE';
5
- static params = '$PARAMS';
6
- static orderBy = '$ORDERBY';
7
- static limit = '$LIMIT';
8
- static values = '$VALUES';
9
- static paramConnector = 'AND';
10
3
  static createParams(queryString, options) {
11
- if (options.where && (queryString.includes(this.where) || queryString.includes(this.params)))
4
+ if (options.where)
12
5
  queryString = QueryParams.createWhereParams(queryString, options.where);
13
- if (options && queryString.includes(this.orderBy))
6
+ if (options.orderBy)
14
7
  queryString = QueryParams.createOrderParams(queryString, options.orderBy);
15
- if (options && queryString.includes(this.limit))
16
- queryString = QueryParams.createLimitParams(queryString, options.limit);
17
- if (options.values && queryString.includes(this.values))
18
- queryString = QueryParams.createValuesParams(queryString, options.values);
8
+ if (options.limit)
9
+ queryString += `LIMIT $limit::BIGINT OFFSET $offset::BIGINT`;
19
10
  return queryString;
20
11
  }
21
- static createValuesParams(sql, values) {
22
- const vals = [];
23
- if (Array.isArray(values[0].value)) {
24
- const quantity = values[0].value.length;
25
- for (let i = 0; i < quantity; i++) {
26
- vals.push(this.createValueParams(values, i));
27
- }
28
- }
29
- else {
30
- vals.push(this.createValueParams(values));
31
- }
32
- sql = sql.replaceAll(this.values, `VALUES ${vals.join(', ')}`);
33
- return sql;
34
- }
35
- static createValueParams(values, index) {
36
- const creatingValues = [];
37
- values.forEach((value) => {
38
- let valueall;
39
- if (index) {
40
- const paramType = QueryParamCreator.getBaseTypeForComplexType(value.type);
41
- valueall = QueryParamCreator.createParamValue(`${value.name}_${index}`, paramType);
42
- }
43
- else {
44
- valueall = QueryParamCreator.createParamValue(`${value.name}_0`, value.type);
45
- }
46
- creatingValues.push(valueall);
47
- });
48
- return `( ${creatingValues.join(', ')} )`;
49
- }
50
12
  static createWhereParams(sql, where) {
51
- let sqlWithParams = '';
52
13
  const params = this.createParamsSql(where.parameters);
53
- if (sql.includes(this.where) || sql.includes(this.params)) {
54
- if (params.length > 0) {
55
- const { leftSql, rightSql } = this.splitSql(sql);
56
- const connector = this.getWhereConnector(sql);
57
- sqlWithParams = `${leftSql} ${connector} ${params.join(` ${where.connector}`)} ${rightSql}`;
58
- }
59
- else
60
- sqlWithParams = this.removeWhereVariable(sql);
61
- }
62
- else
63
- throw new Error(`Brak zmiennej: ${this.where} lub ${this.params} w zapytaniu sql`);
14
+ const connector = 'WHERE';
15
+ const sqlWithParams = `${sql} ${connector} ${params.join(` ${where.connector} `)}`;
64
16
  return sqlWithParams;
65
17
  }
66
- static createOrderParams(sql, orderBy) {
67
- if (orderBy) {
68
- let strToReplace = `ORDER BY `;
69
- orderBy.forEach(item => {
70
- strToReplace += `${item.key} ${item.order} `;
71
- });
72
- sql = sql.replaceAll(this.orderBy, strToReplace);
73
- }
74
- else
75
- sql = sql.replaceAll(this.orderBy, '');
76
- return sql;
77
- }
78
- static createLimitParams(sql, resultLimitation) {
79
- if (resultLimitation) {
80
- sql = sql.replaceAll(this.limit, `LIMIT $limit::BIGINT OFFSET $offset::BIGINT`);
81
- }
82
- else
83
- sql = sql.replaceAll(this.limit, '');
84
- return sql;
85
- }
86
18
  static createParamsSql(parameters) {
87
19
  let sqlParams = [];
88
20
  parameters.forEach((parameter) => {
@@ -92,26 +24,12 @@ export default class QueryParams {
92
24
  });
93
25
  return sqlParams;
94
26
  }
95
- static removeWhereVariable(sql) {
96
- sql = sql.replaceAll(this.where, '');
97
- sql = sql.replaceAll(this.params, '');
27
+ static createOrderParams(sql, orderBy) {
28
+ let sqlString = `ORDER BY `;
29
+ orderBy.forEach(item => {
30
+ sqlString += `${item.key} ${item.order} `;
31
+ });
32
+ sql = `${sql} ${sqlString}`;
98
33
  return sql;
99
34
  }
100
- static splitSql(sql) {
101
- let sqlArray = [];
102
- if (sql.includes(this.where))
103
- sqlArray = sql.split(this.where);
104
- else if (sql.includes(this.params))
105
- sqlArray = sql.split(this.params);
106
- return {
107
- leftSql: sqlArray[0],
108
- rightSql: sqlArray[1]
109
- };
110
- }
111
- static getWhereConnector(sql) {
112
- if (sql.includes(this.where))
113
- return 'WHERE';
114
- else if (sql.includes(this.params))
115
- return 'AND';
116
- }
117
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "vitest",