tspace-mysql 1.4.6 → 1.4.7

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.
@@ -38,101 +38,153 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
38
38
  this._initialConnection();
39
39
  }
40
40
  /**
41
+ * The 'distinct' method is used to apply the DISTINCT keyword to a database query.
41
42
  *
42
- * @param {string} column
43
- * @return {this}
43
+ * It allows you to retrieve unique values from one or more columns in the result set, eliminating duplicate rows.
44
+ * @return {this} this
44
45
  */
45
- pluck(column) {
46
- this.$state.set('PLUCK', column);
46
+ distinct() {
47
+ this._setState('DISTINCT', true);
47
48
  return this;
48
49
  }
49
50
  /**
51
+ * The 'select' method is used to specify which columns you want to retrieve from a database table.
50
52
  *
51
- * @param {...string} columns
53
+ * It allows you to choose the specific columns that should be included in the result set of a database query.
54
+ * @param {string[]} ...columns
52
55
  * @return {this} this
53
56
  */
54
- except(...columns) {
55
- this.$state.set('EXCEPTS', columns.length ? columns : []);
57
+ select(...columns) {
58
+ if (!columns.length)
59
+ return this;
60
+ let select = columns.map((column) => {
61
+ if (column === '*' || (column.includes('*') && /\./.test(column)))
62
+ return column;
63
+ if (/\./.test(column))
64
+ return this.bindColumn(column);
65
+ if (column.includes(this.$constants('RAW')))
66
+ return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
67
+ return `\`${column}\``;
68
+ });
69
+ select = [...this._getState('SELECT'), ...select];
70
+ if (this._getState('DISTINCT') && select.length) {
71
+ select[0] = String(select[0]).includes(this.$constants('DISTINCT'))
72
+ ? select[0]
73
+ : `${this.$constants('DISTINCT')} ${select[0]}`;
74
+ }
75
+ this._setState('SELECT', select);
56
76
  return this;
57
77
  }
58
78
  /**
59
- * data alaways will return void
79
+ * The 'selectRaw' method is used to specify which columns you want to retrieve from a database table.
80
+ *
81
+ * It allows you to choose the specific columns that should be included in the result set of a database query.
82
+ *
83
+ * This method allows you to specify raw-sql parameters for the query.
84
+ * @param {string[]} ...columns
60
85
  * @return {this} this
61
86
  */
62
- void() {
63
- this.$state.set('VOID', true);
87
+ selectRaw(...columns) {
88
+ if (!columns.length)
89
+ return this;
90
+ let select = columns.map((column) => {
91
+ if (column === '*')
92
+ return column;
93
+ if (column.includes('`*`'))
94
+ return column.replace('`*`', '*');
95
+ if (column.includes(this.$constants('RAW')))
96
+ return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
97
+ return column;
98
+ });
99
+ if (this._getState('DISTINCT') && select.length) {
100
+ this._setState('SELECT', select);
101
+ return this;
102
+ }
103
+ this._setState('SELECT', [...this._getState('SELECT'), ...select]);
64
104
  return this;
65
105
  }
66
106
  /**
107
+ * The 'selectObject' method is used to specify which columns you want to retrieve from a database table.
67
108
  *
68
- * @param {...string} columns show only colums selected
109
+ * It allows you to choose the specific columns that should be included in the result set to 'Object' of a database query.
110
+ * @param {string} object table name
111
+ * @param {string} alias as name of the column
69
112
  * @return {this} this
70
113
  */
71
- only(...columns) {
72
- this.$state.set('ONLY', columns);
114
+ selectObject(object, alias) {
115
+ if (!Object.keys(object).length)
116
+ throw new Error("The method 'selectObject' is not supported for empty object");
117
+ let maping = [];
118
+ for (const [key, value] of Object.entries(object)) {
119
+ if (/\./.test(value)) {
120
+ const [table, c] = value.split('.');
121
+ maping = [...maping, `'${key}'`, `\`${table}\`.\`${c}\``];
122
+ continue;
123
+ }
124
+ maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
125
+ }
126
+ const json = `${this.$constants('JSON_OBJECT')}(${maping.join(' , ')}) ${this.$constants('AS')} \`${alias}\``;
127
+ this._setState('SELECT', [...this._getState('SELECT'), json]);
73
128
  return this;
74
129
  }
75
130
  /**
131
+ * The 'pluck' method is used to retrieve the value of a single column from the first result of a query.
76
132
  *
133
+ * It is often used when you need to retrieve a single value,
134
+ * such as an ID or a specific attribute, from a query result.
135
+ * @param {string} column
136
+ * @return {this}
137
+ */
138
+ pluck(column) {
139
+ this._setState('PLUCK', column);
140
+ return this;
141
+ }
142
+ /**
143
+ * The 'except' method is used to specify which columns you don't want to retrieve from a database table.
144
+ *
145
+ * It allows you to choose the specific columns that should be not included in the result set of a database query.
146
+ * @param {...string} columns
77
147
  * @return {this} this
78
148
  */
79
- distinct() {
80
- this.$state.set('DISTINCT', true);
81
- const select = this.$state.get('SELECT');
82
- if (select.includes('*'))
83
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('DISTINCT')} *`);
149
+ except(...columns) {
150
+ this._setState('EXCEPTS', columns.length ? columns : []);
84
151
  return this;
85
152
  }
86
153
  /**
87
- * select data form table
88
- * @param {string[]} ...columns
154
+ * The 'void' method is used to specify which you don't want to return a result from database table.
155
+ *
89
156
  * @return {this} this
90
157
  */
91
- select(...columns) {
92
- if (!columns.length)
93
- return this;
94
- let select = columns.map((column) => {
95
- if (column === '*')
96
- return column;
97
- if (/\./.test(column))
98
- return column;
99
- if (column.includes(this.$constants('RAW')))
100
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
101
- return `\`${column}\``;
102
- }).join(', ');
103
- select = this.$state.get('DISTINCT') && !select.includes(this.$constants('DISTINCT'))
104
- ? `${this.$constants('DISTINCT')} ${select}`
105
- : `${select}`;
106
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
158
+ void() {
159
+ this._setState('VOID', true);
107
160
  return this;
108
161
  }
109
- selectRaw(...columns) {
110
- if (!columns.length)
111
- return this;
112
- let select = columns.map((column) => {
113
- if (column === '*')
114
- return column;
115
- if (column.includes(this.$constants('RAW')))
116
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
117
- return column;
118
- }).join(', ');
119
- select = this.$state.get('DISTINCT') && !select.includes(this.$constants('DISTINCT'))
120
- ? `${this.$constants('DISTINCT')} ${select}`
121
- : `${select}`;
122
- this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
162
+ /**
163
+ * The 'only' method is used to specify which columns you don't want to retrieve from a result.
164
+ *
165
+ * It allows you to choose the specific columns that should be not included in the result.
166
+ * @param {...string} columns show only colums selected
167
+ * @return {this} this
168
+ */
169
+ only(...columns) {
170
+ this._setState('ONLY', columns);
123
171
  return this;
124
172
  }
125
173
  /**
126
- * chunks data from array
174
+ * The 'chunk' method is used to process a large result set from a database query in smaller, manageable "chunks" or segments.
175
+ *
176
+ * It's particularly useful when you need to iterate over a large number of database records without loading all of them into memory at once.
177
+ *
178
+ * This helps prevent memory exhaustion and improves the performance of your application when dealing with large datasets.
127
179
  * @param {number} chunk
128
180
  * @return {this} this
129
181
  */
130
182
  chunk(chunk) {
131
- this.$state.set('CHUNK', chunk);
183
+ this._setState('CHUNK', chunk);
132
184
  return this;
133
185
  }
134
186
  /**
135
- *
187
+ * The 'when' method is used to specify if condition should be true will be next to the actions
136
188
  * @param {string | number | undefined | null | Boolean} condition when condition true will return query callback
137
189
  * @return {this} this
138
190
  */
@@ -145,7 +197,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
145
197
  return this;
146
198
  }
147
199
  /**
148
- * if has 2 arguments default operator '='
200
+ * The 'where' method is used to add conditions to a database query.
201
+ *
202
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
203
+ *
204
+ * If has only 2 arguments default operator '='
149
205
  * @param {string} column if arguments is object
150
206
  * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
151
207
  * @param {any?} value
@@ -158,18 +214,81 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
158
214
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
159
215
  value = this.$utils.escape(value);
160
216
  value = this._valueTrueFalse(value);
161
- this.$state.set('WHERE', [
217
+ this._setState('WHERE', [
162
218
  this._queryWhereIsExists()
163
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
219
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
164
220
  : `${this.$constants('WHERE')}`,
165
- `${this._bindTableAndColumnInQueryWhere(column)}`,
221
+ `${this.bindColumn(column)}`,
166
222
  `${operator}`,
167
223
  `${this._checkValueHasRaw(value)}`
168
224
  ].join(' '));
169
225
  return this;
170
226
  }
171
227
  /**
172
- * where using object operator only '='
228
+ * The 'orWhere' method is used to add conditions to a database query.
229
+ *
230
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
231
+ *
232
+ * If has only 2 arguments default operator '='
233
+ * @param {string} column
234
+ * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
235
+ * @param {any?} value
236
+ * @return {this}
237
+ */
238
+ orWhere(column, operator, value) {
239
+ [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
240
+ value = this.$utils.escape(value);
241
+ value = this._valueTrueFalse(value);
242
+ this._setState('WHERE', [
243
+ this._queryWhereIsExists()
244
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
245
+ : `${this.$constants('WHERE')}`,
246
+ `${this.bindColumn(column)}`,
247
+ `${operator}`,
248
+ `${this._checkValueHasRaw(value)}`
249
+ ].join(' '));
250
+ return this;
251
+ }
252
+ /**
253
+ * The 'whereRaw' method is used to add a raw SQL condition to a database query.
254
+ *
255
+ * It allows you to include custom SQL expressions as conditions in your query,
256
+ * which can be useful for situations where you need to perform complex or custom filtering that cannot be achieved using Laravel's standard query builder methods.
257
+ * @param {string} sql where column with raw sql
258
+ * @return {this} this
259
+ */
260
+ whereRaw(sql) {
261
+ this._setState('WHERE', [
262
+ this._queryWhereIsExists()
263
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
264
+ : `${this.$constants('WHERE')}`,
265
+ `${sql}`,
266
+ ].join(' '));
267
+ return this;
268
+ }
269
+ /**
270
+ * The 'orWhereRaw' method is used to add a raw SQL condition to a database query.
271
+ *
272
+ * It allows you to include custom SQL expressions as conditions in your query,
273
+ * which can be useful for situations where you need to perform complex or custom filtering that cannot be achieved using Laravel's standard query builder methods.
274
+ * @param {string} sql where column with raw sql
275
+ * @return {this} this
276
+ */
277
+ orWhereRaw(sql) {
278
+ this._setState('WHERE', [
279
+ this._queryWhereIsExists()
280
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
281
+ : `${this.$constants('WHERE')}`,
282
+ `${sql}`,
283
+ ].join(' '));
284
+ return this;
285
+ }
286
+ /**
287
+ * The 'whereObject' method is used to add conditions to a database query.
288
+ *
289
+ * It allows you to specify conditions in object that records in the database must meet in order to be included in the result set.
290
+ *
291
+ * This method is defalut operator '=' only
173
292
  * @param {Object} columns
174
293
  * @return {this}
175
294
  */
@@ -177,11 +296,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
177
296
  for (const column in columns) {
178
297
  const operator = '=';
179
298
  const value = this.$utils.escape(columns[column]);
180
- this.$state.set('WHERE', [
299
+ this._setState('WHERE', [
181
300
  this._queryWhereIsExists()
182
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
301
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
183
302
  : `${this.$constants('WHERE')}`,
184
- `${this._bindTableAndColumnInQueryWhere(column)}`,
303
+ `${this.bindColumn(column)}`,
185
304
  `${operator}`,
186
305
  `${this._checkValueHasRaw(value)}`
187
306
  ].join(' '));
@@ -189,87 +308,65 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
189
308
  return this;
190
309
  }
191
310
  /**
192
- * where json target to key in json values default operator '='
311
+ * The 'whereJSON' method is used to add conditions to a database query.
312
+ *
313
+ * It allows you to specify conditions in that records json in the database must meet in order to be included in the result set.
193
314
  * @param {string} column
194
- * @param {object} property
195
- * @property {string} property.targetKey
315
+ * @param {object} property object { key , value , operator }
316
+ * @property {string} property.key
196
317
  * @property {string} property.value
197
318
  * @property {string?} property.operator
198
- * @example
199
319
  * @return {this}
200
320
  */
201
- whereJSON(column, { targetKey, value, operator }) {
321
+ whereJSON(column, { key, value, operator }) {
202
322
  value = this.$utils.escape(value);
203
323
  value = this._valueTrueFalse(value);
204
- this.$state.set('WHERE', [
324
+ this._setState('WHERE', [
205
325
  this._queryWhereIsExists()
206
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
326
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
207
327
  : `${this.$constants('WHERE')}`,
208
- `${this._bindTableAndColumnInQueryWhere(column)}->>'$.${targetKey}'`,
328
+ `${this.bindColumn(column)}->>'$.${key}'`,
209
329
  `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
210
330
  `${this._checkValueHasRaw(value)}`
211
331
  ].join(' '));
212
332
  return this;
213
333
  }
214
334
  /**
215
- * if has 2 arguments default operator '='
216
- * @param {string} column
217
- * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
218
- * @param {any?} value
219
- * @return {this}
335
+ * The 'whereJSON' method is used to add conditions to a database query.
336
+ *
337
+ * It allows you to specify conditions in that records json in the database must meet in order to be included in the result set.
338
+ * @param {string} column
339
+ * @param {object} property object { key , value , operator }
340
+ * @property {string} property.key
341
+ * @property {string} property.value
342
+ * @property {string?} property.operator
343
+ * @return {this}
220
344
  */
221
- orWhere(column, operator, value) {
222
- [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
345
+ whereJson(column, { key, value, operator }) {
223
346
  value = this.$utils.escape(value);
224
347
  value = this._valueTrueFalse(value);
225
- this.$state.set('WHERE', [
348
+ this._setState('WHERE', [
226
349
  this._queryWhereIsExists()
227
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
350
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
228
351
  : `${this.$constants('WHERE')}`,
229
- `${this._bindTableAndColumnInQueryWhere(column)}`,
230
- `${operator}`,
352
+ `${this.bindColumn(column)}->>'$.${key}'`,
353
+ `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
231
354
  `${this._checkValueHasRaw(value)}`
232
355
  ].join(' '));
233
356
  return this;
234
357
  }
235
358
  /**
236
359
  *
237
- * @param {string} sql where column with raw sql
238
- * @return {this} this
239
- */
240
- whereRaw(sql) {
241
- this.$state.set('WHERE', [
242
- this._queryWhereIsExists()
243
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
244
- : `${this.$constants('WHERE')}`,
245
- `${sql}`,
246
- ].join(' '));
247
- return this;
248
- }
249
- /**
250
- *
251
- * @param {string} query where column with raw sql
252
- * @return {this} this
253
- */
254
- orWhereRaw(sql) {
255
- this.$state.set('WHERE', [
256
- this._queryWhereIsExists()
257
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
258
- : `${this.$constants('WHERE')}`,
259
- `${sql}`,
260
- ].join(' '));
261
- return this;
262
- }
263
- /**
360
+ * The 'whereExists' method is used to add a conditional clause to a database query that checks for the existence of related records in a subquery or another table.
264
361
  *
265
- * where exists
362
+ * It allows you to filter records based on whether a specified condition is true for related records.
266
363
  * @param {string} sql
267
364
  * @return {this}
268
365
  */
269
366
  whereExists(sql) {
270
- this.$state.set('WHERE', [
367
+ this._setState('WHERE', [
271
368
  this._queryWhereIsExists()
272
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
369
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
273
370
  : `${this.$constants('WHERE')}`,
274
371
  `${this.$constants('EXISTS')}`,
275
372
  `(${sql})`
@@ -282,11 +379,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
282
379
  * @return {this} this
283
380
  */
284
381
  whereId(id, column = 'id') {
285
- this.$state.set('WHERE', [
382
+ this._setState('WHERE', [
286
383
  this._queryWhereIsExists()
287
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
384
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
288
385
  : `${this.$constants('WHERE')}`,
289
- `${this._bindTableAndColumnInQueryWhere(column)} = ${this.$utils.escape(id)}`,
386
+ `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
290
387
  ].join(' '));
291
388
  return this;
292
389
  }
@@ -297,11 +394,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
297
394
  */
298
395
  whereEmail(email) {
299
396
  const column = 'email';
300
- this.$state.set('WHERE', [
397
+ this._setState('WHERE', [
301
398
  this._queryWhereIsExists()
302
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
399
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
303
400
  : `${this.$constants('WHERE')}`,
304
- `${this._bindTableAndColumnInQueryWhere(column)} = ${this.$utils.escape(email)}`,
401
+ `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
305
402
  ].join(' '));
306
403
  return this;
307
404
  }
@@ -312,105 +409,116 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
312
409
  * @return {this}
313
410
  */
314
411
  whereUser(userId, column = 'user_id') {
315
- this.$state.set('WHERE', [
412
+ this._setState('WHERE', [
316
413
  this._queryWhereIsExists()
317
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
414
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
318
415
  : `${this.$constants('WHERE')}`,
319
- `${this._bindTableAndColumnInQueryWhere(column)} = ${this.$utils.escape(userId)}`,
416
+ `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
320
417
  ].join(' '));
321
418
  return this;
322
419
  }
323
420
  /**
324
- * using array value where in value in array
421
+ * The 'whereIn' method is used to add a conditional clause to a database query that checks if a specified column's value is included in a given array of values.
422
+ *
423
+ * This method is useful when you want to filter records based on a column matching any of the values provided in an array.
325
424
  * @param {string} column
326
425
  * @param {array} array
327
426
  * @return {this}
328
427
  */
329
428
  whereIn(column, array) {
330
429
  if (!Array.isArray(array))
331
- throw new Error(`[${array}] is't array`);
430
+ throw new Error(`This 'whereIn' method is required array only`);
332
431
  const values = array.length
333
432
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
334
433
  : this.$constants('NULL');
335
- this.$state.set('WHERE', [
434
+ this._setState('WHERE', [
336
435
  this._queryWhereIsExists()
337
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
436
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
338
437
  : `${this.$constants('WHERE')}`,
339
- `${this._bindTableAndColumnInQueryWhere(column)}`,
438
+ `${this.bindColumn(column)}`,
340
439
  `${this.$constants('IN')}`,
341
440
  `(${values})`
342
441
  ].join(' '));
343
442
  return this;
344
443
  }
345
444
  /**
346
- * or where in data using array values
445
+ * The 'orWhereIn' method is used to add a conditional clause to a database query that checks if a specified column's value is included in a given array of values.
446
+ *
447
+ * This method is useful when you want to filter records based on a column matching any of the values provided in an array.
347
448
  * @param {string} column
348
449
  * @param {array} array
349
450
  * @return {this}
350
451
  */
351
452
  orWhereIn(column, array) {
352
453
  if (!Array.isArray(array))
353
- throw new Error(`[${array}] is't array`);
454
+ throw new Error(`This 'whereIn' method is required array only`);
354
455
  const values = array.length
355
456
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
356
457
  : this.$constants('NULL');
357
- this.$state.set('WHERE', [
458
+ this._setState('WHERE', [
358
459
  this._queryWhereIsExists()
359
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
460
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
360
461
  : `${this.$constants('WHERE')}`,
361
- `${this._bindTableAndColumnInQueryWhere(column)}`,
462
+ `${this.bindColumn(column)}`,
362
463
  `${this.$constants('IN')}`,
363
464
  `(${values})`
364
465
  ].join(' '));
365
466
  return this;
366
467
  }
367
468
  /**
368
- * where not in data using array values
469
+ * The 'whereNotIn' method is used to add a conditional clause to a database query that checks if a specified column's value is not included in a given array of values.
470
+ *
471
+ * This method is the opposite of whereIn and is useful when you want to filter records based on a column not matching any of the values provided in an array.
369
472
  * @param {string} column
370
473
  * @param {array} array
371
474
  * @return {this}
372
475
  */
373
476
  whereNotIn(column, array) {
374
- const sql = this.$state.get('WHERE');
375
477
  if (!Array.isArray(array))
376
- throw new Error(`[${array}] is't array`);
478
+ throw new Error(`This 'whereIn' method is required array only`);
377
479
  const values = array.length
378
480
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
379
481
  : this.$constants('NULL');
380
- this.$state.set('WHERE', [
482
+ this._setState('WHERE', [
381
483
  this._queryWhereIsExists()
382
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
484
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
383
485
  : `${this.$constants('WHERE')}`,
384
- `${this._bindTableAndColumnInQueryWhere(column)}`,
486
+ `${this.bindColumn(column)}`,
385
487
  `${this.$constants('NOT_IN')}`,
386
488
  `(${values})`
387
489
  ].join(' '));
388
490
  return this;
389
491
  }
390
492
  /**
391
- * where not in data using array values
493
+ * The 'orWhereNotIn' method is used to add a conditional clause to a database query that checks if a specified column's value is not included in a given array of values.
494
+ *
495
+ * This method is the opposite of whereIn and is useful when you want to filter records based on a column not matching any of the values provided in an array.
392
496
  * @param {string} column
393
497
  * @param {array} array
394
498
  * @return {this}
395
499
  */
396
500
  orWhereNotIn(column, array) {
397
501
  if (!Array.isArray(array))
398
- throw new Error(`[${array}] is't array`);
502
+ throw new Error(`This 'whereIn' method is required array only`);
399
503
  const values = array.length
400
504
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
401
505
  : this.$constants('NULL');
402
- this.$state.set('WHERE', [
506
+ this._setState('WHERE', [
403
507
  this._queryWhereIsExists()
404
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
508
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
405
509
  : `${this.$constants('WHERE')}`,
406
- `${this._bindTableAndColumnInQueryWhere(column)}`,
510
+ `${this.bindColumn(column)}`,
407
511
  `${this.$constants('NOT_IN')}`,
408
512
  `(${values})`
409
513
  ].join(' '));
410
514
  return this;
411
515
  }
412
516
  /**
413
- * where sub query using sub query sql
517
+ * The 'whereSubQuery' method is used to add a conditional clause to a database query that involves a subquery.
518
+ *
519
+ * Subqueries also known as nested queries, are queries that are embedded within the main query.
520
+ *
521
+ * They are often used when you need to perform a query to retrieve some values and then use those values as part of the condition in the main query.
414
522
  * @param {string} column
415
523
  * @param {string} subQuery
416
524
  * @return {this}
@@ -418,18 +526,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
418
526
  whereSubQuery(column, subQuery) {
419
527
  if (!this.$utils.isSubQuery(subQuery))
420
528
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
421
- this.$state.set('WHERE', [
529
+ this._setState('WHERE', [
422
530
  this._queryWhereIsExists()
423
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
531
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
424
532
  : `${this.$constants('WHERE')}`,
425
- `${this._bindTableAndColumnInQueryWhere(column)}`,
533
+ `${this.bindColumn(column)}`,
426
534
  `${this.$constants('IN')}`,
427
535
  `(${subQuery})`
428
536
  ].join(' '));
429
537
  return this;
430
538
  }
431
539
  /**
432
- * where not sub query using sub query sql
540
+ * The 'whereNotSubQuery' method is used to add a conditional clause to a database query that involves a subquery.
541
+ *
542
+ * Subqueries also known as nested queries, are queries that are embedded within the main query.
543
+ *
544
+ * They are often used when you need to perform a query to retrieve not some values and then use those values as part of the condition in the main query.
433
545
  * @param {string} column
434
546
  * @param {string} subQuery
435
547
  * @return {this}
@@ -437,18 +549,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
437
549
  whereNotSubQuery(column, subQuery) {
438
550
  if (!this.$utils.isSubQuery(subQuery))
439
551
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
440
- this.$state.set('WHERE', [
552
+ this._setState('WHERE', [
441
553
  this._queryWhereIsExists()
442
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
554
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
443
555
  : `${this.$constants('WHERE')}`,
444
- `${this._bindTableAndColumnInQueryWhere(column)}`,
556
+ `${this.bindColumn(column)}`,
445
557
  `${this.$constants('NOT_IN')}`,
446
558
  `(${subQuery})`
447
559
  ].join(' '));
448
560
  return this;
449
561
  }
450
562
  /**
451
- * or where not sub query using query sql
563
+ * The 'orWhereSubQuery' method is used to add a conditional clause to a database query that involves a subquery.
564
+ *
565
+ * Subqueries also known as nested queries, are queries that are embedded within the main query.
566
+ *
567
+ * They are often used when you need to perform a query to retrieve some values and then use those values as part of the condition in the main query.
452
568
  * @param {string} column
453
569
  * @param {string} subQuery
454
570
  * @return {this}
@@ -456,18 +572,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
456
572
  orWhereSubQuery(column, subQuery) {
457
573
  if (!this.$utils.isSubQuery(subQuery))
458
574
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
459
- this.$state.set('WHERE', [
575
+ this._setState('WHERE', [
460
576
  this._queryWhereIsExists()
461
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
577
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
462
578
  : `${this.$constants('WHERE')}`,
463
- `${this._bindTableAndColumnInQueryWhere(column)}`,
579
+ `${this.bindColumn(column)}`,
464
580
  `${this.$constants('IN')}`,
465
581
  `(${subQuery})`
466
582
  ].join(' '));
467
583
  return this;
468
584
  }
469
585
  /**
470
- * or where not sub query using query sql
586
+ * The 'orWhereNotSubQuery' method is used to add a conditional clause to a database query that involves a subquery.
587
+ *
588
+ * Subqueries also known as nested queries, are queries that are embedded within the main query.
589
+ *
590
+ * They are often used when you need to perform a query to retrieve not some values and then use those values as part of the condition in the main query.
471
591
  * @param {string} column
472
592
  * @param {string} subQuery
473
593
  * @return {this}
@@ -475,18 +595,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
475
595
  orWhereNotSubQuery(column, subQuery) {
476
596
  if (!this.$utils.isSubQuery(subQuery))
477
597
  throw new Error(`This "${subQuery}" is invalid sub query (Sub query Operand should contain 1 column(s) not select * )`);
478
- this.$state.set('WHERE', [
598
+ this._setState('WHERE', [
479
599
  this._queryWhereIsExists()
480
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
600
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
481
601
  : `${this.$constants('WHERE')}`,
482
- `${this._bindTableAndColumnInQueryWhere(column)}`,
602
+ `${this.bindColumn(column)}`,
483
603
  `${this.$constants('NOT_IN')}`,
484
604
  `(${subQuery})`
485
605
  ].join(' '));
486
606
  return this;
487
607
  }
488
608
  /**
489
- * where between using [value1, value2]
609
+ * The 'whereBetween' method is used to add a conditional clause to a database query that checks if a specified column's value falls within a specified range of values.
610
+ *
611
+ * This method is useful when you want to filter records based on a column's value being within a certain numeric or date range.
490
612
  * @param {string} column
491
613
  * @param {array} array
492
614
  * @return {this}
@@ -495,27 +617,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
495
617
  if (!Array.isArray(array))
496
618
  throw new Error("Value is't array");
497
619
  if (!array.length) {
498
- this.$state.set('WHERE', [
620
+ this._setState('WHERE', [
499
621
  this._queryWhereIsExists()
500
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
622
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
501
623
  : `${this.$constants('WHERE')}`,
502
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
624
+ `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
503
625
  `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
504
626
  ].join(' '));
505
627
  return this;
506
628
  }
507
629
  const [value1, value2] = array;
508
- this.$state.set('WHERE', [
630
+ this._setState('WHERE', [
509
631
  this._queryWhereIsExists()
510
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
632
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
511
633
  : `${this.$constants('WHERE')}`,
512
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
634
+ `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
513
635
  `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
514
636
  ].join(' '));
515
637
  return this;
516
638
  }
517
639
  /**
518
- * where between using [value1, value2]
640
+ * The 'orWhereBetween' method is used to add a conditional clause to a database query that checks if a specified column's value falls within a specified range of values.
641
+ *
642
+ * This method is useful when you want to filter records based on a column's value being within a certain numeric or date range.
519
643
  * @param {string} column
520
644
  * @param {array} array
521
645
  * @return {this}
@@ -524,27 +648,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
524
648
  if (!Array.isArray(array))
525
649
  throw new Error("Value is't array");
526
650
  if (!array.length) {
527
- this.$state.set('WHERE', [
651
+ this._setState('WHERE', [
528
652
  this._queryWhereIsExists()
529
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
653
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
530
654
  : `${this.$constants('WHERE')}`,
531
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
655
+ `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
532
656
  `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
533
657
  ].join(' '));
534
658
  return this;
535
659
  }
536
660
  const [value1, value2] = array;
537
- this.$state.set('WHERE', [
661
+ this._setState('WHERE', [
538
662
  this._queryWhereIsExists()
539
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
663
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
540
664
  : `${this.$constants('WHERE')}`,
541
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
665
+ `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
542
666
  `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
543
667
  ].join(' '));
544
668
  return this;
545
669
  }
546
670
  /**
547
- * where not between using [value1, value2]
671
+ * The 'whereNotBetween' method is used to add a conditional clause to a database query that checks if a specified column's value falls within a specified range of values.
672
+ *
673
+ * This method is useful when you want to filter records based on a column's value does not fall within a specified range of values.
548
674
  * @param {string} column
549
675
  * @param {array} array
550
676
  * @return {this}
@@ -553,27 +679,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
553
679
  if (!Array.isArray(array))
554
680
  throw new Error("Value is't array");
555
681
  if (!array.length) {
556
- this.$state.set('WHERE', [
682
+ this._setState('WHERE', [
557
683
  this._queryWhereIsExists()
558
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
684
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
559
685
  : `${this.$constants('WHERE')}`,
560
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
686
+ `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
561
687
  `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
562
688
  ].join(' '));
563
689
  return this;
564
690
  }
565
691
  const [value1, value2] = array;
566
- this.$state.set('WHERE', [
692
+ this._setState('WHERE', [
567
693
  this._queryWhereIsExists()
568
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
694
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
569
695
  : `${this.$constants('WHERE')}`,
570
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
696
+ `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
571
697
  `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
572
698
  ].join(' '));
573
699
  return this;
574
700
  }
575
701
  /**
576
- * where not between using [value1, value2]
702
+ * The 'orWhereNotBetween' method is used to add a conditional clause to a database query that checks if a specified column's value falls within a specified range of values.
703
+ *
704
+ * This method is useful when you want to filter records based on a column's value does not fall within a specified range of values.
577
705
  * @param {string} column
578
706
  * @param {array} array
579
707
  * @return {this}
@@ -582,87 +710,99 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
582
710
  if (!Array.isArray(array))
583
711
  throw new Error("Value is't array");
584
712
  if (!array.length) {
585
- this.$state.set('WHERE', [
713
+ this._setState('WHERE', [
586
714
  this._queryWhereIsExists()
587
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
715
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
588
716
  : `${this.$constants('WHERE')}`,
589
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
717
+ `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
590
718
  `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
591
719
  ].join(' '));
592
720
  return this;
593
721
  }
594
722
  const [value1, value2] = array;
595
- this.$state.set('WHERE', [
723
+ this._setState('WHERE', [
596
724
  this._queryWhereIsExists()
597
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
725
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
598
726
  : `${this.$constants('WHERE')}`,
599
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
727
+ `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
600
728
  `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
601
729
  ].join(' '));
602
730
  return this;
603
731
  }
604
732
  /**
605
- * where null using NULL
733
+ * The 'whereNull' method is used to add a conditional clause to a database query that checks if a specified column's value is NULL.
734
+ *
735
+ * This method is helpful when you want to filter records based on whether a particular column has a NULL value.
606
736
  * @param {string} column
607
737
  * @return {this}
608
738
  */
609
739
  whereNull(column) {
610
- this.$state.set('WHERE', [
740
+ this._setState('WHERE', [
611
741
  this._queryWhereIsExists()
612
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
742
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
613
743
  : `${this.$constants('WHERE')}`,
614
- `${this._bindTableAndColumnInQueryWhere(column)}`,
744
+ `${this.bindColumn(column)}`,
615
745
  `${this.$constants('IS_NULL')}`
616
746
  ].join(' '));
617
747
  return this;
618
748
  }
619
749
  /**
620
- * where null using NULL
621
- * @param {string} column
622
- * @return {this}
623
- */
750
+ * The 'orWhereNull' method is used to add a conditional clause to a database query that checks if a specified column's value is NULL.
751
+ *
752
+ * This method is helpful when you want to filter records based on whether a particular column has a NULL value.
753
+ * @param {string} column
754
+ * @return {this}
755
+ */
624
756
  orWhereNull(column) {
625
- this.$state.set('WHERE', [
757
+ this._setState('WHERE', [
626
758
  this._queryWhereIsExists()
627
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
759
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
628
760
  : `${this.$constants('WHERE')}`,
629
- `${this._bindTableAndColumnInQueryWhere(column)}`,
761
+ `${this.bindColumn(column)}`,
630
762
  `${this.$constants('IS_NULL')}`
631
763
  ].join(' '));
632
764
  return this;
633
765
  }
634
766
  /**
635
- * where not null using NULL
767
+ * The 'whereNotNull' method is used to add a conditional clause to a database query that checks if a specified column's value is not NULL.
768
+ *
769
+ * This method is useful when you want to filter records based on whether a particular column has a non-null value.
636
770
  * @param {string} column
637
771
  * @return {this}
638
772
  */
639
773
  whereNotNull(column) {
640
- this.$state.set('WHERE', [
774
+ this._setState('WHERE', [
641
775
  this._queryWhereIsExists()
642
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
776
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
643
777
  : `${this.$constants('WHERE')}`,
644
- `${this._bindTableAndColumnInQueryWhere(column)}`,
778
+ `${this.bindColumn(column)}`,
645
779
  `${this.$constants('IS_NOT_NULL')}`
646
780
  ].join(' '));
647
781
  return this;
648
782
  }
649
783
  /**
650
- * where not null using NULL
784
+ * The 'orWhereNotNull' method is used to add a conditional clause to a database query that checks if a specified column's value is not NULL.
785
+ *
786
+ * This method is useful when you want to filter records based on whether a particular column has a non-null value.
651
787
  * @param {string} column
652
788
  * @return {this}
653
789
  */
654
790
  orWhereNotNull(column) {
655
- this.$state.set('WHERE', [
791
+ this._setState('WHERE', [
656
792
  this._queryWhereIsExists()
657
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
793
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
658
794
  : `${this.$constants('WHERE')}`,
659
- `${this._bindTableAndColumnInQueryWhere(column)}`,
795
+ `${this.bindColumn(column)}`,
660
796
  `${this.$constants('IS_NOT_NULL')}`
661
797
  ].join(' '));
662
798
  return this;
663
799
  }
664
800
  /**
665
- * where sensitive (uppercase, lowercase)
801
+ * The 'whereSensitive' method is used to add conditions to a database query.
802
+ *
803
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
804
+ *
805
+ * The where method is need to perform a case-sensitive comparison in a query.
666
806
  * @param {string} column
667
807
  * @param {string?} operator = < > != !< !>
668
808
  * @param {any?} value
@@ -672,18 +812,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
672
812
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
673
813
  value = this.$utils.escape(value);
674
814
  value = this._valueTrueFalse(value);
675
- this.$state.set('WHERE', [
815
+ this._setState('WHERE', [
676
816
  this._queryWhereIsExists()
677
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
817
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
678
818
  : `${this.$constants('WHERE')}`,
679
- `${this.$constants('BINARY')} ${this._bindTableAndColumnInQueryWhere(column)}`,
819
+ `${this.$constants('BINARY')} ${this.bindColumn(column)}`,
680
820
  `${operator}`,
681
821
  `${this._checkValueHasRaw(this.$utils.escape(value))}`
682
822
  ].join(' '));
683
823
  return this;
684
824
  }
685
825
  /**
686
- * where Strict (uppercase, lowercase)
826
+ * The 'whereStrict' method is used to add conditions to a database query.
827
+ *
828
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
829
+ *
830
+ * The where method is need to perform a case-sensitive comparison in a query.
687
831
  * @param {string} column
688
832
  * @param {string?} operator = < > != !< !>
689
833
  * @param {any?} value
@@ -693,7 +837,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
693
837
  return this.whereSensitive(column, operator, value);
694
838
  }
695
839
  /**
696
- * or where sensitive (uppercase, lowercase)
840
+ * The 'orWhereSensitive' method is used to add conditions to a database query.
841
+ *
842
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
843
+ *
844
+ * The where method is need to perform a case-sensitive comparison in a query.
697
845
  * @param {string} column
698
846
  * @param {string?} operator = < > != !< !>
699
847
  * @param {any?} value
@@ -703,24 +851,26 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
703
851
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
704
852
  value = this.$utils.escape(value);
705
853
  value = this._valueTrueFalse(value);
706
- this.$state.set('WHERE', [
854
+ this._setState('WHERE', [
707
855
  this._queryWhereIsExists()
708
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
856
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
709
857
  : `${this.$constants('WHERE')}`,
710
- `${this.$constants('BINARY')} ${this._bindTableAndColumnInQueryWhere(column)}`,
858
+ `${this.$constants('BINARY')} ${this.bindColumn(column)}`,
711
859
  `${operator}`,
712
860
  `${this._checkValueHasRaw(this.$utils.escape(value))}`
713
861
  ].join(' '));
714
862
  return this;
715
863
  }
716
864
  /**
717
- * where group query
718
- * @param {function} callback callback query
865
+ * The 'whereQuery' method is used to add conditions to a database query to create a grouped condition.
866
+ *
867
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
868
+ * @param {Function} callback callback query
719
869
  * @return {this}
720
870
  */
721
871
  whereQuery(callback) {
722
872
  var _a;
723
- const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
873
+ const db = new DB_1.DB((_a = this._getState('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
724
874
  const repository = callback(db);
725
875
  if (repository instanceof Promise)
726
876
  throw new Error('"whereQuery" is not supported a Promise');
@@ -730,16 +880,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
730
880
  if (where === '')
731
881
  return this;
732
882
  const query = where.replace('WHERE', '');
733
- this.$state.set('WHERE', [
883
+ this._setState('WHERE', [
734
884
  this._queryWhereIsExists()
735
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
885
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
736
886
  : `${this.$constants('WHERE')}`,
737
887
  `(${query})`
738
888
  ].join(' '));
739
889
  return this;
740
890
  }
741
891
  /**
742
- * where group query
892
+ * The 'whereGroup' method is used to add conditions to a database query to create a grouped condition.
893
+ *
894
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
743
895
  * @param {function} callback callback query
744
896
  * @return {this}
745
897
  */
@@ -747,13 +899,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
747
899
  return this.whereQuery(callback);
748
900
  }
749
901
  /**
750
- * where group query
902
+ * The 'orWhereQuery' method is used to add conditions to a database query to create a grouped condition.
903
+ *
904
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
751
905
  * @param {function} callback callback query
752
906
  * @return {this}
753
907
  */
754
908
  orWhereQuery(callback) {
755
909
  var _a;
756
- const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
910
+ const db = new DB_1.DB((_a = this._getState('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
757
911
  const repository = callback(db);
758
912
  if (repository instanceof Promise)
759
913
  throw new Error('"whereQuery" is not supported a Promise');
@@ -763,14 +917,90 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
763
917
  if (where === '')
764
918
  return this;
765
919
  const query = where.replace('WHERE', '');
766
- this.$state.set('WHERE', [
920
+ this._setState('WHERE', [
767
921
  this._queryWhereIsExists()
768
- ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
922
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
769
923
  : `${this.$constants('WHERE')}`,
770
924
  `(${query})`
771
925
  ].join(' '));
772
926
  return this;
773
927
  }
928
+ /**
929
+ * The 'whereCases' method is used to add conditions with cases to a database query.
930
+ *
931
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
932
+ *
933
+ * @param {Array<{when , then}>} cases used to add conditions when and then
934
+ * @param {string?} elseCase else when end of conditions
935
+ * @return {this}
936
+ */
937
+ whereCases(cases, elseCase) {
938
+ if (!cases.length)
939
+ return this;
940
+ let query = [];
941
+ for (const c of cases) {
942
+ if (c.when == null)
943
+ throw new Error(`can't find when condition`);
944
+ if (c.then == null)
945
+ throw new Error(`can't find then condition`);
946
+ query = [
947
+ ...query,
948
+ `${this.$constants('WHEN')} ${c.when} ${this.$constants('THEN')} ${c.then}`
949
+ ];
950
+ }
951
+ this._setState('WHERE', [
952
+ this._queryWhereIsExists()
953
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
954
+ : `${this.$constants('WHERE')}`,
955
+ [
956
+ '(',
957
+ this.$constants('CASE'),
958
+ query.join(' '),
959
+ elseCase == null ? '' : `ELSE ${elseCase}`,
960
+ this.$constants('END'),
961
+ ')'
962
+ ].join(' ')
963
+ ].join(' '));
964
+ return this;
965
+ }
966
+ /**
967
+ * The 'orWhereCases' method is used to add conditions with cases to a database query.
968
+ *
969
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
970
+ *
971
+ * @param {Array<{when , then}>} cases used to add conditions when and then
972
+ * @param {string?} elseCase else when end of conditions
973
+ * @return {this}
974
+ */
975
+ orWhereCases(cases, elseCase) {
976
+ if (!cases.length)
977
+ return this;
978
+ let query = [];
979
+ for (const c of cases) {
980
+ if (c.when == null)
981
+ throw new Error(`can't find when condition`);
982
+ if (c.then == null)
983
+ throw new Error(`can't find then condition`);
984
+ query = [
985
+ ...query,
986
+ `${this.$constants('WHEN')} ${c.when} ${this.$constants('THEN')} ${c.then}`
987
+ ];
988
+ }
989
+ this._setState('WHERE', [
990
+ this._queryWhereIsExists()
991
+ ? `${this._getState('WHERE')} ${this.$constants('OR')}`
992
+ : `${this.$constants('WHERE')}`,
993
+ [
994
+ '(',
995
+ this.$constants('CASE'),
996
+ query.join(' '),
997
+ elseCase == null ? '' : `ELSE ${elseCase}`,
998
+ this.$constants('END'),
999
+ ')'
1000
+ ].join(' ')
1001
+ ].join(' '));
1002
+ return this;
1003
+ }
774
1004
  /**
775
1005
  * select by cases
776
1006
  * @param {array} cases array object [{ when : 'id < 7' , then : 'id is than under 7'}]
@@ -802,11 +1032,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
802
1032
  }
803
1033
  if (query.length <= 1)
804
1034
  return this;
805
- this.$state.set('SELECT', `${this.$state.get('SELECT')}, ${query.join(' ')} ${this.$constants('AS')} ${as}`);
1035
+ this._setState('SELECT', [...this._getState('SELECT'), `${query.join(' ')} ${this.$constants('AS')} ${as}`]);
806
1036
  return this;
807
1037
  }
808
1038
  /**
1039
+ * The 'join' method is used to perform various types of SQL joins between two or more database tables.
809
1040
  *
1041
+ * Joins are used to combine data from different tables based on a specified condition, allowing you to retrieve data from related tables in a single query.
810
1042
  * @param {string} localKey local key in current table
811
1043
  * @param {string} referenceKey reference key in next table
812
1044
  * @example
@@ -822,22 +1054,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
822
1054
  join(localKey, referenceKey) {
823
1055
  var _a;
824
1056
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
825
- if (this.$state.get('JOIN')) {
826
- this.$state.set('JOIN', [
827
- `${this.$state.get('JOIN')}`,
1057
+ this._setState('JOIN', [
1058
+ ...this._getState('JOIN'),
1059
+ [
828
1060
  `${this.$constants('INNER_JOIN')}`,
829
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
830
- ].join(' '));
831
- return this;
832
- }
833
- this.$state.set('JOIN', [
834
- `${this.$constants('INNER_JOIN')}`,
835
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
836
- ].join(' '));
1061
+ `\`${table}\` ${this.$constants('ON')}`,
1062
+ `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1063
+ ]
1064
+ .join(' ')
1065
+ ]);
837
1066
  return this;
838
1067
  }
839
1068
  /**
1069
+ * The 'rightJoin' method is used to perform a right join operation between two database tables.
1070
+ *
1071
+ * A right join, also known as a right outer join, retrieves all rows from the right table and the matching rows from the left table.
840
1072
  *
1073
+ * If there is no match in the left table, NULL values are returned for columns from the left table
841
1074
  * @param {string} localKey local key in current table
842
1075
  * @param {string} referenceKey reference key in next table
843
1076
  * @return {this}
@@ -845,22 +1078,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
845
1078
  rightJoin(localKey, referenceKey) {
846
1079
  var _a;
847
1080
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
848
- if (this.$state.get('JOIN')) {
849
- this.$state.set('JOIN', [
850
- `${this.$state.get('JOIN')}`,
1081
+ this._setState('JOIN', [
1082
+ ...this._getState('JOIN'),
1083
+ [
851
1084
  `${this.$constants('RIGHT_JOIN')}`,
852
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
853
- ].join(' '));
854
- return this;
855
- }
856
- this.$state.set('JOIN', [
857
- `${this.$constants('RIGHT_JOIN')}`,
858
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
859
- ].join(' '));
1085
+ `\`${table}\` ${this.$constants('ON')}`,
1086
+ `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1087
+ ]
1088
+ .join(' ')
1089
+ ]);
860
1090
  return this;
861
1091
  }
862
1092
  /**
1093
+ * The 'leftJoin' method is used to perform a left join operation between two database tables.
1094
+ *
1095
+ * A left join retrieves all rows from the left table and the matching rows from the right table.
863
1096
  *
1097
+ * If there is no match in the right table, NULL values are returned for columns from the right table.
864
1098
  * @param {string} localKey local key in current table
865
1099
  * @param {string} referenceKey reference key in next table
866
1100
  * @return {this}
@@ -868,22 +1102,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
868
1102
  leftJoin(localKey, referenceKey) {
869
1103
  var _a;
870
1104
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
871
- if (this.$state.get('JOIN')) {
872
- this.$state.set('JOIN', [
873
- `${this.$state.get('JOIN')}`,
1105
+ this._setState('JOIN', [
1106
+ ...this._getState('JOIN'),
1107
+ [
874
1108
  `${this.$constants('LEFT_JOIN')}`,
875
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
876
- ].join(' '));
877
- return this;
878
- }
879
- this.$state.set('JOIN', [
880
- `${this.$constants('LEFT_JOIN')}`,
881
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
882
- ].join(' '));
1109
+ `\`${table}\` ${this.$constants('ON')}`,
1110
+ `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1111
+ ]
1112
+ .join(' ')
1113
+ ]);
883
1114
  return this;
884
1115
  }
885
1116
  /**
1117
+ * The 'crossJoin' method performs a cross join operation between two or more tables.
886
1118
  *
1119
+ * A cross join, also known as a Cartesian join, combines every row from the first table with every row from the second table.
887
1120
  * @param {string} localKey local key in current table
888
1121
  * @param {string} referenceKey reference key in next table
889
1122
  * @return {this}
@@ -891,45 +1124,50 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
891
1124
  crossJoin(localKey, referenceKey) {
892
1125
  var _a;
893
1126
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
894
- if (this.$state.get('JOIN')) {
895
- this.$state.set('JOIN', [
896
- `${this.$state.get('JOIN')}`,
1127
+ this._setState('JOIN', [
1128
+ ...this._getState('JOIN'),
1129
+ [
897
1130
  `${this.$constants('CROSS_JOIN')}`,
898
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
899
- ].join(' '));
900
- return this;
901
- }
902
- this.$state.set('JOIN', [
903
- `${this.$constants('CROSS_JOIN')}`,
904
- `\`${table}\` ${this.$constants('ON')} ${localKey} = ${referenceKey}`
905
- ].join(' '));
1131
+ `\`${table}\` ${this.$constants('ON')}`,
1132
+ `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1133
+ ]
1134
+ .join(' ')
1135
+ ]);
906
1136
  return this;
907
1137
  }
908
1138
  /**
909
- * sort the result in ASC or DESC order.
1139
+ * The 'orderBy' method is used to specify the order in which the results of a database query should be sorted.
1140
+ *
1141
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction (ascending or descending) for each column.
910
1142
  * @param {string} column
911
- * @param {string?} order [order=asc] asc, desc
1143
+ * @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
912
1144
  * @return {this}
913
1145
  */
914
1146
  orderBy(column, order = this.$constants('ASC')) {
915
1147
  if (typeof column !== 'string')
916
1148
  return this;
917
- if (column.includes(this.$constants('RAW'))) {
1149
+ if (column.includes(this.$constants('RAW')) || /\./.test(column)) {
918
1150
  column = column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
919
- this.$state.set('ORDER_BY', [
1151
+ if (/\./.test(column))
1152
+ column = this.bindColumn(column);
1153
+ this._setState('ORDER_BY', [
920
1154
  `${this.$constants('ORDER_BY')}`,
921
1155
  `${column} ${order.toUpperCase()}`
922
1156
  ].join(' '));
923
1157
  return this;
924
1158
  }
925
- this.$state.set('ORDER_BY', [
1159
+ this._setState('ORDER_BY', [
926
1160
  `${this.$constants('ORDER_BY')}`,
927
1161
  `\`${column}\` ${order.toUpperCase()}`
928
1162
  ].join(' '));
929
1163
  return this;
930
1164
  }
931
1165
  /**
932
- * sort the result in ASC or DESC order. can using with raw query
1166
+ * The 'orderByRaw' method is used to specify the order in which the results of a database query should be sorted.
1167
+ *
1168
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction (ascending or descending) for each column.
1169
+ *
1170
+ * This method allows you to specify raw-sql parameters for the query.
933
1171
  * @param {string} column
934
1172
  * @param {string?} order [order=asc] asc, desc
935
1173
  * @return {this}
@@ -938,14 +1176,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
938
1176
  if (column.includes(this.$constants('RAW'))) {
939
1177
  column = column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
940
1178
  }
941
- this.$state.set('ORDER_BY', [
1179
+ this._setState('ORDER_BY', [
942
1180
  `${this.$constants('ORDER_BY')}`,
943
1181
  `${column} ${order.toUpperCase()}`
944
1182
  ].join(' '));
945
1183
  return this;
946
1184
  }
947
1185
  /**
948
- * sort the result in using DESC for order by.
1186
+ * The 'latest' method is used to specify the order in which the results of a database query should be sorted.
1187
+ *
1188
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction descending for each column.
949
1189
  * @param {string?} columns [column=id]
950
1190
  * @return {this}
951
1191
  */
@@ -953,19 +1193,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
953
1193
  let orderByDefault = 'id';
954
1194
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
955
1195
  orderByDefault = columns.map(column => {
1196
+ if (/\./.test(column))
1197
+ return this.bindColumn(column);
956
1198
  if (column.includes(this.$constants('RAW')))
957
1199
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
958
1200
  return `\`${column}\``;
959
1201
  }).join(', ');
960
1202
  }
961
- this.$state.set('ORDER_BY', [
1203
+ this._setState('ORDER_BY', [
962
1204
  `${this.$constants('ORDER_BY')}`,
963
1205
  `${orderByDefault} ${this.$constants('DESC')}`
964
1206
  ].join(' '));
965
1207
  return this;
966
1208
  }
967
1209
  /**
968
- * sort the result in using DESC for order by. can using with raw query
1210
+ * The 'latestRaw' method is used to specify the order in which the results of a database query should be sorted.
1211
+ *
1212
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction descending for each column.
1213
+ *
1214
+ * This method allows you to specify raw-sql parameters for the query.
969
1215
  * @param {string?} columns [column=id]
970
1216
  * @return {this}
971
1217
  */
@@ -978,14 +1224,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
978
1224
  return column;
979
1225
  }).join(', ');
980
1226
  }
981
- this.$state.set('ORDER_BY', [
1227
+ this._setState('ORDER_BY', [
982
1228
  `${this.$constants('ORDER_BY')}`,
983
1229
  `${orderByDefault} ${this.$constants('DESC')}`
984
1230
  ].join(' '));
985
1231
  return this;
986
1232
  }
987
1233
  /**
988
- * sort the result in using ASC for order by.
1234
+ * The 'oldest' method is used to specify the order in which the results of a database query should be sorted.
1235
+ *
1236
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction ascending for each column.
989
1237
  * @param {string?} columns [column=id]
990
1238
  * @return {this}
991
1239
  */
@@ -993,19 +1241,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
993
1241
  let orderByDefault = 'id';
994
1242
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
995
1243
  orderByDefault = columns.map(column => {
1244
+ if (/\./.test(column))
1245
+ return this.bindColumn(column);
996
1246
  if (column.includes(this.$constants('RAW')))
997
1247
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
998
1248
  return `\`${column}\``;
999
1249
  }).join(', ');
1000
1250
  }
1001
- this.$state.set('ORDER_BY', [
1251
+ this._setState('ORDER_BY', [
1002
1252
  `${this.$constants('ORDER_BY')}`,
1003
1253
  `${orderByDefault} ${this.$constants('ASC')}`
1004
1254
  ].join(' '));
1005
1255
  return this;
1006
1256
  }
1007
1257
  /**
1008
- * sort the result in using ASC for order by. can using with raw query
1258
+ * The 'oldestRaw' method is used to specify the order in which the results of a database query should be sorted.
1259
+ *
1260
+ * This method allows you to specify one or more columns by which the result set should be ordered, as well as the sorting direction ascending for each column.
1261
+ *
1262
+ * This method allows you to specify raw-sql parameters for the query.
1009
1263
  * @param {string?} columns [column=id]
1010
1264
  * @return {this}
1011
1265
  */
@@ -1018,14 +1272,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1018
1272
  return column;
1019
1273
  }).join(', ');
1020
1274
  }
1021
- this.$state.set('ORDER_BY', [
1275
+ this._setState('ORDER_BY', [
1022
1276
  `${this.$constants('ORDER_BY')}`,
1023
1277
  `${orderByDefault} ${this.$constants('ASC')}`
1024
1278
  ].join(' '));
1025
1279
  return this;
1026
1280
  }
1027
1281
  /**
1282
+ * The groupBy method is used to group the results of a database query by one or more columns.
1028
1283
  *
1284
+ * It allows you to aggregate data based on the values in specified columns, often in conjunction with aggregate functions like COUNT, SUM, AVG, and MAX.
1285
+ *
1286
+ * Grouping is commonly used for generating summary reports, calculating totals, and performing other aggregate operations on data.
1029
1287
  * @param {string?} columns [column=id]
1030
1288
  * @return {this}
1031
1289
  */
@@ -1038,14 +1296,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1038
1296
  return `\`${column}\``;
1039
1297
  }).join(', ');
1040
1298
  }
1041
- this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1299
+ this._setState('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1042
1300
  return this;
1043
1301
  }
1044
1302
  /**
1045
- *
1046
- * @param {string?} columns [column=id]
1047
- * @return {this}
1048
- */
1303
+ * The groupBy method is used to group the results of a database query by one or more columns.
1304
+ *
1305
+ * It allows you to aggregate data based on the values in specified columns, often in conjunction with aggregate functions like COUNT, SUM, AVG, and MAX.
1306
+ *
1307
+ * Grouping is commonly used for generating summary reports, calculating totals, and performing other aggregate operations on data.
1308
+ *
1309
+ * This method allows you to specify raw-sql parameters for the query.
1310
+ * @param {string?} columns [column=id]
1311
+ * @return {this}
1312
+ */
1049
1313
  groupByRaw(...columns) {
1050
1314
  let groupBy = 'id';
1051
1315
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
@@ -1055,25 +1319,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1055
1319
  return column;
1056
1320
  }).join(', ');
1057
1321
  }
1058
- this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1322
+ this._setState('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1059
1323
  return this;
1060
1324
  }
1061
1325
  /**
1326
+ * The 'having' method is used to add a conditional clause to a database query that filters the result set after the GROUP BY operation has been applied.
1062
1327
  *
1328
+ * It is typically used in conjunction with the GROUP BY clause to filter aggregated data based on some condition.
1329
+ *
1330
+ * The having clause allows you to apply conditions to aggregated values, such as the result of COUNT, SUM, AVG, or other aggregate functions.
1063
1331
  * @param {string} condition
1064
1332
  * @return {this}
1065
1333
  */
1066
1334
  having(condition) {
1067
1335
  if (condition.includes(this.$constants('RAW'))) {
1068
1336
  condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants('RAW'), '');
1069
- this.$state.set('HAVING', `${this.$constants('HAVING')} ${condition}`);
1337
+ this._setState('HAVING', `${this.$constants('HAVING')} ${condition}`);
1070
1338
  return this;
1071
1339
  }
1072
- this.$state.set('HAVING', `${this.$constants('HAVING')} \`${condition}\``);
1340
+ this._setState('HAVING', `${this.$constants('HAVING')} \`${condition}\``);
1073
1341
  return this;
1074
1342
  }
1075
1343
  /**
1344
+ * The 'havingRaw' method is used to add a conditional clause to a database query that filters the result set after the GROUP BY operation has been applied.
1345
+ *
1346
+ * It is typically used in conjunction with the GROUP BY clause to filter aggregated data based on some condition.
1076
1347
  *
1348
+ * The having clause allows you to apply conditions to aggregated values, such as the result of COUNT, SUM, AVG, or other aggregate functions.
1349
+ *
1350
+ * This method allows you to specify raw-sql parameters for the query.
1077
1351
  * @param {string} condition
1078
1352
  * @return {this}
1079
1353
  */
@@ -1081,35 +1355,43 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1081
1355
  if (condition.includes(this.$constants('RAW'))) {
1082
1356
  condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants('RAW'), '');
1083
1357
  }
1084
- this.$state.set('HAVING', `${this.$constants('HAVING')} ${condition}`);
1358
+ this._setState('HAVING', `${this.$constants('HAVING')} ${condition}`);
1085
1359
  return this;
1086
1360
  }
1087
1361
  /**
1088
- * sort the result in random order.
1362
+ * The 'random' method is used to add a random ordering to a database query.
1363
+ *
1364
+ * This method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1089
1365
  * @return {this}
1090
1366
  */
1091
1367
  random() {
1092
- this.$state.set('ORDER_BY', `${this.$constants('ORDER_BY')} ${this.$constants('RAND')}`);
1368
+ this._setState('ORDER_BY', `${this.$constants('ORDER_BY')} ${this.$constants('RAND')}`);
1093
1369
  return this;
1094
1370
  }
1095
1371
  /**
1096
- * sort the result in random order.
1372
+ * The 'inRandom' method is used to add a random ordering to a database query.
1373
+ *
1374
+ * This method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1097
1375
  * @return {this}
1098
1376
  */
1099
1377
  inRandom() {
1100
1378
  return this.random();
1101
1379
  }
1102
1380
  /**
1103
- * limit data
1381
+ * The 'limit' method is used to limit the number of records returned by a database query.
1382
+ *
1383
+ * It allows you to specify the maximum number of rows to retrieve from the database table.
1104
1384
  * @param {number=} number [number=1]
1105
1385
  * @return {this}
1106
1386
  */
1107
1387
  limit(number = 1) {
1108
- this.$state.set('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1388
+ this._setState('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1109
1389
  return this;
1110
1390
  }
1111
1391
  /**
1112
- * limit data
1392
+ * The 'limit' method is used to limit the number of records returned by a database query.
1393
+ *
1394
+ * It allows you to specify the maximum number of rows to retrieve from the database table.
1113
1395
  * @param {number=} number [number=1]
1114
1396
  * @return {this}
1115
1397
  */
@@ -1117,18 +1399,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1117
1399
  return this.limit(number);
1118
1400
  }
1119
1401
  /**
1402
+ * The offset method is used to specify the number of records to skip from the beginning of a result set.
1120
1403
  *
1404
+ * It is often used in combination with the limit method for pagination or to skip a certain number of records when retrieving data from a database table.
1121
1405
  * @param {number=} number [number=1]
1122
1406
  * @return {this}
1123
1407
  */
1124
1408
  offset(number = 1) {
1125
- this.$state.set('OFFSET', `${this.$constants('OFFSET')} ${number}`);
1126
- if (!this.$state.get('LIMIT'))
1127
- this.$state.set('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1409
+ this._setState('OFFSET', `${this.$constants('OFFSET')} ${number}`);
1410
+ if (!this._getState('LIMIT'))
1411
+ this._setState('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1128
1412
  return this;
1129
1413
  }
1130
1414
  /**
1415
+ * The offset method is used to specify the number of records to skip from the beginning of a result set.
1131
1416
  *
1417
+ * It is often used in combination with the limit method for pagination or to skip a certain number of records when retrieving data from a database table.
1132
1418
  * @param {number=} number [number=1]
1133
1419
  * @return {this}
1134
1420
  */
@@ -1136,22 +1422,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1136
1422
  return this.offset(number);
1137
1423
  }
1138
1424
  /**
1139
- *
1425
+ * The 'hidden' method is used to specify which columns you want to hidden result.
1426
+ * It allows you to choose the specific columns that should be hidden in the result.
1140
1427
  * @param {...string} columns
1141
1428
  * @return {this} this
1142
1429
  */
1143
1430
  hidden(...columns) {
1144
- this.$state.set('HIDDEN', columns);
1431
+ this._setState('HIDDEN', columns);
1145
1432
  return this;
1146
1433
  }
1147
1434
  /**
1435
+ * The 'update' method is used to update existing records in a database table that are associated.
1148
1436
  *
1149
- * update data in the database
1437
+ * It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
1438
+ *
1439
+ * It allows you to remove one record that match certain criteria.
1150
1440
  * @param {object} data
1151
1441
  * @param {array?} updateNotExists options for except update some records in your ${data} using name column(s)
1152
1442
  * @return {this} this
1153
1443
  */
1154
1444
  update(data, updateNotExists = []) {
1445
+ this.limit(1);
1155
1446
  if (!Object.keys(data).length)
1156
1447
  throw new Error('This method must be required');
1157
1448
  if (updateNotExists.length) {
@@ -1166,21 +1457,58 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1166
1457
  }
1167
1458
  }
1168
1459
  const query = this._queryUpdate(data);
1169
- this.$state.set('UPDATE', [
1460
+ this._setState('UPDATE', [
1170
1461
  `${this.$constants('UPDATE')}`,
1171
- `${this.$state.get('TABLE_NAME')}`,
1462
+ `${this._getState('TABLE_NAME')}`,
1172
1463
  `${query}`
1173
1464
  ].join(' '));
1174
- this.$state.set('SAVE', 'UPDATE');
1465
+ this._setState('SAVE', 'UPDATE');
1175
1466
  return this;
1176
1467
  }
1177
1468
  /**
1469
+ * The 'updateMany' method is used to update existing records in a database table that are associated.
1470
+ *
1471
+ * It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
1178
1472
  *
1179
- * update record if data is empty in the database
1473
+ * It allows you to remove more records that match certain criteria.
1474
+ * @param {object} data
1475
+ * @param {array?} updateNotExists options for except update some records in your ${data} using name column(s)
1476
+ * @return {this} this
1477
+ */
1478
+ updateMany(data, updateNotExists = []) {
1479
+ if (!Object.keys(data).length)
1480
+ throw new Error('This method must be required');
1481
+ if (updateNotExists.length) {
1482
+ for (const c of updateNotExists) {
1483
+ for (const column in data) {
1484
+ if (c !== column)
1485
+ continue;
1486
+ const value = data[column];
1487
+ data[column] = this._updateHandler(column, value);
1488
+ break;
1489
+ }
1490
+ }
1491
+ }
1492
+ const query = this._queryUpdate(data);
1493
+ this._setState('UPDATE', [
1494
+ `${this.$constants('UPDATE')}`,
1495
+ `${this._getState('TABLE_NAME')}`,
1496
+ `${query}`
1497
+ ].join(' '));
1498
+ this._setState('SAVE', 'UPDATE');
1499
+ return this;
1500
+ }
1501
+ /**
1502
+ * The 'updateNotExists' method is used to update existing records in a database table that are associated.
1503
+ *
1504
+ * It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
1505
+ *
1506
+ * It method will be update record if data is empty or null in the column values
1180
1507
  * @param {object} data
1181
1508
  * @return {this} this
1182
1509
  */
1183
1510
  updateNotExists(data) {
1511
+ this.limit(1);
1184
1512
  if (!Object.keys(data).length)
1185
1513
  throw new Error('This method must be required');
1186
1514
  for (const column in data) {
@@ -1188,17 +1516,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1188
1516
  data[column] = this._updateHandler(column, value);
1189
1517
  }
1190
1518
  const query = this._queryUpdate(data);
1191
- this.$state.set('UPDATE', [
1519
+ this._setState('UPDATE', [
1192
1520
  `${this.$constants('UPDATE')}`,
1193
- `${this.$state.get('TABLE_NAME')}`,
1521
+ `${this._getState('TABLE_NAME')}`,
1194
1522
  `${query}`
1195
1523
  ].join(' '));
1196
- this.$state.set('SAVE', 'UPDATE');
1524
+ this._setState('SAVE', 'UPDATE');
1197
1525
  return this;
1198
1526
  }
1199
1527
  /**
1528
+ * The 'insert' method is used to insert a new record into a database table associated.
1200
1529
  *
1201
- * insert data into the database
1530
+ * It simplifies the process of creating and inserting records.
1202
1531
  * @param {object} data
1203
1532
  * @return {this} this
1204
1533
  */
@@ -1206,17 +1535,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1206
1535
  if (!Object.keys(data).length)
1207
1536
  throw new Error('This method must be required');
1208
1537
  const query = this._queryInsert(data);
1209
- this.$state.set('INSERT', [
1538
+ this._setState('INSERT', [
1210
1539
  `${this.$constants('INSERT')}`,
1211
- `${this.$state.get('TABLE_NAME')}`,
1540
+ `${this._getState('TABLE_NAME')}`,
1212
1541
  `${query}`
1213
1542
  ].join(' '));
1214
- this.$state.set('SAVE', 'INSERT');
1543
+ this._setState('SAVE', 'INSERT');
1215
1544
  return this;
1216
1545
  }
1217
1546
  /**
1547
+ * The 'create' method is used to insert a new record into a database table associated.
1218
1548
  *
1219
- * insert data into the database
1549
+ * It simplifies the process of creating and inserting records.
1220
1550
  * @param {object} data
1221
1551
  * @return {this} this
1222
1552
  */
@@ -1224,17 +1554,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1224
1554
  if (!Object.keys(data).length)
1225
1555
  throw new Error('This method must be required');
1226
1556
  const query = this._queryInsert(data);
1227
- this.$state.set('INSERT', [
1557
+ this._setState('INSERT', [
1228
1558
  `${this.$constants('INSERT')}`,
1229
- `${this.$state.get('TABLE_NAME')}`,
1559
+ `${this._getState('TABLE_NAME')}`,
1230
1560
  `${query}`
1231
1561
  ].join(' '));
1232
- this.$state.set('SAVE', 'INSERT');
1562
+ this._setState('SAVE', 'INSERT');
1233
1563
  return this;
1234
1564
  }
1235
1565
  /**
1566
+ * The 'createMultiple' method is used to insert a new records into a database table associated.
1236
1567
  *
1237
- * insert muliple data into the database
1568
+ * It simplifies the process of creating and inserting records with an array.
1238
1569
  * @param {array} data create multiple data
1239
1570
  * @return {this} this this
1240
1571
  */
@@ -1242,113 +1573,67 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1242
1573
  if (!Object.keys(data).length)
1243
1574
  throw new Error('This method must be required');
1244
1575
  const query = this._queryInsertMultiple(data);
1245
- this.$state.set('INSERT', [
1576
+ this._setState('INSERT', [
1246
1577
  `${this.$constants('INSERT')}`,
1247
- `${this.$state.get('TABLE_NAME')}`,
1578
+ `${this._getState('TABLE_NAME')}`,
1248
1579
  `${query}`
1249
1580
  ].join(' '));
1250
- this.$state.set('SAVE', 'INSERT_MULTIPLE');
1581
+ this._setState('SAVE', 'INSERT_MULTIPLE');
1251
1582
  return this;
1252
1583
  }
1253
1584
  /**
1585
+ * The 'createMany' method is used to insert a new records into a database table associated.
1254
1586
  *
1255
- * insert muliple data into the database
1587
+ * It simplifies the process of creating and inserting records with an array.
1256
1588
  * @param {array} data create multiple data
1257
1589
  * @return {this} this this
1258
1590
  */
1259
- insertMultiple(data) {
1260
- if (!Object.keys(data).length)
1261
- throw new Error('This method must be required');
1262
- const query = this._queryInsertMultiple(data);
1263
- this.$state.set('INSERT', [
1264
- `${this.$constants('INSERT')}`,
1265
- `${this.$state.get('TABLE_NAME')}`,
1266
- `${query}`
1267
- ].join(' '));
1268
- this.$state.set('SAVE', 'INSERT_MULTIPLE');
1269
- return this;
1591
+ createMany(data) {
1592
+ return this.createMultiple(data);
1270
1593
  }
1271
1594
  /**
1595
+ * The 'insertMultiple' method is used to insert a new records into a database table associated.
1272
1596
  *
1273
- * @return {string} return sql query
1274
- */
1275
- toString() {
1276
- const sql = this._buildQueryStatement();
1277
- if (this.$state.get('DEBUG'))
1278
- this.$utils.consoleDebug(sql);
1279
- return this.resultHandler(sql);
1280
- }
1281
- /**
1282
- *
1283
- * @return {string} return sql query
1284
- */
1285
- toSQL() {
1286
- return this.toString();
1287
- }
1288
- /**
1289
- *
1290
- * @return {string}
1291
- */
1292
- toRawSQL() {
1293
- return this.toString();
1294
- }
1295
- /**
1296
- *
1297
- * @param {boolean} debug debug sql statements
1597
+ * It simplifies the process of creating and inserting records with an array.
1598
+ * @param {array} data create multiple data
1298
1599
  * @return {this} this this
1299
1600
  */
1300
- debug(debug = true) {
1301
- this.$state.set('DEBUG', debug);
1302
- return this;
1601
+ insertMultiple(data) {
1602
+ return this.createMultiple(data);
1303
1603
  }
1304
1604
  /**
1605
+ * The 'insertMany' method is used to insert a new records into a database table associated.
1305
1606
  *
1306
- * @param {boolean} debug debug sql statements
1607
+ * It simplifies the process of creating and inserting records with an array.
1608
+ * @param {array} data create multiple data
1307
1609
  * @return {this} this this
1308
1610
  */
1309
- dd(debug = true) {
1310
- this.$state.set('DEBUG', debug);
1311
- return this;
1312
- }
1313
- /**
1314
- * hook function when execute returned result to callback function
1315
- * @param {Function} func function for callback result
1316
- * @return {this}
1317
- */
1318
- hook(func) {
1319
- if (typeof func !== "function")
1320
- throw new Error(`this '${func}' is not a function`);
1321
- this.$state.set('HOOKS', [...this.$state.get('HOOKS'), func]);
1322
- return this;
1323
- }
1324
- /**
1325
- * hook function when execute returned result to callback function
1326
- * @param {Function} func function for callback result
1327
- * @return {this}
1328
- */
1329
- before(func) {
1330
- if (typeof func !== "function")
1331
- throw new Error(`this '${func}' is not a function`);
1332
- this.$state.set('HOOKS', [...this.$state.get('HOOKS'), func]);
1333
- return this;
1611
+ insertMany(data) {
1612
+ return this.createMultiple(data);
1334
1613
  }
1335
1614
  /**
1615
+ * The 'createNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
1336
1616
  *
1617
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
1618
+ * but without raising an error or exception if duplicates are encountered.
1337
1619
  * @param {object} data create not exists data
1338
1620
  * @return {this} this this
1339
1621
  */
1340
1622
  createNotExists(data) {
1341
1623
  const query = this._queryInsert(data);
1342
- this.$state.set('INSERT', [
1624
+ this._setState('INSERT', [
1343
1625
  `${this.$constants('INSERT')}`,
1344
- `${this.$state.get('TABLE_NAME')}`,
1626
+ `${this._getState('TABLE_NAME')}`,
1345
1627
  `${query}`
1346
1628
  ].join(' '));
1347
- this.$state.set('SAVE', 'INSERT_NOT_EXISTS');
1629
+ this._setState('SAVE', 'INSERT_NOT_EXISTS');
1348
1630
  return this;
1349
1631
  }
1350
1632
  /**
1633
+ * The 'insertNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
1351
1634
  *
1635
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
1636
+ * but without raising an error or exception if duplicates are encountered.
1352
1637
  * @param {object} data insert not exists data
1353
1638
  * @return {this} this this
1354
1639
  */
@@ -1357,24 +1642,28 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1357
1642
  return this;
1358
1643
  }
1359
1644
  /**
1645
+ * The 'createOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
1360
1646
  *
1361
- * check data if exists if exists then return result. if not exists insert data
1647
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
1648
+ * but if exists should be returns a result.
1362
1649
  * @param {object} data insert data
1363
1650
  * @return {this} this this
1364
1651
  */
1365
1652
  createOrSelect(data) {
1366
1653
  const queryInsert = this._queryInsert(data);
1367
- this.$state.set('INSERT', [
1654
+ this._setState('INSERT', [
1368
1655
  `${this.$constants('INSERT')}`,
1369
- `${this.$state.get('TABLE_NAME')}`,
1656
+ `${this._getState('TABLE_NAME')}`,
1370
1657
  `${queryInsert}`
1371
1658
  ].join(' '));
1372
- this.$state.set('SAVE', 'INSERT_OR_SELECT');
1659
+ this._setState('SAVE', 'INSERT_OR_SELECT');
1373
1660
  return this;
1374
1661
  }
1375
1662
  /**
1663
+ * The 'insertOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
1376
1664
  *
1377
- * check data if exists if exists then update. if not exists insert
1665
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
1666
+ * but if exists should be returns a result.
1378
1667
  * @param {object} data insert or update data
1379
1668
  * @return {this} this this
1380
1669
  */
@@ -1383,30 +1672,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1383
1672
  return this;
1384
1673
  }
1385
1674
  /**
1675
+ * The 'updateOrCreate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
1386
1676
  *
1387
- * check data if exists if exists then update. if not exists insert
1677
+ * This method is particularly useful when you want to update a record based on certain conditions and,
1678
+ * if the record matching those conditions doesn't exist, create a new one with the provided data.
1388
1679
  * @param {object} data insert or update data
1389
1680
  * @return {this} this this
1390
1681
  */
1391
1682
  updateOrCreate(data) {
1683
+ this.limit(1);
1392
1684
  const queryUpdate = this._queryUpdate(data);
1393
1685
  const queryInsert = this._queryInsert(data);
1394
- this.$state.set('INSERT', [
1686
+ this._setState('INSERT', [
1395
1687
  `${this.$constants('INSERT')}`,
1396
- `${this.$state.get('TABLE_NAME')}`,
1688
+ `${this._getState('TABLE_NAME')}`,
1397
1689
  `${queryInsert}`
1398
1690
  ].join(' '));
1399
- this.$state.set('UPDATE', [
1691
+ this._setState('UPDATE', [
1400
1692
  `${this.$constants('UPDATE')}`,
1401
- `${this.$state.get('TABLE_NAME')}`,
1693
+ `${this._getState('TABLE_NAME')}`,
1402
1694
  `${queryUpdate}`
1403
1695
  ].join(' '));
1404
- this.$state.set('SAVE', 'UPDATE_OR_INSERT');
1696
+ this._setState('SAVE', 'UPDATE_OR_INSERT');
1405
1697
  return this;
1406
1698
  }
1407
1699
  /**
1700
+ * The 'updateOrInsert' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
1408
1701
  *
1409
- * check data if exists if exists then update. if not exists insert
1702
+ * This method is particularly useful when you want to update a record based on certain conditions and,
1703
+ * if the record matching those conditions doesn't exist, create a new one with the provided data.
1410
1704
  * @param {object} data insert or update data
1411
1705
  * @return {this} this this
1412
1706
  */
@@ -1415,8 +1709,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1415
1709
  return this;
1416
1710
  }
1417
1711
  /**
1712
+ * The 'insertOrUpdate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
1418
1713
  *
1419
- * check data if exists if exists then update. if not exists insert
1714
+ * This method is particularly useful when you want to update a record based on certain conditions and,
1715
+ * if the record matching those conditions doesn't exist, create a new one with the provided data.
1420
1716
  * @param {object} data insert or update data
1421
1717
  * @return {this} this this
1422
1718
  */
@@ -1426,7 +1722,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1426
1722
  }
1427
1723
  /**
1428
1724
  *
1429
- * check data if exists if exists then update. if not exists insert
1725
+ * The 'createOrUpdate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
1726
+ *
1727
+ * This method is particularly useful when you want to update a record based on certain conditions and,
1728
+ * if the record matching those conditions doesn't exist, create a new one with the provided data.
1430
1729
  * @param {object} data create or update data
1431
1730
  * @return {this} this this
1432
1731
  */
@@ -1434,6 +1733,91 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1434
1733
  this.updateOrCreate(data);
1435
1734
  return this;
1436
1735
  }
1736
+ /**
1737
+ * The 'toString' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
1738
+ *
1739
+ * This method is particularly useful for debugging and understanding the SQL queries generated by your application.
1740
+ * @return {string} return sql query
1741
+ */
1742
+ toString() {
1743
+ const sql = this._queryBuilder().any();
1744
+ return this._resultHandler(sql);
1745
+ }
1746
+ /**
1747
+ * The 'toSQL' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
1748
+ *
1749
+ * This method is particularly useful for debugging and understanding the SQL queries generated by your application.
1750
+ * @return {string} return sql query
1751
+ */
1752
+ toSQL() {
1753
+ return this.toString();
1754
+ }
1755
+ /**
1756
+ * The 'toRawSQL' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
1757
+ *
1758
+ * This method is particularly useful for debugging and understanding the SQL queries generated by your application.
1759
+ * @return {string}
1760
+ */
1761
+ toRawSQL() {
1762
+ return this.toString();
1763
+ }
1764
+ /**
1765
+ *
1766
+ * @return {string} return table name
1767
+ */
1768
+ getTableName() {
1769
+ return this._getState('TABLE_NAME').replace(/\`/g, '');
1770
+ }
1771
+ /**
1772
+ *
1773
+ * @return {string} return table.column
1774
+ */
1775
+ bindColumn(column) {
1776
+ if (!/\./.test(column))
1777
+ return `${this._getState('TABLE_NAME')}.\`${column}\``;
1778
+ const [table, c] = column.split('.');
1779
+ return `\`${table}\`.\`${c}\``;
1780
+ }
1781
+ /**
1782
+ * The 'debug' method is used to console.log raw SQL query that would be executed by a query builder
1783
+ * @param {boolean} debug debug sql statements
1784
+ * @return {this} this this
1785
+ */
1786
+ debug(debug = true) {
1787
+ this._setState('DEBUG', debug);
1788
+ return this;
1789
+ }
1790
+ /**
1791
+ * The 'dd' method is used to console.log raw SQL query that would be executed by a query builder
1792
+ * @param {boolean} debug debug sql statements
1793
+ * @return {this} this this
1794
+ */
1795
+ dd(debug = true) {
1796
+ this._setState('DEBUG', debug);
1797
+ return this;
1798
+ }
1799
+ /**
1800
+ * The 'hook' method is used function when execute returns a result to callback function
1801
+ * @param {Function} func function for callback result
1802
+ * @return {this}
1803
+ */
1804
+ hook(func) {
1805
+ if (typeof func !== "function")
1806
+ throw new Error(`this '${func}' is not a function`);
1807
+ this._setState('HOOKS', [...this._getState('HOOKS'), func]);
1808
+ return this;
1809
+ }
1810
+ /**
1811
+ * The 'before' method is used function when execute returns a result to callback function
1812
+ * @param {Function} func function for callback result
1813
+ * @return {this}
1814
+ */
1815
+ before(func) {
1816
+ if (typeof func !== "function")
1817
+ throw new Error(`this '${func}' is not a function`);
1818
+ this._setState('HOOKS', [...this._getState('HOOKS'), func]);
1819
+ return this;
1820
+ }
1437
1821
  /**
1438
1822
  *
1439
1823
  * @param {Object} options options for connection database with credentials
@@ -1501,111 +1885,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1501
1885
  return this;
1502
1886
  }
1503
1887
  /**
1504
- * exceptColumns for method except
1505
- * @return {promise<string>} string
1506
- */
1507
- exceptColumns() {
1508
- return __awaiter(this, void 0, void 0, function* () {
1509
- const excepts = this.$state.get('EXCEPTS');
1510
- const hasDot = excepts.some((except) => /\./.test(except));
1511
- const names = excepts.map((except) => {
1512
- if (/\./.test(except))
1513
- return except.split('.')[0];
1514
- return null;
1515
- }).filter((d) => d != null);
1516
- const tableNames = names.length ? [...new Set(names)] : [this.$state.get('TABLE_NAME')];
1517
- const removeExcepts = [];
1518
- for (const tableName of tableNames) {
1519
- const sql = [
1520
- `${this.$constants('SHOW')}`,
1521
- `${this.$constants('COLUMNS')}`,
1522
- `${this.$constants('FROM')}`,
1523
- `${tableName}`
1524
- ].join(' ');
1525
- const rawColumns = yield this.queryStatement(sql);
1526
- const columns = rawColumns.map((column) => column.Field);
1527
- const removeExcept = columns.filter((column) => {
1528
- return excepts.every((except) => {
1529
- if (/\./.test(except)) {
1530
- const [table, _] = except.split('.');
1531
- return except !== `${table}.${column}`;
1532
- }
1533
- return except !== column;
1534
- });
1535
- });
1536
- removeExcepts.push(hasDot ? removeExcept.map(r => `\`${tableName}\`.${r}`) : removeExcept);
1537
- }
1538
- return removeExcepts.flat();
1539
- });
1540
- }
1541
- /**
1542
- * handler query update for except some columns
1543
- * @param {string} column
1544
- * @param {string} value
1545
- * @return {string} string
1546
- */
1547
- _updateHandler(column, value) {
1548
- return DB_1.DB.raw([
1549
- this.$constants('CASE'),
1550
- this.$constants('WHEN'),
1551
- `(\`${column}\` = "" ${this.$constants('OR')} \`${column}\` ${this.$constants('IS_NULL')})`,
1552
- this.$constants('THEN'),
1553
- `"${value !== null && value !== void 0 ? value : ""}" ${this.$constants('ELSE')} \`${column}\``,
1554
- this.$constants('END')
1555
- ].join(' '));
1556
- }
1557
- /**
1888
+ * This 'rawQuery' method is used to execute sql statement
1558
1889
  *
1559
- * generate sql statements
1560
- * @return {string} string generated query string
1561
- */
1562
- _buildQueryStatement() {
1563
- let sql = [];
1564
- while (true) {
1565
- if (this.$state.get('INSERT')) {
1566
- sql = [
1567
- this.$state.get('INSERT')
1568
- ];
1569
- break;
1570
- }
1571
- if (this.$state.get('UPDATE')) {
1572
- sql = [
1573
- this.$state.get('UPDATE'),
1574
- this.$state.get('WHERE')
1575
- ];
1576
- break;
1577
- }
1578
- if (this.$state.get('DELETE')) {
1579
- sql = [
1580
- this.$state.get('DELETE'),
1581
- this.$state.get('WHERE')
1582
- ];
1583
- break;
1584
- }
1585
- sql = [
1586
- this.$state.get('SELECT'),
1587
- this.$state.get('FROM'),
1588
- this.$state.get('TABLE_NAME'),
1589
- this.$state.get('JOIN'),
1590
- this.$state.get('WHERE'),
1591
- this.$state.get('GROUP_BY'),
1592
- this.$state.get('HAVING'),
1593
- this.$state.get('ORDER_BY'),
1594
- this.$state.get('LIMIT'),
1595
- this.$state.get('OFFSET')
1596
- ];
1597
- break;
1598
- }
1599
- return sql.filter(s => s !== '' || s == null).join(' ');
1890
+ * @param {string} sql
1891
+ * @return {promise<any>}
1892
+ */
1893
+ rawQuery(sql) {
1894
+ return __awaiter(this, void 0, void 0, function* () {
1895
+ return yield this._queryStatement(sql);
1896
+ });
1600
1897
  }
1601
1898
  /**
1602
- * execute sql statements with raw sql query
1603
- * @param {string} sql sql execute return data
1899
+ * This 'query' method is used to execute sql statement
1900
+ *
1901
+ * @param {string} sql
1604
1902
  * @return {promise<any>}
1605
1903
  */
1606
- rawQuery(sql) {
1904
+ query(sql) {
1607
1905
  return __awaiter(this, void 0, void 0, function* () {
1608
- return yield this.queryStatement(sql);
1906
+ return yield this._queryStatement(sql);
1609
1907
  });
1610
1908
  }
1611
1909
  /**
@@ -1618,9 +1916,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1618
1916
  increment(column = 'id', value = 1) {
1619
1917
  return __awaiter(this, void 0, void 0, function* () {
1620
1918
  const query = `${this.$constants('SET')} ${column} = ${column} + ${value}`;
1621
- this.$state.set('UPDATE', [
1919
+ this._setState('UPDATE', [
1622
1920
  `${this.$constants('UPDATE')}`,
1623
- `${this.$state.get('TABLE_NAME')}`,
1921
+ `${this._getState('TABLE_NAME')}`,
1624
1922
  `${query}`
1625
1923
  ].join(' '));
1626
1924
  return yield this._update(true);
@@ -1636,49 +1934,60 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1636
1934
  decrement(column = 'id', value = 1) {
1637
1935
  return __awaiter(this, void 0, void 0, function* () {
1638
1936
  const query = `${this.$constants('SET')} ${column} = ${column} - ${value}`;
1639
- this.$state.set('UPDATE', [
1937
+ this._setState('UPDATE', [
1640
1938
  `${this.$constants('UPDATE')}`,
1641
- `${this.$state.get('TABLE_NAME')}`,
1939
+ `${this._getState('TABLE_NAME')}`,
1642
1940
  `${query}`
1643
1941
  ].join(' '));
1644
1942
  return yield this._update(true);
1645
1943
  });
1646
1944
  }
1945
+ version() {
1946
+ return __awaiter(this, void 0, void 0, function* () {
1947
+ const result = yield this._queryStatement(`${this.$constants('SELECT')} VERSION() as version`);
1948
+ return result[0].version;
1949
+ });
1950
+ }
1647
1951
  /**
1648
- * execute data without condition
1952
+ * The 'all' method is used to retrieve all records from a database table associated.
1953
+ *
1954
+ * It returns an array instances, ignore all condition.
1649
1955
  * @return {promise<any>}
1650
1956
  */
1651
1957
  all() {
1652
1958
  return __awaiter(this, void 0, void 0, function* () {
1653
- return yield this.queryStatement([
1959
+ return yield this._queryStatement([
1654
1960
  `${this.$constants('SELECT')}`,
1655
1961
  `*`,
1656
1962
  `${this.$constants('FROM')}`,
1657
- `${this.$state.get('TABLE_NAME')}`
1963
+ `${this._getState('TABLE_NAME')}`
1658
1964
  ].join(' '));
1659
1965
  });
1660
1966
  }
1661
1967
  /**
1968
+ * The 'find' method is used to retrieve a single record from a database table by its primary key.
1662
1969
  *
1663
- * execute data with where by primary key default = id
1970
+ * This method allows you to quickly fetch a specific record by specifying the primary key value, which is typically an integer id.
1664
1971
  * @param {number} id
1665
1972
  * @return {promise<any>}
1666
1973
  */
1667
1974
  find(id) {
1668
1975
  return __awaiter(this, void 0, void 0, function* () {
1669
- const result = yield this.queryStatement([
1976
+ const result = yield this._queryStatement([
1670
1977
  `${this.$constants('SELECT')}`,
1671
1978
  `*`,
1672
1979
  `${this.$constants('FROM')}`,
1673
- `${this.$state.get('TABLE_NAME')}`,
1980
+ `${this._getState('TABLE_NAME')}`,
1674
1981
  `${this.$constants('WHERE')} id = ${id}`
1675
1982
  ].join(' '));
1676
1983
  return (result === null || result === void 0 ? void 0 : result.shift()) || null;
1677
1984
  });
1678
1985
  }
1679
1986
  /**
1987
+ * The 'pagination' method is used to perform pagination on a set of database query results obtained through the Query Builder.
1680
1988
  *
1681
- * execute data page & limit
1989
+ * It allows you to split a large set of query results into smaller, more manageable pages,
1990
+ * making it easier to display data in a web application and improve user experience
1682
1991
  * @param {?object} paginationOptions
1683
1992
  * @param {number} paginationOptions.limit default 15
1684
1993
  * @param {number} paginationOptions.page default 1
@@ -1697,7 +2006,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1697
2006
  const nextPage = currentPage + 1;
1698
2007
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
1699
2008
  const offset = (page - 1) * limit;
1700
- let sql = this._buildQueryStatement();
2009
+ let sql = this._queryBuilder().select();
1701
2010
  if (!sql.includes(this.$constants('LIMIT'))) {
1702
2011
  sql = [
1703
2012
  `${sql}`,
@@ -1707,10 +2016,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1707
2016
  ].join(' ');
1708
2017
  }
1709
2018
  else {
1710
- sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} ${limit} ${this.$constants('OFFSET')} ${offset}`);
2019
+ sql = sql.replace(this._getState('LIMIT'), `${this.$constants('LIMIT')} ${limit} ${this.$constants('OFFSET')} ${offset}`);
1711
2020
  }
1712
- const result = yield this.queryStatement(sql);
1713
- if ((_a = this.$state.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
2021
+ const result = yield this._queryStatement(sql);
2022
+ if ((_a = this._getState('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
1714
2023
  this._hiddenColumn(result);
1715
2024
  if (!result.length)
1716
2025
  return {
@@ -1725,18 +2034,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1725
2034
  },
1726
2035
  data: []
1727
2036
  };
1728
- this.$state.set('SELECT', [
2037
+ const sqlCount = [
1729
2038
  `${this.$constants('SELECT')}`,
1730
2039
  `${this.$constants('COUNT')}(*)`,
1731
2040
  `${this.$constants('AS')} total`
1732
- ].join(' '));
2041
+ ].join(' ');
1733
2042
  const sqlTotal = [
1734
- this.$state.get('SELECT'),
1735
- this.$state.get('FROM'),
1736
- this.$state.get('TABLE_NAME'),
1737
- this.$state.get('WHERE'),
2043
+ sqlCount,
2044
+ this._getState('FROM'),
2045
+ this._getState('TABLE_NAME'),
2046
+ this._getState('WHERE'),
1738
2047
  ].join(' ');
1739
- const count = yield this.queryStatement(sqlTotal);
2048
+ const count = yield this._queryStatement(sqlTotal);
1740
2049
  const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
1741
2050
  let lastPage = Math.ceil(total / limit) || 0;
1742
2051
  lastPage = lastPage > 1 ? lastPage : 1;
@@ -1756,8 +2065,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1756
2065
  });
1757
2066
  }
1758
2067
  /**
2068
+ * The 'paginate' method is used to perform pagination on a set of database query results obtained through the Query Builder.
1759
2069
  *
1760
- * execute data useing page & limit
2070
+ * It allows you to split a large set of query results into smaller, more manageable pages,
2071
+ * making it easier to display data in a web application and improve user experience
1761
2072
  * @param {?object} paginationOptions
1762
2073
  * @param {number} paginationOptions.limit
1763
2074
  * @param {number} paginationOptions.page
@@ -1776,37 +2087,41 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1776
2087
  }
1777
2088
  /**
1778
2089
  *
1779
- * execute data return object | null
2090
+ * The 'first' method is used to retrieve the first record that matches the query conditions.
2091
+ *
2092
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
1780
2093
  * @return {promise<object | null>}
1781
2094
  */
1782
2095
  first() {
1783
2096
  var _a, _b;
1784
2097
  return __awaiter(this, void 0, void 0, function* () {
1785
- if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1786
- this.select(...yield this.exceptColumns());
2098
+ if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2099
+ this.select(...yield this._exceptColumns());
1787
2100
  this.limit(1);
1788
- let sql = this._buildQueryStatement();
1789
- const result = yield this.queryStatement(sql);
1790
- if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2101
+ const sql = this._queryBuilder().select();
2102
+ const result = yield this._queryStatement(sql);
2103
+ if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1791
2104
  this._hiddenColumn(result);
1792
- if (this.$state.get('PLUCK')) {
1793
- const pluck = this.$state.get('PLUCK');
2105
+ if (this._getState('PLUCK')) {
2106
+ const pluck = this._getState('PLUCK');
1794
2107
  const newData = result === null || result === void 0 ? void 0 : result.shift();
1795
2108
  const checkProperty = newData.hasOwnProperty(pluck);
1796
2109
  if (!checkProperty)
1797
2110
  throw new Error(`can't find property '${pluck}' of result`);
1798
2111
  const r = newData[pluck] || null;
1799
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), r);
2112
+ yield this.$utils.hookHandle(this._getState('HOOKS'), r);
1800
2113
  return r;
1801
2114
  }
1802
2115
  const r = (result === null || result === void 0 ? void 0 : result.shift()) || null;
1803
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), r);
1804
- return this.resultHandler(r);
2116
+ yield this.$utils.hookHandle(this._getState('HOOKS'), r);
2117
+ return this._resultHandler(r);
1805
2118
  });
1806
2119
  }
1807
2120
  /**
1808
2121
  *
1809
- * execute data return object | null
2122
+ * The 'findOne' method is used to retrieve the first record that matches the query conditions.
2123
+ *
2124
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
1810
2125
  * @return {promise<object | null>}
1811
2126
  */
1812
2127
  findOne() {
@@ -1815,25 +2130,28 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1815
2130
  });
1816
2131
  }
1817
2132
  /**
2133
+ * The 'firstOrError' method is used to retrieve the first record that matches the query conditions.
2134
+ *
2135
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
1818
2136
  *
1819
- * execute data return object | throw Error
2137
+ * If record is null, this method will throw an error
1820
2138
  * @return {promise<object | Error>}
1821
2139
  */
1822
2140
  firstOrError(message, options) {
1823
2141
  var _a, _b;
1824
2142
  return __awaiter(this, void 0, void 0, function* () {
1825
- if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1826
- this.select(...yield this.exceptColumns());
1827
- let sql = this._buildQueryStatement();
2143
+ if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2144
+ this.select(...yield this._exceptColumns());
2145
+ let sql = this._queryBuilder().select();
1828
2146
  if (!sql.includes(this.$constants('LIMIT')))
1829
2147
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
1830
2148
  else
1831
- sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
1832
- const result = yield this.queryStatement(sql);
1833
- if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2149
+ sql = sql.replace(this._getState('LIMIT'), `${this.$constants('LIMIT')} 1`);
2150
+ const result = yield this._queryStatement(sql);
2151
+ if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1834
2152
  this._hiddenColumn(result);
1835
- if (this.$state.get('PLUCK')) {
1836
- const pluck = this.$state.get('PLUCK');
2153
+ if (this._getState('PLUCK')) {
2154
+ const pluck = this._getState('PLUCK');
1837
2155
  const newData = result === null || result === void 0 ? void 0 : result.shift();
1838
2156
  const checkProperty = newData.hasOwnProperty(pluck);
1839
2157
  if (!checkProperty)
@@ -1844,8 +2162,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1844
2162
  throw { message, code: 400 };
1845
2163
  throw Object.assign({ message }, options);
1846
2164
  }
1847
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), data);
1848
- return this.resultHandler(data);
2165
+ yield this.$utils.hookHandle(this._getState('HOOKS'), data);
2166
+ return this._resultHandler(data);
1849
2167
  }
1850
2168
  const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
1851
2169
  if (data == null) {
@@ -1854,12 +2172,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1854
2172
  }
1855
2173
  throw Object.assign({ message }, options);
1856
2174
  }
1857
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), data);
1858
- return this.resultHandler(data);
2175
+ yield this.$utils.hookHandle(this._getState('HOOKS'), data);
2176
+ return this._resultHandler(data);
1859
2177
  });
1860
2178
  }
1861
2179
  /**
2180
+ * The 'findOneOrError' method is used to retrieve the first record that matches the query conditions.
1862
2181
  *
2182
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
2183
+ *
2184
+ * If record is null, this method will throw an error
1863
2185
  * execute data return object | null
1864
2186
  * @return {promise<object | null>}
1865
2187
  */
@@ -1869,46 +2191,48 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1869
2191
  });
1870
2192
  }
1871
2193
  /**
2194
+ * The 'get' method is used to execute a database query and retrieve the result set that matches the query conditions.
1872
2195
  *
1873
- * execute data return Array
2196
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
1874
2197
  * @return {promise<any[]>}
1875
2198
  */
1876
2199
  get() {
1877
2200
  var _a, _b;
1878
2201
  return __awaiter(this, void 0, void 0, function* () {
1879
- if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
1880
- this.select(...yield this.exceptColumns());
1881
- const sql = this._buildQueryStatement();
1882
- const result = yield this.queryStatement(sql);
1883
- if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2202
+ if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2203
+ this.select(...yield this._exceptColumns());
2204
+ const sql = this._queryBuilder().select();
2205
+ const result = yield this._queryStatement(sql);
2206
+ if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1884
2207
  this._hiddenColumn(result);
1885
- if (this.$state.get('CHUNK')) {
2208
+ if (this._getState('CHUNK')) {
1886
2209
  const data = result.reduce((resultArray, item, index) => {
1887
- const chunkIndex = Math.floor(index / this.$state.get('CHUNK'));
2210
+ const chunkIndex = Math.floor(index / this._getState('CHUNK'));
1888
2211
  if (!resultArray[chunkIndex])
1889
2212
  resultArray[chunkIndex] = [];
1890
2213
  resultArray[chunkIndex].push(item);
1891
2214
  return resultArray;
1892
2215
  }, []);
1893
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), data || []);
1894
- return this.resultHandler(data || []);
2216
+ yield this.$utils.hookHandle(this._getState('HOOKS'), data || []);
2217
+ return this._resultHandler(data || []);
1895
2218
  }
1896
- if (this.$state.get('PLUCK')) {
1897
- const pluck = this.$state.get('PLUCK');
2219
+ if (this._getState('PLUCK')) {
2220
+ const pluck = this._getState('PLUCK');
1898
2221
  const newData = result.map((d) => d[pluck]);
1899
2222
  if (newData.every((d) => d == null)) {
1900
2223
  throw new Error(`can't find property '${pluck}' of result`);
1901
2224
  }
1902
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), newData || []);
1903
- return this.resultHandler(newData || []);
2225
+ yield this.$utils.hookHandle(this._getState('HOOKS'), newData || []);
2226
+ return this._resultHandler(newData || []);
1904
2227
  }
1905
- yield this.$utils.hookHandle(this.$state.get('HOOKS'), result || []);
1906
- return this.resultHandler(result || []);
2228
+ yield this.$utils.hookHandle(this._getState('HOOKS'), result || []);
2229
+ return this._resultHandler(result || []);
1907
2230
  });
1908
2231
  }
1909
2232
  /**
2233
+ * The 'findMany' method is used to execute a database query and retrieve the result set that matches the query conditions.
1910
2234
  *
1911
- * execute data return Array
2235
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
1912
2236
  * @return {promise<any[]>}
1913
2237
  */
1914
2238
  findMany() {
@@ -1918,196 +2242,239 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1918
2242
  }
1919
2243
  /**
1920
2244
  *
1921
- * execute data return json of result
2245
+ * The 'toJSON' method is used to execute a database query and retrieve the result set that matches the query conditions.
2246
+ *
2247
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
2248
+ *
2249
+ * It returns a JSON formatted
1922
2250
  * @return {promise<string>}
1923
2251
  */
1924
2252
  toJSON() {
1925
2253
  return __awaiter(this, void 0, void 0, function* () {
1926
- const sql = this._buildQueryStatement();
1927
- const result = yield this.queryStatement(sql);
1928
- if (this.$state.get('HIDDEN').length)
2254
+ const sql = this._queryBuilder().select();
2255
+ const result = yield this._queryStatement(sql);
2256
+ if (this._getState('HIDDEN').length)
1929
2257
  this._hiddenColumn(result);
1930
- return this.resultHandler(JSON.stringify(result));
2258
+ return this._resultHandler(JSON.stringify(result));
1931
2259
  });
1932
2260
  }
1933
2261
  /**
2262
+ * The 'toArray' method is used to execute a database query and retrieve the result set that matches the query conditions.
2263
+ *
2264
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
1934
2265
  *
1935
- * execute data return array of results
2266
+ * It returns an array formatted
1936
2267
  * @param {string=} column [column=id]
1937
2268
  * @return {promise<Array>}
1938
2269
  */
1939
2270
  toArray(column = 'id') {
1940
2271
  return __awaiter(this, void 0, void 0, function* () {
1941
2272
  this.selectRaw(column);
1942
- const sql = this._buildQueryStatement();
1943
- const result = yield this.queryStatement(sql);
2273
+ const sql = this._queryBuilder().select();
2274
+ const result = yield this._queryStatement(sql);
1944
2275
  const toArray = result.map((data) => data[column]);
1945
- return this.resultHandler(toArray);
2276
+ return this._resultHandler(toArray);
1946
2277
  });
1947
2278
  }
1948
2279
  /**
2280
+ * The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
1949
2281
  *
1950
- * execute data return result is exists
2282
+ * It returns a boolean value indicating whether there are any matching records.
1951
2283
  * @return {promise<boolean>}
1952
2284
  */
1953
2285
  exists() {
1954
2286
  var _a;
1955
2287
  return __awaiter(this, void 0, void 0, function* () {
1956
2288
  this.limit(1);
1957
- const sql = this._buildQueryStatement();
1958
- const result = yield this.queryStatement([
2289
+ const sql = this._queryBuilder().select();
2290
+ const result = yield this._queryStatement([
1959
2291
  `${this.$constants('SELECT')}`,
1960
2292
  `${this.$constants('EXISTS')}`,
1961
2293
  `(${sql})`,
1962
2294
  `${this.$constants('AS')} \`aggregate\``
1963
2295
  ].join(' '));
1964
- return Boolean(this.resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || false));
2296
+ return Boolean(this._resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || false));
1965
2297
  });
1966
2298
  }
1967
2299
  /**
2300
+ * The 'count' method is used to retrieve the total number of records that match the specified query conditions.
1968
2301
  *
1969
- * execute data return number of results
2302
+ * It returns an integer representing the count of records.
1970
2303
  * @param {string=} column [column=id]
1971
2304
  * @return {promise<number>}
1972
2305
  */
1973
2306
  count(column = 'id') {
1974
- var _a;
1975
2307
  return __awaiter(this, void 0, void 0, function* () {
1976
- const distinct = this.$state.get('DISTINCT');
1977
- column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2308
+ const distinct = this._getState('DISTINCT');
2309
+ column = distinct
2310
+ ? `${this.$constants('DISTINCT')} \`${column}\``
2311
+ : `\`${column}\``;
1978
2312
  this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`aggregate\``);
1979
- const sql = this._buildQueryStatement();
1980
- const result = yield this.queryStatement(sql);
1981
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2313
+ const sql = this._queryBuilder().select();
2314
+ const result = yield this._queryStatement(sql);
2315
+ return Number(this._resultHandler(result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) || 0));
1982
2316
  });
1983
2317
  }
1984
2318
  /**
2319
+ * The 'avg' method is used to calculate the average value of a numeric column in a database table.
1985
2320
  *
1986
- * execute data return average of results
2321
+ * It calculates the mean value of the specified column for all records that match the query conditions and returns the result as a floating-point number.
1987
2322
  * @param {string=} column [column=id]
1988
2323
  * @return {promise<number>}
1989
2324
  */
1990
2325
  avg(column = 'id') {
1991
- var _a;
1992
2326
  return __awaiter(this, void 0, void 0, function* () {
1993
- const distinct = this.$state.get('DISTINCT');
2327
+ const distinct = this._getState('DISTINCT');
1994
2328
  column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
1995
2329
  this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} \`aggregate\``);
1996
- const sql = this._buildQueryStatement();
1997
- const result = yield this.queryStatement(sql);
1998
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2330
+ const sql = this._queryBuilder().select();
2331
+ const result = yield this._queryStatement(sql);
2332
+ return Number(this._resultHandler((result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) || 0) / result.length));
1999
2333
  });
2000
2334
  }
2001
2335
  /**
2336
+ * The 'sum' method is used to calculate the sum of values in a numeric column of a database table.
2002
2337
  *
2003
- * execute data return summary of results
2338
+ * It computes the total of the specified column's values for all records that match the query conditions and returns the result as a numeric value.
2004
2339
  * @param {string=} column [column=id]
2005
2340
  * @return {promise<number>}
2006
2341
  */
2007
2342
  sum(column = 'id') {
2008
- var _a;
2009
2343
  return __awaiter(this, void 0, void 0, function* () {
2010
- const distinct = this.$state.get('DISTINCT');
2344
+ const distinct = this._getState('DISTINCT');
2011
2345
  column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2012
2346
  this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2013
- const sql = this._buildQueryStatement();
2014
- const result = yield this.queryStatement(sql);
2015
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2347
+ const sql = this._queryBuilder().select();
2348
+ const result = yield this._queryStatement(sql);
2349
+ return Number(this._resultHandler(result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) || 0));
2016
2350
  });
2017
2351
  }
2018
2352
  /**
2353
+ * The 'max' method is used to retrieve the maximum value of a numeric column in a database table.
2019
2354
  *
2020
- * execute data return maximum of results
2355
+ * It finds the highest value in the specified column among all records that match the query conditions and returns that value.
2021
2356
  * @param {string=} column [column=id]
2022
2357
  * @return {promise<number>}
2023
2358
  */
2024
2359
  max(column = 'id') {
2025
2360
  var _a;
2026
2361
  return __awaiter(this, void 0, void 0, function* () {
2027
- const distinct = this.$state.get('DISTINCT');
2362
+ const distinct = this._getState('DISTINCT');
2028
2363
  column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2029
2364
  this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2030
- const sql = this._buildQueryStatement();
2031
- const result = yield this.queryStatement(sql);
2032
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2365
+ const sql = this._queryBuilder().select();
2366
+ const result = yield this._queryStatement(sql);
2367
+ return Number(this._resultHandler(((_a = result.sort((a, b) => (b === null || b === void 0 ? void 0 : b.aggregate) - (a === null || a === void 0 ? void 0 : a.aggregate))[0]) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2033
2368
  });
2034
2369
  }
2035
2370
  /**
2371
+ * The 'min' method is used to retrieve the minimum (lowest) value of a numeric column in a database table.
2036
2372
  *
2037
- * execute data return minimum of results
2373
+ * It finds the smallest value in the specified column among all records that match the query conditions and returns that value.
2038
2374
  * @param {string=} column [column=id]
2039
2375
  * @return {promise<number>}
2040
2376
  */
2041
2377
  min(column = 'id') {
2042
2378
  var _a;
2043
2379
  return __awaiter(this, void 0, void 0, function* () {
2044
- const distinct = this.$state.get('DISTINCT');
2380
+ const distinct = this._getState('DISTINCT');
2045
2381
  column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2046
2382
  this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2047
- const sql = this._buildQueryStatement();
2048
- const result = yield this.queryStatement(sql);
2049
- return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2383
+ const sql = this._queryBuilder().select();
2384
+ const result = yield this._queryStatement(sql);
2385
+ return Number(this._resultHandler(((_a = result.sort((a, b) => (a === null || a === void 0 ? void 0 : a.aggregate) - (b === null || b === void 0 ? void 0 : b.aggregate))[0]) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
2050
2386
  });
2051
2387
  }
2052
2388
  /**
2389
+ * The 'delete' method is used to delete records from a database table based on the specified query conditions.
2053
2390
  *
2054
- * delete data from database
2391
+ * It allows you to remove one record that match certain criteria.
2055
2392
  * @return {promise<boolean>}
2056
2393
  */
2057
2394
  delete() {
2058
2395
  var _a;
2059
2396
  return __awaiter(this, void 0, void 0, function* () {
2060
- if (!this.$state.get('WHERE')) {
2397
+ if (!this._getState('WHERE')) {
2398
+ throw new Error("can't delete without where condition");
2399
+ }
2400
+ this.limit(1);
2401
+ this._setState('DELETE', [
2402
+ `${this.$constants('DELETE')}`,
2403
+ `${this._getState('FROM')}`,
2404
+ `${this._getState('TABLE_NAME')}`,
2405
+ ].join(' '));
2406
+ const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2407
+ if (result)
2408
+ return Boolean(this._resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
2409
+ return Boolean(this._resultHandler(false));
2410
+ });
2411
+ }
2412
+ /**
2413
+ * The 'deleteMany' method is used to delete records from a database table based on the specified query conditions.
2414
+ *
2415
+ * It allows you to remove more records that match certain criteria.
2416
+ * @return {promise<boolean>}
2417
+ */
2418
+ deleteMany() {
2419
+ var _a;
2420
+ return __awaiter(this, void 0, void 0, function* () {
2421
+ if (!this._getState('WHERE')) {
2061
2422
  throw new Error("can't delete without where condition");
2062
2423
  }
2063
- this.$state.set('DELETE', [
2424
+ this._setState('DELETE', [
2064
2425
  `${this.$constants('DELETE')}`,
2065
- `${this.$state.get('FROM')}`,
2066
- `${this.$state.get('TABLE_NAME')}`,
2067
- `${this.$state.get('WHERE')}`
2426
+ `${this._getState('FROM')}`,
2427
+ `${this._getState('TABLE_NAME')}`,
2068
2428
  ].join(' '));
2069
- const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
2429
+ const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2070
2430
  if (result)
2071
- return Boolean(this.resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
2072
- return Boolean(this.resultHandler(false));
2431
+ return Boolean(this._resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
2432
+ return Boolean(this._resultHandler(false));
2073
2433
  });
2074
2434
  }
2075
2435
  /**
2076
2436
  *
2077
- * delete data from database ignore where condition
2437
+ * The 'delete' method is used to delete records from a database table based on the specified query conditions.
2438
+ *
2439
+ * It allows you to remove one or more records that match certain criteria.
2440
+ *
2441
+ * This method should be ignore the soft delete
2078
2442
  * @return {promise<boolean>}
2079
2443
  */
2080
2444
  forceDelete() {
2081
2445
  var _a, _b;
2082
2446
  return __awaiter(this, void 0, void 0, function* () {
2083
- this.$state.set('DELETE', [
2447
+ this._setState('DELETE', [
2084
2448
  `${this.$constants('DELETE')}`,
2085
- `${this.$state.get('FROM')}`,
2086
- `${this.$state.get('TABLE_NAME')}`,
2087
- `${this.$state.get('WHERE')}`
2449
+ `${this._getState('FROM')}`,
2450
+ `${this._getState('TABLE_NAME')}`,
2451
+ `${this._getState('WHERE')}`
2088
2452
  ].join(' '));
2089
- const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
2453
+ const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2090
2454
  if (result)
2091
- return Boolean(this.resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
2092
- return Boolean(this.resultHandler((_b = !!result) !== null && _b !== void 0 ? _b : false));
2455
+ return Boolean(this._resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
2456
+ return Boolean(this._resultHandler((_b = !!result) !== null && _b !== void 0 ? _b : false));
2093
2457
  });
2094
2458
  }
2095
2459
  /**
2460
+ * The 'getGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
2096
2461
  *
2097
- * execute data return Array *grouping results in column
2462
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
2463
+ *
2464
+ * It returns record an Array-Object key by column *grouping results in column
2098
2465
  * @param {string} column
2099
2466
  * @return {promise<Array>}
2100
2467
  */
2101
2468
  getGroupBy(column) {
2102
2469
  return __awaiter(this, void 0, void 0, function* () {
2103
- this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${column}`);
2104
- this.$state.set('SELECT', [
2105
- `${this.$state.get('SELECT')}`,
2106
- `, ${this.$constants('GROUP_CONCAT')}(id)`,
2107
- `${this.$constants('AS')} data`
2470
+ this.selectRaw([
2471
+ `\`${column}\`,`,
2472
+ `${this.$constants('GROUP_CONCAT')}(id)`,
2473
+ `${this.$constants('AS')} \`data\``
2108
2474
  ].join(' '));
2109
- const sql = this._buildQueryStatement();
2110
- const results = yield this.queryStatement(sql);
2475
+ this.groupBy(column);
2476
+ const sql = this._queryBuilder().select();
2477
+ const results = yield this._queryStatement(sql);
2111
2478
  let data = [];
2112
2479
  results.forEach((result) => {
2113
2480
  var _a, _b;
@@ -2118,12 +2485,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2118
2485
  `${this.$constants('SELECT')}`,
2119
2486
  `*`,
2120
2487
  `${this.$constants('FROM')}`,
2121
- `${this.$state.get('TABLE_NAME')}`,
2488
+ `${this._getState('TABLE_NAME')}`,
2122
2489
  `${this.$constants('WHERE')} id`,
2123
2490
  `${this.$constants('IN')}`,
2124
2491
  `(${data.map((a) => `\'${a}\'`).join(',') || ['0']})`
2125
2492
  ].join(' ');
2126
- const groups = yield this.queryStatement(sqlGroups);
2493
+ const groups = yield this._queryStatement(sqlGroups);
2127
2494
  const resultData = results.map((result) => {
2128
2495
  const id = result[column];
2129
2496
  const newData = groups.filter((data) => data[column] === id);
@@ -2132,12 +2499,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2132
2499
  data: newData
2133
2500
  });
2134
2501
  });
2135
- return this.resultHandler(!resultData);
2502
+ return this._resultHandler(resultData);
2136
2503
  });
2137
2504
  }
2138
2505
  /**
2139
2506
  *
2140
- * execute data return grouping results by index
2507
+ * The 'getGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
2508
+ *
2509
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
2510
+ *
2511
+ * It returns record an Array-Object key by column *grouping results in column
2141
2512
  * @param {string} column
2142
2513
  * @return {promise<Array>}
2143
2514
  */
@@ -2147,7 +2518,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2147
2518
  });
2148
2519
  }
2149
2520
  /**
2150
- * execute data when save *action [insert , update]
2521
+ * The 'save' method is used to persist a new 'Model' or new 'DB' instance or update an existing model instance in the database.
2522
+ *
2523
+ * It's a versatile method that can be used in various scenarios, depending on whether you're working with a new or existing record.
2151
2524
  * @return {Promise<any>} promise
2152
2525
  */
2153
2526
  save() {
@@ -2155,44 +2528,62 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2155
2528
  const attributes = this.$attributes;
2156
2529
  if (attributes != null) {
2157
2530
  while (true) {
2158
- if (this.$state.get('WHERE')) {
2531
+ if (this._getState('WHERE')) {
2159
2532
  const query = this._queryUpdate(attributes);
2160
- this.$state.set('UPDATE', [
2533
+ this._setState('UPDATE', [
2161
2534
  `${this.$constants('UPDATE')}`,
2162
- `${this.$state.get('TABLE_NAME')}`,
2535
+ `${this._getState('TABLE_NAME')}`,
2163
2536
  `${query}`
2164
2537
  ].join(' '));
2165
- this.$state.set('SAVE', 'UPDATE');
2538
+ this._setState('SAVE', 'UPDATE');
2166
2539
  break;
2167
2540
  }
2168
2541
  const query = this._queryInsert(attributes);
2169
- this.$state.set('INSERT', [
2542
+ this._setState('INSERT', [
2170
2543
  `${this.$constants('INSERT')}`,
2171
- `${this.$state.get('TABLE_NAME')}`,
2544
+ `${this._getState('TABLE_NAME')}`,
2172
2545
  `${query}`
2173
2546
  ].join(' '));
2174
- this.$state.set('SAVE', 'INSERT');
2547
+ this._setState('SAVE', 'INSERT');
2175
2548
  break;
2176
2549
  }
2177
2550
  }
2178
- switch (this.$state.get('SAVE')) {
2551
+ switch (this._getState('SAVE')) {
2179
2552
  case 'INSERT_MULTIPLE': return yield this._insertMultiple();
2180
2553
  case 'INSERT': return yield this._insert();
2181
2554
  case 'UPDATE': return yield this._update();
2182
2555
  case 'INSERT_NOT_EXISTS': return yield this._insertNotExists();
2183
2556
  case 'UPDATE_OR_INSERT': return yield this._updateOrInsert();
2184
2557
  case 'INSERT_OR_SELECT': return yield this._insertOrSelect();
2185
- default: throw new Error(`unknown this [${this.$state.get('SAVE')}]`);
2558
+ default: throw new Error(`unknown this [${this._getState('SAVE')}]`);
2186
2559
  }
2187
2560
  });
2188
2561
  }
2189
2562
  /**
2563
+ * The 'showTables' method is used to show schema table.
2564
+ *
2565
+ * @return {Promise<Array>}
2566
+ */
2567
+ showTables() {
2568
+ return __awaiter(this, void 0, void 0, function* () {
2569
+ const sql = [
2570
+ `${this.$constants('SHOW')}`,
2571
+ `${this.$constants('TABLES')}`
2572
+ ].join(' ');
2573
+ const results = yield this._queryStatement(sql);
2574
+ return results
2575
+ .map(table => String(Object.values(table)[0]))
2576
+ .filter(d => d != null || d !== '');
2577
+ });
2578
+ }
2579
+ /**
2580
+ *
2581
+ * The 'showColumns' method is used to show columns table.
2190
2582
  *
2191
- * show columns in table
2192
2583
  * @param {string=} table table name
2193
2584
  * @return {Promise<Array>}
2194
2585
  */
2195
- showColumns(table = this.$state.get('TABLE_NAME')) {
2586
+ showColumns(table = this._getState('TABLE_NAME')) {
2196
2587
  return __awaiter(this, void 0, void 0, function* () {
2197
2588
  const sql = [
2198
2589
  `${this.$constants('SHOW')}`,
@@ -2200,18 +2591,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2200
2591
  `${this.$constants('FROM')}`,
2201
2592
  `\`${table.replace(/\`/g, '')}\``
2202
2593
  ].join(' ');
2203
- const rawColumns = yield this.queryStatement(sql);
2594
+ const rawColumns = yield this._queryStatement(sql);
2204
2595
  const columns = rawColumns.map((column) => column.Field);
2205
2596
  return columns;
2206
2597
  });
2207
2598
  }
2208
2599
  /**
2600
+ * The 'showSchema' method is used to show schema table.
2209
2601
  *
2210
- * show schemas in table
2211
2602
  * @param {string=} table [table= current table name]
2212
2603
  * @return {Promise<Array>}
2213
2604
  */
2214
- showSchemas(table = this.$state.get('TABLE_NAME')) {
2605
+ showSchema(table = this._getState('TABLE_NAME')) {
2215
2606
  return __awaiter(this, void 0, void 0, function* () {
2216
2607
  const sql = [
2217
2608
  `${this.$constants('SHOW')}`,
@@ -2219,7 +2610,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2219
2610
  `${this.$constants('FROM')}`,
2220
2611
  `\`${table.replace(/\`/g, '')}\``
2221
2612
  ].join(' ');
2222
- const raws = yield this.queryStatement(sql);
2613
+ const raws = yield this._queryStatement(sql);
2223
2614
  return raws.map((r) => {
2224
2615
  const schema = [];
2225
2616
  schema.push(`${r.Field}`);
@@ -2247,28 +2638,24 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2247
2638
  });
2248
2639
  }
2249
2640
  /**
2641
+ * The 'showSchemas' method is used to show schema table.
2250
2642
  *
2251
- * get schema from table
2252
- * @return {this} this this
2643
+ * @param {string=} table [table= current table name]
2644
+ * @return {Promise<Array>}
2253
2645
  */
2254
- getSchema() {
2646
+ showSchemas(table = this._getState('TABLE_NAME')) {
2255
2647
  return __awaiter(this, void 0, void 0, function* () {
2256
- const sql = [
2257
- `${this.$constants('SHOW')}`,
2258
- `${this.$constants('COLUMNS')}`,
2259
- `${this.$constants('FROM')}`,
2260
- `\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
2261
- ].join(' ');
2262
- return yield this.queryStatement(sql);
2648
+ return this.showSchema(table);
2263
2649
  });
2264
2650
  }
2265
2651
  /**
2266
2652
  *
2267
- * show values in table
2653
+ * The 'showValues' method is used to show values table.
2654
+ *
2268
2655
  * @param {string=} table table name
2269
2656
  * @return {Promise<Array>}
2270
2657
  */
2271
- showValues(table = this.$state.get('TABLE_NAME')) {
2658
+ showValues(table = this._getState('TABLE_NAME')) {
2272
2659
  return __awaiter(this, void 0, void 0, function* () {
2273
2660
  const sql = [
2274
2661
  `${this.$constants('SELECT')}`,
@@ -2276,39 +2663,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2276
2663
  `${this.$constants('FROM')}`,
2277
2664
  `\`${table.replace(/\`/g, '')}\``
2278
2665
  ].join(' ');
2279
- const raw = yield this.queryStatement(sql);
2666
+ const raw = yield this._queryStatement(sql);
2280
2667
  const values = raw.map((value) => {
2281
2668
  return `(${Object.values(value).map((v) => {
2669
+ if (typeof v === 'object' && v != null && !Array.isArray(v))
2670
+ return `'${JSON.stringify(v)}'`;
2282
2671
  return v == null ? 'NULL' : `'${v}'`;
2283
- }).join(',')})`;
2672
+ }).join(', ')})`;
2284
2673
  });
2285
2674
  return values;
2286
2675
  });
2287
2676
  }
2288
- /**
2289
- *
2290
- * backup this database intro new database same server or to another server
2291
- * @param {Object} backupOptions
2292
- * @param {string} backup.database
2293
- * @param {object?} backup.to
2294
- * @param {string} backup.to.host
2295
- * @param {number} backup.to.port
2296
- * @param {string} backup.to.database
2297
- * @param {string} backup.to.username
2298
- * @param {string} backup.to.password
2299
-
2300
- * @return {Promise<boolean>}
2301
- */
2302
- backup({ database, to }) {
2677
+ _backup({ tables, database }) {
2303
2678
  return __awaiter(this, void 0, void 0, function* () {
2304
- const tables = yield this.queryStatement('SHOW TABLES');
2305
- let backup = [];
2306
- for (const t of tables) {
2307
- const table = String(Object.values(t).shift());
2308
- const schemas = yield this.showSchemas(table);
2679
+ const backup = [];
2680
+ for (const table of tables) {
2681
+ const schemas = yield this.showSchema(table);
2309
2682
  const createTableSQL = [
2310
2683
  `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2311
- `\`${database}.${table}\``,
2684
+ `\`${database}\`.\`${table}\``,
2312
2685
  `(${schemas.join(',')})`,
2313
2686
  `${this.$constants('ENGINE')}`,
2314
2687
  ];
@@ -2318,27 +2691,42 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2318
2691
  const columns = yield this.showColumns(table);
2319
2692
  valueSQL = [
2320
2693
  `${this.$constants('INSERT')}`,
2321
- `\`${database}.${table}\``,
2694
+ `\`${database}\`.\`${table}\``,
2322
2695
  `(${columns.map((column) => `\`${column}\``).join(',')})`,
2323
2696
  `${this.$constants('VALUES')} ${values.join(',')}`
2324
2697
  ];
2325
2698
  }
2326
- backup = [
2327
- ...backup,
2328
- {
2329
- table: createTableSQL.join(' '),
2330
- values: valueSQL.join(' '),
2331
- }
2332
- ];
2699
+ backup.push({
2700
+ table: createTableSQL.join(' '),
2701
+ values: valueSQL.join(' '),
2702
+ });
2333
2703
  }
2704
+ return backup;
2705
+ });
2706
+ }
2707
+ /**
2708
+ *
2709
+ * backup this database intro new database same server or to another server
2710
+ * @param {Object} backupOptions
2711
+ * @param {string} backup.database clone current 'db' in connection to this database
2712
+ * @param {object?} backup.to
2713
+ * @param {string} backup.to.host
2714
+ * @param {number} backup.to.port
2715
+ * @param {string} backup.to.username
2716
+ * @param {string} backup.to.password
2717
+ * @return {Promise<boolean>}
2718
+ */
2719
+ backup({ database, to }) {
2720
+ return __awaiter(this, void 0, void 0, function* () {
2721
+ const tables = yield this.showTables();
2722
+ const backup = yield this._backup({ tables, database });
2334
2723
  if (to != null && Object.keys(to).length)
2335
- this.connection(to);
2336
- yield this.queryStatement(`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\``);
2724
+ this.connection(Object.assign(Object.assign({}, to), { database }));
2725
+ yield this._queryStatement(`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\``);
2337
2726
  for (const b of backup) {
2338
- yield this.queryStatement(b.table);
2339
- if (b.values) {
2340
- yield this.queryStatement(b.values);
2341
- }
2727
+ yield this._queryStatement(b.table);
2728
+ if (b.values)
2729
+ yield this._queryStatement(b.values);
2342
2730
  }
2343
2731
  return true;
2344
2732
  });
@@ -2358,39 +2746,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2358
2746
  * @return {Promise<boolean>}
2359
2747
  */
2360
2748
  backupToFile({ filePath, database, connection }) {
2361
- var _a, _b;
2749
+ var _a;
2362
2750
  return __awaiter(this, void 0, void 0, function* () {
2363
- const tables = yield this.queryStatement(this.$constants('SHOW_TABLES'));
2364
- let backup = [];
2365
- for (const t of tables) {
2366
- const table = String((_a = Object.values(t)) === null || _a === void 0 ? void 0 : _a.shift());
2367
- const schemas = yield this.showSchemas(table);
2368
- const createTableSQL = [
2369
- `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2370
- `\`${table}\``,
2371
- `(${schemas.join(',')})`,
2372
- `${this.$constants('ENGINE')};`,
2373
- ];
2374
- const values = yield this.showValues(table);
2375
- let valueSQL = [];
2376
- if (values.length) {
2377
- const columns = yield this.showColumns(table);
2378
- valueSQL = [
2379
- `${this.$constants('INSERT')}`,
2380
- `\`${table}\``,
2381
- `(${columns.map((column) => `\`${column}\``).join(',')})`,
2382
- `${this.$constants('VALUES')} ${values.join(',')};`
2383
- ];
2384
- }
2385
- backup = [
2386
- ...backup,
2387
- {
2388
- table: createTableSQL.join(' '),
2389
- values: valueSQL.join(' ')
2390
- }
2391
- ];
2392
- }
2393
- if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
2751
+ const tables = yield this.showTables();
2752
+ const backup = (yield this._backup({ tables, database }))
2753
+ .map(b => {
2754
+ return {
2755
+ table: (0, sql_formatter_1.format)(b.table, {
2756
+ language: 'spark',
2757
+ tabWidth: 2,
2758
+ linesBetweenQueries: 1,
2759
+ }) + "\n",
2760
+ values: b.values !== '' ? b.values + "\n" : ""
2761
+ };
2762
+ });
2763
+ if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
2394
2764
  this.connection(connection);
2395
2765
  let sql = [
2396
2766
  `SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";`,
@@ -2405,11 +2775,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2405
2775
  sql = [...sql, b.values];
2406
2776
  }
2407
2777
  }
2408
- fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)([...sql, 'COMMIT;'].join('\n'), {
2409
- language: 'spark',
2410
- tabWidth: 2,
2411
- linesBetweenQueries: 1,
2412
- }));
2778
+ fs_1.default.writeFileSync(filePath, [...sql, 'COMMIT;'].join('\n'));
2413
2779
  return;
2414
2780
  });
2415
2781
  }
@@ -2428,28 +2794,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2428
2794
  * @return {Promise<boolean>}
2429
2795
  */
2430
2796
  backupSchemaToFile({ filePath, database, connection }) {
2431
- var _a, _b;
2797
+ var _a;
2432
2798
  return __awaiter(this, void 0, void 0, function* () {
2433
- const tables = yield this.queryStatement(this.$constants('SHOW_TABLES'));
2434
- let backup = [];
2435
- for (const t of tables) {
2436
- const table = String((_a = Object.values(t)) === null || _a === void 0 ? void 0 : _a.shift());
2437
- const schemas = yield this.showSchemas(table);
2438
- const createTableSQL = [
2439
- `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2440
- `\`${table}\``,
2441
- `(${schemas.join(',')})`,
2442
- `${this.$constants('ENGINE')};`,
2443
- ];
2444
- backup = [
2445
- ...backup,
2446
- {
2447
- table: createTableSQL.join(' ')
2448
- }
2449
- ];
2450
- }
2451
- if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
2799
+ if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
2452
2800
  this.connection(connection);
2801
+ const tables = yield this.showTables();
2802
+ const backup = (yield this._backup({ tables, database }))
2803
+ .map(b => {
2804
+ return {
2805
+ table: (0, sql_formatter_1.format)(b.table, {
2806
+ language: 'spark',
2807
+ tabWidth: 2,
2808
+ linesBetweenQueries: 1,
2809
+ }) + "\n"
2810
+ };
2811
+ });
2453
2812
  let sql = [
2454
2813
  `SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";`,
2455
2814
  `START TRANSACTION;`,
@@ -2457,14 +2816,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2457
2816
  `${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\`;`,
2458
2817
  `USE \`${database}\`;`
2459
2818
  ];
2460
- for (const b of backup) {
2819
+ for (const b of backup)
2461
2820
  sql = [...sql, b.table];
2462
- }
2463
- fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)([...sql, 'COMMIT;'].join('\n'), {
2464
- language: 'spark',
2465
- tabWidth: 2,
2466
- linesBetweenQueries: 1,
2467
- }));
2821
+ fs_1.default.writeFileSync(filePath, [...sql, 'COMMIT;'].join('\n'));
2468
2822
  return;
2469
2823
  });
2470
2824
  }
@@ -2485,7 +2839,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2485
2839
  backupTableToFile({ filePath, table, connection }) {
2486
2840
  var _a;
2487
2841
  return __awaiter(this, void 0, void 0, function* () {
2488
- const schemas = yield this.showSchemas(table);
2842
+ if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
2843
+ this.connection(connection);
2844
+ const schemas = yield this.showSchema(table);
2489
2845
  const createTableSQL = [
2490
2846
  `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2491
2847
  `\`${table}\``,
@@ -2503,14 +2859,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2503
2859
  `${this.$constants('VALUES')} ${values.join(',')};`
2504
2860
  ];
2505
2861
  }
2506
- const sql = [createTableSQL.join(' '), valueSQL.join(' ')];
2507
- if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
2508
- this.connection(connection);
2509
- fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)(sql.join('\n'), {
2510
- language: 'spark',
2511
- tabWidth: 2,
2512
- linesBetweenQueries: 1,
2513
- }));
2862
+ const sql = [
2863
+ (0, sql_formatter_1.format)(createTableSQL.join(' '), {
2864
+ language: 'mysql',
2865
+ tabWidth: 2,
2866
+ linesBetweenQueries: 1,
2867
+ }) + "\n",
2868
+ valueSQL.join(' ')
2869
+ ];
2870
+ fs_1.default.writeFileSync(filePath, [...sql, 'COMMIT;'].join('\n'));
2514
2871
  return;
2515
2872
  });
2516
2873
  }
@@ -2531,7 +2888,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2531
2888
  backupTableSchemaToFile({ filePath, table, connection }) {
2532
2889
  var _a;
2533
2890
  return __awaiter(this, void 0, void 0, function* () {
2534
- const schemas = yield this.showSchemas(table);
2891
+ const schemas = yield this.showSchema(table);
2535
2892
  const createTableSQL = [
2536
2893
  `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2537
2894
  `\`${table}\``,
@@ -2551,7 +2908,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2551
2908
  }
2552
2909
  /**
2553
2910
  *
2554
- * fake data
2911
+ * The 'faker' method is used to insert a new records into a database table associated.
2912
+ *
2913
+ * It simplifies the process of creating and inserting records.
2555
2914
  * @param {number} rows number of rows
2556
2915
  * @return {promise<any>}
2557
2916
  */
@@ -2563,11 +2922,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2563
2922
  `${this.$constants('SHOW')}`,
2564
2923
  `${this.$constants('FIELDS')}`,
2565
2924
  `${this.$constants('FROM')}`,
2566
- `${this.$state.get('TABLE_NAME')}`
2925
+ `${this._getState('TABLE_NAME')}`
2567
2926
  ].join(' ');
2568
- const fields = yield this.queryStatement(sql);
2927
+ const fields = yield this._queryStatement(sql);
2569
2928
  for (let row = 0; row < rows; row++) {
2570
- if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
2929
+ if (this._getState('TABLE_NAME') === '' || this._getState('TABLE_NAME') == null) {
2571
2930
  throw new Error("Unknow this table name");
2572
2931
  }
2573
2932
  let columnAndValue = {};
@@ -2582,12 +2941,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2582
2941
  data = [...data, columnAndValue];
2583
2942
  }
2584
2943
  const query = this._queryInsertMultiple(data);
2585
- this.$state.set('INSERT', [
2944
+ this._setState('INSERT', [
2586
2945
  `${this.$constants('INSERT')}`,
2587
- `${this.$state.get('TABLE_NAME')}`,
2946
+ `${this._getState('TABLE_NAME')}`,
2588
2947
  `${query}`
2589
2948
  ].join(' '));
2590
- this.$state.set('SAVE', 'INSERT_MULTIPLE');
2949
+ this._setState('SAVE', 'INSERT_MULTIPLE');
2591
2950
  return this.save();
2592
2951
  });
2593
2952
  }
@@ -2600,9 +2959,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2600
2959
  return __awaiter(this, void 0, void 0, function* () {
2601
2960
  const sql = [
2602
2961
  `${this.$constants('TRUNCATE_TABLE')}`,
2603
- `${this.$state.get('TABLE_NAME')}`
2962
+ `${this._getState('TABLE_NAME')}`
2604
2963
  ].join(' ');
2605
- yield this.queryStatement(sql);
2964
+ yield this._queryStatement(sql);
2606
2965
  return true;
2607
2966
  });
2608
2967
  }
@@ -2615,118 +2974,233 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2615
2974
  return __awaiter(this, void 0, void 0, function* () {
2616
2975
  const sql = [
2617
2976
  `${this.$constants('DROP_TABLE')}`,
2618
- `${this.$state.get('TABLE_NAME')}`
2977
+ `${this._getState('TABLE_NAME')}`
2619
2978
  ].join(' ');
2620
- yield this.queryStatement(sql);
2979
+ yield this._queryStatement(sql);
2621
2980
  return true;
2622
2981
  });
2623
2982
  }
2624
- resultHandler(data) {
2625
- this.$state.set('RESULT', data);
2983
+ _exceptColumns() {
2984
+ return __awaiter(this, void 0, void 0, function* () {
2985
+ const excepts = this._getState('EXCEPTS');
2986
+ const hasDot = excepts.some((except) => /\./.test(except));
2987
+ const names = excepts.map((except) => {
2988
+ if (/\./.test(except))
2989
+ return except.split('.')[0];
2990
+ return null;
2991
+ }).filter((d) => d != null);
2992
+ const tableNames = names.length ? [...new Set(names)] : [this._getState('TABLE_NAME')];
2993
+ const removeExcepts = [];
2994
+ for (const tableName of tableNames) {
2995
+ const sql = [
2996
+ `${this.$constants('SHOW')}`,
2997
+ `${this.$constants('COLUMNS')}`,
2998
+ `${this.$constants('FROM')}`,
2999
+ `${tableName}`
3000
+ ].join(' ');
3001
+ const rawColumns = yield this._queryStatement(sql);
3002
+ const columns = rawColumns.map((column) => column.Field);
3003
+ const removeExcept = columns.filter((column) => {
3004
+ return excepts.every((except) => {
3005
+ if (/\./.test(except)) {
3006
+ const [table, _] = except.split('.');
3007
+ return except !== `${table}.${column}`;
3008
+ }
3009
+ return except !== column;
3010
+ });
3011
+ });
3012
+ removeExcepts.push(hasDot ? removeExcept.map(r => `\`${tableName}\`.${r}`) : removeExcept);
3013
+ }
3014
+ return removeExcepts.flat();
3015
+ });
3016
+ }
3017
+ _updateHandler(column, value) {
3018
+ return DB_1.DB.raw([
3019
+ this.$constants('CASE'),
3020
+ this.$constants('WHEN'),
3021
+ `(\`${column}\` = "" ${this.$constants('OR')} \`${column}\` ${this.$constants('IS_NULL')})`,
3022
+ this.$constants('THEN'),
3023
+ `"${value !== null && value !== void 0 ? value : ""}" ${this.$constants('ELSE')} \`${column}\``,
3024
+ this.$constants('END')
3025
+ ].join(' '));
3026
+ }
3027
+ copyBuilder(instance, options) {
3028
+ if (!(instance instanceof Builder))
3029
+ throw new Error('Value is not a instanceof Builder');
3030
+ const copy = Object.fromEntries(instance.$state.get());
3031
+ const newInstance = new Builder();
3032
+ newInstance.$state.clone(copy);
3033
+ newInstance.$state.set('SAVE', '');
3034
+ if ((options === null || options === void 0 ? void 0 : options.insert) == null)
3035
+ newInstance.$state.set('INSERT', '');
3036
+ if ((options === null || options === void 0 ? void 0 : options.update) == null)
3037
+ newInstance.$state.set('UPDATE', '');
3038
+ if ((options === null || options === void 0 ? void 0 : options.delete) == null)
3039
+ newInstance.$state.set('DELETE', '');
3040
+ if ((options === null || options === void 0 ? void 0 : options.where) == null)
3041
+ newInstance.$state.set('WHERE', '');
3042
+ if ((options === null || options === void 0 ? void 0 : options.limit) == null)
3043
+ newInstance.$state.set('LIMIT', '');
3044
+ if ((options === null || options === void 0 ? void 0 : options.offset) == null)
3045
+ newInstance.$state.set('OFFSET', '');
3046
+ if ((options === null || options === void 0 ? void 0 : options.groupBy) == null)
3047
+ newInstance.$state.set('GROUP_BY', '');
3048
+ if ((options === null || options === void 0 ? void 0 : options.select) == null)
3049
+ newInstance.$state.set('SELECT', '');
3050
+ if ((options === null || options === void 0 ? void 0 : options.join) == null)
3051
+ newInstance.$state.set('JOIN', '');
3052
+ return newInstance;
3053
+ }
3054
+ _queryBuilder() {
3055
+ return this._buildQueryStatement();
3056
+ }
3057
+ _buildQueryStatement() {
3058
+ const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ');
3059
+ const bindSelect = (values) => {
3060
+ if (!values.length) {
3061
+ if (!this._getState('DISTINCT'))
3062
+ return `${this.$constants('SELECT')} *`;
3063
+ return `${this.$constants('SELECT')} ${this.$constants('DISTINCT')} *`;
3064
+ }
3065
+ const findIndex = values.indexOf('*');
3066
+ if (findIndex > -1) {
3067
+ const removed = values.splice(findIndex, 1);
3068
+ values.unshift(removed[0]);
3069
+ }
3070
+ return `${this.$constants('SELECT')} ${values.join(', ')}`;
3071
+ };
3072
+ const bindJoin = (values) => {
3073
+ if (!values.length)
3074
+ return '';
3075
+ return values.join(' ');
3076
+ };
3077
+ const select = () => buildSQL([
3078
+ bindSelect(this.$state.get('SELECT')),
3079
+ this.$state.get('FROM'),
3080
+ this.$state.get('TABLE_NAME'),
3081
+ bindJoin(this.$state.get('JOIN')),
3082
+ this.$state.get('WHERE'),
3083
+ this.$state.get('GROUP_BY'),
3084
+ this.$state.get('HAVING'),
3085
+ this.$state.get('ORDER_BY'),
3086
+ this.$state.get('LIMIT'),
3087
+ this.$state.get('OFFSET')
3088
+ ]);
3089
+ const insert = () => buildSQL([this.$state.get('INSERT')]);
3090
+ const update = () => buildSQL([this.$state.get('UPDATE'), this.$state.get('WHERE'), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3091
+ const remove = () => buildSQL([this.$state.get('DELETE'), this.$state.get('WHERE'), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3092
+ return {
3093
+ select,
3094
+ insert,
3095
+ update,
3096
+ delete: remove,
3097
+ any: () => {
3098
+ if (this.$state.get('INSERT'))
3099
+ return insert();
3100
+ if (this.$state.get('UPDATE'))
3101
+ return update();
3102
+ if (this.$state.get('DELETE'))
3103
+ return remove();
3104
+ return select();
3105
+ }
3106
+ };
3107
+ }
3108
+ _getState(key) {
3109
+ return this.$state.get(key.toLocaleUpperCase());
3110
+ }
3111
+ _setState(key, value) {
3112
+ return this.$state.set(key, value);
3113
+ }
3114
+ _resultHandler(data) {
3115
+ this._setState('RESULT', data);
2626
3116
  this.$state.resetState();
2627
3117
  this.$logger.reset();
2628
3118
  return data;
2629
3119
  }
2630
- /**
2631
- *
2632
- * @param {string} tableAndLocalKey
2633
- * @param {string?} tableAndForeignKey
2634
- * @return {this}
2635
- */
2636
3120
  whereReference(tableAndLocalKey, tableAndForeignKey) {
2637
- this.$state.set('WHERE', [
3121
+ this._setState('WHERE', [
2638
3122
  this._queryWhereIsExists()
2639
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
3123
+ ? `${this._getState('WHERE')} ${this.$constants('AND')}`
2640
3124
  : `${this.$constants('WHERE')}`,
2641
3125
  `${tableAndLocalKey} = ${tableAndForeignKey}`
2642
3126
  ].join(' '));
2643
3127
  return this;
2644
3128
  }
3129
+ _queryStatement(sql) {
3130
+ return __awaiter(this, void 0, void 0, function* () {
3131
+ if (this._getState('DEBUG'))
3132
+ this.$utils.consoleDebug(sql);
3133
+ const result = yield this.$pool.query(sql);
3134
+ return result;
3135
+ });
3136
+ }
3137
+ _actionStatement({ sql, returnId = false }) {
3138
+ return __awaiter(this, void 0, void 0, function* () {
3139
+ if (this._getState('DEBUG'))
3140
+ this.$utils.consoleDebug(sql);
3141
+ if (returnId) {
3142
+ const result = yield this.$pool.query(sql);
3143
+ return [result.affectedRows, result.insertId];
3144
+ }
3145
+ const { affectedRows: result } = yield this.$pool.query(sql);
3146
+ return result;
3147
+ });
3148
+ }
2645
3149
  _queryWhereIsExists() {
2646
3150
  var _a;
2647
- return ((_a = String(this.$state.get('WHERE'))) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
2648
- }
2649
- _bindTableAndColumnInQueryWhere(column) {
2650
- if (!/\./.test(column))
2651
- return `${this.$state.get('TABLE_NAME')}.\`${column}\``;
2652
- const [table, c] = column.split('.');
2653
- return `\`${table}\`.\`${c}\``;
3151
+ return ((_a = String(this._getState('WHERE'))) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
2654
3152
  }
2655
3153
  _insertNotExists() {
2656
3154
  return __awaiter(this, void 0, void 0, function* () {
2657
- if (!this.$state.get('WHERE'))
3155
+ if (!this._getState('WHERE'))
2658
3156
  throw new Error("Can't insert not exists without where condition");
2659
3157
  let sql = [
2660
3158
  `${this.$constants('SELECT')}`,
2661
3159
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
2662
3160
  `*`,
2663
- `${this.$state.get('FROM')}`,
2664
- `${this.$state.get('TABLE_NAME')}`,
2665
- `${this.$state.get('WHERE')}`,
3161
+ `${this._getState('FROM')}`,
3162
+ `${this._getState('TABLE_NAME')}`,
3163
+ `${this._getState('WHERE')}`,
2666
3164
  `${this.$constants('LIMIT')} 1)`,
2667
3165
  `${this.$constants('AS')} 'exists'`
2668
3166
  ].join(' ');
2669
- const [{ exists: result }] = yield this.queryStatement(sql);
3167
+ const [{ exists: result }] = yield this._queryStatement(sql);
2670
3168
  const check = !!Number.parseInt(result);
2671
3169
  switch (check) {
2672
3170
  case false: {
2673
- const [result, id] = yield this.actionStatement({
2674
- sql: this.$state.get('INSERT'),
3171
+ const [result, id] = yield this._actionStatement({
3172
+ sql: this._getState('INSERT'),
2675
3173
  returnId: true
2676
3174
  });
2677
- if (this.$state.get('VOID') || !result)
2678
- return this.resultHandler(undefined);
2679
- const sql = [
2680
- `${this.$state.get('SELECT')}`,
2681
- `${this.$state.get('FROM')}`,
2682
- `${this.$state.get('TABLE_NAME')}`,
2683
- `${this.$constants('WHERE')} id = ${id}`
2684
- ].join(' ');
2685
- const data = yield this.queryStatement(sql);
2686
- return this.resultHandler((data === null || data === void 0 ? void 0 : data.shift()) || null);
3175
+ if (this._getState('VOID') || !result)
3176
+ return this._resultHandler(undefined);
3177
+ const sql = new Builder()
3178
+ .copyBuilder(this, { select: true })
3179
+ .where('id', id)
3180
+ .toString();
3181
+ const data = yield this._queryStatement(sql);
3182
+ return this._resultHandler((data === null || data === void 0 ? void 0 : data.shift()) || null);
2687
3183
  }
2688
- default: return this.resultHandler(null);
2689
- }
2690
- });
2691
- }
2692
- queryStatement(sql) {
2693
- return __awaiter(this, void 0, void 0, function* () {
2694
- if (this.$state.get('DEBUG'))
2695
- this.$utils.consoleDebug(sql);
2696
- const result = yield this.$pool.query(sql);
2697
- return result;
2698
- });
2699
- }
2700
- actionStatement({ sql, returnId = false }) {
2701
- return __awaiter(this, void 0, void 0, function* () {
2702
- if (this.$state.get('DEBUG'))
2703
- this.$utils.consoleDebug(sql);
2704
- if (returnId) {
2705
- const result = yield this.$pool.query(sql);
2706
- return [result.affectedRows, result.insertId];
3184
+ default: return this._resultHandler(null);
2707
3185
  }
2708
- const { affectedRows: result } = yield this.$pool.query(sql);
2709
- return result;
2710
3186
  });
2711
3187
  }
2712
3188
  _insert() {
2713
3189
  return __awaiter(this, void 0, void 0, function* () {
2714
- const [result, id] = yield this.actionStatement({
2715
- sql: this.$state.get('INSERT'),
3190
+ const [result, id] = yield this._actionStatement({
3191
+ sql: this._getState('INSERT'),
2716
3192
  returnId: true
2717
3193
  });
2718
- if (this.$state.get('VOID') || !result)
2719
- return this.resultHandler(undefined);
2720
- const sql = [
2721
- `${this.$state.get('SELECT')}`,
2722
- `${this.$state.get('FROM')}`,
2723
- `${this.$state.get('TABLE_NAME')}`,
2724
- `${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = ${id}`
2725
- ].join(' ');
2726
- const data = yield this.queryStatement(sql);
3194
+ if (this._getState('VOID') || !result)
3195
+ return this._resultHandler(undefined);
3196
+ const sql = new Builder()
3197
+ .copyBuilder(this, { select: true })
3198
+ .where('id', id)
3199
+ .toString();
3200
+ const data = yield this._queryStatement(sql);
2727
3201
  const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
2728
- this.$state.set('RESULT', resultData);
2729
- return this.resultHandler(resultData);
3202
+ this._setState('RESULT', resultData);
3203
+ return this._resultHandler(resultData);
2730
3204
  });
2731
3205
  }
2732
3206
  _checkValueHasRaw(value) {
@@ -2736,176 +3210,152 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2736
3210
  }
2737
3211
  _insertMultiple() {
2738
3212
  return __awaiter(this, void 0, void 0, function* () {
2739
- const [result, id] = yield this.actionStatement({
2740
- sql: this.$state.get('INSERT'),
3213
+ const [result, id] = yield this._actionStatement({
3214
+ sql: this._queryBuilder().insert(),
2741
3215
  returnId: true
2742
3216
  });
2743
- if (this.$state.get('VOID') || !result)
2744
- return this.resultHandler(undefined);
3217
+ if (this._getState('VOID') || !result)
3218
+ return this._resultHandler(undefined);
2745
3219
  const arrayId = [...Array(result)].map((_, i) => i + id);
2746
- const sql = [
2747
- `${this.$state.get('SELECT')}`,
2748
- `${this.$state.get('FROM')}`,
2749
- `${this.$state.get('TABLE_NAME')}`,
2750
- `${this.$constants('WHERE')} id`,
2751
- `${this.$constants('IN')} (${arrayId})`
2752
- ].join(' ');
2753
- const data = yield this.queryStatement(sql);
3220
+ const sql = new Builder()
3221
+ .copyBuilder(this, { select: true })
3222
+ .whereIn('id', arrayId)
3223
+ .toString();
3224
+ const data = yield this._queryStatement(sql);
2754
3225
  const resultData = data || null;
2755
- return this.resultHandler(resultData);
3226
+ return this._resultHandler(resultData);
2756
3227
  });
2757
3228
  }
2758
3229
  _insertOrSelect() {
2759
3230
  return __awaiter(this, void 0, void 0, function* () {
2760
- if (!this.$state.get('WHERE')) {
3231
+ if (!this._getState('WHERE')) {
2761
3232
  throw new Error("Can't create or select without where condition");
2762
3233
  }
2763
3234
  let sql = [
2764
3235
  `${this.$constants('SELECT')}`,
2765
3236
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
2766
3237
  `*`,
2767
- `${this.$state.get('FROM')}`,
2768
- `${this.$state.get('TABLE_NAME')}`,
2769
- `${this.$state.get('WHERE')}`,
3238
+ `${this._getState('FROM')}`,
3239
+ `${this._getState('TABLE_NAME')}`,
3240
+ `${this._getState('WHERE')}`,
2770
3241
  `${this.$constants('LIMIT')} 1)`,
2771
3242
  `${this.$constants('AS')} 'exists'`
2772
3243
  ].join(' ');
2773
3244
  let check = false;
2774
- const [{ exists: result }] = yield this.queryStatement(sql);
3245
+ const [{ exists: result }] = yield this._queryStatement(sql);
2775
3246
  check = !!parseInt(result);
2776
3247
  switch (check) {
2777
3248
  case false: {
2778
- const [result, id] = yield this.actionStatement({
2779
- sql: this.$state.get('INSERT'),
3249
+ const [result, id] = yield this._actionStatement({
3250
+ sql: this._queryBuilder().insert(),
2780
3251
  returnId: true
2781
3252
  });
2782
- if (this.$state.get('VOID') || !result)
2783
- return this.resultHandler(undefined);
2784
- const sql = [
2785
- `${this.$state.get('SELECT')}`,
2786
- `${this.$state.get('FROM')}`,
2787
- `${this.$state.get('TABLE_NAME')}`,
2788
- `${this.$constants('WHERE')} id = ${id}`
2789
- ].join(' ');
2790
- const data = yield this.queryStatement(sql);
3253
+ if (this._getState('VOID') || !result)
3254
+ return this._resultHandler(undefined);
3255
+ const sql = new Builder()
3256
+ .copyBuilder(this, { select: true })
3257
+ .where('id', id)
3258
+ .toString();
3259
+ const data = yield this._queryStatement(sql);
2791
3260
  const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'insert' }) || null;
2792
- return this.resultHandler(resultData);
3261
+ return this._resultHandler(resultData);
2793
3262
  }
2794
3263
  case true: {
2795
- const data = yield this.queryStatement([
2796
- `${this.$state.get('SELECT')}`,
2797
- `${this.$state.get('FROM')}`,
2798
- `${this.$state.get('TABLE_NAME')}`,
2799
- `${this.$state.get('WHERE')}`
2800
- ].join(' '));
3264
+ const sql = new Builder()
3265
+ .copyBuilder(this, { select: true, where: true })
3266
+ .toString();
3267
+ const data = yield this._queryStatement(sql);
2801
3268
  if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
2802
3269
  for (const val of data) {
2803
3270
  val.$action = 'select';
2804
3271
  }
2805
- return this.resultHandler(data || []);
3272
+ return this._resultHandler(data || []);
2806
3273
  }
2807
3274
  const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'select' }) || null;
2808
- return this.resultHandler(resultData);
3275
+ return this._resultHandler(resultData);
2809
3276
  }
2810
3277
  default: {
2811
- return this.resultHandler(null);
3278
+ return this._resultHandler(null);
2812
3279
  }
2813
3280
  }
2814
3281
  });
2815
3282
  }
2816
3283
  _updateOrInsert() {
2817
3284
  return __awaiter(this, void 0, void 0, function* () {
2818
- if (!this.$state.get('WHERE')) {
3285
+ if (!this._getState('WHERE')) {
2819
3286
  throw new Error("Can't update or insert without where condition");
2820
3287
  }
2821
3288
  let sql = [
2822
3289
  `${this.$constants('SELECT')}`,
2823
3290
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
2824
3291
  `*`,
2825
- `${this.$state.get('FROM')}`,
2826
- `${this.$state.get('TABLE_NAME')}`,
2827
- `${this.$state.get('WHERE')}`,
3292
+ `${this._getState('FROM')}`,
3293
+ `${this._getState('TABLE_NAME')}`,
3294
+ `${this._getState('WHERE')}`,
2828
3295
  `${this.$constants('LIMIT')} 1)`,
2829
3296
  `${this.$constants('AS')} 'exists'`
2830
3297
  ].join(' ');
2831
3298
  let check = false;
2832
- const [{ exists: result }] = yield this.queryStatement(sql);
3299
+ const [{ exists: result }] = yield this._queryStatement(sql);
2833
3300
  check = !!parseInt(result);
2834
3301
  switch (check) {
2835
3302
  case false: {
2836
- const [result, id] = yield this.actionStatement({
2837
- sql: this.$state.get('INSERT'),
3303
+ const [result, id] = yield this._actionStatement({
3304
+ sql: this._queryBuilder().insert(),
2838
3305
  returnId: true
2839
3306
  });
2840
- if (this.$state.get('VOID') || !result)
2841
- return this.resultHandler(undefined);
2842
- const sql = [
2843
- `${this.$state.get('SELECT')}`,
2844
- `${this.$state.get('FROM')}`,
2845
- `${this.$state.get('TABLE_NAME')}`,
2846
- `${this.$constants('WHERE')} id = ${id}`
2847
- ].join(' ');
2848
- const data = yield this.queryStatement(sql);
3307
+ if (this._getState('VOID') || !result)
3308
+ return this._resultHandler(undefined);
3309
+ const sql = new Builder()
3310
+ .copyBuilder(this, { select: true })
3311
+ .where('id', id)
3312
+ .toString();
3313
+ const data = yield this._queryStatement(sql);
2849
3314
  const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'insert' }) || null;
2850
- return this.resultHandler(resultData);
3315
+ return this._resultHandler(resultData);
2851
3316
  }
2852
3317
  case true: {
2853
- const result = yield this.actionStatement({
2854
- sql: [
2855
- `${this.$state.get('UPDATE')}`,
2856
- `${this.$state.get('WHERE')}`
2857
- ].join(' ')
3318
+ const result = yield this._actionStatement({
3319
+ sql: this._queryBuilder().update()
2858
3320
  });
2859
- if (this.$state.get('VOID') || !result)
2860
- return this.resultHandler(null);
2861
- const data = yield this.queryStatement([
2862
- `${this.$state.get('SELECT')}`,
2863
- `${this.$state.get('FROM')}`,
2864
- `${this.$state.get('TABLE_NAME')}`,
2865
- `${this.$state.get('WHERE')}`
2866
- ].join(' '));
3321
+ if (this._getState('VOID') || !result)
3322
+ return this._resultHandler(null);
3323
+ const data = yield this._queryStatement(new Builder().copyBuilder(this, { select: true, where: true }).toString());
2867
3324
  if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
2868
3325
  for (const val of data) {
2869
3326
  val.$action = 'update';
2870
3327
  }
2871
- return this.resultHandler(data || []);
3328
+ return this._resultHandler(data || []);
2872
3329
  }
2873
3330
  const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'update' }) || null;
2874
- return this.resultHandler(resultData);
3331
+ return this._resultHandler(resultData);
2875
3332
  }
2876
3333
  default: {
2877
- return this.resultHandler(null);
3334
+ return this._resultHandler(null);
2878
3335
  }
2879
3336
  }
2880
3337
  });
2881
3338
  }
2882
3339
  _update(ignoreWhere = false) {
2883
3340
  return __awaiter(this, void 0, void 0, function* () {
2884
- if (!this.$state.get('WHERE') && !ignoreWhere)
3341
+ if (!this._getState('WHERE') && !ignoreWhere)
2885
3342
  throw new Error("can't update without where condition");
2886
- const result = yield this.actionStatement({
2887
- sql: [
2888
- `${this.$state.get('UPDATE')}`, `${this.$state.get('WHERE')}`
2889
- ].join(' ')
3343
+ const result = yield this._actionStatement({
3344
+ sql: this._queryBuilder().update()
2890
3345
  });
2891
- if (this.$state.get('VOID') || !result)
2892
- return this.resultHandler(undefined);
2893
- const sql = [
2894
- `${this.$state.get('SELECT')}`,
2895
- `${this.$state.get('FROM')}`,
2896
- `${this.$state.get('TABLE_NAME')}`,
2897
- `${this.$state.get('WHERE')}`
2898
- ].join(' ');
2899
- const data = yield this.queryStatement(sql);
3346
+ if (this._getState('VOID') || !result)
3347
+ return this._resultHandler(undefined);
3348
+ const sql = this._queryBuilder().select();
3349
+ const data = yield this._queryStatement(sql);
2900
3350
  if ((data === null || data === void 0 ? void 0 : data.length) > 1)
2901
- return this.resultHandler(data || []);
3351
+ return this._resultHandler(data || []);
2902
3352
  const res = (data === null || data === void 0 ? void 0 : data.shift()) || null;
2903
- return this.resultHandler(res);
3353
+ return this._resultHandler(res);
2904
3354
  });
2905
3355
  }
2906
3356
  _hiddenColumn(data) {
2907
3357
  var _a;
2908
- const hidden = this.$state.get('HIDDEN');
3358
+ const hidden = this._getState('HIDDEN');
2909
3359
  if ((_a = Object.keys(data)) === null || _a === void 0 ? void 0 : _a.length) {
2910
3360
  hidden.forEach((column) => {
2911
3361
  data.forEach((objColumn) => {
@@ -2916,6 +3366,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2916
3366
  return data;
2917
3367
  }
2918
3368
  _queryUpdate(data) {
3369
+ this.$utils.covertDataToDateIfDate(data);
2919
3370
  const values = Object.entries(data).map(([column, value]) => {
2920
3371
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW'))))
2921
3372
  value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
@@ -2928,6 +3379,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2928
3379
  return `${this.$constants('SET')} ${values}`;
2929
3380
  }
2930
3381
  _queryInsert(data) {
3382
+ this.$utils.covertDataToDateIfDate(data);
2931
3383
  const columns = Object.keys(data).map((column) => `\`${column}\``);
2932
3384
  const values = Object.values(data).map((value) => {
2933
3385
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW'))))
@@ -2948,6 +3400,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2948
3400
  var _a;
2949
3401
  let values = [];
2950
3402
  for (let objects of data) {
3403
+ this.$utils.covertDataToDateIfDate(data);
2951
3404
  const vals = Object.values(objects).map((value) => {
2952
3405
  if (typeof value === 'string')
2953
3406
  value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');