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

com.jk.data.datasource.JKDatabase Maven / Gradle / Ivy

Go to download

This contains a set of API's that ease the database programming with Java, in both: JDBC and JPA Persisitnce).

There is a newer version: 7.0.0-M7
Show newest version
/*
 * Copyright 2002-2022 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://smart-api.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * 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.
 */
package com.jk.data.datasource;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Types;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.hibernate.dialect.DB2390Dialect;
import org.hibernate.dialect.DB2zDialect;
import org.hibernate.dialect.Database;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HANAColumnStoreDialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQL8Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgresPlusDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseASEDialect;
import org.springframework.util.ReflectionUtils;

import com.jk.core.datatypes.JKDataType;
import com.jk.core.datatypes.JKDataTypeMapping;
import com.jk.core.util.JK;
import com.jk.core.util.JKCollectionUtil;
import com.jk.core.util.JKConversionUtil;
import com.jk.core.util.JKJdbcUtil;
import com.jk.core.util.JKObjectUtil;
import com.jk.data.vendors.h2.H2PhysicalNamingStrategy;
import com.jk.data.vendors.mysql.MysqlPhysicalNamingStrategy;
import com.jk.data.vendors.oracle.OraclePhysicalNamingStrategy;

// TODO: Auto-generated Javadoc
/**
 * The Enum JKDatabase.
 */
public enum JKDatabase {

	/** The db2. */
	DB2 {

		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(DB2zDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:db2";
		}

		@Override
		public String driver() {
			return "com.ibm.db2.jcc.DB2Driver";
		}
	},

	/** The derby. */
	DERBY {

		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(DerbyDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:derby";
		}

		@Override
		public String driver() {
			return "org.apache.derby.jdbc.EmbeddedDriver";
		}
	},

	/** The enterprisedb. */
	ENTERPRISEDB {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(PostgresPlusDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:enterprisedb";
		}
	},

	/** The h2. */
	H2 {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(H2Dialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:h2";
		}

		@Override
		public String getPhyiscalNamingStrategy() {
			return H2PhysicalNamingStrategy.class.getName();
		}

		@Override
		public String driver() {
			return "org.h2.Driver";
		}
	},

	/** The hana. */
	HANA {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(HANAColumnStoreDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:hana";
		}
	},

	/** The hsql. */
	HSQL {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(HSQLDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:hsql";
		}
	},

	/** The mariadb. */
	MARIADB {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(MariaDBDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:mariadb";
		}

		@Override
		public String driver() {
			return "org.mariadb.jdbc.Driver";
		}
	},

	/** The mysql. */
	MYSQL {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(MySQLDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:mysql";
		}

		@Override
		public String getPhyiscalNamingStrategy() {
			return MysqlPhysicalNamingStrategy.class.getName();
		}

		@Override
		public String driver() {
			return "com.mysql.jdbc.Driver";
		}
	},

	/** The oracle. */
	ORACLE {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(OracleDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:oracle";
		}

		public String getPhyiscalNamingStrategy() {
			return OraclePhysicalNamingStrategy.class.getName();
		}

		@Override
		public String driver() {
			return "oracle.jdbc.driver.OracleDriver";
		}

		@Override
		public String getTestQuery() {
			return "SELECT 1 FROM DUAL";
		}
	},

	/** The postgresql. */
	POSTGRESQL {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(PostgresPlusDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:postgresql";
		}

		@Override
		public String driver() {
			return "org.postgresql.Driver";
		}
	},

	/** The sqlserver. */
	SQLSERVER {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(SQLServerDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:sqlserver";
		}

		@Override
		public String driver() {
			return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		}
	},

	/** The sybase. */
	SYBASE {
		Dialect instance;

		public Dialect dialect() {
			if (instance == null) {
				instance = JK.newInstance(SybaseASEDialect.class);
			}
			return instance;
		}

		@Override
		public String urlPrefix() {
			return "jdbc:sybase";
		}
	};

	/**
	 * Dialect.
	 *
	 * @return the dialect
	 */
	public abstract Dialect dialect();

	/**
	 * Url prefix.
	 *
	 * @return the string
	 */
	public abstract String urlPrefix();

	/**
	 * Gets the database by url.
	 *
	 * @param url the url
	 * @return the database by url
	 */
	public static JKDatabase getDatabaseByUrl(String url) {
		if (url == null) {
			JK.exception("Database URL cannot be null, checkout your configuration");
		}
		JKDatabase[] values = JKDatabase.values();
		for (JKDatabase db : values) {
			if (url.startsWith(db.urlPrefix())) {
				return db;
			}
		}
		return null;
	}

	/**
	 * Gets the database.
	 *
	 * @return the database
	 */
	public Database getDatabase() {
		return Database.valueOf(toString());
	}

	/**
	 * To sql code from java type.
	 *
	 * @param clas the clas
	 * @return the int
	 */
	public int toSqlCodeFromJavaType(Class clas) {
		if (clas.isPrimitive()) {
			clas = JK.getWrapperForPrimitive(clas);
		}
		JKDataType type = JKDataTypeMapping.getType(clas);
		return type.getCode();
	}

	/**
	 * To sql type text from java type.
	 *
	 * @param clas       the clas
	 * @param length     the length
	 * @param preceision the preceision
	 * @param scale      the scale
	 * @return the string
	 */
	public String toSqlTypeTextFromJavaType(Class clas, int length, int preceision, int scale) {
		int typeJdbcCode = toSqlCodeFromJavaType(clas);
		// return dialect().getTypeName(typeJdbcCode, length, preceision, scale);
		return getTypeName(typeJdbcCode, length, preceision, scale);
	}

