All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venky.swf.db.H2Helper Maven / Gradle / Ivy

The newest version!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.venky.swf.db;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;
import java.util.TimeZone;

import com.venky.core.date.DateUtils;
import com.venky.core.util.Bucket;
import com.venky.swf.db.model.Count;
import com.venky.swf.db.model.Model;
import com.venky.swf.db.table.Table;
import com.venky.swf.exceptions.SWFTimeoutException;
import com.venky.swf.sql.DDL;
import com.venky.swf.sql.DDL.AlterTable;
import com.venky.swf.sql.DataManupulationStatement;
import com.venky.swf.sql.Select;

/**
 *
 * @author venky
 */
public class H2Helper extends JdbcTypeHelper{

	@Override
	public boolean isSavepointManagedByJdbc(){
    	return true;
    }
	public boolean isAutoCommitOnDDL(){
		return true;
	}

	@Override
    public String getAutoIncrementInstruction() {
            return (" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH "+ getPrimaryKeyOffset() +")");
    }
	
    @Override
    public String getCurrentTimeStampKW(){
    	return "CURRENT_TIMESTAMP";
    }
    @Override
    public String getCurrentDateKW(){
    	return "CURRENT_DATE";
    }

    public String getForUpdateLiteral(){
    	return " FOR UPDATE ";
    }
    public boolean isQueryTimeoutSupported(){ 
		return true;
	}
    public boolean hasTransactionRolledBack(Throwable ex){
    	if (super.hasTransactionRolledBack(ex) || ex instanceof SWFTimeoutException){
    		return true;
    	}
    	return false;
    }
    public boolean isQueryTimeoutException(SQLException ex){
    	if (super.isQueryTimeoutException(ex) || ex.getMessage().contains("Statement was canceled or the session timed out") || ex.getErrorCode() == 50200 || ex.getErrorCode() == 57014 || ex.getErrorCode() == 90039){
    		return true;
    	}
		return false;
	}
    
    public boolean isNoWaitSupported(){
		return false;
	}
	public String getNoWaitLiteral(){
		return "";
	}
    
    
    @Override
	public String getDefaultKW(TypeRef ref, Object value) {
    	if (value instanceof Boolean){
    		if ((Boolean)value){
    			return "1";
    		}else {
    			return "0";
    		}
    	}
    	return super.getDefaultKW(ref, value);
	}
    

    protected H2Helper() {
            /**
             * Specify size and scale for a data type only if the database accepts
             * them during table creation
             */
            registerjdbcSQLType(Boolean.class, new TypeRef(
                            java.sql.Types.BOOLEAN, "BOOLEAN", 0, 0, false,false,
                            new BooleanConverter()));
            registerjdbcSQLType(boolean.class, new TypeRef(
                            java.sql.Types.BOOLEAN, "BOOLEAN", 0, 0, false,false,
                            new BooleanConverter()));

            registerjdbcSQLType(Byte.class, new TypeRef(java.sql.Types.TINYINT,
                            "TINYINT", 0, 0, false,false,new ByteConverter()));

            registerjdbcSQLType(byte.class, new TypeRef(java.sql.Types.TINYINT,
                            "TINYINT", 0, 0, false,false,new ByteConverter()));

            registerjdbcSQLType(Short.class,
                            new TypeRef(java.sql.Types.SMALLINT, "SMALLINT", 0, 0,false,false,
                                            new ShortConverter()));
            registerjdbcSQLType(short.class,
                            new TypeRef(java.sql.Types.SMALLINT, "SMALLINT", 0, 0,false,false,
                                            new ShortConverter()));

            registerjdbcSQLType(Integer.class,
                            new TypeRef(java.sql.Types.INTEGER, "INTEGER", 0, 0,false,false,
                                            new IntegerConverter()));
            registerjdbcSQLType(int.class, new TypeRef(java.sql.Types.INTEGER,
                            "INTEGER", 0, 0,false,false, new IntegerConverter()));

            registerjdbcSQLType(Long.class, new TypeRef(java.sql.Types.BIGINT,
                            "BIGINT", 0, 0, false,false,new LongConverter()));
            registerjdbcSQLType(long.class, new TypeRef(java.sql.Types.BIGINT,
                            "BIGINT", 0, 0, false,false, new LongConverter()));

            registerjdbcSQLType(BigDecimal.class, new TypeRef(
                            java.sql.Types.NUMERIC, "NUMERIC", 15, 10, false,false,
                            new BigDecimalConverter()));// also NUMERIC
            registerjdbcSQLType(BigDecimal.class, new TypeRef(
                    java.sql.Types.DECIMAL, "DECIMAL", 15, 10, false,false,
                    new BigDecimalConverter()));// also NUMERIC


            registerjdbcSQLType(Float.class, new TypeRef(java.sql.Types.REAL,
                            "REAL", 0, 0, false,false,new FloatConverter()));
            registerjdbcSQLType(float.class, new TypeRef(java.sql.Types.REAL,
                            "REAL", 0, 0, false,false,new FloatConverter()));

            registerjdbcSQLType(Double.class, new TypeRef(
                            java.sql.Types.REAL, "REAL", 0, 0,false,false, new DoubleConverter())); // ALSO
            registerjdbcSQLType(double.class, new TypeRef(
                            java.sql.Types.REAL, "REAL", 0, 0, false,false,new DoubleConverter())); // ALSO

            registerjdbcSQLType(Bucket.class, new TypeRef(
            				java.sql.Types.REAL, "REAL", 0, 0, false,false,new BucketConverter())); // ALSO
                                                                                                                                                            // FLOAT

            registerjdbcSQLType(Date.class, new TypeRef(java.sql.Types.DATE,
                            "DATE", 0, 0, true,false,new DateConverter(DateUtils.ISO_DATE_FORMAT_STR,TimeZone.getDefault())));

            registerjdbcSQLType(Time.class, new TypeRef




© 2015 - 2025 Weber Informatics LLC | Privacy Policy