From 7cf2c32a5b868d727d349f1d15a9f7b67ecf925d Mon Sep 17 00:00:00 2001 From: xulei Date: Fri, 19 Jan 2018 11:30:18 +0800 Subject: [PATCH] Add hibernate dialect support. --- tools/hibernate-dialect/.gitignore | 9 + tools/hibernate-dialect/Makefile | 33 ++ tools/hibernate-dialect/README.txt | 20 ++ tools/hibernate-dialect/pom.xml | 55 ++++ .../hibernate/dialect/TrafodionDialect.java | 285 ++++++++++++++++++ 5 files changed, 402 insertions(+) create mode 100644 tools/hibernate-dialect/.gitignore create mode 100644 tools/hibernate-dialect/Makefile create mode 100644 tools/hibernate-dialect/README.txt create mode 100644 tools/hibernate-dialect/pom.xml create mode 100644 tools/hibernate-dialect/src/main/java/org/trafodion/hibernate/dialect/TrafodionDialect.java diff --git a/tools/hibernate-dialect/.gitignore b/tools/hibernate-dialect/.gitignore new file mode 100644 index 0000000000..3f3319fa32 --- /dev/null +++ b/tools/hibernate-dialect/.gitignore @@ -0,0 +1,9 @@ +.classpath +.project +.settings +mvn.out +target/* +/target/ +*.log +/LICENSE +/NOTICE diff --git a/tools/hibernate-dialect/Makefile b/tools/hibernate-dialect/Makefile new file mode 100644 index 0000000000..006c848ff1 --- /dev/null +++ b/tools/hibernate-dialect/Makefile @@ -0,0 +1,33 @@ +# @@@ START COPYRIGHT @@@ +# # +# # Licensed to the Apache Software Foundation (ASF) under one +# # or more contributor license agreements. See the NOTICE file +# # distributed with this work for additional information +# # regarding copyright ownership. The ASF licenses this file +# # to you under the Apache License, Version 2.0 (the +# # "License"); you may not use this file except in compliance +# # with the License. You may obtain a copy of the License at +# # +# # http://www.apache.org/licenses/LICENSE-2.0 +# # +# # Unless required by applicable law or agreed to in writing, +# # software distributed under the License is distributed on an +# # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# # KIND, either express or implied. See the License for the +# # specific language governing permissions and limitations +# # under the License. +# # +# # @@@ END COPYRIGHT @@@ + + +.NOTPARALLEL: all + +all: build_all + +build_all: + echo "$(MAVEN) package -DskipTests" + set -o pipefail && $(MAVEN) package -DskipTests + +clean: + $(MAVEN) clean + $(RM) -r target/* diff --git a/tools/hibernate-dialect/README.txt b/tools/hibernate-dialect/README.txt new file mode 100644 index 0000000000..71575245d8 --- /dev/null +++ b/tools/hibernate-dialect/README.txt @@ -0,0 +1,20 @@ +Trafodion hibernate dialect +=============================== +This is a tool for hibernate to use trafodion. +This tool now support hibernate version 4.x . +To use this tool, you should make all ,then load the jar file to your project,and add following config to you hibernate.cfg.xml file: + +org.hibernate.dialect.TrafodionDialect + +ABOUT HIBERNATE: + +Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC). + +benefit: + +Hibernate is one of the most common ORM frameworks and has a very high rate of use in the Java Web project, supporting it to improve the usability of trafodion in OLTP. + +To build: +>cd +> make all + diff --git a/tools/hibernate-dialect/pom.xml b/tools/hibernate-dialect/pom.xml new file mode 100644 index 0000000000..51b4e85bf1 --- /dev/null +++ b/tools/hibernate-dialect/pom.xml @@ -0,0 +1,55 @@ + + + + + +4.0.0 + + org.trafodion + hibernate-dialect + ${env.TRAFODION_VER} + jar + + Hibernate dialect tool jar + http://wiki.trafodion.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + org.hibernate + hibernate-core + 4.3.10.Final + + + diff --git a/tools/hibernate-dialect/src/main/java/org/trafodion/hibernate/dialect/TrafodionDialect.java b/tools/hibernate-dialect/src/main/java/org/trafodion/hibernate/dialect/TrafodionDialect.java new file mode 100644 index 0000000000..886b644ac6 --- /dev/null +++ b/tools/hibernate-dialect/src/main/java/org/trafodion/hibernate/dialect/TrafodionDialect.java @@ -0,0 +1,285 @@ +// @@@ START COPYRIGHT @@@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// +// @@@ END COPYRIGHT @@@ +package org.trafodion.hibernate.dialect; + +import java.sql.Types; + +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.Dialect; +import org.hibernate.dialect.function.NoArgSQLFunction; +import org.hibernate.dialect.function.NvlFunction; +import org.hibernate.dialect.function.SQLFunctionTemplate; +import org.hibernate.dialect.function.StandardSQLFunction; +import org.hibernate.dialect.function.VarArgsSQLFunction; +import org.hibernate.type.StandardBasicTypes; +import org.hibernate.type.descriptor.sql.BitTypeDescriptor; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; + + +public class TrafodionDialect extends Dialect { + + private static final int PARAM_LIST_SIZE_LIMIT = 1000; + + public TrafodionDialect() { + super(); + registerCharacterTypeMappings(); + registerNumericTypeMappings(); + registerDateTimeTypeMappings(); + registerLargeObjectTypeMappings(); + registerReverseHibernateTypeMappings(); + registerFunctions(); + registerDefaultProperties(); + } + + protected void registerCharacterTypeMappings() { + registerColumnType( Types.CHAR, "char(1)" ); + registerColumnType( Types.VARCHAR, "varchar($l)" ); + } + + protected void registerNumericTypeMappings() { + registerColumnType( Types.BIT, "numeric(1,0)" ); + registerColumnType( Types.TINYINT, "numeric(1,0)" ); + registerColumnType( Types.SMALLINT, "smallint" ); + registerColumnType( Types.BIGINT, "largeint" ); + registerColumnType( Types.INTEGER, "integer" ); + + registerColumnType( Types.FLOAT, "real" ); + registerColumnType( Types.DOUBLE, "double precision" ); + registerColumnType( Types.NUMERIC, "numeric($p,$s)" ); + registerColumnType( Types.DECIMAL, "decimal($p,$s)" ); + + registerColumnType( Types.BOOLEAN, "numeric(1,0)" ); + } + + protected void registerDateTimeTypeMappings() { + registerColumnType( Types.DATE, "date" ); + registerColumnType( Types.TIME, "time" ); + registerColumnType( Types.TIMESTAMP, "timestamp" ); + } + + protected void registerLargeObjectTypeMappings() { + registerColumnType( Types.BINARY, "blob" ); + registerColumnType( Types.VARBINARY, "blob" ); + + registerColumnType( Types.BLOB, "blob" ); + registerColumnType( Types.CLOB, "clob" ); + + registerColumnType( Types.LONGVARCHAR, "clob" ); + registerColumnType( Types.LONGVARBINARY, "blob" ); + } + + protected void registerReverseHibernateTypeMappings() { + } + + protected void registerFunctions() { + registerFunction( "abs", new StandardSQLFunction("abs") ); + registerFunction( "sign", new StandardSQLFunction("sign", StandardBasicTypes.INTEGER) ); + + registerFunction( "acos", new StandardSQLFunction("acos", StandardBasicTypes.DOUBLE) ); + registerFunction( "asin", new StandardSQLFunction("asin", StandardBasicTypes.DOUBLE) ); + registerFunction( "atan", new StandardSQLFunction("atan", StandardBasicTypes.DOUBLE) ); + registerFunction( "bitand", new StandardSQLFunction("bitand") ); + registerFunction( "cos", new StandardSQLFunction("cos", StandardBasicTypes.DOUBLE) ); + registerFunction( "cosh", new StandardSQLFunction("cosh", StandardBasicTypes.DOUBLE) ); + registerFunction( "exp", new StandardSQLFunction("exp", StandardBasicTypes.DOUBLE) ); + registerFunction( "log", new StandardSQLFunction("log", StandardBasicTypes.DOUBLE) ); + registerFunction( "sin", new StandardSQLFunction("sin", StandardBasicTypes.DOUBLE) ); + registerFunction( "sinh", new StandardSQLFunction("sinh", StandardBasicTypes.DOUBLE) ); + registerFunction( "stddev", new StandardSQLFunction("stddev", StandardBasicTypes.DOUBLE) ); + registerFunction( "sqrt", new StandardSQLFunction("sqrt", StandardBasicTypes.DOUBLE) ); + registerFunction( "tan", new StandardSQLFunction("tan", StandardBasicTypes.DOUBLE) ); + registerFunction( "tanh", new StandardSQLFunction("tanh", StandardBasicTypes.DOUBLE) ); + registerFunction( "variance", new StandardSQLFunction("variance", StandardBasicTypes.DOUBLE) ); + + registerFunction( "round", new StandardSQLFunction("round") ); + registerFunction( "trunc", new StandardSQLFunction("trunc") ); + registerFunction( "ceiling", new StandardSQLFunction("ceiling") ); + registerFunction( "floor", new StandardSQLFunction("floor") ); + + registerFunction( "char", new StandardSQLFunction("char", StandardBasicTypes.CHARACTER) ); + registerFunction( "lower", new StandardSQLFunction("lower") ); + registerFunction( "ltrim", new StandardSQLFunction("ltrim") ); + registerFunction( "rtrim", new StandardSQLFunction("rtrim") ); + registerFunction( "upper", new StandardSQLFunction("upper") ); + registerFunction( "ascii", new StandardSQLFunction("ascii", StandardBasicTypes.INTEGER) ); + + registerFunction( "current_date", new NoArgSQLFunction("current_date", StandardBasicTypes.DATE, false) ); + registerFunction( "current_time", new NoArgSQLFunction("current_time", StandardBasicTypes.TIME, false) ); + registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", StandardBasicTypes.TIMESTAMP, false) ); + + registerFunction( "user", new NoArgSQLFunction("user", StandardBasicTypes.STRING, false) ); + + // Multi-param string dialect functions... + registerFunction( "concat", new VarArgsSQLFunction(StandardBasicTypes.STRING, "", "||", "") ); + registerFunction( "instr", new StandardSQLFunction("instr", StandardBasicTypes.INTEGER) ); + registerFunction( "lpad", new StandardSQLFunction("lpad", StandardBasicTypes.STRING) ); + registerFunction( "replace", new StandardSQLFunction("replace", StandardBasicTypes.STRING) ); + registerFunction( "rpad", new StandardSQLFunction("rpad", StandardBasicTypes.STRING) ); + registerFunction( "substr", new StandardSQLFunction("substr", StandardBasicTypes.STRING) ); + registerFunction( "translate", new StandardSQLFunction("translate", StandardBasicTypes.STRING) ); + + registerFunction( "substring", new StandardSQLFunction( "substr", StandardBasicTypes.STRING ) ); + registerFunction( "locate", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "instr(?2,?1)" ) ); + registerFunction( "coalesce", new NvlFunction() ); + + // Multi-param numeric dialect functions... + registerFunction( "atan2", new StandardSQLFunction("atan2", StandardBasicTypes.FLOAT) ); + registerFunction( "mod", new StandardSQLFunction("mod", StandardBasicTypes.INTEGER) ); + registerFunction( "nvl", new StandardSQLFunction("nvl") ); + registerFunction( "power", new StandardSQLFunction("power", StandardBasicTypes.FLOAT) ); + + // Multi-param date dialect functions... + registerFunction( "add_months", new StandardSQLFunction("add_months", StandardBasicTypes.DATE) ); + } + + protected void registerDefaultProperties() { + getDefaultProperties().setProperty( Environment.USE_STREAMS_FOR_BINARY, "true" ); + getDefaultProperties().setProperty( Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE ); + getDefaultProperties().setProperty( Environment.USE_GET_GENERATED_KEYS, "true" ); + } + + @Override + protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) { + return sqlCode == Types.BOOLEAN ? BitTypeDescriptor.INSTANCE : super.getSqlTypeDescriptorOverride( sqlCode ); + } + + @Override + public String getCrossJoinSeparator() { + return " cross join "; + } + +/* @Override + public LimitHandler getLimitHandler() { + return LIMIT_HANDLER; + } +*/ + @Override + public String getLimitString(String sql, boolean hasOffset) { + sql = sql.trim(); + return sql + (hasOffset ? " limit ? " : " limit ?"); + } + + @Override + public String getCurrentTimestampSelectString() { + return "select current_timestamp from (values(1)) x"; + } + + @Override + public String getCurrentTimestampSQLFunctionName() { + return "current_timestamp"; + } + + + @Override + public String getAddColumnString() { + return "add "; + } + + @Override + public String getSequenceNextValString(String sequenceName) { + return "select seqnum(" + sequenceName + ") from (values(1)) x"; + } + + @Override + public String getCreateSequenceString(String sequenceName) { + return "create sequence " + sequenceName; + } + + @Override + public String getDropSequenceString(String sequenceName) { + return "drop sequence " + sequenceName; + } + + @Override + public boolean dropConstraints() { + return false; + } + + @Override + public boolean supportsSequences() { + return true; + } + + @Override + public boolean supportsPooledSequences() { + return true; + } + + @Override + public boolean supportsLimit() { + return true; + } + + @Override + public String getQuerySequencesString() { + return " get all sequences"; + } + + @Override + public boolean supportsUnionAll() { + return true; + } + + @Override + public boolean supportsCommentOn() { + return true; + } + + @Override + public boolean supportsCurrentTimestampSelection() { + return true; + } + + @Override + public boolean isCurrentTimestampSelectStringCallable() { + return false; + } + + @Override + public boolean supportsEmptyInList() { + return false; + } + + @Override + public boolean supportsExistsInSelect() { + return false; + } + + @Override + public int getInExpressionCountLimit() { + return PARAM_LIST_SIZE_LIMIT; + } + + @Override + public boolean forceLobAsLastValue() { + return false; + } + + @Override + public boolean useFollowOnLocking() { + return false; + } + + @Override + public String getNotExpression( String expression ) { + return "not (" + expression + ")"; + } +}