Example usage for org.hibernate.dialect Dialect getTypeName

List of usage examples for org.hibernate.dialect Dialect getTypeName

Introduction

In this page you can find the example usage for org.hibernate.dialect Dialect getTypeName.

Prototype

public String getTypeName(int code) throws HibernateException 

Source Link

Document

Get the name of the database type associated with the given Types typecode.

Usage

From source file:com.evolveum.midpoint.repo.sql.SqlAuditServiceImpl.java

License:Apache License

/**
 * This method creates temporary table for cleanup audit method.
 *
 * @param session/*from   www .j  a  v  a2 s  . c o  m*/
 * @param dialect
 * @param tempTable
 */
private void createTemporaryTable(Session session, final Dialect dialect, final String tempTable) {
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            //check if table exists
            try {
                Statement s = connection.createStatement();
                s.execute("select id from " + tempTable + " where id = 1");
                //table already exists
                return;
            } catch (Exception ex) {
                //we expect this on the first time
            }

            StringBuilder sb = new StringBuilder();
            sb.append(dialect.getCreateTemporaryTableString());
            sb.append(' ').append(tempTable).append(" (id ");
            sb.append(dialect.getTypeName(Types.BIGINT));
            sb.append(" not null)");
            sb.append(dialect.getCreateTemporaryTablePostfix());

            Statement s = connection.createStatement();
            s.execute(sb.toString());
        }
    });
}

From source file:com.redprairie.moca.server.db.TU_JDBCAdapter.java

License:Open Source License

private void createTableAndInsert(DBType dbType, Connection connection, long insertCount) throws SQLException {
    DialectFactoryImpl dialectFactory = new DialectFactoryImpl();
    dialectFactory.setClassLoaderService(new ClassLoaderServiceImpl());

    Dialect dialect;
    switch (dbType) {
    case MSSQL:/*from w ww.j  a v  a 2 s . co  m*/
        dialect = dialectFactory.buildDialect(
                Collections.singletonMap(AvailableSettings.DIALECT, UnicodeSQLServerDialect.class.getName()),
                null);
        break;
    case ORACLE:
        dialect = dialectFactory.buildDialect(
                Collections.singletonMap(AvailableSettings.DIALECT, Oracle10gDialect.class.getName()), null);
        break;
    case H2:
        dialect = dialectFactory.buildDialect(
                Collections.singletonMap(AvailableSettings.DIALECT, H2Dialect.class.getName()), null);
        break;
    default:
        throw new RuntimeException("There was no database supplied!");
    }
    StringBuilder tableCreate = new StringBuilder().append(dialect.getCreateTableString()).append(' ')
            .append(TABLENAME).append(" ( name ").append(dialect.getTypeName(Types.INTEGER)).append(" )");

    Statement stmt = connection.createStatement();
    try {
        stmt.execute(tableCreate.toString());
    } finally {
        stmt.close();
    }

    PreparedStatement pstmt = connection.prepareStatement("INSERT INTO QueryLimitTable (name) VALUES(?)");

    try {
        for (long i = 0; i < insertCount; i++) {
            pstmt.setLong(1, i);
            pstmt.addBatch();
        }

        pstmt.executeBatch();
    } finally {
        pstmt.close();
    }
}

From source file:net.lshift.hibernate.migrations.InsertBuilder.java

License:Apache License

/**
 * A very crude differentiation for dialects that support SQL boolean types as opposed to mapping them as 1s or 0s.
 *//*from w  ww .j a v  a  2s .  c  o  m*/
private Object convertValueType(Dialect dialect, Object value) {
    if (value.equals("0") || value.equals("1")) {
        String type = dialect.getTypeName(Types.BIT);
        if (type.contains("bit")) {
            return !value.equals("0");
        }
    }
    return value;
}

From source file:org.agnitas.dao.impl.ProfileFieldDaoImpl.java

License:Open Source License

public boolean addProfileField(int companyID, String fieldname, String fieldType, int length,
        String fieldDefault, boolean notNull) throws Exception {
    JdbcTemplate jdbc = new JdbcTemplate((DataSource) applicationContext.getBean("dataSource"));
    String name = AgnUtils.getDefaultValue("jdbc.dialect");
    Dialect dia = null;
    int jsqlType = -1;
    String dbType = null;/*from   w w w.  jav a 2  s .co m*/
    String defaultSQL = "";
    String sql = "";

    if (fieldDefault != null && fieldDefault.compareTo("") != 0) {
        if (fieldType.compareTo("VARCHAR") == 0) {
            defaultSQL = " DEFAULT '" + fieldDefault + "'";
        } else {
            defaultSQL = " DEFAULT " + fieldDefault;
        }
    }
    Class cl = null;

    cl = Class.forName("java.sql.Types");
    jsqlType = cl.getDeclaredField(fieldType).getInt(null);
    cl = Class.forName(name);
    dia = (Dialect) cl.getConstructor(new Class[0]).newInstance(new Object[0]);
    dbType = dia.getTypeName(jsqlType);
    /* Bugfix for oracle
     * Oracle dialect returns long for varchar
     */
    if (fieldType.equals("VARCHAR")) {
        dbType = "VARCHAR";
    }
    /* Bugfix for mysql.
     * The jdbc-Driver for mysql maps VARCHAR to longtext.
     * This might be ok in most cases, but longtext doesn't support
     * length restrictions. So the correct tpye for mysql should be
     * varchar.
     */
    if (fieldType.equals("VARCHAR") && dbType.equals("longtext") && length > 0) {
        dbType = "varchar";
    }

    sql = "ALTER TABLE customer_" + companyID + "_tbl ADD (";
    sql += fieldname.toLowerCase() + " " + dbType;
    if (fieldType.compareTo("VARCHAR") == 0) {
        if (length <= 0) {
            length = 100;
        }
        sql += "(" + length + ")";
    }
    sql += defaultSQL;

    if (notNull) {
        sql += " NOT NULL";
    }
    sql += ")";
    try {
        jdbc.execute(sql);
    } catch (Exception e) {
        AgnUtils.sendExceptionMail("sql:" + sql, e);
        AgnUtils.logger().error("SQL: " + sql);
        AgnUtils.logger().error("Exception: " + e);
        AgnUtils.logger().error(AgnUtils.getStackTrace(e));
        return false;
    }
    return true;
}

From source file:org.jboss.ejb3.entity.JTATableIdGenerator.java

License:Open Source License

public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
    return new String[] {
            "create table " + tableName + " ( " + columnName + " " + dialect.getTypeName(Types.BIGINT) + " )",
            "insert into " + tableName + " values ( 0 )" };
}