package ragnardb.runtime;

import gw.lang.reflect.IType;
import gw.util.GosuExceptionUtil;
import ragnardb.plugin.ISQLQueryResultType;
import ragnardb.plugin.ISQLTableType;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by klu on 7/8/2015.
 */
public class ExecutableQuery<T> extends SQLQuery<T>{
  private String statement;
  private IType returnType;
  private ISQLTableType returnTable;

  public ExecutableQuery(ITypeToSQLMetadata md, IType rootType, String s, IType ret, ISQLTableType rT){
    super(md, rootType);
    statement = s;
    returnType = ret;
    returnTable = rT;
  }

  public ExecutableQuery<T> setup(){
    ExecutableQuery<T> query = new ExecutableQuery<T>(_metadata, _rootType, this.statement, returnType, returnTable);
    return query;
  }

  @Override
  public Iterator<T> iterator(){
    try{
      List<T> results = new LinkedList<>();
      Iterable<SQLRecord> records = SQLRecord.executeStatement(statement, _rootType);
      if(returnType instanceof ISQLTableType){
        for(SQLRecord record: records){
          results.add((T) record);
        }
      } else if(returnType instanceof ISQLQueryResultType){
        for(SQLRecord record: records){
          results.add((T) record);
        }
      }
      return results;
    }catch(SQLException e){
      throw GosuExceptionUtil.forceThrow(e);
    }
  }
}

@@ -917,19 +917,8 @@
 ()%7B%0A
-    try%7B%0A  
     
@@ -959,18 +959,16 @@
 ();%0A    
-  
 Iterable
@@ -993,44 +993,101 @@
 s = 
-SQLRecord.executeStatement(statement
+() -%3E %7B%0A      try%0A      %7B%0A        return SQLRecord.select( statement, Collections.emptyList()
 , _r
@@ -1093,21 +1093,131 @@
 rootType
+ 
 );%0A  
+    %7D%0A      catch( SQLException e )%0A      %7B%0A        throw GosuExceptionUtil.forceThrow( e );%0A      %7D%0A    %7D;%0A%0A
     if(r
@@ -1251,34 +1251,32 @@
 bleType)%7B%0A      
- 
- 
 for(SQLRecord re
@@ -1291,34 +1291,32 @@
 cords)%7B%0A        
-  
 results.add((T) 
@@ -1316,34 +1316,32 @@
 dd((T) record);%0A
-  
       %7D%0A      %7D 
@@ -1336,18 +1336,16 @@
   %7D%0A    
-  
 %7D else i
@@ -1386,26 +1386,24 @@
 esultType)%7B%0A
-  
       for(SQ
@@ -1432,26 +1432,24 @@
 s)%7B%0A        
-  
 results.add(
@@ -1471,22 +1471,16 @@
     
-  
 %7D%0A    
- 
- 
 %7D%0A
-  
     
@@ -1497,88 +1497,20 @@
 ults
-;%0A    %7Dcatch(SQLException e)%7B%0A      throw GosuExceptionUtil.forceThrow(e);%0A    %7D
+.iterator();
 %0A  %7D

Fix compilation error after pull