Author: shawkins
Date: 2009-10-05 10:44:52 -0400 (Mon, 05 Oct 2009)
New Revision: 1521
Added:
trunk/adminshell/src/main/java/com/metamatrix/script/shell/Util.java
Modified:
trunk/adminshell/src/main/resources/scripts/assert.bsh
trunk/adminshell/src/main/resources/scripts/jdbc.bsh
trunk/adminshell/src/main/resources/scripts/util.bsh
trunk/client-jdbc/src/main/java/com/metamatrix/script/io/ResultSetReader.java
trunk/common-core/src/main/java/com/metamatrix/core/util/ObjectConverterUtil.java
Log:
TEIID-845 TEIID-866 TEIID-830 correcting the reported recount, changing the handling of
statements to not assume resultsets, and added a utility method for showing the query
plan.
Added: trunk/adminshell/src/main/java/com/metamatrix/script/shell/Util.java
===================================================================
--- trunk/adminshell/src/main/java/com/metamatrix/script/shell/Util.java
(rev 0)
+++ trunk/adminshell/src/main/java/com/metamatrix/script/shell/Util.java 2009-10-05
14:44:52 UTC (rev 1521)
@@ -0,0 +1,131 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package com.metamatrix.script.shell;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import com.metamatrix.core.util.ObjectConverterUtil;
+import com.metamatrix.jdbc.util.MMJDBCURL;
+
+public class Util {
+
+ public static byte[] readBinaryFile(String fileName) throws IOException {
+ InputStream is = null;
+
+ if(fileName == null) {
+ throw new IOException("fileName is null");
+ }
+ try {
+ //try to load file from the classpath
+ is = Object.class.getResourceAsStream("/"+fileName);
+
+ byte[] result;
+ if (is == null) {
+ //load from "hardcoded" path
+ is = new FileInputStream(new File(fileName));
+ }
+
+
+ }catch(Exception e) {
+ if (is == null) {
+ try {
+ //load from "hardcoded" path
+ is = new FileInputStream(new File(fileName));
+ }catch(Exception e2) {
+
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ }
+
+ //convert to bytes
+ byte[] result = ObjectConverterUtil.convertToByteArray(is);
+ try {
+ is.close();
+ }catch(Exception e3) {
+ }
+ return result;
+ }
+
+ public static char[] readTextFile(String fileName) throws IOException {
+ if(fileName == null) {
+ throw new IOException("fileName is null");
+ }
+ char[] result = null;
+
+ try {
+ File file = new File(fileName);
+
+ // changed to use the ObectConverterUtil, instead of the
+ // convertToCharArray() method because it doesn't completely
+ // convert the file, the XML reader throws a malform exception
+ // the test case for ServerAdminImpl also the ObjectConverterUtil
+ // that's why this was changed to use it
+ result = ObjectConverterUtil.convertFileToCharArray(file, null);
+
+ }catch(Exception e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ public static void cleanUpDirectory(String dirName, String[] filesToKeep){
+ File dir = new File(dirName);
+ if (dir.exists()) {
+ File[] files = dir.listFiles();
+ for (File f:files) {
+ if (f.getName().endsWith(".deleted")) {
+ continue;
+ }
+ boolean delete = true;
+ for (String keep:filesToKeep) {
+ if (f.getName().equalsIgnoreCase(keep)) {
+ delete = false;
+ break;
+ }
+ }
+ if (delete) f.delete();
+ }
+ }
+ }
+
+ public static char[] convertToCharArray(InputStream in) throws IOException {
+ return ObjectConverterUtil.convertToCharArray(in, Integer.MAX_VALUE, null);
+ }
+
+ public static String extractVDBName(String url) {
+ MMJDBCURL mmurl = new MMJDBCURL(url);
+ return mmurl.getVDBName();
+ }
+
+ public static String extractHost(String url) {
+ MMJDBCURL mmurl = new MMJDBCURL(url);
+ return mmurl.getConnectionURL();
+ }
+
+}
Property changes on: trunk/adminshell/src/main/java/com/metamatrix/script/shell/Util.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/adminshell/src/main/resources/scripts/assert.bsh
===================================================================
--- trunk/adminshell/src/main/resources/scripts/assert.bsh 2009-10-02 19:08:46 UTC (rev
1520)
+++ trunk/adminshell/src/main/resources/scripts/assert.bsh 2009-10-05 14:44:52 UTC (rev
1521)
@@ -1,185 +1,8 @@
import junit.framework.*;
+static import junit.framework.Assert.*;
import com.metamatrix.script.junit.*;
-// This file has all the assetion routines that JUnit Assert class provides
+// This file has all the assertion routines that JUnit Assert class provides
-void assertEquals(boolean expected, boolean actual) {
- Assert.assertEquals(expected, actual);
-}
-
-
-void assertEquals(byte expected, byte actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(char expected, char actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(
- double expected,
- double actual,
- double delta) {
- Assert.assertEquals(expected, actual, delta);
-}
-
-void assertEquals(
- float expected,
- float actual,
- float delta) {
- Assert.assertEquals(expected, actual, delta);
-}
-
-void assertEquals(int expected, int actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(Object expected, Object actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(
- String message,
- boolean expected,
- boolean actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- byte expected,
- byte actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- char expected,
- char actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- double expected,
- double actual,
- double delta) {
- Assert.assertEquals(message, expected, actual, delta);
-}
-
-void assertEquals(
- String message,
- float expected,
- float actual,
- float delta) {
- Assert.assertEquals(message, expected, actual, delta);
-}
-
-void assertEquals(String message, int expected, int actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- Object expected,
- Object actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(String expected, String actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(
- String message,
- String expected,
- String actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- long expected,
- long actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(
- String message,
- short expected,
- short actual) {
- Assert.assertEquals(message, expected, actual);
-}
-
-void assertEquals(long expected, long actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertEquals(short expected, short actual) {
- Assert.assertEquals(expected, actual);
-}
-
-void assertFalse(boolean condition) {
- Assert.assertFalse(condition);
-}
-
-void assertFalse(String message, boolean condition) {
- Assert.assertFalse(message, condition);
-}
-
-void assertNotNull(Object object) {
- Assert.assertNotNull(object);
-}
-
-void assertNotNull(String message, Object object) {
- Assert.assertNotNull(message, object);
-}
-
-void assertNotSame(Object expected, Object actual) {
- Assert.assertNotSame(expected, actual);
-}
-
-void assertNotSame(
- String message,
- Object expected,
- Object actual) {
- Assert.assertNotSame(message, expected, actual);
-}
-
-void assertNull(Object object) {
- Assert.assertNull(object);
-}
-
-void assertNull(String message, Object object) {
- Assert.assertNull(message, object);
-}
-
-void assertSame(Object expected, Object actual) {
- Assert.assertSame(expected, actual);
-}
-
-void assertSame(
- String message,
- Object expected,
- Object actual) {
- Assert.assertSame(message, expected, actual);
-}
-
-void assertTrue(boolean condition) {
- Assert.assertTrue(condition);
-}
-
-void assertTrue(String message, boolean condition) {
- Assert.assertTrue(message, condition);
-}
-
-void fail() {
- Assert.fail();
-}
-
-void fail(String message) {
- Assert.fail(message);
-}
-
void runTests(String scriptName) {
BshTestSuite suite = new BshTestSuite(scriptName);
suite.addTest(scriptName); //$NON-NLS-1$
Modified: trunk/adminshell/src/main/resources/scripts/jdbc.bsh
===================================================================
--- trunk/adminshell/src/main/resources/scripts/jdbc.bsh 2009-10-02 19:08:46 UTC (rev
1520)
+++ trunk/adminshell/src/main/resources/scripts/jdbc.bsh 2009-10-05 14:44:52 UTC (rev
1521)
@@ -11,6 +11,7 @@
import java.sql.*;
import com.metamatrix.script.io.*;
+import com.metamatrix.jdbc.api.*;
protected Connection getConnection() {
if (currentContext().internalConnection != void &&
currentContext().internalConnection != null) {
@@ -144,26 +145,8 @@
/**
* Execute a Statement and save the ResultSet or update count.
*/
-void execute(String sql) throws SQLException {
- checkConnection();
-
- // close last statement and result set if necessary
- closeStatement();
-
- // create new statement
- currentContext().internalStatement =
currentContext().internalConnection.createStatement();
- try {
- debug("Executing sql: " + sql);
- currentContext().internalResultSet =
currentContext().internalStatement.executeQuery(sql);
- if (interactive()) {
- record("printResults();\n");
- printResults();
- }
- } catch(SQLException e) {
- closeStatement();
- debug(e);
- throw e;
- }
+void execute(String sql) throws SQLException {
+ execute(sql, null);
}
/**
@@ -187,10 +170,7 @@
try {
debug("Executing batch of : " + cmds.length);
currentContext().internalUpdatedList =
currentContext().internalStatement.executeBatch();
- if (interactive()) {
- record("printResults();\n");
- printResults();
- }
+ print ("update counts: " +
Arrays.toString(currentContext().internalUpdatedList));
} catch(SQLException e) {
closeStatement();
debug(e);
@@ -229,47 +209,31 @@
// close last statement and result set if necessary
closeStatement();
-
- if ( (sql.indexOf("?") != -1) && (params == null || params.length
== 0)) {
- throw new SQLException ("Wrong!, No Parameters supplied to
statement.");
- }
-
- sqlLow = sql.toLowerCase();
-
+
try {
- if (sqlLow.startsWith("select")) {
+ if (params != null) {
debug("Executing a prepared Statement:"+sql);
currentContext().internalStatement =
currentContext().internalConnection.prepareStatement(sql);
setStatementDefaults();
setParameters(params);
- currentContext().internalResultSet =
currentContext().internalStatement.executeQuery();
+ hasResultSet = currentContext().internalStatement.execute();
}
- else if (sqlLow.startsWith("update") ||
sqlLow.startsWith("delete") || sqlLow.startsWith("insert")) {
- debug("Executing a prepared statement:"+sql);
- currentContext().internalStatement =
currentContext().internalConnection.prepareStatement(sql);
+ else {
+ debug("Executing statement:"+sql);
+ currentContext().internalStatement =
currentContext().internalConnection.createStatement();
setStatementDefaults();
- setParameters(params);
- currentContext().internalResultSet = null;
- int row_affected = currentContext().internalStatement.executeUpdate();
- print (row_affected+" rows got affected.");
+ hasResultSet = currentContext().internalStatement.execute(sql);
}
- else if (sqlLow.startsWith("exec ") ) {
- sql = sql.substring(4);
-
- debug ("Executing a Callable statement:"+sql);
- currentContext().internalStatement =
currentContext().internalConnection.prepareCall("{?=call " + sql +
"}");
- setStatementDefaults();
- setParameters(params);
- currentContext().internalResultSet =
currentContext().internalStatement.executeQuery();
- }
- else {
- throw new SQLException("Not a valid statement!, it must start with
(Select|Insert|Update|Delete|Stored Proc)");
+ if (hasResultSet) {
+ currentContext().internalResultSet =
currentContext().internalStatement.getResultSet();
+ if (interactive()) {
+ record("printResults();\n");
+ printResults();
+ }
+ } else {
+ currentContext().internalUpdateCount =
currentContext().internalStatement.getUpdateCount();
+ print ("update count:" +currentContext().internalUpdateCount);
}
-
- if (interactive()) {
- record("printResults();\n");
- printResults();
- }
} catch(SQLException e) {
closeStatement();
debug(e);
@@ -294,6 +258,14 @@
printResults(currentContext().internalResultSet, fileName);
}
+void showPlan() {
+ PlanNode queryPlan =
((MMResultSet)currentContext().internalStatement).getPlanDescription();
+ if (queryPlan != null) {
+ print(XMLOutputVisitor.convertToXML(queryPlan));
+ } else {
+ print("No plan provided - add OPTION SHOWPLAN");
+ }
+}
void walkResults() {
rs = currentContext().internalResultSet;
@@ -311,11 +283,10 @@
print("ResultSet is null");
return;
}
- row = -1; // -1 to compensate for the header row.
- BufferedReader in = new BufferedReader(new ResultSetReader(results));
+ ResultSetReader reader = new ResultSetReader(results);
+ BufferedReader in = new BufferedReader(reader);
String line = in.readLine();
while(line != null) {
- row++;
if (comparePrint) {
line=line.replaceAll("\"", "\\\\\"");
print("\""+line+"\",");
@@ -325,7 +296,7 @@
}
line = in.readLine();
}
- print("Fetched "+row+" rows\n");
+ print("Fetched "+reader.getRowCount()+" rows\n");
}
private void printResults(ResultSet results, File resultsFile) throws SQLException {
@@ -333,17 +304,16 @@
print("ResultSet is null");
return;
}
- row = -1; // -1 to compensate for the header row.
- BufferedReader in = new BufferedReader(new ResultSetReader(results));
+ ResultSetReader reader = new ResultSetReader(results);
+ BufferedReader in = new BufferedReader(reader);
PrintWriter writer = new PrintWriter(new FileWriter(resultsFile));
String line = in.readLine();
while(line != null) {
- row++;
writer.println(line);
line = in.readLine();
}
writer.close();
- print("Wrote "+row+" rows to
File="+resultsFile.getName()+"\n");
+ print("Wrote "+reader.getRowCount()+" rows to
File="+resultsFile.getName()+"\n");
}
void assertRowCount(int expected) {
@@ -368,7 +338,8 @@
if(currentContext().internalStatement != void &&
currentContext().internalStatement != null) {
Statement stmt = currentContext().internalStatement;
closeResultSet();
-
+ currentContext().internalUpdateCount = -1;
+ currentContext().internalUpdatedList = null;
try {
debug("Closing statement...");
stmt.close();
Modified: trunk/adminshell/src/main/resources/scripts/util.bsh
===================================================================
--- trunk/adminshell/src/main/resources/scripts/util.bsh 2009-10-02 19:08:46 UTC (rev
1520)
+++ trunk/adminshell/src/main/resources/scripts/util.bsh 2009-10-05 14:44:52 UTC (rev
1521)
@@ -3,6 +3,8 @@
import com.metamatrix.core.util.*;
import java.lang.reflect.*;
import com.metamatrix.common.comm.exception.*;
+static import com.metamatrix.core.util.ObjectConverterUtil.*;
+static import com.metamatrix.script.shell.Util.*;
debug=false;
@@ -14,105 +16,6 @@
return currentContext().internalPrompt;
}
-byte[] readBinaryFile(String fileName) {
- InputStream is = null;
-
- if(fileName == null) {
- throw new IOException("fileName is null");
- }
- try {
- //try to load file from the classpath
- is = Object.class.getResourceAsStream("/"+fileName);
-
- byte[] result;
- if (is == null) {
- //load from "hardcoded" path
- is = new FileInputStream(new File(fileName));
- }
-
-
- }catch(Exception e) {
- if (is == null) {
- try {
- //load from "hardcoded" path
- is = new FileInputStream(new File(fileName));
- }catch(Exception e2) {
-
- e.printStackTrace();
- return null;
- }
- }
-
- }
-
- //convert to bytes
- result = convertToByteArray(is);
- try {
- is.close();
- }catch(Exception e3) {
- }
- return result;
-}
-
-char[] readTextFile(String fileName) {
- if(fileName == null) {
- throw new IOException("fileName is null");
- }
- char[] result = null;
-
- try {
- File file = new File(fileName);
-
- // changed to use the ObectConverterUtil, instead of the
- // convertToCharArray() method because it doesn't completely
- // convert the file, the XML reader throws a malform exception
- // the test case for ServerAdminImpl also the ObjectConverterUtil
- // that's why this was changed to use it
- result = ObjectConverterUtil.convertFileToCharArray(file, null);
-
- }catch(e) {
- e.printStackTrace();
- }
- return result;
-}
-
-byte[] convertToByteArray(InputStream in) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream(10 * 1024);
- int b = 0;
- while ((b = in.read()) != -1) {
- out.write(b);
- }
- return out.toByteArray();
-}
-
-char[] convertToCharArray(InputStream in) throws IOException {
- CharArrayWriter out = new CharArrayWriter(10 * 1024);
- int b = 0;
- while ((b = in.read()) != -1) {
- out.write(b);
- }
- return out.toCharArray();
-}
-
-cleanUpDirectory(String dirName, String[] filesToKeep){
- dir = new File(dirName);
- if (dir.exists()) {
- files = dir.listFiles();
- for (File f:files) {
- delete = true;
- for (String keep:filesToKeep) {
- if (f.getName().equalsIgnoreCase(keep)) {
- delete = false;
- }
- if (f.getName().endsWith(".deleted")) {
- delete = false;
- }
- }
- if (delete) f.delete();
- }
- }
-}
-
void checkAdmin() {
context = currentContext();
if (context == void || context == null) {
@@ -142,38 +45,6 @@
}
}
-String extractVDBName(url) {
- str = "jdbc:metamatrix:";
- strteiid = "jdbc:teiid:";
- if (url.startsWith(str)) {
- int at = url.indexOf("@");
- if (at != -1) {
- return url.substring(str.length(), at);
- }
- }
- else if (url.startsWith(strteiid)) {
- int at = url.indexOf("@");
- if (at != -1) {
- return url.substring(strteiid.length(), at);
- }
- }
- return "";
-}
-
-String extractHost(url) {
- str = "jdbc:metamatrix:";
- strteiid = "jdbc:teiid:";
- if (url.startsWith(str)) {
- int at = url.indexOf("@")+1;
- return url.substring(at, url.indexOf(";", at));
- }
- else if (url.startsWith(strteiid)) {
- int at = url.indexOf("@")+1;
- return url.substring(at, url.indexOf(";", at));
- }
- return "";
-}
-
class ExceptionHandler implements InvocationHandler{
Object impl;
ExceptionHandler(Object obj){
Modified: trunk/client-jdbc/src/main/java/com/metamatrix/script/io/ResultSetReader.java
===================================================================
---
trunk/client-jdbc/src/main/java/com/metamatrix/script/io/ResultSetReader.java 2009-10-02
19:08:46 UTC (rev 1520)
+++
trunk/client-jdbc/src/main/java/com/metamatrix/script/io/ResultSetReader.java 2009-10-05
14:44:52 UTC (rev 1521)
@@ -60,6 +60,8 @@
boolean firstTime = true;
int[] columnTypes = null;
+ private int rowCount;
+
public ResultSetReader(ResultSet in) {
this.source = in;
}
@@ -103,6 +105,7 @@
// if you get here then we are ready to read the results.
if (source.next()) {
+ rowCount++;
StringBuffer sb = new StringBuffer();
// Walk through column values in this row
for (int col = 1; col <= columnCount; col++) {
@@ -133,6 +136,10 @@
return null;
}
+ public int getRowCount() {
+ return rowCount;
+ }
+
/**
* Get the first line from the result set. This is the resultset metadata line where
* we gather the column names and their types.
Modified:
trunk/common-core/src/main/java/com/metamatrix/core/util/ObjectConverterUtil.java
===================================================================
---
trunk/common-core/src/main/java/com/metamatrix/core/util/ObjectConverterUtil.java 2009-10-02
19:08:46 UTC (rev 1520)
+++
trunk/common-core/src/main/java/com/metamatrix/core/util/ObjectConverterUtil.java 2009-10-05
14:44:52 UTC (rev 1521)
@@ -45,7 +45,7 @@
private static final int DEFAULT_READING_SIZE = 8192;
- protected static byte[] convertToByteArray(final java.sql.Blob data) throws
MetaMatrixCoreException {
+ protected static byte[] convertBlobToByteArray(final java.sql.Blob data) throws
MetaMatrixCoreException {
try {
// Open a stream to read the BLOB data
InputStream l_blobStream = data.getBinaryStream();
@@ -82,7 +82,9 @@
} else if (data instanceof byte[]) {
return (byte[]) data;
} else if (data instanceof java.sql.Blob) {
- return convertToByteArray((java.sql.Blob) data);
+ return convertBlobToByteArray((java.sql.Blob) data);
+ } else if (data instanceof File) {
+ return convertFileToByteArray((File)data);
}
final Object[] params = new Object[]{data.getClass().getName()};
throw new
MetaMatrixCoreException(CorePlugin.Util.getString("ObjectConverterUtil.Object_type_not_supported_for_object_conversion._3",params));
//$NON-NLS-1$