[hibernate-commits] Hibernate SVN: r12842 - in shards/trunk: src/test/org/hibernate/shards and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sat Jul 28 17:28:06 EDT 2007


Author: max.ross
Date: 2007-07-28 17:28:06 -0400 (Sat, 28 Jul 2007)
New Revision: 12842

Added:
   shards/trunk/src/test/org/hibernate/shards/example/
   shards/trunk/src/test/org/hibernate/shards/example/WeatherReport.java
   shards/trunk/src/test/org/hibernate/shards/example/WeatherReportApp.java
   shards/trunk/src/test/org/hibernate/shards/example/hibernate0.cfg.xml
   shards/trunk/src/test/org/hibernate/shards/example/hibernate1.cfg.xml
   shards/trunk/src/test/org/hibernate/shards/example/hibernate2.cfg.xml
   shards/trunk/src/test/org/hibernate/shards/example/weather.hbm.xml
   shards/trunk/src/test/org/hibernate/shards/util/DatabaseUtils.java
Modified:
   shards/trunk/doc/reference/en/modules/configuration.xml
   shards/trunk/doc/reference/en/modules/limitations.xml
   shards/trunk/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java
   shards/trunk/src/test/org/hibernate/shards/integration/platform/hsql/HSQLDatabasePlatform.java
Log:
added in a sample app that matches the samples in the documentation

Modified: shards/trunk/doc/reference/en/modules/configuration.xml
===================================================================
--- shards/trunk/doc/reference/en/modules/configuration.xml	2007-07-28 06:39:31 UTC (rev 12841)
+++ shards/trunk/doc/reference/en/modules/configuration.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -132,7 +132,7 @@
 15
 16    ShardStrategyFactory buildShardStrategyFactory() {
 17        ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory() {
-18            public ShardStrategy newShardStrategy(List shardIds) {
+18            public ShardStrategy newShardStrategy(List<ShardId> shardIds) {
 19                RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds);
 20                ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(loadBalancer);
 21                ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds);
@@ -223,7 +223,7 @@
  3      <session-factory name="HibernateSessionFactory0"> <!-- note the different name -->
  4        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  5        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- 6        <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
+ 6        <property name="connection.url">jdbc:mysql://dbhost0:3306/mydb</property>
  7        <property name="connection.username">my_user</property>
  8        <property name="connection.password">my_password</property>
  9        <property name="hibernate.connection.shard_id">0</property> <!-- new -->
@@ -237,7 +237,7 @@
  3      <session-factory name="HibernateSessionFactory1"> <!-- note the different name -->
  4        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  5        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- 6        <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
+ 6        <property name="connection.url">jdbc:mysql://dbhost1:3306/mydb</property>
  7        <property name="connection.username">my_user</property>
  8        <property name="connection.password">my_password</property>
  9        <property name="hibernate.connection.shard_id">1</property> <!-- new -->
@@ -246,7 +246,8 @@
  12  </hibernate-configuration>
                 ]]></programlisting>
             We'll skip the contents of shard2.hibernate.cfg.xml because the pattern should by now be obvious. We're
-            giving each session factory a unique name via the name attribute of the session-factory element, and we're
+            giving each session factory a unique name via the name attribute of the session-factory element,
+            and we're associating each session factory with a different database server.  We're
             also giving each session factory a shard id. This is required. If you try to configure a
             <classname>ShardedSessionFactory</classname> with a <classname>Configuration</classname>
             object that does not have a shard id you'll get an error. At the moment we require that the shard id of one

Modified: shards/trunk/doc/reference/en/modules/limitations.xml
===================================================================
--- shards/trunk/doc/reference/en/modules/limitations.xml	2007-07-28 06:39:31 UTC (rev 12841)
+++ shards/trunk/doc/reference/en/modules/limitations.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -12,7 +12,7 @@
             <classname>ShardedCriteriaImpl</classname>, and <classname>ShardedQueryImpl</classname>.
         </para>
     </sect1>
-    <sect1 id="shards-limitations-cross-shard" revision="1">
+    <sect1 id="shards-limitations-cross-shard" revision="2">
         <title>Cross-Shard Object Graphs</title>
         <para>
             Hibernate Shards does not currently support cross-shard object graphs.

