alasql 4.18.0 → 4.19.1

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.
@@ -49,8 +49,11 @@ function compileToJS(sql, databaseid) {
49
49
  const orderfn = ${orderfnStr};
50
50
  const removeKeys = ${JSON.stringify(query.removeKeys || [])};
51
51
  const distinct = ${JSON.stringify(query.distinct)};
52
- const limit = ${JSON.stringify(query.limit)};
53
- const offset = ${JSON.stringify(query.offset)};
52
+ const fixedLimit = ${JSON.stringify(query.limit)};
53
+ const fixedOffset = ${JSON.stringify(query.offset)};
54
+ const limitParam = ${JSON.stringify(query.limitParam)};
55
+ const offsetParam = ${JSON.stringify(query.offsetParam)};
56
+ const percent = ${JSON.stringify(query.percent)};
54
57
  const alias = ${alias};
55
58
 
56
59
  // Get data from source (either params or database table)
@@ -67,6 +70,8 @@ function compileToJS(sql, databaseid) {
67
70
  }
68
71
 
69
72
  // Execute query
73
+ const limit = typeof limitParam !== 'undefined' ? (params ? params[limitParam] : undefined) : fixedLimit;
74
+ const offset = typeof offsetParam !== 'undefined' ? (params ? params[offsetParam] : undefined) : fixedOffset;
70
75
  let result = [];
71
76
  for (let i = 0; i < data.length; i++) {
72
77
  const p = {};
@@ -101,11 +106,15 @@ function compileToJS(sql, databaseid) {
101
106
  }
102
107
 
103
108
  // Apply OFFSET and LIMIT
104
- if (offset) {
105
- result = result.slice(offset);
106
- }
107
109
  if (limit) {
108
- result = result.slice(0, limit);
110
+ let start = offset | 0 || 0;
111
+ start = start < 0 ? 0 : start;
112
+ const end = percent ? (((result.length * limit) / 100) | 0) + start : (limit | 0) + start;
113
+ result = result.slice(start, end);
114
+ } else if (offset) {
115
+ let start = offset | 0 || 0;
116
+ start = start < 0 ? 0 : start;
117
+ result = result.slice(start);
109
118
  }
110
119
 
111
120
  if (cb) cb(result);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "alasql",
3
3
  "description": "Use SQL to select and filter javascript data - including relational joins and search in nested objects (JSON). Export to and import from Excel and CSV",
4
- "version": "4.18.0",
4
+ "version": "4.19.1",
5
5
  "author": "Andrey Gershun <agershun@gmail.com>",
6
6
  "contributors": [
7
7
  "Mathias Wulff <m@rawu.dk>"
@@ -64,11 +64,11 @@
64
64
  "blueimp-md5": "2.19.0",
65
65
  "cmdmix": "2.2.2",
66
66
  "dom-storage": "2.1.0",
67
- "esbuild": "0.28.1",
67
+ "esbuild": "0.28.2",
68
68
  "git-branch-is": "5.0.0",
69
69
  "husky": "9.1.7",
70
70
  "jison": "^0.4.18",
71
- "open": "11.0.0",
71
+ "open": "11.0.2",
72
72
  "prettier": "3.8.3",
73
73
  "react-native-fetch-blob": "^0.10.8",
74
74
  "rexreplace": "7.1.14",
@@ -82,7 +82,7 @@
82
82
  "json5": "2",
83
83
  "underscore": "1",
84
84
  "glob-parent": "6",
85
- "decode-uri-component": "0.4",
85
+ "decode-uri-component": "0.5",
86
86
  "semver": "7",
87
87
  "follow-redirects": "^1.16.0",
88
88
  "js-yaml": "4",
package/types/alasql.d.ts CHANGED
@@ -4,11 +4,11 @@ declare module 'alasql' {
4
4
  import * as xlsx from 'xlsx';
5
5
 
6
6
  // Callback with error-first convention and optional data
7
- interface AlaSQLCallback<T = unknown> {
7
+ export interface AlaSQLCallback<T = unknown> {
8
8
  (err: Error | null, data?: T): void;
9
9
  }
10
10
 
11
- interface AlaSQLOptions {
11
+ export interface AlaSQLOptions {
12
12
  errorlog: boolean;
13
13
  valueof: boolean;
14
14
  dropifnotexists: boolean; // drop database in any case
@@ -35,34 +35,100 @@ declare module 'alasql' {
35
35
  }
36
36
 
37
37
  // compiled Statement
38
- interface AlaSQLStatement {
38
+ export interface AlaSQLStatement {
39
39
  <T = unknown>(params?: any, cb?: AlaSQLCallback<T>, scope?: unknown): T;
40
40
  }
41
41
 
42
+ export type AlaSQLDefaultColumns = {[columnName: string]: string};
43
+
44
+ /**
45
+ * Base parsed expression node.
46
+ * Node shapes below are part of the public parser AST surface and follow semver.
47
+ */
48
+ export interface AlaSQLExpression {
49
+ toJS(context: string, tableid: string | number, defcols: AlaSQLDefaultColumns | null): string;
50
+ }
51
+
52
+ export interface Op extends AlaSQLExpression {
53
+ left: ExpressionNode;
54
+ op: string;
55
+ right?: ExpressionNode;
56
+ right1?: ExpressionNode;
57
+ right2?: ExpressionNode;
58
+ }
59
+
60
+ export interface Column extends AlaSQLExpression {
61
+ columnid: string;
62
+ tableid?: string;
63
+ databaseid?: string;
64
+ }
65
+
66
+ export interface ParamValue extends AlaSQLExpression {
67
+ param: string | number;
68
+ }
69
+
70
+ export interface NumValue extends AlaSQLExpression {
71
+ value: number;
72
+ }
73
+
74
+ export interface StringValue extends AlaSQLExpression {
75
+ value: string;
76
+ }
77
+
78
+ export interface LogicValue extends AlaSQLExpression {
79
+ value: boolean;
80
+ }
81
+
82
+ export interface UniOp extends AlaSQLExpression {
83
+ op: string | null;
84
+ right: ExpressionNode;
85
+ }
86
+
87
+ export type ExpressionNode =
88
+ | Op
89
+ | Column
90
+ | ParamValue
91
+ | NumValue
92
+ | StringValue
93
+ | LogicValue
94
+ | UniOp;
95
+
96
+ export interface ExpressionWrapper extends AlaSQLExpression {
97
+ expression: ExpressionNode;
98
+ reduced?: boolean;
99
+ }
100
+
101
+ export interface Statement {
102
+ compile(databaseid: string): AlaSQLStatement;
103
+ where?: ExpressionWrapper;
104
+ [key: string]: unknown;
105
+ }
106
+
42
107
  // abstract Syntax Tree
43
- interface AlaSQLAST {
108
+ export interface AlaSQLAST {
109
+ statements: Statement[];
44
110
  compile(databaseid: string): AlaSQLStatement;
45
111
  }
46
112
 
47
113
  // see https://github.com/alasql/alasql/wiki/User%20Defined%20Functions
48
- interface userDefinedFunction {
114
+ export interface userDefinedFunction {
49
115
  (...x: unknown[]): unknown;
50
116
  }
51
117
 
52
- interface userDefinedFunctionLookUp {
118
+ export interface userDefinedFunctionLookUp {
53
119
  [x: string]: userDefinedFunction;
54
120
  }
55
121
 
56
122
  // see https://github.com/alasql/alasql/wiki/User%20Defined%20Functions
57
- interface userAggregator {
123
+ export interface userAggregator {
58
124
  (value: unknown, accumulator: unknown, stage: number): unknown;
59
125
  }
60
126
 
61
- interface userAggregatorLookUp {
127
+ export interface userAggregatorLookUp {
62
128
  [x: string]: userAggregator;
63
129
  }
64
130
 
65
- interface userFromFunction {
131
+ export interface userFromFunction {
66
132
  (
67
133
  dataReference: unknown,
68
134
  options: unknown,
@@ -72,7 +138,7 @@ declare module 'alasql' {
72
138
  ): void;
73
139
  }
74
140
 
75
- interface userFromFunctionLookUp {
141
+ export interface userFromFunctionLookUp {
76
142
  [x: string]: userFromFunction;
77
143
  }
78
144
 
@@ -81,7 +147,7 @@ declare module 'alasql' {
81
147
  *
82
148
  * @interface database
83
149
  */
84
- interface database {
150
+ export interface database {
85
151
  /**
86
152
  * The database ID.
87
153
  *
@@ -104,7 +170,7 @@ declare module 'alasql' {
104
170
  *
105
171
  * @interface table
106
172
  */
107
- interface table {
173
+ export interface table {
108
174
  /**
109
175
  * The array of data stored in the table which can be queried
110
176
  *
@@ -119,7 +185,7 @@ declare module 'alasql' {
119
185
  *
120
186
  * @interface databaseLookUp
121
187
  */
122
- interface databaseLookUp {
188
+ export interface databaseLookUp {
123
189
  [databaseName: string]: database;
124
190
  }
125
191
 
@@ -128,11 +194,11 @@ declare module 'alasql' {
128
194
  *
129
195
  * @interface tableLookUp
130
196
  */
131
- interface tableLookUp {
197
+ export interface tableLookUp {
132
198
  [tableName: string]: table;
133
199
  }
134
200
 
135
- interface Database {
201
+ export interface Database {
136
202
  new (databaseid?: string): Database;
137
203
  databaseid: string;
138
204
  dbversion: number;
@@ -150,7 +216,7 @@ declare module 'alasql' {
150
216
  autoval(tablename: string, colname: string, getNext: boolean): unknown;
151
217
  }
152
218
 
153
- interface AlaSQL {
219
+ export interface AlaSQL {
154
220
  options: AlaSQLOptions;
155
221
  error: Error;
156
222
  <T = unknown>(sql: string, params?: any, cb?: AlaSQLCallback<T>, scope?: unknown): T;