@@ -8,7 +8,17 @@ export class Converter
88
99 run ( need_append_get_suffix = true ) {
1010 let sections = [ ]
11- sections . push ( this . resolveMainTableSection ( ) ) ;
11+
12+ let relation_node = this . ast . body . Select . from [ 0 ] . relation ;
13+
14+ if ( propertyExistsInObjectAndNotNull ( relation_node , 'Table' ) ) {
15+ sections . push ( this . resolveMainTableSection ( ) ) ;
16+ } else if ( propertyExistsInObjectAndNotNull ( relation_node , 'Derived' ) ) {
17+ sections . push ( this . resolveFromSubSection ( ) ) ;
18+ } else {
19+ throw 'Logic error, unhandled relation type' ;
20+ }
21+
1222 let join_section = '' ;
1323
1424 // Resolve 'join' section before 'where' section, because need find joined table alias
@@ -46,13 +56,13 @@ export class Converter
4656 }
4757
4858 resolveTableNameFromRelationNode ( relation_node ) {
49- let table_name = relation_node . Table . name [ 0 ] . value ;
59+ let table_name = relation_node . Table . name [ 0 ] . value ;
5060
51- if ( propertyExistsInObjectAndNotNull ( relation_node . Table , 'alias' ) ) {
52- this . table_name_by_alias [ relation_node . Table . alias . name . value ] = table_name ;
53- }
61+ if ( propertyExistsInObjectAndNotNull ( relation_node . Table , 'alias' ) ) {
62+ this . table_name_by_alias [ relation_node . Table . alias . name . value ] = table_name ;
63+ }
5464
55- return quote ( table_name ) ;
65+ return quote ( table_name ) ;
5666 }
5767
5868 /**
@@ -62,6 +72,15 @@ export class Converter
6272 return 'DB::table(' + this . resolveTableNameFromRelationNode ( this . ast . body . Select . from [ 0 ] . relation ) + ')' ;
6373 }
6474
75+ /**
76+ * @return {string }
77+ */
78+ resolveFromSubSection ( ) {
79+ return 'DB::query()->fromSub(function ($query) {\n'
80+ + '\t' + addTabToEveryLine ( ( new Converter ( this . ast . body . Select . from [ 0 ] . relation . Derived . subquery ) . run ( false ) ) . replace ( 'DB::table' , '$query->from' ) , 2 ) + ';\n'
81+ + '},' + quote ( this . ast . body . Select . from [ 0 ] . relation . Derived . alias . name . value ) + ')' ;
82+ }
83+
6584 resolveWhereSection ( selection_node ) {
6685 let condition_type = getNestedUniqueKeyFromObject ( selection_node ) ;
6786 let condition = getNestedUniqueValueFromObject ( selection_node ) ;
@@ -90,7 +109,7 @@ export class Converter
90109 conditions . push ( this . addPrefix2Methods ( op , method_name ) + '(' + column + ',' + '[' + list . join ( ', ' ) + '])' ) ;
91110 } else if ( condition_type === 'Nested' ) {
92111 conditions . push (
93- this . addPrefix2Methods ( op , 'where' ) + '(function ($query) {\n'
112+ this . addPrefix2Methods ( op , method_name ) + '(function ($query) {\n'
94113 + '\t$query->' + addTabToEveryLine ( this . resolveWhereSection ( condition ) , 2 ) + ';\n})'
95114 ) ;
96115 } else if ( condition_type === 'BinaryOp' ) {
@@ -317,12 +336,20 @@ export class Converter
317336 let joined_table = this . resolveTableNameFromRelationNode ( join . relation ) ;
318337
319338 if ( conditions . length === 1 ) {
320- let left ;
321- let on_condition ;
322- let right ;
323- [ left , on_condition , right ] = this . parseBinaryOpNode ( join_operator . On . BinaryOp ) ;
324-
325- this . joins . push ( join_method + '(' + joined_table + ',' + left + ',' + on_condition + ',' + right + ')' ) ;
339+ if ( propertyExistsInObjectAndNotNull ( join_operator . On , 'BinaryOp' ) ) {
340+ let left ;
341+ let on_condition ;
342+ let right ;
343+ [ left , on_condition , right ] = this . parseBinaryOpNode ( join_operator . On . BinaryOp ) ;
344+
345+ this . joins . push ( join_method + '(' + joined_table + ',' + left + ',' + on_condition + ',' + right + ')' ) ;
346+ } else if ( propertyExistsInObjectAndNotNull ( join_operator . On , 'Nested' ) ) {
347+ let conditions = this . prepareConditions ( 'Nested' , join_operator . On . Nested , '' , 'on' ) ;
348+
349+ this . joins . push ( conditions [ 0 ] ) ;
350+ } else {
351+ throw 'Logic error, unhandled on type' ;
352+ }
326353 } else {
327354 this . joins . push ( join_method + '(' + joined_table + ','
328355 + 'function($join) {\n\t'
0 commit comments