dbtabla 0.8.7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +30 -30
- package/eslint.config.js +22 -0
- package/lib/Connect.js +56 -39
- package/lib/dbResult.js +2 -2
- package/lib/dbTabla.js +48 -41
- package/lib/procesingSql.js +57 -57
- package/license +1 -1
- package/package.json +10 -6
- package/refactor_dbtabla.js +36 -0
package/.eslintrc.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es6": true,
|
|
5
|
-
"node": true
|
|
6
|
-
},
|
|
7
|
-
"extends": "eslint:recommended",
|
|
8
|
-
"parserOptions": {
|
|
9
|
-
"ecmaVersion": 2018,
|
|
10
|
-
"sourceType": "module"
|
|
11
|
-
},
|
|
12
|
-
"rules": {
|
|
13
|
-
"indent": [
|
|
14
|
-
"error",
|
|
15
|
-
4
|
|
16
|
-
],
|
|
17
|
-
"linebreak-style": [
|
|
18
|
-
"error",
|
|
19
|
-
"windows"
|
|
20
|
-
],
|
|
21
|
-
"quotes": [
|
|
22
|
-
"error",
|
|
23
|
-
"double"
|
|
24
|
-
],
|
|
25
|
-
"semi": [
|
|
26
|
-
"error",
|
|
27
|
-
"never"
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es6": true,
|
|
5
|
+
"node": true
|
|
6
|
+
},
|
|
7
|
+
"extends": "eslint:recommended",
|
|
8
|
+
"parserOptions": {
|
|
9
|
+
"ecmaVersion": 2018,
|
|
10
|
+
"sourceType": "module"
|
|
11
|
+
},
|
|
12
|
+
"rules": {
|
|
13
|
+
"indent": [
|
|
14
|
+
"error",
|
|
15
|
+
4
|
|
16
|
+
],
|
|
17
|
+
"linebreak-style": [
|
|
18
|
+
"error",
|
|
19
|
+
"windows"
|
|
20
|
+
],
|
|
21
|
+
"quotes": [
|
|
22
|
+
"error",
|
|
23
|
+
"double"
|
|
24
|
+
],
|
|
25
|
+
"semi": [
|
|
26
|
+
"error",
|
|
27
|
+
"never"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
};
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const js = require("@eslint/js");
|
|
2
|
+
const globals = require("globals");
|
|
3
|
+
|
|
4
|
+
module.exports = [
|
|
5
|
+
js.configs.recommended,
|
|
6
|
+
{
|
|
7
|
+
languageOptions: {
|
|
8
|
+
ecmaVersion: 2024,
|
|
9
|
+
sourceType: "module",
|
|
10
|
+
globals: {
|
|
11
|
+
...globals.node,
|
|
12
|
+
...globals.browser
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
rules: {
|
|
16
|
+
"indent": ["error", 4],
|
|
17
|
+
"linebreak-style": ["error", "windows"],
|
|
18
|
+
"quotes": ["error", "double"],
|
|
19
|
+
"semi": ["error", "never"]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
];
|
package/lib/Connect.js
CHANGED
|
@@ -2,12 +2,22 @@ const fs=require("fs")
|
|
|
2
2
|
const path=require("path")
|
|
3
3
|
const dbTablaModel=require("tabla-model")
|
|
4
4
|
const dbTabla=require(__dirname+"/dbTabla.js")
|
|
5
|
+
const procesingSql = require('./procesingSql')
|
|
5
6
|
/**
|
|
6
7
|
* Connect
|
|
7
8
|
* clase abstracta para crear la clase de conexion a la base de datos
|
|
8
9
|
*/
|
|
9
|
-
class Connect
|
|
10
|
-
|
|
10
|
+
class Connect {
|
|
11
|
+
#escapeChar;
|
|
12
|
+
#reserveIdentifiers;
|
|
13
|
+
#ar_aliased_tables;
|
|
14
|
+
#dbprefix;
|
|
15
|
+
#swap_pre;
|
|
16
|
+
#information_schema;
|
|
17
|
+
#models;
|
|
18
|
+
#caheTablas;
|
|
19
|
+
#createdTables;
|
|
20
|
+
#escapeString;
|
|
11
21
|
/**
|
|
12
22
|
* constructor de la clase
|
|
13
23
|
* @param {object} params - parametros de conexion
|
|
@@ -15,15 +25,15 @@ class Connect
|
|
|
15
25
|
constructor(params,typeDB="")
|
|
16
26
|
{
|
|
17
27
|
this.config=params
|
|
18
|
-
this
|
|
19
|
-
this
|
|
20
|
-
this
|
|
21
|
-
this
|
|
22
|
-
this
|
|
23
|
-
this
|
|
24
|
-
this
|
|
28
|
+
this.#escapeChar=""
|
|
29
|
+
this.#reserveIdentifiers=["*"]
|
|
30
|
+
this.#ar_aliased_tables=[]
|
|
31
|
+
this.#dbprefix=""
|
|
32
|
+
this.#swap_pre=""
|
|
33
|
+
this.#information_schema = " "
|
|
34
|
+
this.#models={}
|
|
25
35
|
this.typeDB=typeDB
|
|
26
|
-
this
|
|
36
|
+
this.#caheTablas={}
|
|
27
37
|
}
|
|
28
38
|
/**
|
|
29
39
|
* retorna la configuracion para los helpers
|
|
@@ -32,13 +42,13 @@ class Connect
|
|
|
32
42
|
helpersConf()
|
|
33
43
|
{
|
|
34
44
|
return {
|
|
35
|
-
escapeChar:this
|
|
36
|
-
reserveIdentifiers:this
|
|
37
|
-
ar_aliased_tables:this
|
|
38
|
-
dbprefix:this
|
|
39
|
-
swap_pre:this
|
|
45
|
+
escapeChar:this.#escapeChar,
|
|
46
|
+
reserveIdentifiers:this.#reserveIdentifiers,
|
|
47
|
+
ar_aliased_tables:this.#ar_aliased_tables,
|
|
48
|
+
dbprefix:this.#dbprefix,
|
|
49
|
+
swap_pre:this.#swap_pre,
|
|
40
50
|
typeDB:this.typeDB,
|
|
41
|
-
escapeString:typeof this
|
|
51
|
+
escapeString:typeof this.#escapeString==="function"?e=>this.#escapeString(e):null
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
/**
|
|
@@ -49,7 +59,7 @@ class Connect
|
|
|
49
59
|
model(tabla)
|
|
50
60
|
{
|
|
51
61
|
|
|
52
|
-
return this
|
|
62
|
+
return this.#models[tabla] instanceof dbTablaModel?this.#models[tabla]:false
|
|
53
63
|
}
|
|
54
64
|
/**
|
|
55
65
|
* agrega un modelo a la lista de modelos interna
|
|
@@ -59,11 +69,11 @@ class Connect
|
|
|
59
69
|
{
|
|
60
70
|
if(model instanceof dbTablaModel)
|
|
61
71
|
{
|
|
62
|
-
this
|
|
72
|
+
this.#models[model.tabla]=model
|
|
63
73
|
}else if(typeof model==="object" && model!==null){
|
|
64
|
-
this
|
|
74
|
+
this.#models[model.tabla]= new dbTablaModel(model.tabla,model)
|
|
65
75
|
}else {
|
|
66
|
-
this
|
|
76
|
+
this.#models[model.tabla]= new dbTablaModel(model,this.helpersConf().escapeChar)
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
}
|
|
@@ -113,12 +123,12 @@ class Connect
|
|
|
113
123
|
*/
|
|
114
124
|
tabla(tabla,callback,verify=false)
|
|
115
125
|
{
|
|
116
|
-
if(typeof this
|
|
126
|
+
if(typeof this.#caheTablas[tabla]!=="undefined")
|
|
117
127
|
{
|
|
118
|
-
typeof callback==="function"?callback(this
|
|
119
|
-
return this
|
|
128
|
+
typeof callback==="function"?callback(this.#caheTablas[tabla]):null
|
|
129
|
+
return this.#caheTablas[tabla]
|
|
120
130
|
}
|
|
121
|
-
return this
|
|
131
|
+
return this.#caheTablas[tabla] = new dbTabla({
|
|
122
132
|
tabla:tabla,
|
|
123
133
|
connection:this,
|
|
124
134
|
callback:callback,
|
|
@@ -149,7 +159,7 @@ class Connect
|
|
|
149
159
|
* @param {string} table - nombre de la tabla
|
|
150
160
|
* @return {Promise}
|
|
151
161
|
*/
|
|
152
|
-
|
|
162
|
+
#keysInTable(table)
|
|
153
163
|
{
|
|
154
164
|
return new Promise((res)=>
|
|
155
165
|
{
|
|
@@ -176,9 +186,11 @@ class Connect
|
|
|
176
186
|
rej()
|
|
177
187
|
}else
|
|
178
188
|
{
|
|
189
|
+
|
|
179
190
|
if(create)
|
|
180
191
|
{
|
|
181
|
-
|
|
192
|
+
|
|
193
|
+
this.#createTable(this.model(tabla))
|
|
182
194
|
.then(res).catch(rej)
|
|
183
195
|
}else
|
|
184
196
|
{
|
|
@@ -194,49 +206,54 @@ class Connect
|
|
|
194
206
|
* @param {dbTablaModel} model - objeto del modelo
|
|
195
207
|
* @return {Promise}
|
|
196
208
|
*/
|
|
197
|
-
|
|
209
|
+
#createTable(model)
|
|
198
210
|
{
|
|
211
|
+
|
|
199
212
|
return new Promise((res,rej)=>
|
|
200
213
|
{
|
|
201
|
-
this
|
|
214
|
+
this.#createdTables={}
|
|
202
215
|
let rescursive=(foreingKey,i)=>
|
|
203
216
|
{
|
|
217
|
+
|
|
204
218
|
if(foreingKey[i]!==undefined)
|
|
205
219
|
{
|
|
206
220
|
try
|
|
207
221
|
{
|
|
208
222
|
this.tabla(foreingKey[i].reference,(t)=>
|
|
209
223
|
{
|
|
210
|
-
|
|
211
|
-
this.__initializeTable(t).then(()=>
|
|
212
|
-
{
|
|
213
|
-
i++
|
|
214
|
-
rescursive(foreingKey,i)
|
|
215
|
-
}).catch(rej)
|
|
224
|
+
rescursive(foreingKey,++i)
|
|
216
225
|
},true)
|
|
217
226
|
}catch(e){rej(e)}
|
|
218
227
|
}else {
|
|
228
|
+
|
|
219
229
|
this.query(model.sql(this)).then(()=>{
|
|
220
|
-
this
|
|
221
|
-
|
|
230
|
+
this.#createdTables[model.tabla]=true
|
|
231
|
+
this.#initializeTable(model.tabla).then(()=>
|
|
232
|
+
{
|
|
233
|
+
|
|
234
|
+
res(model.getData())
|
|
235
|
+
}).catch(rej)
|
|
222
236
|
}).catch(rej)
|
|
223
237
|
}
|
|
224
238
|
}
|
|
225
239
|
rescursive(model.foreingKey(),0)
|
|
226
240
|
})
|
|
227
241
|
}
|
|
228
|
-
|
|
242
|
+
#initializeTable(tab)
|
|
229
243
|
{
|
|
230
244
|
return new Promise(async(res)=>
|
|
231
245
|
{
|
|
232
|
-
|
|
246
|
+
|
|
247
|
+
let sql=new procesingSql(this.model(tab).getData(),this.helpersConf())
|
|
248
|
+
let init=this.model(tab).getData().init
|
|
249
|
+
|
|
233
250
|
if(init.length<1)
|
|
234
251
|
{
|
|
235
252
|
return res()
|
|
236
253
|
}
|
|
237
254
|
for(let item of init)
|
|
238
255
|
{
|
|
239
|
-
await
|
|
256
|
+
await this.query(sql.insert(item))
|
|
240
257
|
}
|
|
241
258
|
res()
|
|
242
259
|
|
package/lib/dbResult.js
CHANGED
|
@@ -20,9 +20,9 @@ class dbResult extends Array
|
|
|
20
20
|
Object.defineProperty(this, "$sql", {
|
|
21
21
|
configurable : true,
|
|
22
22
|
enumerable : false,
|
|
23
|
-
value : dbTabla.
|
|
23
|
+
value : dbTabla.lastSql,
|
|
24
24
|
})
|
|
25
|
-
//this.sql=dbTabla.
|
|
25
|
+
//this.sql=dbTabla.lastSql
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
module.exports=dbResult
|
package/lib/dbTabla.js
CHANGED
|
@@ -5,23 +5,30 @@ const dbTablaError = require("./dbTablaError")
|
|
|
5
5
|
* dbTabla
|
|
6
6
|
* representacion de una tabla en la base de datos
|
|
7
7
|
*/
|
|
8
|
-
class dbTabla
|
|
9
|
-
|
|
8
|
+
class dbTabla {
|
|
9
|
+
#connection;
|
|
10
|
+
#lastSql;
|
|
11
|
+
#keys;
|
|
12
|
+
#callbackKey;
|
|
13
|
+
#config;
|
|
14
|
+
|
|
15
|
+
get lastSql() { return this.#lastSql; }
|
|
16
|
+
|
|
10
17
|
/**
|
|
11
18
|
* @param {object} tabla, connection, callback, config
|
|
12
19
|
* @param {boolean} ret indica si verificara la existencia de la tabla o esperara la primera consulta
|
|
13
20
|
*/
|
|
14
21
|
constructor({tabla,connection,callback,config},ret=false)
|
|
15
22
|
{
|
|
16
|
-
this
|
|
17
|
-
this
|
|
18
|
-
this
|
|
23
|
+
this.#connection=connection
|
|
24
|
+
this.#lastSql=""
|
|
25
|
+
this.#keys=false
|
|
19
26
|
this.tabla=tabla || null
|
|
20
|
-
this
|
|
21
|
-
this
|
|
27
|
+
this.#callbackKey=callback ||( e=>e)
|
|
28
|
+
this.#config=config || {}
|
|
22
29
|
this.typeDB=connection.typeDB
|
|
23
30
|
if(ret)
|
|
24
|
-
this
|
|
31
|
+
this.#verifyKeys()
|
|
25
32
|
}
|
|
26
33
|
/**
|
|
27
34
|
* agrega la propiedad $sql que continen
|
|
@@ -29,13 +36,13 @@ class dbTabla
|
|
|
29
36
|
* @param {object}
|
|
30
37
|
* @return {object}
|
|
31
38
|
*/
|
|
32
|
-
|
|
39
|
+
#PropertyOk(ok)
|
|
33
40
|
{
|
|
34
41
|
if(typeof ok ==="object")
|
|
35
42
|
Object.defineProperty(ok, "$sql", {
|
|
36
43
|
configurable : true,
|
|
37
44
|
enumerable : false,
|
|
38
|
-
value : this
|
|
45
|
+
value : this.#lastSql,
|
|
39
46
|
})
|
|
40
47
|
return ok
|
|
41
48
|
}
|
|
@@ -43,28 +50,28 @@ class dbTabla
|
|
|
43
50
|
* verifica la existencia de la tabla y obtiene los metadatos
|
|
44
51
|
* @param {function} call
|
|
45
52
|
*/
|
|
46
|
-
|
|
53
|
+
#verifyKeys(call=e=>e)
|
|
47
54
|
{
|
|
48
|
-
if(!this
|
|
55
|
+
if(!this.#keys)
|
|
49
56
|
{
|
|
50
|
-
this.
|
|
57
|
+
this.#connection.__keysInTable(this.tabla)
|
|
51
58
|
.then(keys=>
|
|
52
59
|
{
|
|
53
60
|
this.colum=keys.colum
|
|
54
|
-
this.sql=new procesingSql(keys, this
|
|
55
|
-
this
|
|
61
|
+
this.sql=new procesingSql(keys, this.#config)
|
|
62
|
+
this.#keys=true
|
|
56
63
|
if(keys.methods)
|
|
57
64
|
for(let i in keys.methods)
|
|
58
65
|
{
|
|
59
|
-
this[i]=(...params)=>keys.methods[i](this,this
|
|
66
|
+
this[i]=(...params)=>keys.methods[i](this,this.#connection,...params)
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
this
|
|
69
|
+
this.#callbackKey(this)
|
|
63
70
|
call()
|
|
64
71
|
}).catch(e=>{
|
|
65
72
|
|
|
66
73
|
|
|
67
|
-
this
|
|
74
|
+
this.#callbackKey(null,e)
|
|
68
75
|
call(e)
|
|
69
76
|
|
|
70
77
|
|
|
@@ -79,7 +86,7 @@ class dbTabla
|
|
|
79
86
|
* @param {array|object} resultado obtenido de la consulta ejecutada
|
|
80
87
|
* @return {dbResult}
|
|
81
88
|
*/
|
|
82
|
-
|
|
89
|
+
#formatResult(result)
|
|
83
90
|
{
|
|
84
91
|
return new dbResult(this,result)
|
|
85
92
|
}
|
|
@@ -99,7 +106,7 @@ class dbTabla
|
|
|
99
106
|
return new Promise((resolve,reject)=>
|
|
100
107
|
{
|
|
101
108
|
|
|
102
|
-
this
|
|
109
|
+
this.#verifyKeys(e=>
|
|
103
110
|
{
|
|
104
111
|
if(e)
|
|
105
112
|
{
|
|
@@ -107,14 +114,14 @@ class dbTabla
|
|
|
107
114
|
}
|
|
108
115
|
if(params instanceof Array && params.length==1)
|
|
109
116
|
{
|
|
110
|
-
this
|
|
117
|
+
this.#lastSql=this.sql.insert(params[0])
|
|
111
118
|
}else {
|
|
112
|
-
this
|
|
119
|
+
this.#lastSql=this.sql.insert(params)
|
|
113
120
|
}
|
|
114
121
|
|
|
115
|
-
this.
|
|
122
|
+
this.#connection.query(this.#lastSql).then(d=>{
|
|
116
123
|
|
|
117
|
-
resolve(this
|
|
124
|
+
resolve(this.#PropertyOk(d))
|
|
118
125
|
}).catch(e=>reject(e))
|
|
119
126
|
})
|
|
120
127
|
})
|
|
@@ -151,15 +158,15 @@ class dbTabla
|
|
|
151
158
|
{
|
|
152
159
|
return new Promise((resolve,reject)=>
|
|
153
160
|
{
|
|
154
|
-
this
|
|
161
|
+
this.#verifyKeys(e=>
|
|
155
162
|
{
|
|
156
163
|
if(e)
|
|
157
164
|
{
|
|
158
165
|
return reject(e)
|
|
159
166
|
}
|
|
160
|
-
this
|
|
161
|
-
this.
|
|
162
|
-
resolve(this
|
|
167
|
+
this.#lastSql = this.sql.select(...params)
|
|
168
|
+
this.#connection.query(this.#lastSql).then(d=>{
|
|
169
|
+
resolve(this.#formatResult(d))
|
|
163
170
|
}).catch(e=>reject(e))
|
|
164
171
|
})
|
|
165
172
|
|
|
@@ -271,11 +278,11 @@ class dbTabla
|
|
|
271
278
|
{
|
|
272
279
|
return new Promise((resolve,reject)=>
|
|
273
280
|
{
|
|
274
|
-
this
|
|
281
|
+
this.#verifyKeys(()=>
|
|
275
282
|
{
|
|
276
|
-
this
|
|
277
|
-
this.
|
|
278
|
-
resolve(this
|
|
283
|
+
this.#lastSql = this.sql.busqueda(cadena,campo_bus,campos,where,joins,group,having,order,limit)
|
|
284
|
+
this.#connection.query(this.#lastSql).then(d=>{
|
|
285
|
+
resolve(this.#formatResult(d))
|
|
279
286
|
}).catch(e=>reject(e))
|
|
280
287
|
})
|
|
281
288
|
|
|
@@ -324,18 +331,18 @@ class dbTabla
|
|
|
324
331
|
}
|
|
325
332
|
return new Promise((resolve,reject)=>
|
|
326
333
|
{
|
|
327
|
-
this
|
|
334
|
+
this.#verifyKeys(e=>
|
|
328
335
|
{
|
|
329
336
|
if(e)
|
|
330
337
|
{
|
|
331
338
|
return reject(e)
|
|
332
339
|
}
|
|
333
|
-
this
|
|
334
|
-
if( this
|
|
340
|
+
this.#lastSql =this.sql.update(sets,where,reject)
|
|
341
|
+
if( this.#lastSql)
|
|
335
342
|
{
|
|
336
|
-
this.
|
|
343
|
+
this.#connection.query(this.#lastSql).then(d=>{
|
|
337
344
|
|
|
338
|
-
resolve(this
|
|
345
|
+
resolve(this.#PropertyOk(d))
|
|
339
346
|
}).catch(e=>reject(e))
|
|
340
347
|
}
|
|
341
348
|
})
|
|
@@ -377,16 +384,16 @@ class dbTabla
|
|
|
377
384
|
}
|
|
378
385
|
return new Promise((resolve,reject)=>
|
|
379
386
|
{
|
|
380
|
-
this
|
|
387
|
+
this.#verifyKeys(e=>
|
|
381
388
|
{
|
|
382
389
|
if(e)
|
|
383
390
|
{
|
|
384
391
|
return reject(e)
|
|
385
392
|
}
|
|
386
|
-
this
|
|
387
|
-
this.
|
|
393
|
+
this.#lastSql =this.sql.delete(where)
|
|
394
|
+
this.#connection.query(this.#lastSql).then(d=>{
|
|
388
395
|
|
|
389
|
-
resolve(this
|
|
396
|
+
resolve(this.#PropertyOk(d))
|
|
390
397
|
}).catch(e=>reject(e))
|
|
391
398
|
})
|
|
392
399
|
})
|
package/lib/procesingSql.js
CHANGED
|
@@ -4,8 +4,8 @@ const SQLITE3_DB=require("./const").SQLITE3_DB
|
|
|
4
4
|
* prosesingSql
|
|
5
5
|
* crea consultas sql insert, select, update, delete validas para una tabla
|
|
6
6
|
*/
|
|
7
|
-
class prosesingSql
|
|
8
|
-
|
|
7
|
+
class prosesingSql {
|
|
8
|
+
#helpers;
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
11
|
* @param {object} colum, prymary, autoincrement, OrderColum, TypeDB - metadadtos de la tabla
|
|
@@ -14,7 +14,7 @@ class prosesingSql
|
|
|
14
14
|
constructor(keys,config)
|
|
15
15
|
{
|
|
16
16
|
this.tabla=keys.tabla
|
|
17
|
-
this
|
|
17
|
+
this.#helpers=new dbHelpers(config)
|
|
18
18
|
this.colums=keys.colums
|
|
19
19
|
this.primary=this.colums.filter(p=>p.primary)
|
|
20
20
|
let auto=this.colums.find(p=>p.autoincrement)
|
|
@@ -116,26 +116,26 @@ class prosesingSql
|
|
|
116
116
|
{
|
|
117
117
|
campo_bus=this.OrderColum
|
|
118
118
|
}
|
|
119
|
-
let search=this
|
|
120
|
-
let params=this
|
|
119
|
+
let search=this.#campoBusqueda(this.#helpers.filterSqlI(cadena),campo_bus)
|
|
120
|
+
let params=this.#resolveParamsSelect(campos,joins,where,group,having,order,limit)
|
|
121
121
|
let arrSql=[],
|
|
122
|
-
arrJoin=this
|
|
123
|
-
w=this
|
|
124
|
-
g=this
|
|
125
|
-
h=this
|
|
126
|
-
o=this
|
|
127
|
-
l=this
|
|
122
|
+
arrJoin=this.#join(params.joins),
|
|
123
|
+
w=this.#booleanSql("WHERE",params.where),
|
|
124
|
+
g=this.#groupBy(params.group),
|
|
125
|
+
h=this.#booleanSql("HAVING",params.having),
|
|
126
|
+
o=this.#orderBy(params.order),
|
|
127
|
+
l=this.#limit(params.limit)
|
|
128
128
|
|
|
129
129
|
if(!w)
|
|
130
130
|
{
|
|
131
|
-
w=this
|
|
131
|
+
w=this.#booleanSql("WHERE",`(${search})>1`)
|
|
132
132
|
}else {
|
|
133
133
|
|
|
134
|
-
w =this
|
|
134
|
+
w =this.#booleanSql("WHERE",`(${w.replace(/^[\s]*where[\s]+(.*)/i,"$1")}) AND (${search})>1`)
|
|
135
135
|
}
|
|
136
136
|
if(!o)
|
|
137
137
|
{
|
|
138
|
-
o = this
|
|
138
|
+
o = this.#orderBy("puntaje_busqueda DESC")
|
|
139
139
|
}
|
|
140
140
|
if(params.campos instanceof Array)
|
|
141
141
|
{
|
|
@@ -143,9 +143,9 @@ class prosesingSql
|
|
|
143
143
|
}else{
|
|
144
144
|
params.campos=[`(${search}) as puntaje_busqueda`,this.tabla + ".*"]
|
|
145
145
|
}
|
|
146
|
-
arrSql.push(this
|
|
146
|
+
arrSql.push(this.#campos(params.campos))
|
|
147
147
|
arrSql.push("FROM")
|
|
148
|
-
arrSql.push(this.
|
|
148
|
+
arrSql.push(this.#helpers.protectIdentifiers(this.tabla))
|
|
149
149
|
if(arrJoin.length>0)
|
|
150
150
|
arrSql.push(arrJoin.join(" "))
|
|
151
151
|
if(w)
|
|
@@ -166,7 +166,7 @@ class prosesingSql
|
|
|
166
166
|
* @param {array} campos en los que se realizara la busqueda
|
|
167
167
|
* @return {string}
|
|
168
168
|
*/
|
|
169
|
-
|
|
169
|
+
#campoBusqueda(cadena, campos)
|
|
170
170
|
{
|
|
171
171
|
if (cadena instanceof Array)
|
|
172
172
|
{
|
|
@@ -176,7 +176,7 @@ class prosesingSql
|
|
|
176
176
|
let select = ""
|
|
177
177
|
for(let i in campos)
|
|
178
178
|
{
|
|
179
|
-
let campo=this.
|
|
179
|
+
let campo=this.#helpers.protectIdentifiers(campos[i])
|
|
180
180
|
select+=`(${campo} is NOT NULL AND ${campo} like '%${cadena}%')+(${campo} is NOT NULL AND ${campo} like '${cadena}%')+`
|
|
181
181
|
}
|
|
182
182
|
|
|
@@ -188,7 +188,7 @@ class prosesingSql
|
|
|
188
188
|
continue
|
|
189
189
|
for(let j in campos)
|
|
190
190
|
{
|
|
191
|
-
let campo =this.
|
|
191
|
+
let campo =this.#helpers.protectIdentifiers(campos[j])
|
|
192
192
|
select += `(${campo} is NOT NULL AND ${campo} like '%${palabra}%') + (${campo} is NOT NULL AND ${campo} like '${palabra}%')+`
|
|
193
193
|
|
|
194
194
|
}
|
|
@@ -202,7 +202,7 @@ class prosesingSql
|
|
|
202
202
|
select+="((CONCAT("
|
|
203
203
|
for(let i in campos)
|
|
204
204
|
{
|
|
205
|
-
let campo=this.
|
|
205
|
+
let campo=this.#helpers.protectIdentifiers(campos[i])
|
|
206
206
|
let espace = !/[\d]+/.test(i) ? "''" : "' '"
|
|
207
207
|
select+=`IF(${campo} IS NOT NULL,${campo},' '),${espace},`
|
|
208
208
|
}
|
|
@@ -211,7 +211,7 @@ class prosesingSql
|
|
|
211
211
|
} else if (campos.length == 1)
|
|
212
212
|
{
|
|
213
213
|
select+="(0)"
|
|
214
|
-
campos[0] = this.
|
|
214
|
+
campos[0] = this.#helpers.protectIdentifiers(campos[0])
|
|
215
215
|
// solo=`* (IF(${campos[0]}='${cadena}',0,1))+(IF(${campos[0]}='${cadena}',1,0))`
|
|
216
216
|
}
|
|
217
217
|
} else
|
|
@@ -227,7 +227,7 @@ class prosesingSql
|
|
|
227
227
|
*/
|
|
228
228
|
delete(where)
|
|
229
229
|
{
|
|
230
|
-
return `DELETE FROM ${this.
|
|
230
|
+
return `DELETE FROM ${this.#helpers.protectIdentifiers(this.tabla)} ${this.#booleanSql("WHERE",where)};`
|
|
231
231
|
}
|
|
232
232
|
/**
|
|
233
233
|
* genera una sentencia sql update valida
|
|
@@ -264,11 +264,11 @@ class prosesingSql
|
|
|
264
264
|
data=this.bindDataSmt(i,sets[i])
|
|
265
265
|
}else
|
|
266
266
|
{
|
|
267
|
-
data=this.
|
|
267
|
+
data=this.#helpers.formatVarInsert(sets[i],i)
|
|
268
268
|
}
|
|
269
|
-
col.push(this.
|
|
269
|
+
col.push(this.#helpers.protectIdentifiers(i) +"="+ data)
|
|
270
270
|
}
|
|
271
|
-
return `UPDATE ${this.
|
|
271
|
+
return `UPDATE ${this.#helpers.protectIdentifiers(this.tabla)} SET ${col.join(",")} ${this.#booleanSql("WHERE",where)};`
|
|
272
272
|
}
|
|
273
273
|
/**
|
|
274
274
|
* genera una sentencia insert sql valida
|
|
@@ -287,7 +287,7 @@ class prosesingSql
|
|
|
287
287
|
{
|
|
288
288
|
campos[coll.name]=this.nextAutoincrementValue
|
|
289
289
|
}
|
|
290
|
-
if(this
|
|
290
|
+
if(this.#verifyCampo(coll,campos[coll.name]))
|
|
291
291
|
{
|
|
292
292
|
attrs.push(coll.name)
|
|
293
293
|
if(this.smt)
|
|
@@ -295,7 +295,7 @@ class prosesingSql
|
|
|
295
295
|
col.push(this.bindDataSmt(coll.name,campos[coll.name]))
|
|
296
296
|
}else
|
|
297
297
|
{
|
|
298
|
-
col.push(this.
|
|
298
|
+
col.push(this.#helpers.formatVarInsert(campos[coll.name],coll.name))
|
|
299
299
|
}
|
|
300
300
|
}
|
|
301
301
|
}
|
|
@@ -309,7 +309,7 @@ class prosesingSql
|
|
|
309
309
|
{
|
|
310
310
|
campos[i]=this.nextAutoincrementValue
|
|
311
311
|
}
|
|
312
|
-
if(this
|
|
312
|
+
if(this.#verifyCampo(coll,campos[i]))
|
|
313
313
|
{
|
|
314
314
|
attrs.push(coll.name)
|
|
315
315
|
if(this.smt)
|
|
@@ -317,7 +317,7 @@ class prosesingSql
|
|
|
317
317
|
col.push(this.bindDataSmt(coll.name,campos[i]))
|
|
318
318
|
}else
|
|
319
319
|
{
|
|
320
|
-
col.push(this.
|
|
320
|
+
col.push(this.#helpers.formatVarInsert(campos[i],coll.name))
|
|
321
321
|
}
|
|
322
322
|
}
|
|
323
323
|
}
|
|
@@ -325,13 +325,13 @@ class prosesingSql
|
|
|
325
325
|
let columnas = ""
|
|
326
326
|
if (attrs)
|
|
327
327
|
{
|
|
328
|
-
columnas = `(${this.
|
|
328
|
+
columnas = `(${this.#helpers.campos(attrs) })`
|
|
329
329
|
}
|
|
330
|
-
return `INSERT INTO ${this.
|
|
330
|
+
return `INSERT INTO ${this.#helpers.protectIdentifiers(this.tabla)} ${columnas} VALUES (${col.join(",")});`
|
|
331
331
|
|
|
332
332
|
}
|
|
333
333
|
|
|
334
|
-
|
|
334
|
+
#verifyCampo(coll,value)
|
|
335
335
|
{
|
|
336
336
|
return !((
|
|
337
337
|
(coll.autoincrement || coll.defaultNull) &&
|
|
@@ -398,18 +398,18 @@ class prosesingSql
|
|
|
398
398
|
{
|
|
399
399
|
let params=[]
|
|
400
400
|
|
|
401
|
-
params=this
|
|
401
|
+
params=this.#resolveParamsSelect(campos,joins,where,group,having,order,limit)
|
|
402
402
|
let arrSql=[],
|
|
403
|
-
arrJoin=this
|
|
404
|
-
w=this
|
|
405
|
-
g=this
|
|
406
|
-
h=this
|
|
407
|
-
o=this
|
|
408
|
-
l=this
|
|
409
|
-
|
|
410
|
-
arrSql.push(this
|
|
403
|
+
arrJoin=this.#join(params.joins),
|
|
404
|
+
w=this.#booleanSql("WHERE",params.where),
|
|
405
|
+
g=this.#groupBy(params.group),
|
|
406
|
+
h=this.#booleanSql("HAVING",params.having),
|
|
407
|
+
o=this.#orderBy(params.order),
|
|
408
|
+
l=this.#limit(params.limit)
|
|
409
|
+
|
|
410
|
+
arrSql.push(this.#campos(params.campos))
|
|
411
411
|
arrSql.push("FROM")
|
|
412
|
-
arrSql.push(this.
|
|
412
|
+
arrSql.push(this.#helpers.protectIdentifiers(this.tabla))
|
|
413
413
|
if(arrJoin.length>0)
|
|
414
414
|
arrSql.push(arrJoin.join(" "))
|
|
415
415
|
if(w)
|
|
@@ -434,7 +434,7 @@ class prosesingSql
|
|
|
434
434
|
* es NATURAL
|
|
435
435
|
* @return {string}
|
|
436
436
|
*/
|
|
437
|
-
|
|
437
|
+
#join(join)
|
|
438
438
|
{
|
|
439
439
|
let J = []
|
|
440
440
|
//let keys = this.OrderColum
|
|
@@ -454,21 +454,21 @@ class prosesingSql
|
|
|
454
454
|
if(typeof v==="string" && /(!=|>=|<=|=|>|<)/.test(v))
|
|
455
455
|
{
|
|
456
456
|
|
|
457
|
-
let u=this.
|
|
457
|
+
let u=this.#helpers.protectIdentifiersBolean(v)
|
|
458
458
|
using = `ON(${u})`
|
|
459
459
|
}else if( v instanceof Array)
|
|
460
460
|
{
|
|
461
461
|
for(let i in v)
|
|
462
462
|
{
|
|
463
|
-
v[i]=this.
|
|
463
|
+
v[i]=this.#helpers.protectIdentifiers(v[i])
|
|
464
464
|
}
|
|
465
465
|
let joined=v.join(",")
|
|
466
466
|
using = `USING(${joined})`
|
|
467
467
|
}else if(typeof v==="object")
|
|
468
468
|
{
|
|
469
|
-
using = `ON(${this.
|
|
469
|
+
using = `ON(${this.#helpers.object2boleandSql(v,true)})`
|
|
470
470
|
}else {
|
|
471
|
-
using = `USING(${this.
|
|
471
|
+
using = `USING(${this.#helpers.protectIdentifiers(v)})`
|
|
472
472
|
}
|
|
473
473
|
}else
|
|
474
474
|
{
|
|
@@ -495,7 +495,7 @@ class prosesingSql
|
|
|
495
495
|
|
|
496
496
|
}
|
|
497
497
|
//" " . $tipe . " JOIN " . $this->Driver->ProtectIdentifiers($tab->Tabla()) . " " . $using
|
|
498
|
-
J.push(`${tipe} JOIN ${this.
|
|
498
|
+
J.push(`${tipe} JOIN ${this.#helpers.protectIdentifiers(tabla)} ${using}`.replace(/ +$/,""))
|
|
499
499
|
|
|
500
500
|
}
|
|
501
501
|
}
|
|
@@ -508,11 +508,11 @@ class prosesingSql
|
|
|
508
508
|
* @param {string|object} sql
|
|
509
509
|
* @return {string}
|
|
510
510
|
*/
|
|
511
|
-
|
|
511
|
+
#booleanSql(type,sql)
|
|
512
512
|
{
|
|
513
513
|
if (typeof sql==="object" && sql!==null)
|
|
514
514
|
{
|
|
515
|
-
return type+" " +this.
|
|
515
|
+
return type+" " +this.#helpers.object2boleandSql(sql)
|
|
516
516
|
}else if(sql!==undefined && sql!==null)
|
|
517
517
|
{
|
|
518
518
|
return type+" " +sql.replace(new RegExp(`^[\\s]*${type}[\\s]+(.*)`,"i"),"$1")
|
|
@@ -525,20 +525,20 @@ class prosesingSql
|
|
|
525
525
|
* @param {array} campos - lista de campos
|
|
526
526
|
* @return {string}
|
|
527
527
|
*/
|
|
528
|
-
|
|
528
|
+
#campos(campos)
|
|
529
529
|
{
|
|
530
530
|
if (!(campos instanceof Array) || campos===[])
|
|
531
531
|
{
|
|
532
|
-
return this.
|
|
532
|
+
return this.#helpers.campos([ this.tabla + ".*"])
|
|
533
533
|
}
|
|
534
|
-
return this.
|
|
534
|
+
return this.#helpers.campos(campos)
|
|
535
535
|
}
|
|
536
536
|
/**
|
|
537
537
|
* genera el sql para pa el group by de una sentencia select
|
|
538
538
|
* @param {array|string} group - lista de campos
|
|
539
539
|
* @return {string}
|
|
540
540
|
*/
|
|
541
|
-
|
|
541
|
+
#groupBy(group)
|
|
542
542
|
{
|
|
543
543
|
if (group instanceof Array)
|
|
544
544
|
{
|
|
@@ -557,7 +557,7 @@ class prosesingSql
|
|
|
557
557
|
* @param {array|string} order - lista de campos
|
|
558
558
|
* @return {string}
|
|
559
559
|
*/
|
|
560
|
-
|
|
560
|
+
#orderBy(order)
|
|
561
561
|
{
|
|
562
562
|
if (order instanceof Array)
|
|
563
563
|
{
|
|
@@ -576,7 +576,7 @@ class prosesingSql
|
|
|
576
576
|
* @param {array|string} limit - lista de campos
|
|
577
577
|
* @return {string}
|
|
578
578
|
*/
|
|
579
|
-
|
|
579
|
+
#limit(limit)
|
|
580
580
|
{
|
|
581
581
|
if(/^[\s]*LIMIT[\s]+/i.test(limit))
|
|
582
582
|
{
|
|
@@ -599,7 +599,7 @@ class prosesingSql
|
|
|
599
599
|
* @param {array|string|object} limit
|
|
600
600
|
* @return {object} - parametros
|
|
601
601
|
*/
|
|
602
|
-
|
|
602
|
+
#resolveParamsSelect(campos,joins,where,group,having,order,limit)
|
|
603
603
|
{
|
|
604
604
|
if(typeof campos==="object" && campos!==null && !(campos instanceof Array) &&
|
|
605
605
|
( campos.campos!==undefined
|
package/license
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c) 2018 Enyerber Franco (enyerverfranco@
|
|
1
|
+
Copyright (c) 2018 Enyerber Franco (enyerverfranco@gmail.com) and contributors
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
4
|
of this software and associated documentation files (the "Software"), to deal
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbtabla",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "interface de alto nivel para sql",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "enyerverfranco@gmail.com",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"main": "index.js",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"lint": "eslint ./index.js ./lib",
|
|
14
|
-
"test": "
|
|
14
|
+
"test": "node --test ./test/index.js"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
17
|
"sql",
|
|
@@ -20,10 +20,14 @@
|
|
|
20
20
|
"posgresql"
|
|
21
21
|
],
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"eslint": "^
|
|
24
|
-
"
|
|
23
|
+
"@eslint/js": "^10.0.1",
|
|
24
|
+
"eslint": "^10.1.0",
|
|
25
|
+
"globals": "^17.4.0"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"tabla-model": "^
|
|
28
|
+
"tabla-model": "^2.0.0"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18.0.0"
|
|
28
32
|
}
|
|
29
|
-
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const libDir = 'c:/programacion/dbtabla/dbtabla/lib';
|
|
5
|
+
|
|
6
|
+
// 1. Refactor Connect.js
|
|
7
|
+
let connectFile = path.join(libDir, 'Connect.js');
|
|
8
|
+
let connectCode = fs.readFileSync(connectFile, 'utf8');
|
|
9
|
+
connectCode = connectCode.replace(/class Connect\s*\{/, 'class Connect {\n #escapeChar;\n #reserveIdentifiers;\n #ar_aliased_tables;\n #dbprefix;\n #swap_pre;\n #information_schema;\n #models;\n #caheTablas;\n #createdTables;\n #escapeString;');
|
|
10
|
+
connectCode = connectCode.replace(/this\.__/g, 'this.#');
|
|
11
|
+
connectCode = connectCode.replace(/^(\s*)__([a-zA-Z0-9_]+)\s*\(/gm, '$1#$2(');
|
|
12
|
+
fs.writeFileSync(connectFile, connectCode);
|
|
13
|
+
|
|
14
|
+
// 2. Refactor dbTabla.js
|
|
15
|
+
let dbTablaFile = path.join(libDir, 'dbTabla.js');
|
|
16
|
+
let dbTablaCode = fs.readFileSync(dbTablaFile, 'utf8');
|
|
17
|
+
dbTablaCode = dbTablaCode.replace(/class dbTabla\s*\{/, 'class dbTabla {\n #connection;\n #lastSql;\n #keys;\n #callbackKey;\n #config;\n\n get lastSql() { return this.#lastSql; }\n');
|
|
18
|
+
dbTablaCode = dbTablaCode.replace(/this\.__/g, 'this.#');
|
|
19
|
+
dbTablaCode = dbTablaCode.replace(/^(\s*)__([a-zA-Z0-9_]+)\s*\(/gm, '$1#$2(');
|
|
20
|
+
fs.writeFileSync(dbTablaFile, dbTablaCode);
|
|
21
|
+
|
|
22
|
+
// 3. Refactor procesingSql.js
|
|
23
|
+
let procesingSqlFile = path.join(libDir, 'procesingSql.js');
|
|
24
|
+
let procesingSqlCode = fs.readFileSync(procesingSqlFile, 'utf8');
|
|
25
|
+
procesingSqlCode = procesingSqlCode.replace(/class prosesingSql\s*\{/, 'class prosesingSql {\n #helpers;');
|
|
26
|
+
procesingSqlCode = procesingSqlCode.replace(/this\.__/g, 'this.#');
|
|
27
|
+
procesingSqlCode = procesingSqlCode.replace(/^(\s*)__([a-zA-Z0-9_]+)\s*\(/gm, '$1#$2(');
|
|
28
|
+
fs.writeFileSync(procesingSqlFile, procesingSqlCode);
|
|
29
|
+
|
|
30
|
+
// 4. Refactor dbResult.js (read exposed property)
|
|
31
|
+
let dbResultFile = path.join(libDir, 'dbResult.js');
|
|
32
|
+
let dbResultCode = fs.readFileSync(dbResultFile, 'utf8');
|
|
33
|
+
dbResultCode = dbResultCode.replace(/dbTabla\.__lastSql/g, 'dbTabla.lastSql');
|
|
34
|
+
fs.writeFileSync(dbResultFile, dbResultCode);
|
|
35
|
+
|
|
36
|
+
console.log('Refactoring completed successfully.');
|