Added: shards/trunk/src/test/org/hibernate/shards/example/WeatherReport.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/WeatherReport.java	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/WeatherReport.java	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,105 @@
+/**
+ * Copyright (C) 2007 Google Inc.
+ *
+ * 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 org.hibernate.shards.example;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ * Model object for our example app
+ *
+ * @author maxr at google.com (Max Ross)
+ */
+public class WeatherReport {
+  private BigInteger reportId;
+  private String continent;
+  private BigDecimal latitude;
+  private BigDecimal longitude;
+  private int temperature;
+  private Date reportTime;
+
+
+  public BigInteger getReportId() {
+    return reportId;
+  }
+
+  public void setReportId(BigInteger reportId) {
+    this.reportId = reportId;
+  }
+
+  public String getContinent() {
+    return continent;
+  }
+
+  public void setContinent(String continent) {
+    this.continent = continent;
+  }
+
+  public BigDecimal getLatitude() {
+    return latitude;
+  }
+
+  public void setLatitude(BigDecimal latitude) {
+    this.latitude = latitude;
+  }
+
+  public BigDecimal getLongitude() {
+    return longitude;
+  }
+
+  public void setLongitude(BigDecimal longitude) {
+    this.longitude = longitude;
+  }
+
+  public int getTemperature() {
+    return temperature;
+  }
+
+  public void setTemperature(int temperature) {
+    this.temperature = temperature;
+  }
+
+  public Date getReportTime() {
+    return reportTime;
+  }
+
+  public void setReportTime(Date reportTime) {
+    this.reportTime = reportTime;
+  }
+
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    WeatherReport that = (WeatherReport) o;
+
+    return !(reportId != null ? !reportId.equals(that.reportId)
+        : that.reportId != null);
+
+  }
+
+  public int hashCode() {
+    return (reportId != null ? reportId.hashCode() : 0);
+  }
+}

