Author: michiel
Date: 2010-03-18 19:04:13 +0100 (Thu, 18 Mar 2010)
New Revision: 41525

Added:
   mmbase/trunk/core/src/main/java/org/mmbase/storage/util/Sql.java
Modified:
   
mmbase/trunk/core/src/main/java/org/mmbase/util/xml/applicationdata/resources/application_1_1.dtd
Log:
small application of   MMB-1837

Copied: mmbase/trunk/core/src/main/java/org/mmbase/storage/util/Sql.java (from 
rev 41524, 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/storage/util/Sql.java)
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/storage/util/Sql.java            
                (rev 0)
+++ mmbase/trunk/core/src/main/java/org/mmbase/storage/util/Sql.java    
2010-03-18 18:04:13 UTC (rev 41525)
@@ -0,0 +1,101 @@
+/*
+
+This software is OSI Certified Open Source Software.
+OSI Certified is a certification mark of the Open Source Initiative.
+
+The license (Mozilla version 1.0) can be read at the MMBase site.
+See http://www.MMBase.org/license
+
+*/
+package org.mmbase.storage.util;
+
+import java.util.*;
+import java.sql.*;
+import javax.sql.*;
+
+
+import org.mmbase.module.core.MMBase;
+import org.mmbase.storage.implementation.database.Attributes;
+import org.mmbase.util.logging.*;
+
+
+/**
+ * Meant to be used as an 'AfterDeployment' job of an app1 xml. Automaticly 
execute some SQL.
+ * E.g.
+  <afterdeployment>
+    <runnable version="10" class="org.mmbase.storage.util.Sql">
+      <param name="query">alter table $PREFIX_blocks add refreshpolicy 
TEXT</param>
+    </runnable>
+    <runnable version="10" class="org.mmbase.storage.util.Sql">
+      <param name="query">alter table $PREFIX_blocks add 
refreshpolicyparam TEXT</param>
+    </runnable>
+  </afterdeployment>
+ *
+ * @author Michiel Meeuwissen
+ * @version $Id: StorageReader.java 36284 2009-06-22 20:58:09Z michiel $
+ * @since MMBase-1.9.3
+ */
+public class Sql implements Runnable {
+
+    private static final Logger log = Logging.getLoggerInstance(Sql.class);
+    private String query;
+    private int max = Integer.MAX_VALUE;
+
+    public void setQuery(String q) {
+        query = q;
+    }
+
+    public void run() {
+        Connection con = null;
+        Statement stmt = null;
+        try {
+            DataSource dataSource = (DataSource) 
MMBase.getMMBase().getStorageManagerFactory().getAttribute(Attributes.DATA_SOURCE);
+            con = dataSource.getConnection();
+            stmt = con.createStatement();
+            query = query.replace("$PREFIX", MMBase.getMMBase().getBaseName());
+            ResultSet rs = 
stmt.executeQuery(org.mmbase.util.transformers.Xml.XMLUnescape(query));
+            {
+                StringBuilder head = new StringBuilder();
+                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++)  {
+                    if (i > 1) {
+                        head.append(",");
+                    }
+                    head.append(rs.getMetaData().getColumnName(i));
+                }
+                log.info(head);
+            }
+            int seq = 0;
+            while(true) {
+                boolean valid = rs.next();
+                seq ++;
+                if (seq >= max) break;
+                if (! valid) break;
+                StringBuilder line = new StringBuilder();
+                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
+                    if (i > 1) {
+                        line.append(",");
+                    }
+                    line.append(rs.getString(i)).append(',');
+                }
+            }
+        } catch (SQLException  sqe) {
+            throw new RuntimeException(sqe);
+        } finally {
+            try {
+                if (stmt != null) {
+                    stmt.close();
+                }
+            } catch (Exception g) {}
+            try {
+                if (con != null) {
+                    con.close();
+                }
+            } catch (Exception g) {}
+        }
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getName() + ":" + query;
+    }
+}

Modified: 
mmbase/trunk/core/src/main/java/org/mmbase/util/xml/applicationdata/resources/application_1_1.dtd
===================================================================
--- 
mmbase/trunk/core/src/main/java/org/mmbase/util/xml/applicationdata/resources/application_1_1.dtd
   2010-03-18 18:03:08 UTC (rev 41524)
+++ 
mmbase/trunk/core/src/main/java/org/mmbase/util/xml/applicationdata/resources/application_1_1.dtd
   2010-03-18 18:04:13 UTC (rev 41525)
@@ -57,10 +57,14 @@
 
 
 <!ELEMENT afterdeployment (runnable*)>
-<!ELEMENT runnable EMPTY>
+<!ELEMENT runnable (param*)>
 <!ATTLIST runnable class CDATA #REQUIRED>
 <!ATTLIST runnable version CDATA #IMPLIED>
 
+<!ELEMENT param (#PCDATA)>
+<!ATTLIST param name CDATA #REQUIRED>
+
+
 <!ELEMENT description (#PCDATA)>
 <!ELEMENT install-notice (#PCDATA)>
 

_______________________________________________
Cvs mailing list
Cvs@lists.mmbase.org
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to