package org.voltdb.utils;

import java.io.FileReader;

import org.voltdb.ServerThread;
import org.voltdb.VoltDB;
import org.voltdb.VoltTable;
import org.voltdb.VoltDB.Configuration;
import org.voltdb.VoltType;
import org.voltdb.client.Client;
import org.voltdb.client.ClientFactory;
import org.voltdb.compiler.VoltProjectBuilder;

import au.com.bytecode.opencsv_voltpatches.CSVReader;

import junit.framework.TestCase;

public class TestCSVLoader extends TestCase {
	public void testSimple() throws Exception {
        String simpleSchema =
            "create table BLAH (" +
            "IVAL bigint default 0 not null, " +
            //"TVAL timestamp default null," +
            "DVAL decimal default null," +
            "VVAL varchar(10) default null, " +
            "PRIMARY KEY(IVAL));";

        String pathToCatalog = Configuration.getPathToCatalogForTest("csv.jar");
        String pathToDeployment = Configuration.getPathToCatalogForTest("csv.xml");

        VoltProjectBuilder builder = new VoltProjectBuilder();
        builder.addLiteralSchema(simpleSchema);
        builder.addPartitionInfo("BLAH", "IVAL");
        builder.addStmtProcedure("Insert", "insert into blah values (?, ?, ?);", null);
        builder.addStmtProcedure("InsertWithDate", "INSERT INTO BLAH VALUES (974599638818488300, 5, 'nullchar');");
        boolean success = builder.compile(pathToCatalog, 2, 1, 0);
        assertTrue(success);
        MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);

        VoltDB.Configuration config = new VoltDB.Configuration();
        config.m_pathToCatalog = pathToCatalog;
        config.m_pathToDeployment = pathToDeployment;
        ServerThread localServer = new ServerThread(config);

        Client client = null;
        try {
            localServer.start();
            localServer.waitForInitialization();

            String []params = {"/Users/xinjia/testdb.csv","Insert"};
            
            CSVLoader.main(params);
            
            // do the test
            client = ClientFactory.createClient();
            client.createConnection("localhost");
            
            final CSVReader reader = new CSVReader(new FileReader(params[0]));
            int lineCount = 0;
            while (reader.readNext() != null) {
            	lineCount++;
            }
            
            VoltTable modCount;
            modCount = client.callProcedure("@AdHoc", "SELECT COUNT(*) FROM BLAH;").getResults()[0];
            int rowct = 0;
            while(modCount.advanceRow()) {
            	rowct = (Integer) modCount.get(0, VoltType.INTEGER);
            }
            System.out.println(String.format("The rows infected: (%d,%s)", lineCount, rowct));
            assertEquals(lineCount, rowct);
            
            modCount = client.callProcedure("@AdHoc", "SELECT * FROM BLAH;").getResults()[0];
            System.out.println("data inserted to table BLAH:\n" + modCount);
            
        }
        finally {
            if (client != null) client.close();
            client = null;

            if (localServer != null) {
                localServer.shutdown();
                localServer.join();
            }
            localServer = null;

            // no clue how helpful this is
            System.gc();
        }
        
        
	}
	
	
	
	
	
}

@@ -587,19 +587,131 @@
    %22
-IVAL bigint
+clm_tinyint tinyint default null, %22 +%0A            %22clm_smallint smallint default null, %22 +%0A            %22clm_integer integer
  def
@@ -747,25 +747,26 @@
     
-//%22TVAL timestamp
+%22clm_bigint bigint
  def
@@ -767,32 +767,33 @@
 nt default null,
+ 
 %22 +%0A            
@@ -796,21 +796,92 @@
     
-%22DVAL decimal
+%0A            %22clm_float float default null, %22 +%0A            %22clm_timestamp timestamp
  def
@@ -890,16 +890,17 @@
 lt null,
+ 
 %22 +%0A    
@@ -912,12 +912,18 @@
    %22
-VVAL
+clm_string
  var
@@ -966,29 +966,112 @@
    %22
-PRIMARY KEY(IVAL)
+clm_decimal decimal default null, %22 +%0A            %22clm_varinary varbinary default null
 );
+ 
 %22;
+%0A            
 %0A%0A  
@@ -1391,12 +1391,19 @@
 %22, %22
-IVAL
+clm_integer
 %22);%0A
@@ -1402,32 +1402,34 @@
 eger%22);%0A        
+//
 builder.addStmtP
@@ -1492,32 +1492,34 @@
  null);%0A        
+//
 builder.addStmtP
@@ -2211,23 +2211,123 @@
 ert%22
-%7D;%0A            
+,%22--columns%22, %220,2,1%22%7D;%0A            //String %5B%5Dparams = %7B%22/Users/xinjia/testdb.csv%22,%22Insert%22,%22--columns%22, %220,2,1%22%7D;
 %0A   

Add different data types into the table schema