@@ -34,7 +34,6 @@ class Converter
3434{
3535 constructor ( ast ) {
3636 this . ast = ast ;
37- this . wheres = [ ] ;
3837 this . joins = [ ] ;
3938 this . table_name_by_alias = { } ;
4039 }
@@ -54,8 +53,8 @@ class Converter
5453 res = res + '->' + join_section + '\n' ;
5554 }
5655
57- if ( this . hasWhereSection ( ) ) {
58- res = res + '->' + this . resolveWhereSection ( ) + '\n' ;
56+ if ( propertyExistsInObjectAndNotNull ( this . ast . body . Select , 'selection' ) ) {
57+ res = res + '->' + this . resolveWhereSection ( this . ast . body . Select . selection ) + '\n' ;
5958 }
6059
6160 if ( this . hasGroupBySection ( ) ) {
@@ -90,57 +89,54 @@ class Converter
9089 return 'DB::table(' + this . resolveTableNameFromRelationNode ( this . ast . body . Select . from [ 0 ] . relation ) + ')' ;
9190 }
9291
93- /**
94- * @return {boolean }
95- */
96- hasWhereSection ( ) {
97- return propertyExistsInObjectAndNotNull ( this . ast . body . Select , 'selection' ) ;
98- }
92+ resolveWhereSection ( selection_node ) {
93+ let condition_type = getNestedUniqueKeyFromObject ( selection_node ) ;
94+ let condition = getNestedUniqueValueFromObject ( selection_node ) ;
9995
100- resolveWhereSection ( ) {
101- assert ( this . ast . body . Select . selection !== null , 'selection section must exist' ) ;
102-
103- let condition_type = getNestedUniqueKeyFromObject ( this . ast . body . Select . selection ) ;
104- let condition = getNestedUniqueValueFromObject ( this . ast . body . Select . selection ) ;
105-
106- this . prepareWheres ( condition_type , condition , '' ) ;
107-
108- return this . wheres . join ( '\n->' ) ;
96+ return this . prepareWheres ( condition_type , condition , '' ) . join ( '\n->' ) ;
10997 }
11098
11199 /**
112100 * @param {string } condition_type
113101 * @param {Object } condition
114102 * @param {Object } op one of ['', 'And', 'Or']
115- * @return {void }
103+ * @param {string[] } wheres
104+ * @return {string[] }
116105 */
117- prepareWheres ( condition_type , condition , op ) {
106+ prepareWheres ( condition_type , condition , op , wheres = [ ] ) {
118107 if ( condition_type === 'IsNull' || condition_type === 'IsNotNull' ) {
119108 let method_name = condition_type === 'IsNull' ? 'whereNull' : 'whereNotNull' ;
120- this . wheres . push ( this . addPrefix2WhereMethods ( op , method_name ) + '(' + this . convertIdentifier2qualifiedColumn ( condition . CompoundIdentifier ) + ')' ) ;
109+ wheres . push ( this . addPrefix2WhereMethods ( op , method_name ) + '(' + this . convertIdentifier2qualifiedColumn ( condition . CompoundIdentifier ) + ')' ) ;
121110 } else if ( condition_type === 'InList' ) {
122111 let column = this . convertIdentifier2qualifiedColumn ( getNestedUniqueValueFromObject ( condition . expr ) ) ;
123112 let list = condition . list . map ( ( i ) => this . resolveValue ( getNestedUniqueValueFromObject ( i ) ) ) ;
124113
125114 let method_name = condition . negated ? 'whereNotIn' : 'whereIn' ;
126- this . wheres . push ( this . addPrefix2WhereMethods ( op , method_name ) + '(' + column + ',' + '[' + list . join ( ', ' ) + '])' ) ;
115+ wheres . push ( this . addPrefix2WhereMethods ( op , method_name ) + '(' + column + ',' + '[' + list . join ( ', ' ) + '])' ) ;
116+ } else if ( condition_type === 'Nested' ) {
117+ wheres . push (
118+ this . addPrefix2WhereMethods ( op , 'where' ) + '(function ($query) {\n'
119+ + '\t$query->' + addTabToEveryLine ( this . resolveWhereSection ( condition ) , 2 ) + ';\n})'
120+ ) ;
127121 } else if ( condition_type === 'BinaryOp' ) {
128122 if ( condition . op === 'And' || condition . op === 'Or' ) {
129123 let left_condition_type = getNestedUniqueKeyFromObject ( condition . left ) ;
130124 let left_condition = getNestedUniqueValueFromObject ( condition . left ) ;
131- this . prepareWheres ( left_condition_type , left_condition , op ) ;
125+ wheres = wheres . concat ( this . prepareWheres ( left_condition_type , left_condition , op ) ) ;
132126
133127 let right_condition_type = getNestedUniqueKeyFromObject ( condition . right ) ;
134128 let right_condition = getNestedUniqueValueFromObject ( condition . right ) ;
135- this . prepareWheres ( right_condition_type , right_condition , condition . op ) ;
129+ wheres = wheres . concat ( this . prepareWheres ( right_condition_type , right_condition , condition . op ) )
136130 } else {
137131 let left = this . convertIdentifier2qualifiedColumn ( getNestedUniqueValueFromObject ( condition . left ) ) ;
138132 let right = this . resolveValue ( getNestedUniqueValueFromObject ( condition . right ) )
139- this . wheres . push ( this . addPrefix2WhereMethods ( op , 'where' ) + '(' + left + ',' + this . transformBinaryOp ( condition . op ) + ',' + right + ')' ) ;
133+ wheres . push ( this . addPrefix2WhereMethods ( op , 'where' ) + '(' + left + ',' + this . transformBinaryOp ( condition . op ) + ',' + right + ')' ) ;
140134 }
141135 } else {
142136 throw 'Logic error, unhandled condition type [' + condition_type + ']' ;
143137 }
138+
139+ return wheres ;
144140 }
145141
146142 /**
@@ -182,7 +178,7 @@ class Converter
182178 } else if ( propertyExistsInObjectAndNotNull ( select_item , 'UnnamedExpr' ) ) {
183179 res . push ( this . resolveSelectSectionItem ( select_item . UnnamedExpr ) ) ;
184180 } else if ( select_item === 'Wildcard' ) {
185- res . push ( '*' ) ;
181+ res . push ( quote ( '*' ) ) ;
186182 } else if ( propertyExistsInObjectAndNotNull ( select_item , 'QualifiedWildcard' ) ) {
187183 res . push ( quote ( this . getActualTableName ( select_item . QualifiedWildcard [ 0 ] . value ) + '.*' ) )
188184 } else {
0 commit comments