	/**
	 * To java type.
	 *
	 * @param sqlDataTypeNumber the sql data type number
	 * @return the class
	 */
	public Class toJavaType(int sqlDataTypeNumber) {
		JKDataType sqlDataType = JKDataTypeMapping.getType(sqlDataTypeNumber);
		return sqlDataType.getJavaType();
	}

	/**
	 * To type.
	 *
	 * @param sqlDataType the sql data type
	 * @return the JK type
	 */
	public JKDataType toType(int sqlDataType) {
		return JKDataTypeMapping.getType(sqlDataType);
	}

	/**
	 * Sets the parameter.
	 *
	 * @param ps    the ps
	 * @param index the index
	 * @param type  the type
	 * @param value the value
	 * @throws SQLException the SQL exception
	 */
	public void setParameter(PreparedStatement ps, int index, JKDataType type, Object value) throws SQLException {
		switch (type.getCode()) {
		case JKDataType.JAVA_ENUM:
			JK.error("hayne");
		case JKDataType.JAVA_CLASS:
			ps.setString(index, ((Class) value).getName());
			return;
		case JKDataType.PROPERTIES:
			ps.setString(index, JKCollectionUtil.formatProperties((Properties) value));
			return;
		case Types.LONGVARBINARY:
		case Types.BINARY:
			final byte[] data = (byte[]) value;
			ps.setBinaryStream(index, new ByteArrayInputStream(data), data.length);
			break;

		case Types.BOOLEAN:
		case Types.TINYINT:
		case Types.BIT:
			// add support for tiny integers represented as numbers
			ps.setInt(index, JKConversionUtil.toBoolean(value) ? 1 : 0);
			break;
		case Types.DECIMAL:
			ps.setDouble(index, JKConversionUtil.toDouble(value));
			break;
		case Types.DATE:
			ps.setDate(index, new java.sql.Date(((Date) value).getTime()));
			break;
		case Types.TIME:
			ps.setTime(index, new Time(((Date) value).getTime()));
			break;
		case Types.VARCHAR:
		case Types.LONGVARCHAR:
			ps.setString(index, value.toString());
			break;
		case Types.INTEGER:
			ps.setInt(index, JKConversionUtil.toInteger(value));
			break;
		default:
			ps.setObject(index, value);
		}
	}

	/**
	 * Read result.
	 *
	 * @param rs        the rs
	 * @param type      the type
	 * @param fieldName the field name
	 * @return the object
	 * @throws SQLException the SQL exception
	 */
	public Object readResult(ResultSet rs, JKDataType type, String fieldName) throws SQLException {
		switch (type.getCode()) {
		case JKDataType.JAVA_CLASS:
			String className = rs.getString(fieldName);
			if (className != null) {
				className = className.replaceAll("class ", "");
			}
			return JK.type(className);
		case JKDataType.PROPERTIES:
			String propString = rs.getString(fieldName);
			return JKCollectionUtil.toProperties(propString);
		case Types.BOOLEAN:
		case Types.TINYINT:
		case Types.BIT:
			return rs.getInt(fieldName) == 1;
		case Types.INTEGER:
		case Types.NUMERIC:
		case JKDataType.THREE_DIGITS_NUMBER_BELOW_100:
		case JKDataType.PASSWORD:
			return rs.getInt(fieldName);
		case Types.DOUBLE:
		case Types.REAL:
		case Types.DECIMAL:
			return rs.getDouble(fieldName);
		case Types.FLOAT:
			return rs.getFloat(fieldName);
		case Types.VARCHAR:
		case Types.CLOB:
		case Types.NCLOB:
		case Types.LONGVARCHAR:
		case Types.CHAR:
			return rs.getString(fieldName);
		case Types.BLOB:
		case Types.VARBINARY:
		case Types.LONGVARBINARY:
		case Types.BINARY:// binary
			return JKJdbcUtil.getBinaryStream(rs, fieldName);
		case Types.DATE:
			return rs.getDate(fieldName);
		case Types.TIME:
			return rs.getTime(fieldName);
		}
		return rs.getObject(fieldName);

	}

	/**
	 * Gets the type name.
	 *
	 * @param typeCode  the type code
	 * @param maxLength the max length
	 * @param precision the precision
	 * @param scale     the scale
	 * @return the type name
	 */
	public String getTypeName(int typeCode, int maxLength, int precision, int scale) {
		JK.implementMe();
		return null;
		// return dialect().getTypeName(typeCode, maxLength, precision, scale);
		// return JKObjectUtil.callMethod(dialect(), "columnType", true,
		// typeCode).toString();
	}

	/**
	 * Gets the phyiscal naming strategy.
	 *
	 * @return the phyiscal naming strategy
	 */
	public String getPhyiscalNamingStrategy() {
		return null;
	}

	/**
	 * Driver.
	 *
	 * @return the string
	 */
	public String driver() {
		return null;
	}

	/**
	 * Gets the test query.
	 *
	 * @return the test query
	 */
	public String getTestQuery() {
		return "SELECT 1";
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		System.out.println(MYSQL.toSqlTypeTextFromJavaType(Integer.class, 0, 0, 0));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy