1- import { DatabaseBaseTransaction } from ' ./../definitions/database-definition' ;
1+ import { DatabaseBaseTransaction } from " ./../definitions/database-definition" ;
22import { DatabaseBase , DatabaseObject , DatabaseResult , DatabaseTransaction } from "../definitions/database-definition" ;
33import { QueryCompiled } from "./query-compiled" ;
4+ import { ReplacementParam } from "./replacement-param" ;
45
56export class ExecutableBuilder {
67
@@ -9,11 +10,12 @@ export class ExecutableBuilder {
910 }
1011
1112 public execute (
12- compiled : QueryCompiled ,
13+ compiled : QueryCompiled [ ] ,
1314 database : DatabaseBase ,
14- ) : Promise < DatabaseResult > {
15+ ) : Promise < DatabaseResult [ ] > {
1516 this . log ( compiled ) ;
16- return this . executeSql ( database , compiled ) ;
17+ return this . executorLinked ( compiled , [ ] , database ) ;
18+ // return this.executeSql(database, compiled);
1719 }
1820
1921 private executeSql (
@@ -39,6 +41,49 @@ export class ExecutableBuilder {
3941 } ) ;
4042 }
4143
44+ private checkParams (
45+ script : QueryCompiled , resultadosAnteriores : DatabaseResult [ ]
46+ ) : QueryCompiled {
47+ const paramsResult : any [ ] = [ ] ;
48+ script . params . forEach ( param => {
49+ if ( param instanceof ReplacementParam ) {
50+ let value = resultadosAnteriores as any ;
51+ param . properties . forEach ( property => {
52+ value = value [ property ] ;
53+ } ) ;
54+ paramsResult . push ( value ) ;
55+ } else {
56+ paramsResult . push ( param ) ;
57+ }
58+ } ) ;
59+ script . params = paramsResult ;
60+ return script ;
61+ }
62+
63+ private executorLinked (
64+ compiled : QueryCompiled [ ] ,
65+ dataResultsApplied : DatabaseResult [ ] ,
66+ database : DatabaseBase
67+ ) : Promise < DatabaseResult [ ] > {
68+ return new Promise ( ( resolve , reject ) => {
69+ if ( compiled && compiled . length > 0 ) {
70+ this . executeSql ( database , this . checkParams ( compiled [ 0 ] , dataResultsApplied ) )
71+ . then ( result => {
72+ // remove o item executado
73+ compiled . shift ( ) ;
74+ this . executorLinked ( compiled , dataResultsApplied . concat ( [ result ] ) , database )
75+ . then ( res => {
76+ resolve ( [ result ] . concat ( res ) ) ;
77+ } )
78+ . catch ( ( err : any ) => reject ( err ) ) ;
79+ } )
80+ . catch ( ( err : any ) => reject ( err ) ) ;
81+ } else {
82+ resolve ( [ ] ) ;
83+ }
84+ } ) ;
85+ }
86+
4287 private log ( log : any ) {
4388 if ( this . enableLog ) {
4489 // tslint:disable-next-line
0 commit comments