2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcore.database.backend;
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.Driver;
22 import java.sql.DriverManager;
23 import java.sql.SQLException;
24 import java.text.MessageFormat;
25 import java.util.Enumeration;
26 import org.mxchange.jcore.BaseFrameworkSystem;
27 import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
28 import org.mxchange.jcore.database.result.Result;
29 import org.mxchange.jcore.database.storage.Storable;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException;
35 * Generall database backend
37 * @author Roland Haeder
39 public abstract class BaseDatabaseBackend extends BaseFrameworkSystem {
42 * No instances from this class
44 protected BaseDatabaseBackend () {
48 * Validates driver name and throws an exception if the driver is not valid
50 * @param driverName Driver name (e.g. "mysql")
51 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the given driver name cannot be solved into a driver instance
53 protected void validateDriver (final String driverName) throws UnsupportedDatabaseDriverException {
55 this.getLogger().trace(MessageFormat.format("driverName={0} - CALLED!", driverName)); //NOI18N
57 // driverName shall not be null
58 if (null == driverName) {
60 throw new NullPointerException("driverName is null");
63 // Get all registered/installed drivers
64 Enumeration<Driver> drivers = DriverManager.getDrivers();
66 // Default is not found
67 boolean isFound = false;
70 while (drivers.hasMoreElements()) {
72 Driver driver = drivers.nextElement();
75 this.getLogger().debug(MessageFormat.format("Driver {0} is installed.", driver)); //NOI18N
78 String className = driver.getClass().getName();
81 this.getLogger().debug(MessageFormat.format("className={0}", className));
84 if (className.contains(driverName)) {
86 this.getLogger().debug("Found driver!"); //NOI18N
95 this.getLogger().debug("isFound=" + isFound);
100 throw new UnsupportedDatabaseDriverException(driverName);
104 this.getLogger().trace("EXIT!"); //NOI18N
107 public int numRows (final SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
109 this.getLogger().trace("criteria=" + criteria + " - CALLED!");
111 // Criteria should not be null
112 if (null == criteria) {
114 throw new NullPointerException("criteria is null");
118 Result<? extends Storable> result = this.doSelectByCriteria(criteria);
121 int numRows = result.size();
124 this.getLogger().trace("numRows=" + numRows + " - EXIT!");
131 * Runs a SELECT "query" on database backend
133 * @param criteria Criteria instance
134 * @return Result instance
135 * @throws java.io.IOException If any IO error occurs
136 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
137 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is badly damaged
138 * @throws java.sql.SQLException If any SQL error occurs
139 * @throws java.lang.NoSuchMethodException If a method was not found
140 * @throws java.lang.IllegalAccessException If the method cannot be accessed
141 * @throws java.lang.reflect.InvocationTargetException Any other problems?
143 public abstract Result<? extends Storable> doSelectByCriteria (SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;