Added: shards/trunk/src/test/org/hibernate/shards/example/WeatherReportApp.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/WeatherReportApp.java	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/WeatherReportApp.java	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,160 @@
+/**
+ * Copyright (C) 2007 Google Inc.
+ *
+ * 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 org.hibernate.shards.example;
+
+import org.hibernate.Criteria;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.shards.ShardId;
+import org.hibernate.shards.ShardedConfiguration;
+import org.hibernate.shards.cfg.ConfigurationToShardConfigurationAdapter;
+import org.hibernate.shards.cfg.ShardConfiguration;
+import org.hibernate.shards.integration.IdGenType;
+import org.hibernate.shards.loadbalance.RoundRobinShardLoadBalancer;
+import org.hibernate.shards.strategy.ShardStrategy;
+import org.hibernate.shards.strategy.ShardStrategyFactory;
+import org.hibernate.shards.strategy.ShardStrategyImpl;
+import org.hibernate.shards.strategy.access.SequentialShardAccessStrategy;
+import org.hibernate.shards.strategy.access.ShardAccessStrategy;
+import org.hibernate.shards.strategy.resolution.AllShardsShardResolutionStrategy;
+import org.hibernate.shards.strategy.resolution.ShardResolutionStrategy;
+import org.hibernate.shards.strategy.selection.RoundRobinShardSelectionStrategy;
+import org.hibernate.shards.strategy.selection.ShardSelectionStrategy;
+import org.hibernate.shards.util.DatabaseUtils;
+
+import java.math.BigDecimal;
+import java.net.URL;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * This is the sample app we use in the documentation.
+ *
+ * @author maxr at google.com (Max Ross)
+ */
+public class WeatherReportApp {
+
+  private SessionFactory sessionFactory;
+
+  public static void main(String[] args) throws Exception {
+    WeatherReportApp app = new WeatherReportApp();
+    app.run();
+  }
+
+  private void run() throws SQLException {
+    createSchema();
+    sessionFactory = createSessionFactory();
+
+    addData();
+
+    Session session = sessionFactory.openSession();
+    try {
+      Criteria crit = session.createCriteria(WeatherReport.class);
+      List count = crit.list();
+      System.out.println(count.size());
+      crit.add(Restrictions.gt("temperature", 33));
+      List reports = crit.list();
+      System.out.println(reports.size());
+    } finally {
+      session.close();
+    }
+  }
+
+  private void addData() {
+    Session session = sessionFactory.openSession();
+    try {
+      session.beginTransaction();
+      WeatherReport report = new WeatherReport();
+      report.setContinent("North America");
+      report.setLatitude(new BigDecimal(25));
+      report.setLongitude(new BigDecimal(30));
+      report.setReportTime(new Date());
+      report.setTemperature(44);
+      session.save(report);
+
+      report = new WeatherReport();
+      report.setContinent("Africa");
+      report.setLatitude(new BigDecimal(44));
+      report.setLongitude(new BigDecimal(99));
+      report.setReportTime(new Date());
+      report.setTemperature(31);
+      session.save(report);
+
+      report = new WeatherReport();
+      report.setContinent("Asia");
+      report.setLatitude(new BigDecimal(13));
+      report.setLongitude(new BigDecimal(12));
+      report.setReportTime(new Date());
+      report.setTemperature(104);
+      session.save(report);
+      session.getTransaction().commit();
+    } finally {
+      session.close();
+    }
+  }
+
+  private void createSchema() throws SQLException {
+    for(int i = 0; i < 3; i++) {
+      DatabaseUtils.destroyDatabase(i, IdGenType.SIMPLE);
+      DatabaseUtils.createDatabase(i, IdGenType.SIMPLE);
+    }
+
+  }
+
+  public SessionFactory createSessionFactory() {
+    Configuration prototypeConfig = new Configuration()
+        .configure(getClass().getResource("hibernate0.cfg.xml"));
+    prototypeConfig.addURL(getClass().getResource("weather.hbm.xml"));
+    List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>();
+    shardConfigs.add(buildShardConfig(getClass().getResource("hibernate0.cfg.xml")));
+    shardConfigs.add(buildShardConfig(getClass().getResource("hibernate1.cfg.xml")));
+    shardConfigs.add(buildShardConfig(getClass().getResource("hibernate2.cfg.xml")));
+    ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
+    ShardedConfiguration shardedConfig = new ShardedConfiguration(
+        prototypeConfig,
+        shardConfigs,
+        shardStrategyFactory);
+    return shardedConfig.buildShardedSessionFactory();
+  }
+
+  ShardStrategyFactory buildShardStrategyFactory() {
+    return new ShardStrategyFactory() {
+      public ShardStrategy newShardStrategy(List<ShardId> shardIds) {
+        RoundRobinShardLoadBalancer loadBalancer
+            = new RoundRobinShardLoadBalancer(shardIds);
+        ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(
+            loadBalancer);
+        ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(
+            shardIds);
+        ShardAccessStrategy pas = new SequentialShardAccessStrategy();
+        return new ShardStrategyImpl(pss, prs, pas);
+      }
+    };
+  }
+
+  ShardConfiguration buildShardConfig(URL configFile) {
+    Configuration config = new Configuration().configure(configFile);
+    return new ConfigurationToShardConfigurationAdapter(config);
+  }
+
+
+}

Added: shards/trunk/src/test/org/hibernate/shards/example/hibernate0.cfg.xml
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/hibernate0.cfg.xml	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/hibernate0.cfg.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,16 @@
+<!DOCTYPE hibernate-configuration PUBLIC
+	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+  <session-factory name="HibernateSessionFactory0">
+    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
+    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
+    <property name="connection.url">jdbc:hsqldb:mem:shard0</property>
+    <property name="connection.username">sa</property>
+    <property name="connection.password"></property>
+    <property name="hibernate.connection.shard_id">0</property>
+    <property name="hibernate.shard.enable_cross_shard_relationship_checks">
+      true
+    </property>
+  </session-factory>
+</hibernate-configuration>

Added: shards/trunk/src/test/org/hibernate/shards/example/hibernate1.cfg.xml
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/hibernate1.cfg.xml	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/hibernate1.cfg.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,16 @@
+<!DOCTYPE hibernate-configuration PUBLIC
+	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+  <session-factory name="HibernateSessionFactory1">
+    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
+    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
+    <property name="connection.url">jdbc:hsqldb:mem:shard1</property>
+    <property name="connection.username">sa</property>
+    <property name="connection.password"></property>
+    <property name="hibernate.connection.shard_id">1</property>
+    <property name="hibernate.shard.enable_cross_shard_relationship_checks">
+      true
+    </property>
+  </session-factory>
+</hibernate-configuration>

Added: shards/trunk/src/test/org/hibernate/shards/example/hibernate2.cfg.xml
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/hibernate2.cfg.xml	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/hibernate2.cfg.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,16 @@
+<!DOCTYPE hibernate-configuration PUBLIC
+	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+  <session-factory name="HibernateSessionFactory2">
+    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
+    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
+    <property name="connection.url">jdbc:hsqldb:mem:shard2</property>
+    <property name="connection.username">sa</property>
+    <property name="connection.password"></property>
+    <property name="hibernate.connection.shard_id">2</property>
+    <property name="hibernate.shard.enable_cross_shard_relationship_checks">
+      true
+    </property>
+  </session-factory>
+</hibernate-configuration>

Added: shards/trunk/src/test/org/hibernate/shards/example/weather.hbm.xml
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/example/weather.hbm.xml	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/example/weather.hbm.xml	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,14 @@
+<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
+  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+<hibernate-mapping package="org.hibernate.shards.example">
+    <class name="WeatherReport" table="WEATHER_REPORT">
+        <id name="reportId" column="REPORT_ID" type="big_integer">
+          <generator class="org.hibernate.shards.id.ShardedUUIDGenerator"/>
+        </id>
+        <property name="continent" column="CONTINENT"/>
+        <property name="latitude" column="LATITUDE"/>
+        <property name="longitude" column="LONGITUDE"/>
+        <property name="temperature" column="TEMPERATURE"/>
+        <property name="reportTime" type="timestamp" column="REPORT_TIME"/>
+    </class>
+</hibernate-mapping>

Modified: shards/trunk/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java	2007-07-28 06:39:31 UTC (rev 12841)
+++ shards/trunk/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java	2007-07-28 21:28:06 UTC (rev 12842)
@@ -44,16 +44,13 @@
 import org.hibernate.shards.strategy.resolution.ShardResolutionStrategy;
 import org.hibernate.shards.strategy.selection.RoundRobinShardSelectionStrategy;
 import org.hibernate.shards.strategy.selection.ShardSelectionStrategy;
-import org.hibernate.shards.util.JdbcUtil;
+import org.hibernate.shards.util.DatabaseUtils;
 import org.hibernate.shards.util.Lists;
 import org.hibernate.shards.util.Maps;
 
 import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Executors;
@@ -101,8 +98,8 @@
   protected void setUp() throws Exception {
     super.setUp();
     for(int i = 0; i < getNumDatabases(); i++) {
-      destroyDatabase(i);
-      createDatabase(i);
+      DatabaseUtils.destroyDatabase(i, getIdGenType());
+      DatabaseUtils.createDatabase(i, getIdGenType());
     }
     Configuration prototypeConfig = buildPrototypeConfig();
     List<ShardConfiguration> configurations = buildConfigurations();
@@ -212,54 +209,7 @@
     super.tearDown();
   }
 
