Open Source Repository

Home /hibernate/hibernate-3.2.6.ga | Repository Home



org/hibernate/tool/hbm2ddl/DatabaseMetadata.java
//$Id: DatabaseMetadata.java 14032 2007-09-28 03:18:36Z d.plentz $
package org.hibernate.tool.hbm2ddl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.exception.SQLExceptionConverter;
import org.hibernate.mapping.Table;
import org.hibernate.dialect.Dialect;
import org.hibernate.util.StringHelper;

/**
 * JDBC database metadata
 @author Christoph Sturm, Teodor Danciu
 */
public class DatabaseMetadata {
  
  private static final Log log = LogFactory.getLog(DatabaseMetadata.class);
  
  private final Map tables = new HashMap();
  private final Set sequences = new HashSet();
  private final boolean extras;

  private DatabaseMetaData meta;
  private SQLExceptionConverter sqlExceptionConverter;

  public DatabaseMetadata(Connection connection, Dialect dialectthrows SQLException {
    this(connection, dialect, true);
  }

  public DatabaseMetadata(Connection connection, Dialect dialect, boolean extrasthrows SQLException {
    sqlExceptionConverter = dialect.buildSQLExceptionConverter();
    meta = connection.getMetaData();
    this.extras = extras;
    initSequences(connection, dialect);
  }

  private static final String[] TYPES = {"TABLE""VIEW"};

  public TableMetadata getTableMetadata(String name, String schema, String catalog, boolean isQuotedthrows HibernateException {

    Object identifier = identifier(catalog, schema, name);
    TableMetadata table = (TableMetadatatables.get(identifier);
    if (table!=null) {
      return table;
    }
    else {
      
      try {
        ResultSet rs = null;
        try {
          if ( (isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
            rs = meta.getTables(catalog, schema, name, TYPES);
          else if ( (isQuoted && meta.storesUpperCaseQuotedIdentifiers()) 
            || (!isQuoted && meta.storesUpperCaseIdentifiers() )) {
            rs = meta.getTables
                StringHelper.toUpperCase(catalog)
                StringHelper.toUpperCase(schema)
                StringHelper.toUpperCase(name)
                TYPES 
              );
          }
          else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
              || (!isQuoted && meta.storesLowerCaseIdentifiers() )) {
            rs = meta.getTables
                StringHelper.toLowerCase(catalog)
                StringHelper.toLowerCase(schema)
                StringHelper.toLowerCase(name)
                TYPES 
              );
          }
          else {
            rs = meta.getTables(catalog, schema, name, TYPES);
          }
          
          while rs.next() ) {
            String tableName = rs.getString("TABLE_NAME");
            if name.equalsIgnoreCase(tableName) ) {
              table = new TableMetadata(rs, meta, extras);
              tables.put(identifier, table);
              return table;
            }
          }
          
          log.info("table not found: " + name);
          return null;

        }
        finally {
          if (rs!=nullrs.close();
        }
      }
      catch (SQLException sqle) {
        throw JDBCExceptionHelper.convert(
                        sqlExceptionConverter,
                sqle,
                "could not get table metadata: " + name
          );
      }
    }

  }

  private Object identifier(String catalog, String schema, String name) {
    return Table.qualify(catalog,schema,name);
  }

  private void initSequences(Connection connection, Dialect dialectthrows SQLException {
    if dialect.supportsSequences() ) {
      String sql = dialect.getQuerySequencesString();
      if (sql!=null) {
  
        Statement statement = null;
        ResultSet rs = null;
        try {
          statement = connection.createStatement();
          rs = statement.executeQuery(sql);
    
          while rs.next() ) {
            sequences.addrs.getString(1).toLowerCase().trim() );
          }
        }
        finally {
          if (rs!=nullrs.close();
          if (statement!=nullstatement.close();
        }
        
      }
    }
  }

  public boolean isSequence(Object key) {
    if (key instanceof String){
      String[] strings = StringHelper.split("."(Stringkey);
      return sequences.containsstrings[strings.length-1].toLowerCase());
    }
    return false;
  }

   public boolean isTable(Object keythrows HibernateException {
     if(key instanceof String) {
      Table tbl = new Table((String)key);
      if getTableMetadatatbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null ) {
         return true;
       else {
         String[] strings = StringHelper.split("."(Stringkey);
         if(strings.length==3) {
          tbl = new Table(strings[2]);
          tbl.setCatalog(strings[0]);
          tbl.setSchema(strings[1]);
          return getTableMetadatatbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null;
         else if (strings.length==2) {
          tbl = new Table(strings[1]);
          tbl.setSchema(strings[0]);
          return getTableMetadatatbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null;
         }
       }
     }
     return false;
   }
   
  public String toString() {
    return "DatabaseMetadata" + tables.keySet().toString() + sequences.toString();
  }
}