Author: tmortagne
Date: 2007-10-24 11:31:48 +0200 (Wed, 24 Oct 2007)
New Revision: 5485

Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
Log:
XWIKI-1740: Can now create empty wiki, browse a sub wiki etc in HQSL and DERBY. 
There are still problems yet :
- cannot create wiki from package
- cannot create wiki from template wiki (same hibernate errors than "from 
package" feature)
- HSQLDB seems to not save schemas/databases others that the main one

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
 2007-10-24 09:31:45 UTC (rev 5484)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
 2007-10-24 09:31:48 UTC (rev 5485)
@@ -5,6 +5,7 @@
 import com.xpn.xwiki.XWikiException;
 import com.xpn.xwiki.monitor.api.MonitorPlugin;
 import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.store.DatabaseProduct;
 import com.xpn.xwiki.util.Util;
 import com.xpn.xwiki.web.XWikiRequest;
 
@@ -271,59 +272,106 @@
     }
 
     /**
-     * This function gets the schema update scripts generated by comparing the 
current database
-     * with the current hibernate mapping config.
+     * Convert wiki name in database/schema name.
+     * 
+     * @param wikiName the wiki name to convert.
+     * @param context the XWiki context.
+     * @return the database/schema name.
+     */
+    protected String getSchemaFromWikiName(String wikiName, XWikiContext 
context)
+    {
+        if (wikiName == null) {
+            return null;
+        }
+
+        DatabaseProduct databaseProduct = getDatabaseProductName(context);
+
+        wikiName = wikiName.toLowerCase();
+
+        if (databaseProduct == DatabaseProduct.DERBY) {
+            return wikiName.equals(context.getMainXWiki().toLowerCase()) ? 
"APP" : wikiName
+                .replace('-', '_');
+        } else if (databaseProduct == DatabaseProduct.HSQLDB) {
+            return wikiName.equals(context.getMainXWiki().toLowerCase()) ? 
"PUBLIC" : wikiName
+                .replace('-', '_');
+        } else
+            return wikiName.replace('-', '_');
+    }
+
+    /**
+     * Convert context's database in real database/schema name.
+     * 
+     * @param context the XWiki context.
+     * @return the database/schema name.
+     */
+    protected String getSchemaFromWikiName(XWikiContext context)
+    {
+        return getSchemaFromWikiName(context.getDatabase(), context);
+    }
+
+    /**
+     * This function gets the schema update scripts generated by comparing the 
current database woth
+     * the current hibernate mapping config.
+     * 
      * @param config
      * @param context
      * @return
      * @throws HibernateException
      */
