db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r426849 - in /db/derby/code/trunk/java/tools/org/apache/derby: iapi/tools/i18n/LocalizedResource.java impl/tools/ij/Main.java impl/tools/ij/utilMain.java tools/ij.java
Date Sat, 29 Jul 2006 23:20:41 GMT
Author: djd
Date: Sat Jul 29 16:20:40 2006
New Revision: 426849

URL: http://svn.apache.org/viewvc?rev=426849&view=rev
Log:
DERBY-1609 (partial) Add a runScript method to ij. Initial version runs a script
successfully using the passed in connection. More work is needed to avoid the
standard ij startup which opens connections and to return the number of errors.

Modified:
    db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/Main.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java
    db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java

Modified: db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java?rev=426849&r1=426848&r2=426849&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
(original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
Sat Jul 29 16:20:40 2006
@@ -44,9 +44,9 @@
 
 public final class LocalizedResource  implements java.security.PrivilegedAction {
 
-	private static boolean HAVE_BIG_DECIMAL;
+	private static final boolean HAVE_BIG_DECIMAL;
 	
-	{
+	static {
 		boolean haveBigDecimal;
 		try {
 			Class.forName("java.math.BigDecimal");
@@ -242,6 +242,14 @@
 		catch(UnsupportedEncodingException e){
 		}
 		return new LocalizedOutput(o);
+	}
+	/**
+	 * Get a new LocalizedOutput with the given encoding.
+	 * @throws UnsupportedEncodingException
+	 */
+	public LocalizedOutput getNewEncodedOutput(OutputStream o,
+			String encoding) throws UnsupportedEncodingException{
+		return new LocalizedOutput(o, encoding);
 	}
 	public String getTextMessage(String key ) {
 		if ( res == null){

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/Main.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/Main.java?rev=426849&r1=426848&r2=426849&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/Main.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/Main.java Sat Jul 29 16:20:40
2006
@@ -203,6 +203,12 @@
 		LocalizedInput[] inA = { in } ;
 		utilInstance.go(inA, out,connAttributeDefaults);
 	}
+	
+	public void goScript(Connection conn,
+			LocalizedInput in)
+{
+	utilInstance.goScript(conn, in);
+}	
 
 	/**
 	 * create an ij tool waiting to be given input and output streams.

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java?rev=426849&r1=426848&r2=426849&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java Sat Jul 29
16:20:40 2006
@@ -60,15 +60,15 @@
  */
 public class utilMain implements java.security.PrivilegedAction {
 
-  private static Class[] CONN_PARAM = { Integer.TYPE };
-  private static Object[] CONN_ARG = { new Integer(JDBC30Translation.CLOSE_CURSORS_AT_COMMIT)};
+  private static final Class[] CONN_PARAM = { Integer.TYPE };
+  private static final Object[] CONN_ARG = { new Integer(JDBC30Translation.CLOSE_CURSORS_AT_COMMIT)};
 
-	StatementFinder[] commandGrabber;
+	private StatementFinder[] commandGrabber;
 	UCode_CharStream charStream;
 	ijTokenManager ijTokMgr;
 	ij ijParser;
 	ConnectionEnv[] connEnv;
-	int currCE;
+	private int currCE;
 	private int		numConnections;
 	private boolean fileInput;
 	private boolean initialFileInput;
@@ -177,10 +177,6 @@
 	public void go(LocalizedInput[] in, LocalizedOutput out,
 				   Properties connAttributeDefaults) throws ijFatalException
 	{
-		boolean done = false;
-
-		String command = null;
-
 		this.out = out;
 		this.connAttributeDefaults = connAttributeDefaults;
 		
@@ -237,7 +233,38 @@
          		}
       		}
     	}
+		this.out = out;
+		runScriptGuts();
+		cleanupGo(in);
+	}
+	
+	/**
+	 * Support to run a script. Performs minimal setup
+	 * to set the passed in connection into the existing
+	 * ij setup, ConnectionEnv.
+	 * @param conn
+	 * @param in
+	 */
+	public void goScript(Connection conn,
+			LocalizedInput in)
+	{
+		JDBCDisplayUtil.showSelectCount = false;
+		connEnv[0].addSession(conn, (String) null);
+		fileInput = initialFileInput = !in.isStandardInput();
+		commandGrabber[0].ReInit(in);
+		runScriptGuts();
+	}
+	
+	/**
+	 * Run the guts of the script. Split out to allow
+	 * calling from the full ij and the minimal goScript.
+	 *
+	 */
+	private void runScriptGuts() {
 
+		
+		boolean done = false;
+		String command = null;
 		while (!ijParser.exit && !done) {
 			try{
 				ijParser.setConnection(connEnv[currCE], (numConnections > 1));
@@ -247,8 +274,7 @@
 
 			connEnv[currCE].doPrompt(true, out);
    			try {
-				command = null;
-
+   				command = null;
 				out.flush();
 				command = commandGrabber[currCE].nextStatement();
 
@@ -326,6 +352,15 @@
 			/* Go to the next connection/user, if there is one */
 			currCE = ++currCE % connEnv.length;
 		}
+	}
+	
+	/**
+	 * Perform cleanup after a script has been run.
+	 * Close the input streams if required and shutdown
+	 * derby on an exit.
+	 * @param in
+	 */
+	private void cleanupGo(LocalizedInput[] in) {
 
 		// we need to close all sessions when done; otherwise we have
 		// a problem when a single VM runs successive IJ threads
@@ -339,8 +374,7 @@
 		// similarly must close input files
 		for (int i = 0; i < numConnections; i++) {
 			try {
-				if (!in[i].isStandardInput() )
-					in[i].close();	
+				in[i].close();	
 			} catch (Exception e ) {
     			  	out.println(langUtil.getTextMessage("IJ_CannotCloseInFile",
 					e.toString()));

Modified: db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java?rev=426849&r1=426848&r2=426849&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java Sat Jul 29 16:20:40 2006
@@ -21,10 +21,18 @@
 package org.apache.derby.tools;
 
 import org.apache.derby.iapi.services.info.JVMInfo;
+import org.apache.derby.iapi.tools.i18n.LocalizedInput;
+import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
+import org.apache.derby.iapi.tools.i18n.LocalizedResource;
 
 import org.apache.derby.impl.tools.ij.Main;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.sql.Connection;
+import java.util.Properties;
 
 /**
 	
@@ -59,6 +67,53 @@
 	  {
 		  org.apache.derby.impl.tools.ij.Main14.main(args);
 	  }
+  }
+  
+  /**
+   * Run a SQL script from an InputStream and write
+   * the resulting output to the provided PrintStream.
+   * 
+   * @param conn Connection to be used as the script's default connection. 
+   * @param sqlIn InputStream for the script.
+   * @param inputEncoding Encoding of the script.
+   * @param sqlOut PrintStream for the script's output
+   * @param outputEncoding Output encoding to use.
+   * @return Number of SQLExceptions thrown during the execution, -1 if not known.
+   * @throws UnsupportedEncodingException
+   */
+  public static int runScript(
+		  Connection conn,
+		  InputStream sqlIn,
+		  String inputEncoding,
+		  PrintStream sqlOut,
+		  String outputEncoding)
+		  throws UnsupportedEncodingException
+  {
+	  LocalizedOutput lo = 
+		  outputEncoding == null ?
+				  LocalizedResource.getInstance().
+		            getNewOutput(sqlOut)
+	             :  
+		          LocalizedResource.getInstance().
+                    getNewEncodedOutput(sqlOut, outputEncoding);
+
+	  Main ijE;
+	  if (JVMInfo.JDK_ID == JVMInfo.J2SE_13)
+	  {
+		  ijE = new Main(lo);
+	  }
+	  else
+	  {
+		  ijE = new org.apache.derby.impl.tools.ij.Main14(lo);
+	  }	  
+	  
+	  LocalizedInput li = LocalizedResource.getInstance().
+	            getNewEncodedInput(sqlIn, inputEncoding);
+	  
+	  
+	  ijE.goScript(conn, li);
+	  
+	  return -1;
   }
 
   private ij() { // no instances allowed



Mime
View raw message