/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.dbmeta.tools;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import fi.tnie.db.env.util.ResultSetWriter;
import fi.tnie.util.cli.Argument;
import fi.tnie.util.cli.CommandLine;
import fi.tnie.util.cli.Parser;
import fi.tnie.util.io.IOHelper;

public class KeyGenerationInfo
    extends CatalogTool {
    
    public static final Argument QUERY_FILE = new Argument("query-file", 1, 1);
    private String query;    
    
    /**
     * @param args
     */
    public static void main(String[] args) {        
        System.exit(new KeyGenerationInfo().run(args));
    }
    
    @Override
    public void run()
        throws ToolException {            
      try {                        
          String q = getQuery();             
          
          Connection c = getConnection();
          c.setAutoCommit(false);
          
          Statement st = c.createStatement();          
          
          int uc = st.executeUpdate(q, Statement.RETURN_GENERATED_KEYS);
          
          System.out.println("update-count: " + uc);
          
          ResultSet rs = st.getGeneratedKeys();
          
          ResultSetWriter rw = new ResultSetWriter(System.out, false);
                              
          rs.close();
      }
      catch (Exception e) {
          e.printStackTrace();
          throw new ToolException(-1, e.getMessage(), e);
      }      
    }
    
    @Override
    protected void init(CommandLine cl) 
        throws ToolException {     
        super.init(cl);
        
        String qf = cl.value(QUERY_FILE);
        
        if (qf == null) {
            throw new ToolException("no query file specified");            
        }
        
        try {
            String q = new IOHelper().read(new File(qf));
            setQuery(q);
        } 
        catch (IOException e) {         
            e.printStackTrace();
        }
        
        
        
    }    
        
    @Override
    protected void prepare(Parser p) {        
        super.prepare(p);   
        
        Argument a = QUERY_FILE;
        
        if (!p.containsArgument(a.getName())) {        
            p.addArgument(a);            
        }        
    }

//    private Table getTable(Catalog catalog, String schemaName, String tableName) {
//        if (schemaName == null) {
//            throw new NullPointerException("'schemaName' must not be null");
//        }
//                    
//        Schema schema = catalog.schemas().get(schemaName);
//            
//        if (schema == null) {
//            throw new IllegalArgumentException("no such schema: " + schemaName);
//        }
//        
//        Table table = schema.tables().get(tableName);
//            
//        if (table == null) {
//            throw new IllegalArgumentException("no such table: " + tableName);
//        }
//        
//        return table;
//    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }    
}

@@ -117,24 +117,57 @@
 OException;%0A
+import java.nio.charset.Charset;%0A
 import java.
@@ -475,24 +475,25 @@
 gTool %7B%0A    
+%09
 %0A    public 
@@ -1673,32 +1673,48 @@
 it(cl);%0A        
+                
 %0A        String 
@@ -1740,33 +1740,127 @@
 _FILE);%0A        
-%0A
+String q = null;%0A        %0A        try %7B%0A%09        IOHelper ih = new IOHelper();%09    %0A%09        %0A%09
         if (qf =
@@ -1865,24 +1865,25 @@
  == null) %7B%0A
+%09
             
@@ -1886,67 +1886,101 @@
     
-throw new ToolException(%22no query file specified%22
+// read from stdin:        %0A%09        %09String name = Charset.defaultCharset().name(
 );
+%09
         
     
@@ -1979,31 +1979,80 @@
     
+%09%0A%09 
     
-%0A
        
- %7D%0A
+q = ih.read(System.in, name, 4096);            %0A%09
         
 %0A   
@@ -2043,33 +2043,35 @@
   %0A%09        
-%0A
+%7D%0A%09
         
 try %7B%0A      
@@ -2058,22 +2058,24 @@
         
-try
+else
  %7B%0A
+%09
         
@@ -2082,33 +2082,14 @@
     
-String q = new IOHelper()
+q = ih
 .rea
@@ -2108,33 +2108,32 @@
 f));
-%0A
+%09      
       
+%0A%09
       
-setQuery(q);
+  %7D
 %0A   
@@ -2193,34 +2193,41 @@
         
-e.printStackTrace(
+throw new ToolException(e
 );%0A     
@@ -2240,33 +2240,44 @@
        %0A        
-%0A
+setQuery(q);
         %0A    %7D  

Make query to be read from stdin if command line argument is not present