-    public String[] getSchemaUpdateScript(Configuration config, XWikiContext 
context) throws HibernateException {
+    public String[] getSchemaUpdateScript(Configuration config, XWikiContext 
context)
+        throws HibernateException
+    {
         String[] schemaSQL = null;
 
         Session session;
         Connection connection;
         DatabaseMetadata meta;
-        Statement stmt=null;
+        Statement stmt = null;
         Dialect dialect = 
Dialect.getDialect(getConfiguration().getProperties());
         boolean bTransaction = true;
         String dschema = null;
-        boolean isOracle = false;
 
         try {
             bTransaction = beginTransaction(false, context);
             session = getSession(context);
             connection = session.connection();
             setDatabase(session, context);
-            isOracle = (DatabaseProduct.ORACLE == 
getDatabaseProductName(context)); 
             
-            if (isOracle) {
+            String contextSchema = getSchemaFromWikiName(context);
+
+            DatabaseProduct databaseProduct = getDatabaseProductName(context);
+            if (databaseProduct == DatabaseProduct.ORACLE
+                || databaseProduct == DatabaseProduct.HSQLDB || 
databaseProduct == DatabaseProduct.DERBY) {
                 dschema = config.getProperty(Environment.DEFAULT_SCHEMA);
-                config.setProperty(Environment.DEFAULT_SCHEMA, 
context.getDatabase());
+                config.setProperty(Environment.DEFAULT_SCHEMA, contextSchema);
                 Iterator iter = config.getTableMappings();
-                while ( iter.hasNext() ) {
+                while (iter.hasNext()) {
                     Table table = (Table) iter.next();
-                    table.setSchema(context.getDatabase());
+                    table.setSchema(contextSchema);
                 }
             }
+
             meta = new DatabaseMetadata(connection, dialect);
             stmt = connection.createStatement();
             schemaSQL = config.generateSchemaUpdateScript(dialect, meta);
-        }
-        catch (Exception e) {
-            if ( log.isErrorEnabled() ) log.error("Failed creating schema 
update script", e);
-        }
-        finally {
+        } catch (Exception e) {
+            if (log.isErrorEnabled())
+                log.error("Failed creating schema update script", e);
+        } finally {
             try {
-                if (stmt!=null) stmt.close();
-                if (bTransaction)
+                if (stmt != null) {
+                    stmt.close();
+                }
+                if (bTransaction) {
                     endTransaction(context, false, false);
-                if (dschema!=null)
+                }
+                if (dschema != null) {
                     config.setProperty(Environment.DEFAULT_SCHEMA, dschema);
+                }
+            } catch (Exception e) {
             }
-            catch (Exception e) {
-            }
         }
+        
         return schemaSQL;
     }
 
@@ -432,73 +480,73 @@
     }
 
     /**
-     * Virtual Wikis
-     * Allows to switch database connection
+     * Virtual Wikis Allows to switch database connection
+     * 
      * @param session
      * @param context
      * @throws XWikiException
      */
-    public void setDatabase(Session session, XWikiContext context) throws 
XWikiException {
+    public void setDatabase(Session session, XWikiContext context) throws 
XWikiException
+    {
         if (isVirtual(context)) {
-            String database = context.getDatabase();
             try {
-                if ( log.isDebugEnabled() ) log.debug("Switch database to: " + 
database);
-                if (database!=null) {
-                       
-                       DatabaseProduct databaseProduct = 
getDatabaseProductName(context);
+                if (log.isDebugEnabled())
+                    log.debug("Switch database to: " + context.getDatabase());
+                
+                if (context.getDatabase() != null) {
+                    String schemaName = getSchemaFromWikiName(context);
+                    
+                    DatabaseProduct databaseProduct = 
getDatabaseProductName(context);
                     if (DatabaseProduct.ORACLE == databaseProduct) {
                         Statement stmt = null;
                         try {
                             stmt = session.connection().createStatement();
-                            stmt.execute("alter session set current_schema = " 
+ database);
+                            stmt.execute("alter session set current_schema = " 
+ schemaName);
                         } finally {
                             try {
-                                if (stmt!=null)
+                                if (stmt != null)
                                     stmt.close();
-                            } catch (Exception e) {}
+                            } catch (Exception e) {
+                            }
                         }
                     } else if (DatabaseProduct.DERBY == databaseProduct) {
-                                               Statement stmt = null;
-                                               try {
-                                                       stmt = 
session.connection().createStatement();
-                                                       // Use the default 
Derby database ("APP") instead of "xwiki" 
-                            // (considering that "xwiki" is main wiki)
-                                                       stmt.execute("SET 
SCHEMA "
-                                + (database.toLowerCase().equals("xwiki") ? 
"APP" : database));
-                                               } finally {
-                                                       try {
-                                                               if (stmt != 
null)
-                                                                       
stmt.close();
-                                                       } catch (Exception e) {
-                                                       }
-                                               }
+                        Statement stmt = null;
+                        try {
+                            stmt = session.connection().createStatement();
+                            stmt.execute("SET SCHEMA " + schemaName);
+                        } finally {
+                            try {
+                                if (stmt != null)
+                                    stmt.close();
+                            } catch (Exception e) {
+                            }
+                        }
                     } else if (DatabaseProduct.HSQLDB == databaseProduct) {
-                                               Statement stmt = null;
-                                               try {
-                                                       stmt = 
session.connection().createStatement();
-                                                       // Use the default HSQL 
database ("PUBLIC") instead of "xwiki"
-                            // (considering that "xwiki" is main wiki)
-                                                       stmt.execute("SET 
SCHEMA "
-                                + (database.toLowerCase().equals("xwiki") ? 
"PUBLIC" : database));
-                                               } finally {
-                                                       try {
-                                                               if (stmt != 
null)
-                                                                       
stmt.close();
-                                                       } catch (Exception e) {
-                                                       }
-                                               }
-                                       } else {
+                        Statement stmt = null;
+                        try {
+                            stmt = session.connection().createStatement();
+                            stmt.execute("SET SCHEMA " + schemaName);
+                        } finally {
+                            try {
+                                if (stmt != null)
+                                    stmt.close();
+                            } catch (Exception e) {
+                            }
+                        }
+                    } else {
                         String catalog = session.connection().getCatalog();
-                        catalog = (catalog==null) ? null : 
catalog.replace('_', '-');
-                        if (!database.equals(catalog))
-                            
session.connection().setCatalog(database.replace('-','_'));
+                        catalog = (catalog == null) ? null : 
catalog.replace('_', '-');
+                        if (!schemaName.equals(catalog))
+                            session.connection().setCatalog(schemaName);
                     }
                 }
             } catch (Exception e) {
-                Object[] args = { database };
-                throw new XWikiException( XWikiException.MODULE_XWIKI_STORE,
+                Object[] args = {context.getDatabase()};
+                throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                     XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SWITCH_DATABASE,
-                        "Exception while switching to database {0}", e, args);
+                    "Exception while switching to database {0}",
+                    e,
+                    args);
             }
         }
     }

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
     2007-10-24 09:31:45 UTC (rev 5484)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
     2007-10-24 09:31:48 UTC (rev 5485)
@@ -35,6 +35,7 @@
 import com.xpn.xwiki.objects.classes.TextAreaClass;
 import com.xpn.xwiki.render.XWikiRenderer;
 import com.xpn.xwiki.stats.impl.XWikiStats;
+import com.xpn.xwiki.store.DatabaseProduct;
 import com.xpn.xwiki.util.Util;
 import com.xpn.xwiki.web.Utils;
 import org.apache.commons.lang.ArrayUtils;
@@ -124,16 +125,18 @@
             Connection connection = session.connection();
             stmt = connection.createStatement();
 
+            String schema = getSchemaFromWikiName(wikiName, context);
+
             DatabaseProduct databaseProduct = getDatabaseProductName(context);
             if (DatabaseProduct.ORACLE == databaseProduct) {
-                stmt.execute("create user " + wikiName + " identified by " + 
wikiName);
-                stmt.execute("grant resource to " + wikiName);
+                stmt.execute("create user " + schema + " identified by " + 
schema);
+                stmt.execute("grant resource to " + schema);
             } else if (DatabaseProduct.DERBY == databaseProduct) {
-                               stmt.execute("CREATE SCHEMA " + 
wikiName.replace('-', '_'));
+                stmt.execute("CREATE SCHEMA " + schema);
             } else if (DatabaseProduct.HSQLDB == databaseProduct) {
-                               stmt.execute("CREATE SCHEMA " + 
wikiName.replace('-', '_') + " AUTHORIZATION DBA");
+                stmt.execute("CREATE SCHEMA " + schema + " AUTHORIZATION DBA");
             } else {
-                stmt.execute("create database " + wikiName.replace('-','_'));
+                stmt.execute("create database " + schema);
             }
 
             endTransaction(context, true);

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to