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

com.jk.data.vendors.mysql.MysqlDataAccess 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-2021 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.vendors.mysql;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Map;

import com.jk.core.util.JKConversionUtil;
import com.jk.data.dataaccess.core.JKDataAccessImpl;
import com.jk.data.dataaccess.core.JKFinder;
import com.jk.data.datasource.JKDataSource;

// TODO: Auto-generated Javadoc
/**
 * The Class MysqlDataAccess.
 */
public class MysqlDataAccess extends JKDataAccessImpl {

	/**
	 * Instantiates a new mysql data access.
	 *
	 * @param dataSource the data source
	 */
	public MysqlDataAccess(JKDataSource dataSource) {
		super(dataSource);
	}


	/**
	 * Blob to byte array.
	 *
	 * @param blob the blob
	 * @return the byte[]
	 * @throws IOException  Signals that an I/O exception has occurred.
	 * @throws SQLException the SQL exception
	 */
	// /////////////////////////////////////////////////////////////////////
	public static byte[] blobToByteArray(final Blob blob) throws IOException, SQLException {
		final InputStream inputStream = blob.getBinaryStream();
		int inByte;
		byte[] returnBytes;
		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		while ((inByte = inputStream.read()) != -1) {
			byteArrayOutputStream.write(inByte);
		}
		returnBytes = byteArrayOutputStream.toByteArray();
		return returnBytes;
	}


	// // ///////////////////////////////////////////////////////////////////
	// @Override
	// public CachedRowSet execute(final String query, final int fromRowIndex,
	// final int toRowIndex,Object...params) throws JKDataAccessException {
	// final String sql = "select * FROM (?) AS T limit ?,?";
	// final String compiledSql = JdbcUtil.compileSql(sql, new Object[] { query,
	// fromRowIndex, toRowIndex - fromRowIndex });
	// return executeQuery(compiledSql);
	// }

	/**
	 * Gets the rows count.
	 *
	 * @param query  the query
	 * @param params the params
	 * @return the rows count
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jk.db.test.dataaccess.core.JKAbstractPlainDataAccess#getRowsCount(java.
	 * lang.String)
	 */
	@Override
	// ///////////////////////////////////////////////////////////////////
	public int getRowsCount(final String query, Object... params) {
		final String sql = "SELECT COUNT(*) FROM (" + query + ") as T";
		return JKConversionUtil.toInteger(executeSingleOutputQuery(sql, params));
	}

	/**
	 * Gets the system date.
	 *
	 * @return the system date
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jk.db.test.dataaccess.core.JKAbstractPlainDataAccess#getSystemDate()
	 */
	@Override
	public Timestamp getSystemDate() {
		final JKFinder finder = new JKFinder() {

			@Override
			public String getQuery() {
				return "SELECT NOW()  ";
			}

			@Override
			public Object populate(final ResultSet rs) throws SQLException {
				final Timestamp date = rs.getTimestamp(1);
				return date;
			}

			@Override
			public void setParamters(final PreparedStatement ps) throws SQLException {
			}
		};
		return (java.sql.Timestamp) findRecord(finder);
	}

	/**
	 * Checks if is duplicate key.
	 *
	 * @param e the e
	 * @return true, if is duplicate key
	 * @throws SQLException the SQL exception
	 */
	// /////////////////////////////////////////////////////////////////////
	protected boolean isDuplicateKey(final SQLException e) throws SQLException {
		return isDuplicateKey(e, true);
	}

	/**
	 * Checks if is duplicate key.
	 *
	 * @param e              the e
	 * @param throwException the throw exception
	 * @return true, if is duplicate key
	 * @throws SQLException the SQL exception
	 */
	// /////////////////////////////////////////////////////////////////////
	protected boolean isDuplicateKey(final SQLException e, final boolean throwException) throws SQLException {
		if (e.getErrorCode() == 1) {
			return true;
		}
		if (throwException) {
			throw e;
		}
		return false;
	}

	/**
	 * Describe table.
	 *
	 * @param name the name
	 * @return the map
	 */
	/* (non-Javadoc)
	 * @see com.jk.db.test.dataaccess.core.JKDefaultDataAccess#describeTable(java.lang.String)
	 */
	@Override
	public Map describeTable(String name) {
		try {
			return super.describeTable(name);
		} catch (RuntimeException e) {
			if (e.getMessage().contains("doesn't exist")) {
				return Collections.EMPTY_MAP;
			}
			throw e;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy