Changeset 4099


Ignore:
Timestamp:
Jan 23, 2008, 4:37:23 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

References #888: Make dynamic table creation code more stable

Now works for MySQL. Need to investigate if it is possible to solve for Postgres as well.

Location:
trunk/src/core/net/sf/basedb/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r4034 r4099  
    575575      tx = HibernateUtil.newTransaction(session);
    576576      Connection connection = HibernateUtil.getConnection(session);
    577 
    578       if (!tableExists(table.getTableName(db), connection))
     577      String tableName = table.getTableName(db);
     578
     579      if (!tableExists(tableName, connection))
    579580      {
    580581        Table hibernateTable = newDynamicMapping(db, table);
    581582        Dialect dialect = getDialect();
    582         String dynamicCatalog = quote(Application.getDynamicCatalog());
    583         String dynamicSchema = quote(Application.getDynamicSchema());
    584 
    585         String sql = hibernateTable.sqlCreateString(dialect, null, dynamicCatalog, dynamicSchema);
     583        String dynamicCatalog = Application.getDynamicCatalog();
     584        String quotedCatalog = quote(dynamicCatalog);
     585        String dynamicSchema = Application.getDynamicSchema();
     586        String quotedSchema = quote(dynamicSchema);
     587
     588        String sql = hibernateTable.sqlCreateString(dialect, null, quotedCatalog, quotedSchema);
     589        sql = dbEngine.makeSafeCreateTable(sql, dynamicCatalog, dynamicSchema, tableName);
     590        System.out.println(sql);
    586591        logSql.debug(sql);
    587592       
     
    593598        {
    594599          Index ii = (Index)i.next();
    595           sql = ii.sqlCreateString(dialect, null, dynamicCatalog, dynamicSchema);
     600          sql = ii.sqlCreateString(dialect, null, quotedCatalog, quotedSchema);
    596601          logSql.debug(sql);
    597602          s.executeUpdate(sql);
  • trunk/src/core/net/sf/basedb/core/dbengine/AbstractDbEngine.java

    r3675 r4099  
    5353    -------------------------------------------
    5454  */
     55  /**
     56    Return the SQL unmodified.
     57    @since 2.6
     58  */
     59  public String makeSafeCreateTable(String sql, String catalog, String schema, String table)
     60  {
     61    return sql;
     62  }
    5563  /**
    5664    Most databases doesn't. Any more than Oracle?
  • trunk/src/core/net/sf/basedb/core/dbengine/DbEngine.java

    r3675 r4099  
    135135 
    136136  /**
     137    Create a "safe" CREATE TABLE query string that will not fail if the table already
     138    exists. Implementors should inject proper SQL code in a way that is supported in the
     139    underlying database. The MySQL engine, for example, replaces CREATE TABLE with
     140    CREATE TABLE IF NOT EXISTS. If the underlying database doesn't support this kind
     141    of check the original SQL should be returned umodified.
     142   
     143    @param sql The original SQL
     144    @param catalog
     145    @param catalog The name of the catalog (database) where the table is
     146      being create, or null if it is located in the current catalog
     147    @param schema The name of the schema where the table is being created, or
     148      null if is located in the current schema
     149    @param table The name of the table that is being created
     150    @return The modified or unmodified SQL statement
     151    @since 2.6
     152  */
     153  public String makeSafeCreateTable(String sql, String catalog, String schema, String table);
     154 
     155  /**
    137156    If the database does case sensitive or case insensitive string
    138157    comparison in expressions.
  • trunk/src/core/net/sf/basedb/core/dbengine/MySQLEngine.java

    r3675 r4099  
    2525
    2626import java.util.Set;
     27import java.util.regex.Matcher;
     28import java.util.regex.Pattern;
    2729
    2830/**
     
    123125
    124126  /**
     127    Replace CREATE TABLE with CREATE TABLE IF NOT EXISTS...
     128    @since 2.6
     129  */
     130  public String makeSafeCreateTable(String sql, String catalog, String schema, String table)
     131  {
     132    Pattern p = Pattern.compile("CREATE TABLE", Pattern.CASE_INSENSITIVE);
     133    Matcher m = p.matcher(sql);
     134    sql = m.replaceFirst("CREATE TABLE IF NOT EXISTS");
     135    return sql;
     136  }
     137 
     138  /**
    125139    Returns TRUE.
    126140    @since 2.4
Note: See TracChangeset for help on using the changeset viewer.