]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java
Preprared jcore for EJB applications:
[jcore.git] / src / org / mxchange / jcore / database / backend / BaseDatabaseBackend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcore.database.backend;
18
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;
33
34 /**
35  * Generall database backend
36  *
37  * @author Roland Haeder
38  */
39 public abstract class BaseDatabaseBackend extends BaseFrameworkSystem {
40
41         /**
42          * No instances from this class
43          */
44         protected BaseDatabaseBackend () {
45         }
46
47         /**
48          * Validates driver name and throws an exception if the driver is not valid
49          *
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
52          */
53         protected void validateDriver (final String driverName) throws UnsupportedDatabaseDriverException {
54                 // Trace message
55                 this.getLogger().trace(MessageFormat.format("driverName={0} - CALLED!", driverName)); //NOI18N
56
57                 // driverName shall not be null
58                 if (null == driverName) {
59                         // Is null
60                         throw new NullPointerException("driverName is null");
61                 }
62
63                 // Get all registered/installed drivers
64                 Enumeration<Driver> drivers = DriverManager.getDrivers();
65
66                 // Default is not found
67                 boolean isFound = false;
68
69                 // Search for it
70                 while (drivers.hasMoreElements()) {
71                         // Get element
72                         Driver driver = drivers.nextElement();
73
74                         // Debug message
75                         this.getLogger().debug(MessageFormat.format("Driver {0} is installed.", driver)); //NOI18N
76
77                         // Get class name
78                         String className = driver.getClass().getName();
79
80                         // Debug message
81                         this.getLogger().debug(MessageFormat.format("className={0}", className));
82
83                         // Is this wanted?
84                         if (className.contains(driverName)) {
85                                 // Debug message
86                                 this.getLogger().debug("Found driver!"); //NOI18N
87
88                                 // Found it
89                                 isFound = true;
90                                 break;
91                         }
92                 }
93
94                 // Debug message
95                 this.getLogger().debug("isFound=" + isFound);
96
97                 // Is it found?
98                 if (!isFound) {
99                         // Throw exception
100                         throw new UnsupportedDatabaseDriverException(driverName);
101                 }
102
103                 // Trace message
104                 this.getLogger().trace("EXIT!"); //NOI18N
105         }
106
107         public int numRows (final SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
108                 // Trace message
109                 this.getLogger().trace("criteria=" + criteria + " - CALLED!");
110
111                 // Criteria should not be null
112                 if (null == criteria) {
113                         // Abort here
114                         throw new NullPointerException("criteria is null");
115                 }
116
117                 // Run "SELECT"
118                 Result<? extends Storable> result = this.doSelectByCriteria(criteria);
119
120                 // Get size
121                 int numRows = result.size();
122
123                 // Trace message
124                 this.getLogger().trace("numRows=" + numRows + " - EXIT!");
125
126                 // Return it
127                 return numRows;
128         }
129
130         /**
131          *  Runs a SELECT "query" on database backend
132          * 
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?
142          */
143         public abstract Result<? extends Storable> doSelectByCriteria (SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
144 }