← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12047: Quoting sql view names in order to handle special characters and HSQLDB, which does not accept le...

 

------------------------------------------------------------
revno: 12047
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-09-11 18:52:21 +0200
message:
  Quoting sql view names in order to handle special characters and HSQLDB, which does not accept leading underscores in names
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewExpandStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java	2013-09-11 15:26:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java	2013-09-11 16:52:21 +0000
@@ -87,7 +87,7 @@
     {
         final Pattern p = Pattern.compile( "\\W" );
 
-        String input = new String( this.name );
+        String input = name;
         
         String[] items = p.split( input.trim().replaceAll( "_", "" ) );
 
@@ -95,7 +95,7 @@
 
         for ( String s : items )
         {
-            input += (s.equals( "" ) == true) ? "" : ("_" + s);
+            input += s.isEmpty() ? "" : ( "_" + s );
         }
 
         return PREFIX_VIEWNAME + input;

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewExpandStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewExpandStore.java	2013-08-23 16:00:30 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewExpandStore.java	2013-09-11 16:52:21 +0000
@@ -33,6 +33,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.jdbc.StatementBuilder;
 import org.hisp.dhis.sqlview.SqlView;
 import org.hisp.dhis.sqlview.SqlViewExpandStore;
 import org.hisp.dhis.system.util.SqlHelper;
@@ -50,7 +51,6 @@
     private static final Log log = LogFactory.getLog( JdbcSqlViewExpandStore.class );
     
     private static final String PREFIX_CREATEVIEW_QUERY = "CREATE VIEW ";
-    private static final String PREFIX_DROPVIEW_QUERY = "DROP VIEW IF EXISTS ";
     private static final String PREFIX_SELECT_QUERY = "SELECT * FROM ";
 
     // -------------------------------------------------------------------------
@@ -63,6 +63,13 @@
     {
         this.jdbcTemplate = jdbcTemplate;
     }
+    
+    private StatementBuilder statementBuilder;
+
+    public void setStatementBuilder( StatementBuilder statementBuilder )
+    {
+        this.statementBuilder = statementBuilder;
+    }
 
     // -------------------------------------------------------------------------
     // Implementing methods
@@ -73,7 +80,7 @@
     {
         try
         {
-            jdbcTemplate.queryForRowSet( "select * from " + viewTableName.toLowerCase() + " limit 1" );
+            jdbcTemplate.queryForRowSet( "select * from " + statementBuilder.columnQuote( viewTableName.toLowerCase() ) + " limit 1" );
             
             return true;
         }
@@ -90,7 +97,7 @@
 
         dropViewTable( viewName );
 
-        final String sql = PREFIX_CREATEVIEW_QUERY + viewName + " AS " + sqlViewInstance.getSqlQuery();
+        final String sql = PREFIX_CREATEVIEW_QUERY + statementBuilder.columnQuote( viewName ) + " AS " + sqlViewInstance.getSqlQuery();
         
         log.debug( "Create view SQL: " + sql );
         
@@ -109,7 +116,7 @@
     @Override
     public void setUpDataSqlViewTable( Grid grid, String viewTableName, Map<String, String> criteria )
     {
-        String sql = PREFIX_SELECT_QUERY + viewTableName;
+        String sql = PREFIX_SELECT_QUERY + statementBuilder.columnQuote( viewTableName );
         
         if ( criteria != null && !criteria.isEmpty() )
         {
@@ -157,7 +164,7 @@
     {
         try
         {
-            jdbcTemplate.update( PREFIX_DROPVIEW_QUERY + viewName );
+            jdbcTemplate.update( "DROP VIEW IF EXISTS " + statementBuilder.columnQuote( viewName ) );
         }
         catch ( BadSqlGrammarException ex )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2013-09-01 18:30:05 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2013-09-11 16:52:21 +0000
@@ -92,6 +92,7 @@
 
   <bean id="org.hisp.dhis.sqlview.SqlViewExpandStore" class="org.hisp.dhis.sqlview.jdbc.JdbcSqlViewExpandStore">
     <property name="jdbcTemplate" ref="jdbcTemplate" />
+    <property name="statementBuilder" ref="statementBuilder" />
   </bean>
 
   <bean id="org.hisp.dhis.sqlview.SqlViewService" class="org.hisp.dhis.sqlview.DefaultSqlViewService">

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2013-09-10 14:22:41 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2013-09-11 16:52:21 +0000
@@ -59,6 +59,7 @@
      * @return the SQL encoded value.
      */
     String encode( String value, boolean quote );
+    
     /**
      * Returns the character used to quote database table and column names.
      * 
@@ -67,6 +68,14 @@
     String getColumnQuote();
     
     /**
+     * Wraps the given column or table in quotes.
+     * 
+     * @param column the column or table name.
+     * @return the column or table name wrapped in quotes.
+     */
+    String columnQuote( String column );
+    
+    /**
      * Returns statement for vacuum and analyze operations for a table. Returns
      * null if such statement is not relevant.
      * 

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java	2013-09-10 14:22:41 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java	2013-09-11 16:52:21 +0000
@@ -56,6 +56,11 @@
         
         return quote ? ( QUOTE + value + QUOTE ) : value;
     }
+    
+    public String columnQuote( String column )
+    {
+        return column != null ? ( getColumnQuote() + column + getColumnQuote() ) : null;
+    }
 
     @Override
     public String getPeriodIdentifierStatement( Period period )