You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ma...@apache.org on 2012/06/29 22:24:21 UTC

svn commit: r1355552 - in /db/derby/code/trunk/java: engine/org/apache/derby/catalog/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: mamta
Date: Fri Jun 29 20:24:20 2012
New Revision: 1355552

URL: http://svn.apache.org/viewvc?rev=1355552&view=rev
Log:
DERBY-5750 Sending an empty string as table name to compress table procedure or empty string as index name to update statistics procedure makes the parser throw an exception. 


Committing changes for DERBY-5750 which will provide following functionality 
a)if schema name is provided as an empty string, we will throw SQLState.LANG_SCHEMA_DOES_NOT_EXIST
b)if table name is provided as an empty string, we will throw SQLState.LANG_TABLE_NOT_FOUND
c)if index name is provided as an empty string(this is for update and drop statistics procedures), we will throw 
	SQLState.LANG_INDEX_NOT_FOUND
d)if schema name is null, we will use current schema to resolve the table name
e)if table name is null, we will throw SQLState.LANG_TABLE_NOT_FOUND
f)if index name is null, we will drop/update statisitcs for all the indexes for the given table.

I have added few test cases for each of these procedures. 


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CompressTableTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UpdateStatisticsTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java?rev=1355552&r1=1355551&r2=1355552&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java Fri Jun 29 20:24:20 2012
@@ -724,14 +724,20 @@ public class SystemProcedures  {
      *     SYSCS_UTIL.SYSCS_UPDATE_STATISTICS
      * <p>
      *
-     * @param schemaname    schema name of the index(es) whose statistics will
-     *                      be updated. Must be non-null, no default is used.
+     * @param schemaname    schema name of the table/index(es) whose 
+     *                      statistics will be updated. null will mean use
+     *                      the current schema to resolve the table name.
+     *                      Empty string for schema name will raise an 
+     *                      exception.
      * @param tablename     table name of the index(es) whose statistics will
-     *                      be updated. Must be non-null.
-     * @param indexname     Can be null. If not null or emptry string then the
-     *                      user wants to update the statistics for only this
-     *                      index. If null, then update the statistics for all
-     *                      the indexes for the given table name.
+     *                      be updated. A null value or an empty string will
+     *                      throw table not found exception. Must be non-null.
+     * @param indexname    If null, then update the statistics for all the 
+     *                      indexes for the given table name. If not null and
+     *                      not empty string, then the user wants to update the
+     *                      statistics for only the give index name.
+     *                      Empty string for index name will raise an 
+     *                      exception.
      *
 	 * @exception  SQLException
      **/
@@ -741,16 +747,24 @@ public class SystemProcedures  {
     	    String  indexname)
     throws SQLException
     {
-        String escapedSchema = IdUtil.normalToDelimited(schemaname);
-        String escapedTableName = IdUtil.normalToDelimited(tablename);
-        String query = "alter table " + escapedSchema + "." + escapedTableName;
+        StringBuffer query = new StringBuffer();
+        query.append("alter table ");
+        query.append(basicSchemaTableValidation(schemaname,tablename));
+
+        //Index name can't be empty string
+        if (indexname != null && indexname.length()==0)
+			throw PublicAPI.wrapStandardException(
+					StandardException.newException(
+							SQLState.LANG_INDEX_NOT_FOUND, 
+							indexname));
+
         if (indexname == null)
-        	query = query + " all update statistics ";
+        	query.append(" all update statistics ");
         else
-        	query = query + " update statistics " + IdUtil.normalToDelimited(indexname);
+        	query.append(" update statistics " + IdUtil.normalToDelimited(indexname));
         Connection conn = getDefaultConn();
 
-        PreparedStatement ps = conn.prepareStatement(query);
+        PreparedStatement ps = conn.prepareStatement(query.toString());
         ps.executeUpdate();
         ps.close();
 
@@ -763,16 +777,21 @@ public class SystemProcedures  {
      * 2)a specific index on a table.
      * 
      * @param schemaname    schema name of the table/index(es) whose 
-     *                      statistics will be dropped. Must be non-null, 
-     *                      no default is used.
+     *                      statistics will be dropped. null will mean use
+     *                      the current schema to resolve the table name.
+     *                      Empty string for schema name will raise an 
+     *                      exception.
      * @param tablename     table name of the index(es) whose statistics will
-     *                      be dropped. Must be non-null.
-     * @param indexname     Can be null. If not null or emptry string then the
-     *                      user wants to drop the statistics for only this
-     *                      index. If null, then drop the statistics for all
-     *                      the indexes for the given table name.
+     *                      be dropped. A null value or an empty string will
+     *                      throw table not found exception. Must be non-null.
+     * @param indexname     If null, then drop the statistics for all the 
+     *                      indexes for the given table name. If not null and
+     *                      not empty string, then the user wants to drop the
+     *                      statistics for only the give index name.
+     *                      Empty string for index name will raise an 
+     *                      exception.
      *
-	 * @exception  SQLException
+	 * @exception  SQLException 
      */
     public static void SYSCS_DROP_STATISTICS(
     String  schemaname,
@@ -780,16 +799,24 @@ public class SystemProcedures  {
     String  indexname)
         throws SQLException
     {
-        String escapedSchema = IdUtil.normalToDelimited(schemaname);
-        String escapedTableName = IdUtil.normalToDelimited(tablename);
-        String query = "alter table " + escapedSchema + "." + escapedTableName;
+        StringBuffer query = new StringBuffer();
+        query.append("alter table ");
+        query.append(basicSchemaTableValidation(schemaname,tablename));
+
+        //Index name can't be empty string
+        if (indexname != null && indexname.length()==0)
+			throw PublicAPI.wrapStandardException(
+					StandardException.newException(
+							SQLState.LANG_INDEX_NOT_FOUND, 
+							indexname));
+        
         if (indexname == null)
-        	query = query + " all drop statistics ";
+        	query.append(" all drop statistics ");
         else
-        	query = query + " statistics drop " + IdUtil.normalToDelimited(indexname);
+        	query.append(" statistics drop " + IdUtil.normalToDelimited(indexname));
         Connection conn = getDefaultConn();
 
-        PreparedStatement ps = conn.prepareStatement(query);
+        PreparedStatement ps = conn.prepareStatement(query.toString());
         ps.executeUpdate();
         ps.close();
 
@@ -797,6 +824,44 @@ public class SystemProcedures  {
     }
 
     /**
+     * Do following checks
+     * a)Schema name can't be empty string
+     * b)If schema name is null, then we use current schema
+     * c)Table name can't be null or empty string
+     * 
+     * @param schemaname    If schema name is null, then we will use the 
+     *                      current schema to resolve the table name. Empty
+     *                      string for schema name will raise an exception.
+     * @param tablename     If table name is null or an empty string, we will
+     *                      throw table not found exception.
+     * @return schemaname.tablename or tablename
+     * @throws SQLException 
+     *         a)if schema name is empty string
+     *         b)if table name is empty string
+     *         c)if table name is null
+     */
+    private static String basicSchemaTableValidation(
+    String schemaname, String tablename) 
+        throws SQLException
+    {
+        //Schema name can't be empty string
+        if (schemaname != null && schemaname.length()==0)
+			throw PublicAPI.wrapStandardException(
+					StandardException.newException(
+							SQLState.LANG_SCHEMA_DOES_NOT_EXIST, 
+							schemaname));
+
+        //Table name can't be null or empty string
+        if ((tablename==null) || tablename.length()==0)
+			throw PublicAPI.wrapStandardException(
+					StandardException.newException(
+							SQLState.LANG_TABLE_NOT_FOUND, 
+							tablename));
+        	        
+        return IdUtil.mkQualifiedName(schemaname, tablename);
+    }
+
+    /**
      * Compress the table.
      * <p>
      * Calls the "alter table compress {sequential}" sql.  This syntax
@@ -806,31 +871,32 @@ public class SystemProcedures  {
      *     SYSCS_UTIL.SYSCS_COMPRESS_TABLE
      * <p>
      *
-     * @param schema        schema name of the table to compress.  Must be
-     *                      non-null, no default is used.
-     * @param tablename     table name of the table to compress.  Must be
-     *                      non-null.
+     * @param schemaname    schema name of the table to compress. null will 
+     *                      mean use the current schema to resolve the table
+     *                      name. Empty string for schema name will raise an 
+     *                      exception.
+     * @param tablename     table name of the table to compress. A null value 
+     *                      or an empty string will throw table not found 
+     *                      exception. Must be non-null.
      * @param sequential    if non-zero then rebuild indexes sequentially,
      *                      if 0 then rebuild all indexes in parallel.
      *
 	 * @exception  StandardException  Standard exception policy.
      **/
     public static void SYSCS_COMPRESS_TABLE(
-    String  schema,
+    String  schemaname,
     String  tablename,
     short     sequential)
         throws SQLException
     {
-
-        String escapedSchema = IdUtil.normalToDelimited(schema);
-        String escapedTableName = IdUtil.normalToDelimited(tablename);
-        String query = 
-            "alter table " + escapedSchema + "." + escapedTableName +
-			" compress" +  (sequential != 0 ? " sequential" : "");
+        StringBuffer query = new StringBuffer();
+        query.append("alter table ");
+        query.append(basicSchemaTableValidation(schemaname,tablename));
+        query.append(" compress" +  (sequential != 0 ? " sequential" : ""));
 
 		Connection conn = getDefaultConn();
         
-        PreparedStatement ps = conn.prepareStatement(query);
+        PreparedStatement ps = conn.prepareStatement(query.toString());
 		ps.executeUpdate();
         ps.close();
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CompressTableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CompressTableTest.java?rev=1355552&r1=1355551&r2=1355552&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CompressTableTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CompressTableTest.java Fri Jun 29 20:24:20 2012
@@ -22,6 +22,7 @@ limitations under the License.
 package org.apache.derbyTesting.functionTests.tests.lang;
 
 import java.sql.Connection;
+import java.sql.ResultSet;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Statement;
@@ -49,6 +50,53 @@ public class CompressTableTest extends B
                 TestConfiguration.embeddedSuite(CompressTableTest.class));
     }
 
+    //DERBY-5750(Sending an empty string as table name to compress table 
+    // procedure or empty string as index name to update statistics procedure 
+    // makes the parser throw an exception.)
+    //
+    //No table name will result in the same exception that a user would
+    // get when issuing the compress table sql directly without the table name
+    // eg alter table compress sequential
+    // Notice that the table name is missing in the compress sql above
+    public void testCompressTableWithEmptyParamsDerby5750() throws SQLException {
+        Statement s = createStatement();
+        s.execute("create table DERBY5750_t1 (c11 int)");
+        
+        //Following 2 statements will give exception since there is no schema
+        // named empty string
+        assertStatementError(
+        		"42Y07", s,
+        		"call syscs_util.syscs_compress_table('','DERBY5750_T1',1)");
+        assertStatementError(
+        		"42Y07", s,
+        		"call syscs_util.syscs_compress_table('','',1)");
+
+        //null schema name will translate to current schema
+        s.execute("call syscs_util.syscs_compress_table(null,'DERBY5750_T1',1)");
+
+        //Following 2 statements will give exception since there is no table  
+        // named empty string
+        assertStatementError(
+        		"42X05", s,
+        		"call syscs_util.syscs_compress_table(null,'',1)");
+        assertStatementError(
+        		"42X05", s,
+                "call syscs_util.syscs_compress_table('APP','',1)");
+
+        //Following 2 statements will give exception since table name can't 
+        // be null
+        assertStatementError(
+        		"42X05", s,
+        		"call syscs_util.syscs_compress_table(null,null,1)");
+        assertStatementError(
+        		"42X05", s,
+        		"call syscs_util.syscs_compress_table('APP',null,1)");
+
+        s.execute("call syscs_util.syscs_compress_table('APP','DERBY5750_T1',1)");
+        
+        s.execute("drop table DERBY5750_t1");    	
+    }
+    
     /**
      * Test that SYSCS_COMPRESS_TABLE and SYSCS_INPLACE_COMPRESS_TABLE work
      * when the table name contains a double quote. It used to raise a syntax

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UpdateStatisticsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UpdateStatisticsTest.java?rev=1355552&r1=1355551&r2=1355552&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UpdateStatisticsTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UpdateStatisticsTest.java Fri Jun 29 20:24:20 2012
@@ -108,6 +108,100 @@ public class UpdateStatisticsTest extend
         assertEquals(initialStatsCount, stats.getStats().length);
     }
 
+    //DERBY-5750(Sending an empty string as table name to compress table 
+    // procedure or empty string as index name to update statistics procedure 
+    // makes the parser throw an exception.)
+    //
+    //No table name will result in exception since Derby doesn't know table
+    // whose statistics it needs to be update/drop
+    public void testStatisticsProcsWithEmptyParamsDerby5750() throws SQLException {
+        Statement s = createStatement();
+        s.execute("create table DERBY5750_t1 (c11 int)");
+        s.executeUpdate("CREATE INDEX DERBY5750_I1 ON DERBY5750_t1(c11)");
+        //Following statements will give exceptions since there is no schema
+        // named empty string
+        assertStatementError(
+                "42Y07", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'','DERBY5750_T1','DERBY5750_I1')");
+        assertStatementError(
+                "42Y07", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'','DERBY5750_T1','DERBY5750_I1')");
+        assertStatementError(
+                "42Y07", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'','','DERBY5750_I1')");
+        assertStatementError(
+                "42Y07", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'','','DERBY5750_I1')");
+
+        //null schema name will translate to current schema
+        s.execute("call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "null,'DERBY5750_T1','DERBY5750_I1')");
+        s.execute("call syscs_util.SYSCS_DROP_STATISTICS(" +
+                "null,'DERBY5750_T1','DERBY5750_I1')");
+        
+        //Following statements will give exceptions since there is no table  
+        // named empty string
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "null,'','DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "null,'','DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'APP','','DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'APP','','DERBY5750_I1')");
+
+        //Following statements will give exceptions since table name can't 
+        // be null
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "null,null,'DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "null,null,'DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'APP',null,'DERBY5750_I1')");
+        assertStatementError(
+                "42X05", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'APP',null,'DERBY5750_I1')");
+
+        //Provide all the 3 params, schema, table and index name
+        s.execute("call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'APP','DERBY5750_T1','DERBY5750_I1')");
+        s.execute("call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'APP','DERBY5750_T1','DERBY5750_I1')");
+        
+        
+        //Following statements will give exceptions since there is no index  
+        // named empty string
+        assertStatementError(
+                "42X65", s,
+                "call syscs_util.SYSCS_UPDATE_STATISTICS("+
+                "'APP','DERBY5750_T1','')");
+        assertStatementError(
+                "42X65", s,
+                "call syscs_util.SYSCS_DROP_STATISTICS("+
+                "'APP','DERBY5750_T1','')");
+        
+        s.execute("drop table DERBY5750_t1");    	
+    }
+
     /**
      * Test for update statistics
      */