-  public Connection createConnection(int index) throws SQLException {
-    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
-    return
-        DriverManager.getConnection(
-            dbPlatform.getUrl(index),
-            dbPlatform.getUser(),
-            dbPlatform.getPassword());
-  }
-
-  private void destroyDatabase(int index) throws SQLException {
-    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
-    Connection conn = createConnection(index);
-    try {
-      for(String statement : dbPlatform.getDropTableStatements(getIdGenType())) {
-        try {
-          JdbcUtil.executeUpdate(conn, statement, false);
-        } catch (SQLException sqle) {
-          // not interested, keep moving
-        }
-      }
-    } finally {
-      conn.close();
-    }
-  }
-
-  private void createDatabase(int index) throws SQLException {
-    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
-    Connection conn = createConnection(index);
-    try {
-      for(String statement : dbPlatform.getCreateTableStatements(getIdGenType())) {
-        JdbcUtil.executeUpdate(conn, statement, false);
-      }
-      createDatabaseHook(conn);
-    } finally {
-      conn.close();
-    }
-  }
-
   /**
-   * Override if you want additional tables in your schema
-   * @param conn the connection
-   * @throws SQLException thrown if any of the operations performed with the
-   * connection throws the same
-   */
-  protected void createDatabaseHook(Connection conn) throws SQLException {
-  }
-
-  /**
    * Override if you want more than the default
    * @return the number of databases
    */

Modified: shards/trunk/src/test/org/hibernate/shards/integration/platform/hsql/HSQLDatabasePlatform.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/integration/platform/hsql/HSQLDatabasePlatform.java	2007-07-28 06:39:31 UTC (rev 12841)
+++ shards/trunk/src/test/org/hibernate/shards/integration/platform/hsql/HSQLDatabasePlatform.java	2007-07-28 21:28:06 UTC (rev 12842)
@@ -55,6 +55,7 @@
     ,"CREATE TABLE Escalator (escalatorId DECIMAL(40,0) PRIMARY KEY, bottomFloorId DECIMAL(40,0), topFloorId DECIMAL(40,0))"
     ,"CREATE TABLE Person (personId DECIMAL(40,0) PRIMARY KEY, name VARCHAR(50), tenantId DECIMAL(40,0), officeId DECIMAL(40,0))"
     ,"CREATE TABLE IdIsBaseType (idIsBaseTypeId DECIMAL(40,0) PRIMARY KEY, value VARCHAR(50))"
+    ,"CREATE TABLE WEATHER_REPORT (REPORT_ID DECIMAL(40,0) PRIMARY KEY, CONTINENT VARCHAR(50), LATITUDE DECIMAL(10,2), LONGITUDE DECIMAL(10,2), TEMPERATURE DECIMAL(10,0), REPORT_TIME TIMESTAMP)"
   );
 
   protected static final List<String> DROP_TABLE_STATEMENTS = Lists.newArrayList(

Added: shards/trunk/src/test/org/hibernate/shards/util/DatabaseUtils.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/util/DatabaseUtils.java	                        (rev 0)
+++ shards/trunk/src/test/org/hibernate/shards/util/DatabaseUtils.java	2007-07-28 21:28:06 UTC (rev 12842)
@@ -0,0 +1,71 @@
+/**
+ * Copyright (C) 2007 Google Inc.
+ *
+ * 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 org.hibernate.shards.util;
+
+import org.hibernate.shards.integration.IdGenType;
+import org.hibernate.shards.integration.platform.DatabasePlatform;
+import org.hibernate.shards.integration.platform.DatabasePlatformFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+/**
+ * @author maxr at google.com (Max Ross)
+ */
+public class DatabaseUtils {
+
+  public static Connection createConnection(int index) throws SQLException {
+    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
+    return
+        DriverManager.getConnection(
+            dbPlatform.getUrl(index),
+            dbPlatform.getUser(),
+            dbPlatform.getPassword());
+  }
+
+  public static void destroyDatabase(int index, IdGenType idGenType) throws SQLException {
+    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
+    Connection conn = createConnection(index);
+    try {
+      for(String statement : dbPlatform.getDropTableStatements(idGenType)) {
+        try {
+          JdbcUtil.executeUpdate(conn, statement, false);
+        } catch (SQLException sqle) {
+          // not interested, keep moving
+        }
+      }
+    } finally {
+      conn.close();
+    }
+  }
+
+  public static void createDatabase(int index, IdGenType idGenType) throws SQLException {
+    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
+    Connection conn = createConnection(index);
+    try {
+      for(String statement : dbPlatform.getCreateTableStatements(idGenType)) {
+        JdbcUtil.executeUpdate(conn, statement, false);
+      }
+    } finally {
+      conn.close();
+    }
+  }
+
+
+}




More information about the hibernate-commits mailing list