1
1
import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
2
- import { EntityType , FilteringExpressionsTree , IExpressionTree , IgxGridComponent , IgxQueryBuilderComponent } from 'igniteui-angular' ;
2
+ import { EntityType , FilteringExpressionsTree , IExpressionTree , IgxGridComponent , IgxQueryBuilderComponent , IgxStringFilteringOperand } from 'igniteui-angular' ;
3
3
import { initDbQuery } from './data-query' ;
4
4
import { format } from 'sql-formatter' ;
5
5
import initSqlJs from 'sql.js' ;
@@ -38,13 +38,24 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
38
38
}
39
39
40
40
public ngOnInit ( ) : void {
41
- this . initializeDbAndEntities ( ) ;
41
+ this . initializeDbAndEntities ( ) . then ( ( ) => {
42
+ this . fetchTablesAndSetEntities ( ) ;
43
+ this . setInitialExpressionTree ( ) ;
44
+ this . executeQuery ( ) ;
45
+ } ) ;
42
46
}
43
47
44
48
public ngOnDestroy ( ) : void {
45
49
this . db . close ( ) ;
46
50
}
47
51
52
+ public executeQuery ( ) {
53
+ const sqlQuery = this . transformExpressionTreeToSqlQuery ( this . expressionTree ) ;
54
+ this . sqlQuery = format ( sqlQuery ) ;
55
+ this . sqlQueryResult = this . db . exec ( this . sqlQuery ) ;
56
+ this . setGridData ( ) ;
57
+ }
58
+
48
59
private async initializeDbAndEntities ( ) {
49
60
const SQL = await initSqlJs ( {
50
61
locateFile : file => `https://sql.js.org/dist/${ file } `
@@ -55,8 +66,9 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
55
66
56
67
// create tables and insert data
57
68
this . db . run ( initDbQuery ) ;
69
+ }
58
70
59
-
71
+ private fetchTablesAndSetEntities ( ) {
60
72
// get table names with fields and field types
61
73
const tables = this . db . exec ( "SELECT name FROM sqlite_master WHERE type='table'" ) ;
62
74
@@ -94,14 +106,32 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
94
106
this . entities = entities ;
95
107
}
96
108
97
- public handleExpressionTreeChange ( event : IExpressionTree ) {
98
- const sqlQuery = this . transformExpressionTreeToSqlQuery ( this . queryBuilder . expressionTree ) ;
99
- this . sqlQuery = format ( sqlQuery ) ;
100
- this . sqlQueryResult = this . db . exec ( this . sqlQuery ) ;
101
- this . setGridData ( ) ;
109
+ private setInitialExpressionTree ( ) {
110
+ const categoriesTree = new FilteringExpressionsTree ( 0 , undefined , 'categories' , [ 'category_id' ] ) ;
111
+ categoriesTree . filteringOperands . push ( {
112
+ fieldName : 'category_name' ,
113
+ conditionName : IgxStringFilteringOperand . instance ( ) . condition ( 'equals' ) . name ,
114
+ searchVal : 'Beverages'
115
+ } ) ;
116
+
117
+ const productsTree = new FilteringExpressionsTree ( 0 , undefined , 'products' , [ 'supplier_id' ] ) ;
118
+ productsTree . filteringOperands . push ( {
119
+ fieldName : 'category_id' ,
120
+ conditionName : IgxStringFilteringOperand . instance ( ) . condition ( 'inQuery' ) . name ,
121
+ searchTree : categoriesTree
122
+ } ) ;
123
+
124
+ const suppliersTree = new FilteringExpressionsTree ( 0 , undefined , 'suppliers' , [ '*' ] ) ;
125
+ suppliersTree . filteringOperands . push ( {
126
+ fieldName : 'supplier_id' ,
127
+ conditionName : IgxStringFilteringOperand . instance ( ) . condition ( 'inQuery' ) . name ,
128
+ searchTree : productsTree
129
+ } ) ;
130
+
131
+ this . expressionTree = suppliersTree ;
102
132
}
103
133
104
- private transformExpressionTreeToSqlQuery ( tree : any ) : string {
134
+ private transformExpressionTreeToSqlQuery ( tree : IExpressionTree ) : string {
105
135
if ( ! tree ) {
106
136
return '' ;
107
137
}
@@ -137,11 +167,13 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
137
167
private buildCondition ( operand : any ) : string {
138
168
const field = operand . fieldName ;
139
169
const value = operand . searchVal ;
140
- const condition = operand . condition . name ;
170
+ const condition = operand . conditionName ;
141
171
142
172
switch ( condition ) {
143
173
case 'equals' :
144
174
return `${ field } = '${ value } '` ;
175
+ case 'doesNotEqual' :
176
+ return `${ field } <> '${ value } '` ;
145
177
case 'contains' :
146
178
return `${ field } LIKE '%${ value } %'` ;
147
179
case 'startsWith' :
@@ -156,8 +188,6 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
156
188
return `${ field } >= ${ value } ` ;
157
189
case 'lessThanOrEqualTo' :
158
190
return `${ field } <= ${ value } ` ;
159
- case 'doesNotEqual' :
160
- return `${ field } <> ${ value } ` ;
161
191
case 'doesNotContain' :
162
192
return `${ field } NOT LIKE '%${ value } %'` ;
163
193
case 'doesNotStartWith' :
@@ -168,7 +198,7 @@ export class QueryBuilderSqlSampleComponent implements OnInit, OnDestroy {
168
198
return `${ field } IS NULL` ;
169
199
case 'notNull' :
170
200
return `${ field } IS NOT NULL` ;
171
- case 'empty' :
201
+ case 'empty' :
172
202
return `${ field } = ''` ;
173
203
case 'notEmpty' :
174
204
return `${ field } <> ''` ;
0 commit comments