masterrecord 0.0.24 → 0.0.26
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/DeleteManager.js +51 -0
- package/Entity/EntityModel.js +94 -30
- package/Entity/EntityModelBuilder.js +6 -28
- package/Entity/EntityTrackerModel.js +200 -20
- package/InsertManager.js +138 -0
- package/MYSQLEngine.js +409 -0
- package/Masterrecord.js +178 -119
- package/Migrations/cli.js +3 -2
- package/Migrations/migrations.js +45 -26
- package/QueryLanguage/queryManager.js +66 -0
- package/QueryLanguage/queryMethods.js +171 -0
- package/QueryLanguage/queryScript.js +331 -0
- package/SQLLiteEngine.js +409 -0
- package/Tools.js +97 -34
- package/package.json +7 -11
- package/QueryLanguage/_Expression.js +0 -322
- package/QueryLanguage/_LogicalQuery.js +0 -23
- package/QueryLanguage/_OperatorList.js +0 -88
- package/QueryLanguage/_QueryModel.js +0 -442
- package/QueryLanguage/_Tokenization.js +0 -173
- package/QueryLanguage/__Query.js +0 -386
- package/QueryLanguage/_simpleQuery.js +0 -184
- package/QueryLanguage/queryBuilder.js +0 -52
- package/SQLiteEngine.js +0 -56
package/QueryLanguage/__Query.js
DELETED
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var Expression = require("masterrecord/Expression");
|
|
3
|
-
// TODO: support for MySQL and Postgres Comming
|
|
4
|
-
/// TODO: Adding a ! ads not to the query
|
|
5
|
-
|
|
6
|
-
// IDEAS
|
|
7
|
-
// https://stackoverflow.com/questions/10455414/with-entity-framework-is-it-better-to-use-first-or-take1-for-top-1
|
|
8
|
-
|
|
9
|
-
// NEXT VERSION ADD LAMBDA SUPPORT
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class QueryLanguage{
|
|
13
|
-
constructor(model, modelName, context) {
|
|
14
|
-
this.__info__ = {};
|
|
15
|
-
this.__info__.context = context;
|
|
16
|
-
this.__info__.sexpress = new Expression();
|
|
17
|
-
this.__info__.name = modelName;
|
|
18
|
-
this.__info__.model = model;
|
|
19
|
-
this.__info__.selectFields = [];
|
|
20
|
-
this.__info__.orders = [];
|
|
21
|
-
this.__info__.groups = [];
|
|
22
|
-
this.__info__.where = [];
|
|
23
|
-
this.__info__.joins = [];
|
|
24
|
-
this.__info__.skipValue = null;
|
|
25
|
-
this.__info__.except = null;
|
|
26
|
-
this.__info__.limitValue = null;
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
skip(amount){
|
|
31
|
-
/*
|
|
32
|
-
|
|
33
|
-
SELECT Name,
|
|
34
|
-
ProductNumber,
|
|
35
|
-
StandardCost
|
|
36
|
-
FROM Production.Product
|
|
37
|
-
ORDER BY StandardCost
|
|
38
|
-
OFFSET 10 ROWS
|
|
39
|
-
FETCH NEXT 10 ROWS ONLY
|
|
40
|
-
|
|
41
|
-
// You can use OFFSET without FETCH, but FETCH can’t be used by itself.
|
|
42
|
-
// Regardless, OFFSET must be used with an ORDER BY clause.
|
|
43
|
-
// The reason is simple as OFFSET and FETCH are part of the ORDER BY clause
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
let skip = Math.floor(amount);
|
|
47
|
-
if (skip < 0) {
|
|
48
|
-
throw new Error("Negative values are not allowed.");
|
|
49
|
-
}
|
|
50
|
-
this.__info__.skipValue = skip === 0 ? null : skip;
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
take(amount){
|
|
55
|
-
// SELECT * FROM Table ORDER BY ID DESC LIMIT AMOUNT
|
|
56
|
-
let take = Math.floor(amount);
|
|
57
|
-
if (take < 0) {
|
|
58
|
-
throw new Error("Negative values are not allowed.");
|
|
59
|
-
}
|
|
60
|
-
this.__info__.limitValue = take === 0 ? null : take;
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
last(exp){
|
|
65
|
-
// SELECT * FROM Table ORDER BY ID DESC LIMIT 1
|
|
66
|
-
const expr = this.__info__.sexpress.matchExpression(exp); // will return selectFields
|
|
67
|
-
this.__info__.orders.push({ name: "LAST", func: expr.selectFields + " DESC", arg: "DESC" });
|
|
68
|
-
this.__info__.limitValue = 1;
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
orderByDescending(expr){
|
|
73
|
-
// SELECT * FROM Table ORDER BY ID DESC
|
|
74
|
-
const expr = this.__info__.sexpress.matchExpression(exp);
|
|
75
|
-
this.__info__.orders.push({ name: "DESC", func: expr.selectFields + " DESC", arg: "DESC" });
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
orderBy(expr){
|
|
80
|
-
// SELECT * FROM Table ORDER BY ID ASC
|
|
81
|
-
const expr = this.__info__.sexpress.matchExpression(exp);
|
|
82
|
-
this.__info__.orders.push({ name: "ORDER BY", func:"ORDER BY " + expr.selectFields + " ASC", arg: "ASC" });
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
groupBy(exp){
|
|
87
|
-
/*
|
|
88
|
-
SELECT column_name(s)
|
|
89
|
-
FROM table_name
|
|
90
|
-
WHERE condition
|
|
91
|
-
GROUP BY column1, column2
|
|
92
|
-
ORDER BY column_name(s);
|
|
93
|
-
*/
|
|
94
|
-
const expr = this.__info__.sexpress.matchExpression(exp);
|
|
95
|
-
this.__info__.groups.push({name: "GROUP BY", func:"GROUP BY " + expr.selectFields + " ASC", order: "ASC" });
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
union(expr){
|
|
100
|
-
// https://www.tabsoverspaces.com/233043-union-and-concat-in-linq-to-entities
|
|
101
|
-
// https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-4-query-methods
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
find(id){
|
|
105
|
-
// Select * where id = "yaho"
|
|
106
|
-
this.__info__.where.push({ ame: "ID", func: "ID = " + id, arg: "=" , value: id});
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
where(exp, ...args){
|
|
111
|
-
// TODO: MUST PARSE CONTAINS AND LIKE FUNCTIONS
|
|
112
|
-
//https://github.com/Hookyns/unimapperjs/blob/master/src/Query.js;
|
|
113
|
-
this.__info__.sexpress.addExpression(exp, ...args);
|
|
114
|
-
var where = this.__info__.sexpress.whereArgs;
|
|
115
|
-
var conditions = this.__info__.sexpress.conditions
|
|
116
|
-
return this;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
except(exp){
|
|
120
|
-
/*
|
|
121
|
-
The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows
|
|
122
|
-
from the first SELECT statement that are not returned by the second SELECT statement
|
|
123
|
-
This means EXCEPT returns only rows, which are not available in the second SELECT statement.
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
/*
|
|
127
|
-
SELECT ID, NAME, AMOUNT, DATE
|
|
128
|
-
FROM CUSTOMERS
|
|
129
|
-
LEFT JOIN ORDERS
|
|
130
|
-
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
|
|
131
|
-
EXCEPT
|
|
132
|
-
SELECT ID, NAME, AMOUNT, DATE
|
|
133
|
-
FROM CUSTOMERS
|
|
134
|
-
RIGHT JOIN ORDERS
|
|
135
|
-
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
|
|
136
|
-
*/
|
|
137
|
-
this.__info__.except = this.__info__.sexpress.matchExpression(exp);
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
any(exp, ...args){
|
|
142
|
-
// https://stackoverflow.com/questions/12429185/linq-to-entities-any-vs-first-vs-exists
|
|
143
|
-
// checks is this expression exist
|
|
144
|
-
// this.__info__.where = null;
|
|
145
|
-
// SELECT *
|
|
146
|
-
// FROM customers
|
|
147
|
-
// WHERE EXISTS (SELECT *
|
|
148
|
-
// FROM order_details
|
|
149
|
-
// WHERE customers.customer_id = order_details.customer_id);
|
|
150
|
-
|
|
151
|
-
this.__info__.where = [{name: "EXISTS", func: "EXISTS(" + this.__info__.sexpress.addExpression(exp, ...args) + ")"}];
|
|
152
|
-
return this;
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
//============================================================================================
|
|
158
|
-
// ============================== */ Aggregate functions /* ===============================
|
|
159
|
-
//============================================================================================
|
|
160
|
-
|
|
161
|
-
// function returns the number of rows that matches a specified criteria.
|
|
162
|
-
select(exp){
|
|
163
|
-
// nested selects,
|
|
164
|
-
// https://benjii.me/2018/01/expression-projection-magic-entity-framework-core/
|
|
165
|
-
|
|
166
|
-
const expr = this.__info__.sexpress.initExpression(exp, this.__info__.model,this.__info__.name); // will return selectFields
|
|
167
|
-
var entity = expr.entity;
|
|
168
|
-
|
|
169
|
-
// loop through fields
|
|
170
|
-
for (const field of expr.selectFields) {
|
|
171
|
-
this.__info__.selectFields.push({func: "[" + entity + "]." + "[" + field + "]"});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
for (const virtualObj of expr.virtualFields) {
|
|
175
|
-
var nestedField = []
|
|
176
|
-
// if context has many/collection of the field name the next item after context should be an arg
|
|
177
|
-
// if context has on of the field it's not a collection then the next field should be a field of context.
|
|
178
|
-
var virtualObjff = this.__info__.sexpress.getContext(virtualObj, this.__info__.context);
|
|
179
|
-
// virtualObjff returns the next item with the context
|
|
180
|
-
// todo does it pass the rule
|
|
181
|
-
if(virtualObjff.type === "collection"){
|
|
182
|
-
// next item should be an argument
|
|
183
|
-
// TODO: this will create a new instance of query language and call all its arguments and return context values.
|
|
184
|
-
var nestedInstance = this.__info__.sexpress.nestedQueryBuilder(virtualObjff);
|
|
185
|
-
nestedField.push(nestedInstance);
|
|
186
|
-
// or throw an error
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
else{
|
|
190
|
-
// TODO: run loop aagain and build nested objects
|
|
191
|
-
}
|
|
192
|
-
var name = "";
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// TODO : all nested fields of that specific context should have an argument on the next call
|
|
196
|
-
|
|
197
|
-
// TODO:
|
|
198
|
-
// loop through virtualFields
|
|
199
|
-
// create new object using the virtual name
|
|
200
|
-
// if it doesn't exist then throw error
|
|
201
|
-
// then get the next element on the string
|
|
202
|
-
|
|
203
|
-
// check if the next element is a field of that context.
|
|
204
|
-
// if not then check if its a nested object
|
|
205
|
-
// if its not a nested object and not field then throw error
|
|
206
|
-
// if it is then call that object with the inner expression.
|
|
207
|
-
// if it is a field then get the next element after that
|
|
208
|
-
|
|
209
|
-
// then check if the element is a nested arguments
|
|
210
|
-
// if it is then get the inside of that string and pass it to the nested arguemnt
|
|
211
|
-
// then run build query
|
|
212
|
-
// return that query and add it the select fields func name.
|
|
213
|
-
// loop through nestedFields
|
|
214
|
-
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
distinct(){
|
|
219
|
-
// SELECT DISTINCT
|
|
220
|
-
this.__info__.selectFields.push({name: "DISTINCT", func: "DISTINCT", arg: ""});
|
|
221
|
-
return this;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
count(columnName){
|
|
225
|
-
// todo: if columnName not added put *
|
|
226
|
-
// SELECT COUNT(column_name)
|
|
227
|
-
this.__info__.selectFields.push({ name: "COUNT", func: "COUNT(" + columnName + ")", arg: columnName});
|
|
228
|
-
return this;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// function returns the average value of a numeric column.
|
|
232
|
-
average(columnName){
|
|
233
|
-
// SELECT AVG(column_name)
|
|
234
|
-
this.__info__.selectFields.push({ name: "AVG", func: "AVG(" + columnName + ")", arg: columnName});
|
|
235
|
-
return this;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// function returns the total sum of a numeric column.
|
|
239
|
-
sum(columnName){
|
|
240
|
-
// SELECT SUM(column_name)
|
|
241
|
-
this.__info__.selectFields.push({ name: "SUM", func: "SUM(" + columnName + ")", arg: columnName});
|
|
242
|
-
return this;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// function returns the largest value of the selected column.
|
|
246
|
-
max(columnName){
|
|
247
|
-
//SELECT MAX(column_name)
|
|
248
|
-
this.__info__.selectFields.push({ name: "MAX", func: "MAX(" + columnName + ")", arg: columnName});
|
|
249
|
-
return this;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// function returns the smallest value of the selected column.
|
|
253
|
-
min(columnName){
|
|
254
|
-
// SELECT MIN(column_name)
|
|
255
|
-
this.__info__.selectFields.push({ name: "MIN", func: "MIN(" + columnName + ")", arg: columnName});
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
//============================================================================================
|
|
261
|
-
// ============================== */ Join functions /* ===============================
|
|
262
|
-
//============================================================================================
|
|
263
|
-
|
|
264
|
-
join(tableName, primaryKeyExp, foreignKeyExp ){
|
|
265
|
-
// SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
|
|
266
|
-
// FROM Orders
|
|
267
|
-
// INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
|
|
268
|
-
const primaryKeyExpr = this.__info__.sexpress.matchExpression(primaryKeyExp);
|
|
269
|
-
const ForeignKeyExpr = this.__info__.sexpress.matchExpression(foreignKeyExp);
|
|
270
|
-
|
|
271
|
-
this.__info__.joins.push({ func: "JOIN", arg:{primaryKeyExpr : primaryKeyExpr, ForeignKeyExpr: ForeignKeyExpr} });
|
|
272
|
-
// RETURNS AN ARRAY OBJECTS
|
|
273
|
-
// [{
|
|
274
|
-
// FIRSTNAME: RICHARD,
|
|
275
|
-
// NICKNAME : JAMES
|
|
276
|
-
// },
|
|
277
|
-
// {
|
|
278
|
-
// FIRSTNAME: ROBERT,
|
|
279
|
-
// NICKNAME : ALEX
|
|
280
|
-
// }]
|
|
281
|
-
return this;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
groupJoin(tableName, primaryKeyExp, foreignKeyExp ){
|
|
285
|
-
// https://sqlzoo.net/wiki/The_JOIN_operation
|
|
286
|
-
//https://arnhem.luminis.eu/linq-and-entity-framework-some-dos-and-donts/
|
|
287
|
-
//IMPORTANT https://weblogs.thinktecture.com/pawel/2018/04/entity-framework-core-performance-beware-of-n1-queries.html
|
|
288
|
-
// SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
|
|
289
|
-
// FROM Orders
|
|
290
|
-
// INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
|
|
291
|
-
const primaryKeyExpr = this.__info__.sexpress.matchExpression(primaryKeyExp);
|
|
292
|
-
const ForeignKeyExpr = this.__info__.sexpress.matchExpression(foreignKeyExp);
|
|
293
|
-
|
|
294
|
-
this.__info__.joins.push({ func: "GROUPJOIN", arg:{primaryKeyExpr : primaryKeyExpr, ForeignKeyExpr: ForeignKeyExpr} });
|
|
295
|
-
// https://stackoverflow.com/questions/15595289/linq-to-entities-join-vs-groupjoin
|
|
296
|
-
// RETURNS AN ARRAY OF GROUPED ARRAYS OBJECTS BY WHATEVER PRIMARY KEY YOU USED
|
|
297
|
-
// [
|
|
298
|
-
//[{
|
|
299
|
-
// FIRSTNAME: RICHARD,
|
|
300
|
-
// NICKNAME : JAMES
|
|
301
|
-
// }
|
|
302
|
-
//],
|
|
303
|
-
// [{
|
|
304
|
-
// FIRSTNAME: ROBERT,
|
|
305
|
-
// NICKNAME : ALEX
|
|
306
|
-
// }]
|
|
307
|
-
// ]
|
|
308
|
-
return this;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
//============================================================================================
|
|
312
|
-
// ============================== */ trigger will make call /* ===============================
|
|
313
|
-
//============================================================================================
|
|
314
|
-
|
|
315
|
-
// Anything that returns a concrete object or data structure
|
|
316
|
-
// (Count, Sum Single, First, ToList, ToArray, etc.) is evaluated immediately, so
|
|
317
|
-
// SingleOrDefault certainly does.
|
|
318
|
-
|
|
319
|
-
// Anything that returns an IQueryable<T> (Select, GroupBy, Take) will be deferred
|
|
320
|
-
// (so that operations can be chained), so Queryable.Union will be deferred.
|
|
321
|
-
|
|
322
|
-
// Anything that returns an IEnumerable<T> will also be deferred, but subsequent queries
|
|
323
|
-
// will be done in Linq-to-objects, so subsequent operations
|
|
324
|
-
// won't be translated to SQL. (Empty is an exception since there's not really
|
|
325
|
-
// anything to defer - it just returns an empty collection)
|
|
326
|
-
|
|
327
|
-
// Entity Framework
|
|
328
|
-
// SqlQuery
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
// THESE ARE RETURN TYPES METHOD'S: these dont return this;
|
|
332
|
-
// ToArray does the exact thing as tolist just syntax suger.
|
|
333
|
-
// ToDictionary(kvp => kvp.Key, kvp => kvp.Value) // this will turn it into an object literal with column name and value.
|
|
334
|
-
// ToList<TSource> return array list
|
|
335
|
-
// single // return a single object literal.
|
|
336
|
-
|
|
337
|
-
// https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/supported-and-unsupported-linq-methods-linq-to-entities
|
|
338
|
-
toArray(){
|
|
339
|
-
// TODO: run query and parse results
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
toDictionary(){
|
|
343
|
-
// TODO: run query and parse results
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
toList(){
|
|
347
|
-
// TODO: run query and parse results
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
single(){
|
|
353
|
-
// TODO: run query and parse results
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
module.exports = QueryLanguage;
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
// this.trigger = function(queryName, query){
|
|
363
|
-
// let queryString = "";
|
|
364
|
-
// switch(queryName) {
|
|
365
|
-
// case "count":
|
|
366
|
-
// queryString += query;
|
|
367
|
-
// break;
|
|
368
|
-
// case undefined:
|
|
369
|
-
// queryString += `SELECT ${this.name}.* FROM ${this.name}`;
|
|
370
|
-
// break;
|
|
371
|
-
|
|
372
|
-
// }
|
|
373
|
-
|
|
374
|
-
// for (var i = 0; i < this.query.length; i++) {
|
|
375
|
-
// queryString += " " + this.query[i];
|
|
376
|
-
// }
|
|
377
|
-
|
|
378
|
-
// var select = selectColumns(this._model);
|
|
379
|
-
// // TODO: COMBINE THIS_MODEL WITH THE MODEL THAT IS BEING RETURNED
|
|
380
|
-
// // loop though model
|
|
381
|
-
// //
|
|
382
|
-
// var qq = "select "+ select +" "+query;
|
|
383
|
-
// var data = Masterrecord.db.prepare(qq).get();
|
|
384
|
-
// return callGets(data, this._model);
|
|
385
|
-
|
|
386
|
-
// }
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
// ALL THIS SHOULD DO IS BUILD A SQL QUERY
|
|
2
|
-
var EntityTrackerModel = require('masterrecord/Entity/EntityTrackerModel');
|
|
3
|
-
var Tokenize = require('masterrecord/QueryLanguage/_Tokenization');
|
|
4
|
-
var QueryModel = require('masterrecord/QueryLanguage/_QueryModel');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class simpleQuery{
|
|
8
|
-
constructor(model, context) {
|
|
9
|
-
this.__model = model;
|
|
10
|
-
this.__context = context;
|
|
11
|
-
this.__contextList = context.__allContexts;
|
|
12
|
-
this.__queryModel = new QueryModel(model, context);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
raw(query){
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
add(entityValue){
|
|
20
|
-
// This will call context API to REMOVE entity to update list
|
|
21
|
-
var ent = EntityTrackerModel.build(entityValue, this.__model);
|
|
22
|
-
ent.__state = "insert";
|
|
23
|
-
this.__context.__Track(ent);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
remove(entityValue){
|
|
27
|
-
// This will call context API to REMOVE entity to Delete list
|
|
28
|
-
var ent = EntityTrackerModel.build(entityValue, this.__model);
|
|
29
|
-
ent.__state = "delete";
|
|
30
|
-
this.__context.__Track(ent);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
track(entityValue){
|
|
34
|
-
var ent = EntityTrackerModel.build(entityValue, this.__model);
|
|
35
|
-
ent.__state = "track";
|
|
36
|
-
return this.__context.__Track(ent);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
toList(){
|
|
41
|
-
// Rule you cannot do fucntions on queryNames s.count() will through error
|
|
42
|
-
/// select delimiter
|
|
43
|
-
// select [ID] AS [ID],
|
|
44
|
-
// CASE WHEN ([Extent1].[Name] LIKE N'%jist%') THEN cast(1 as bit) WHEN ( NOT ([Extent1].[Name] LIKE N'%Jist%')) THEN cast(0 as bit) END AS [C1],
|
|
45
|
-
// cannot do where on fields that dont have relationships;;
|
|
46
|
-
var qq = `s => ({
|
|
47
|
-
id: s.Id,
|
|
48
|
-
jj: s.Name != "richy" && s.james === "josj",
|
|
49
|
-
name: !s.Name.contains("jist"),
|
|
50
|
-
NumberOfFruits : s.Fruits.where(r => r.name == "richard").single().Courses.distinct()
|
|
51
|
-
}`;
|
|
52
|
-
|
|
53
|
-
var were = `s => s.Fruits.where(r => r.name == "richard").single().flys.distinct().toList()`;
|
|
54
|
-
|
|
55
|
-
var QueryModelTest = {
|
|
56
|
-
name : "posts",
|
|
57
|
-
limit:{},
|
|
58
|
-
dateRange: "",
|
|
59
|
-
conditions:[],
|
|
60
|
-
selects: [{
|
|
61
|
-
returnName : "id",
|
|
62
|
-
field : {
|
|
63
|
-
property: "s",
|
|
64
|
-
name : "id"
|
|
65
|
-
},
|
|
66
|
-
property : "s",
|
|
67
|
-
},{
|
|
68
|
-
returnName : "jj",
|
|
69
|
-
field : {
|
|
70
|
-
property : "s",
|
|
71
|
-
name : "Name"
|
|
72
|
-
},
|
|
73
|
-
property : "s",
|
|
74
|
-
operators : [{
|
|
75
|
-
property : "s",
|
|
76
|
-
field: "Name",
|
|
77
|
-
operator: "notEqual",
|
|
78
|
-
value : "richy",
|
|
79
|
-
logicalOperator : "and"
|
|
80
|
-
},{
|
|
81
|
-
property : "s",
|
|
82
|
-
field: "james",
|
|
83
|
-
operator: "equal",
|
|
84
|
-
value : "josj",
|
|
85
|
-
logicalOperator : ""
|
|
86
|
-
}]
|
|
87
|
-
},{
|
|
88
|
-
returnName : "name",
|
|
89
|
-
field : {
|
|
90
|
-
property : "s",
|
|
91
|
-
name : "name"
|
|
92
|
-
},
|
|
93
|
-
property : "s",
|
|
94
|
-
operator: [{
|
|
95
|
-
property : "s",
|
|
96
|
-
field: "name",
|
|
97
|
-
operator: "not",
|
|
98
|
-
value : "",
|
|
99
|
-
logicalOperator : "",
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
property : "s",
|
|
103
|
-
field: "Name",
|
|
104
|
-
operator: "in",
|
|
105
|
-
value : "jist",
|
|
106
|
-
logicalOperator : "",
|
|
107
|
-
}]
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
returnName : "NumberOfFruits",
|
|
111
|
-
fields : {},
|
|
112
|
-
property : "s",
|
|
113
|
-
operator: [],
|
|
114
|
-
}
|
|
115
|
-
],
|
|
116
|
-
relationships : [{ // union all relationships together
|
|
117
|
-
name: "Fruits", // could be changed nested type
|
|
118
|
-
dateRange: "",
|
|
119
|
-
limits: {
|
|
120
|
-
amount :1,
|
|
121
|
-
object : "single" // nested limit will produce inner select top
|
|
122
|
-
},
|
|
123
|
-
selects:[],
|
|
124
|
-
relationships: [{
|
|
125
|
-
name: "flys", // could be changed nested type
|
|
126
|
-
dateRange: "",
|
|
127
|
-
limits: {},
|
|
128
|
-
selects:[],
|
|
129
|
-
relationships: [],
|
|
130
|
-
conditions: []
|
|
131
|
-
}],
|
|
132
|
-
conditions: [{
|
|
133
|
-
type: "where",
|
|
134
|
-
context: "r",
|
|
135
|
-
field : {
|
|
136
|
-
property : "r",
|
|
137
|
-
name : "id"
|
|
138
|
-
},
|
|
139
|
-
operators: [{
|
|
140
|
-
property : "r",
|
|
141
|
-
field: "name",
|
|
142
|
-
operator: "equal",
|
|
143
|
-
value : "richard",
|
|
144
|
-
logicalOperator : ""
|
|
145
|
-
}],
|
|
146
|
-
}]
|
|
147
|
-
}]
|
|
148
|
-
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
var tt = new Tokenize();
|
|
153
|
-
var tokenList = tt.Tokenize(qq);
|
|
154
|
-
var james = this.__queryModel.select(tokenList);
|
|
155
|
-
var jj = "";
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
where(query){
|
|
159
|
-
if(query !== undefined || query !== null){
|
|
160
|
-
var tt = new Tokenize();
|
|
161
|
-
var tokenList = tt.Tokenize(query);
|
|
162
|
-
this.__queryModel.condition(tokenList, "where");
|
|
163
|
-
}
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
select(query){
|
|
168
|
-
var tt = new Tokenize();
|
|
169
|
-
var tokenList = tt.Tokenize(query);
|
|
170
|
-
this.__queryModel.select(tokenList);
|
|
171
|
-
return this;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// can only be used at the end of a query not an inner query
|
|
175
|
-
single(){
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
distinct(){
|
|
180
|
-
return this;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
module.exports = simpleQuery;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// version 1.0.15
|
|
3
|
-
|
|
4
|
-
var entityTrackerModel = require('masterrecord/Entity/EntityTrackerModel');
|
|
5
|
-
|
|
6
|
-
class queryBuilder{
|
|
7
|
-
constructor(model, context) {
|
|
8
|
-
this.__model = model;
|
|
9
|
-
this.__context = context;
|
|
10
|
-
this.__contextList = context.__allContexts;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
raw(query){
|
|
14
|
-
// get the query
|
|
15
|
-
var entityValue = this.__context._SQLEngine.get(query);
|
|
16
|
-
if(entityValue){
|
|
17
|
-
var ent = new entityTrackerModel();
|
|
18
|
-
ent.build(entityValue, this.__model);
|
|
19
|
-
ent.__state = "track";
|
|
20
|
-
this.__context.__Track(ent);
|
|
21
|
-
return ent;
|
|
22
|
-
}else{
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
add(entityValue){
|
|
29
|
-
// This will call context API to REMOVE entity to update list
|
|
30
|
-
var ent = new entityTrackerModel();
|
|
31
|
-
ent.build(entityValue, this.__model);
|
|
32
|
-
ent.__state = "insert";
|
|
33
|
-
this.__context.__Track(ent);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
remove(entityValue){
|
|
37
|
-
// This will call context API to REMOVE entity to Delete list
|
|
38
|
-
var ent = new entityTrackerModel();
|
|
39
|
-
ent.build(entityValue, this.__model);
|
|
40
|
-
ent.__state = "delete";
|
|
41
|
-
this.__context.__Track(ent);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
track(entityValue){
|
|
45
|
-
var ent = new entityTrackerModel();
|
|
46
|
-
ent.build(entityValue, this.__model);
|
|
47
|
-
ent.__state = "track";
|
|
48
|
-
return this.__context.__Track(ent);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = queryBuilder;
|
package/SQLiteEngine.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
class SQLiteEngine {
|
|
3
|
-
update(query){
|
|
4
|
-
var query = ` UPDATE ${query.tableName}
|
|
5
|
-
SET ${query.arg}
|
|
6
|
-
WHERE ${query.primaryKey} = ${query.value}` // primary key for that table =
|
|
7
|
-
return this.execute(query);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
delete(query){
|
|
11
|
-
var query = `DELETE FROM ${query.tableName} WHERE ${query.primaryKey} = ${query.value}`;
|
|
12
|
-
return this.execute(query);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
insert(query){
|
|
16
|
-
var query = `INSERT INTO ${query.tableName} (${query.columns})
|
|
17
|
-
VALUES (${query.values})`;
|
|
18
|
-
return this.execute(query);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
execute(query){
|
|
22
|
-
|
|
23
|
-
switch(this.db.__name) {
|
|
24
|
-
case "better-sqlite3":
|
|
25
|
-
console.log("SQL:", query);
|
|
26
|
-
return this.db.exec(query);
|
|
27
|
-
// code block
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get(query){
|
|
33
|
-
switch(this.db.__name) {
|
|
34
|
-
case "better-sqlite3":
|
|
35
|
-
try {
|
|
36
|
-
console.log("SQL:", query);
|
|
37
|
-
return this.db.prepare(query).get();
|
|
38
|
-
} catch (err) {
|
|
39
|
-
console.error(err);
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
// code block
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
setDB(env, sqlName){
|
|
48
|
-
const sqlite3 = require(sqlName);
|
|
49
|
-
let DBAddress = `${env.connection}${env.env}.sqlite3`;
|
|
50
|
-
this.db = new sqlite3(DBAddress, env);
|
|
51
|
-
db.__name = sqlName;
|
|
52
|
-
return db;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
module.exports = SQLiteEngine;
|