@mlagie/sql-connector 1.4.8 → 1.5.0

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,73 @@
1
+ const formatObject = require("./formatObject");
2
+ const generateCondition = require("./generateCondition");
3
+
4
+ function buildField(field) {
5
+ // 👉 cas simple : string (nom de colonne)
6
+ if (typeof field === 'string') {
7
+ return field;
8
+ }
9
+
10
+ let sql = '';
11
+
12
+ if (field.sum)
13
+ sql = `SUM(${field.sum})`;
14
+ else if (field.dateFormat) {
15
+ const [col, format] = field.dateFormat;
16
+ sql = `DATE_FORMAT(${col}, '${format}')`;
17
+ }
18
+ else if (field.col)
19
+ sql = field.col;
20
+
21
+ if (field.as)
22
+ sql += ` AS ${field.as}`;
23
+ else if (field.sum)
24
+ sql += ` AS ${field.sum}`;
25
+ return sql;
26
+ }
27
+
28
+ function buildSelect(select = []) {
29
+ if (!select || select.length === 0) {
30
+ return '*';
31
+ }
32
+
33
+ return select
34
+ .map(field => buildField(field))
35
+ .join(',\n');
36
+ }
37
+
38
+ function buildQueryParts(options) {
39
+ const parts = [];
40
+
41
+ if (options.where) {
42
+ if (typeof options.where === 'string') {
43
+ parts.push(`WHERE ${options.where}`);
44
+ } else {
45
+ parts.push(`WHERE ${generateCondition(formatObject(options.where))}`);
46
+ }
47
+ }
48
+
49
+ if (options.groupBy) {
50
+ parts.push(`GROUP BY ${options.groupBy.join(', ')}`);
51
+ }
52
+
53
+ if (options.having) {
54
+ parts.push(`HAVING ${options.having}`);
55
+ }
56
+
57
+ if (options.orderBy) {
58
+ const order = options.orderBy.map(o =>
59
+ typeof o === 'string'
60
+ ? o
61
+ : `${o.field} ${o.direction || 'ASC'}`
62
+ );
63
+ parts.push(`ORDER BY ${order.join(', ')}`);
64
+ }
65
+
66
+ if (options.limit) {
67
+ parts.push(`LIMIT ${options.limit}`);
68
+ }
69
+
70
+ return parts.join('\n\n');
71
+ }
72
+
73
+ module.exports = { buildQueryParts, buildSelect }
@@ -59,6 +59,7 @@ module.exports = function (filter, isUpdate = false, schema = null) {
59
59
  // Comportement par défaut
60
60
  const conditions = filteredKeys.map((key, index) => {
61
61
  let value = filteredValues[index];
62
+
62
63
  if (typeof value === 'string') {
63
64
  value = value.trim();
64
65
  if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {