@@ -8,11 +8,74 @@ module.exports = (ast, db) => {
88 const data = db . db [ table ] ;
99 if ( ! data ) throw new Error ( `Table \"${ table } \" does not exist` ) ;
1010
11- const fn = chunk (
12- data . data . map ( ( m ) => ( { key : m . key , data : m . data ?? null } ) ) ,
13- 2
14- ) ;
15-
16- if ( limit >= 0 ) return fn . slice ( 0 , limit ) ;
17- return fn ;
11+ if ( ! ast . where ) {
12+ const fn = chunk (
13+ data . data . map ( ( m ) => ( { key : m . key , data : m . data ?? null } ) ) ,
14+ 2
15+ ) ;
16+ if ( limit >= 0 ) return handle ( fn . slice ( 0 , limit ) ) ;
17+ return handle ( fn ) ;
18+ } else {
19+ const bin = binOp ( data , { op : ast . where . operator , left : ast . where . left . value , right : ast . where . right . value } ) ;
20+ if ( limit >= 0 ) return handle ( bin . data ?. slice ( 0 , limit ) ?? [ ] ) ;
21+ return handle ( bin . data ?? [ ] ) ;
22+ }
1823} ;
24+
25+ function handle ( d ) {
26+ if ( ! d ) return [ ] ;
27+
28+ return Object . defineProperties ( d , {
29+ parse : {
30+ value : ( ) => {
31+ return d . map ( col => {
32+ let obj = { } ;
33+
34+ col . forEach ( keys => {
35+ obj [ keys . key ] = keys . data ;
36+ } ) ;
37+
38+ return obj ;
39+ } )
40+ }
41+ } ,
42+ } ) ;
43+ }
44+
45+ function binOp ( data , clause ) {
46+ switch ( clause . op ) {
47+ case '=' : {
48+ const dat = [ ] ;
49+ const child = chunk ( data . data , 2 ) ;
50+ for ( let i = 0 ; i < child . length ; i ++ ) {
51+ if ( child [ i ] . some ( m => m . key === clause . left && m . data === clause . right ) ) dat . push ( child [ i ] ) ;
52+ }
53+
54+ try {
55+ return {
56+ data : dat
57+ } ;
58+ } catch {
59+ throw new Error ( `Could not verify operation "${ clause . left } = ${ clause . right } "` ) ;
60+ }
61+ }
62+
63+ case 'IS NOT' : {
64+ const dat = [ ] ;
65+ const child = chunk ( data . data , 2 ) ;
66+ for ( let i = 0 ; i < child . length ; i ++ ) {
67+ if ( child [ i ] . some ( m => m . key === clause . left && m . data !== clause . right ) ) dat . push ( [ child [ i ] ] ) ;
68+ }
69+
70+ try {
71+ return {
72+ data : dat
73+ } ;
74+ } catch {
75+ throw new Error ( `Could not verify operation "${ clause . left } = ${ clause . right } "` ) ;
76+ }
77+ }
78+ default :
79+ return { data : [ ] } ;
80+ }
81+ }
0 commit comments