

import java.io.*;
import java.sql.*;
import java.util.ArrayList;

import org.postgresql.Driver;
import org.postgresql.PGConnection;
import org.postgresql.copy.*;
import org.postgresql.core.*;

public class EvejrDBCopy implements Runnable {

   private PipedOutputStream pipe;
   private PGConnection connection;
   private String tableNameAndContents; // e.g. "TABLE(index, value)"
   
   public EvejrDBCopy(Connection conn, String tableNameAndContents) throws SQLException
   {
      this.connection = (PGConnection)conn;
      this.tableNameAndContents = tableNameAndContents;
      pipe = new PipedOutputStream();
   }
   
   public void run()
   {
      try
      {
         //InputStream from = new PipedInputStream(pipe);
         PipedInputStream from = new PipedInputStream(pipe);
         CopyManager cm = new CopyManager((BaseConnection) connection);
         String copyString = "copy "+tableNameAndContents+" from STDIN with csv";
         System.out.println(copyString);
         cm.copyIn(copyString, from);
      }
      catch (IOException e)
      {
         System.err.println(e.getMessage());
         System.err.println(e.getLocalizedMessage());
         e.printStackTrace();
      }
      catch (SQLException e)
      {
         System.err.println(e.getMessage());
         System.err.println(e.getLocalizedMessage());
         e.printStackTrace();
      }
   }   
   
   public PipedOutputStream pipe()
   {
      return pipe;
   }
   
   
   public static void main(String[] args)
   {
      ArrayList<String> tableNames = new ArrayList<String>();
      for (int i=1; i<11; i++)
      {
         tableNames.add("nkptest"+i);
      }
      long ROWCOUNT = 100000;
      double VALUE = 3.14;
      Date validTime = new Date(System.currentTimeMillis());
      String validTimeString = validTime.toString();
      try
      {
         String url = "jdbc:postgresql://griddb3:5432/grid";
         String user = "gridadm";
         String pass = "XXXXXX";
         Driver driver = new Driver();
         DriverManager.registerDriver(driver);
         Connection connection = DriverManager.getConnection(url, user, pass);
         for (String tableName : tableNames)
         {
            tableName = "temporary_"+tableName;
            String fullContentDefinition = 
               "( gridpoint_id integer NOT NULL, valid_at timestamp without time zone NOT NULL, parameter_value real NOT NULL )";
            String contentDefinition = "( gridpoint_id, valid_at, parameter_value )"; 
            Statement tableStm = connection.createStatement();
            tableStm.execute("DROP TABLE IF EXISTS "+tableName);
            tableStm.execute("CREATE TABLE "+tableName+" "+fullContentDefinition);
            EvejrDBCopy evCopy = new EvejrDBCopy(connection, tableName + contentDefinition);
            Thread t = new Thread(evCopy);
            t.start();
            PrintWriter dataStream = new PrintWriter(evCopy.pipe(), true);
            try
            {
               for (int i = 0; i<ROWCOUNT; i++)
               {
                  {
                     String insert = i + ",\"" + validTimeString + "\"," + VALUE;
                     dataStream.println(insert);
                  }
               }
            }
            finally 
            {
               dataStream.close();
            }
            while (t.isAlive()) { }
         }   
         connection.close();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }   
}

