import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
+import javax.naming.NamingException;
import org.mxchange.jcore.FrameworkInterface;
import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
import org.mxchange.jcore.database.result.Result;
* Tries a connection to the database
*
* @throws java.sql.SQLException If the connection attempt fails
+ * @throws javax.naming.NamingException May be thrown by a backend implementation
*/
- public void connectToDatabase () throws SQLException;
+ public void connectToDatabase () throws SQLException, NamingException;
/**
- * Inserts given dataset instance and returns a Result instance on success
+ * Inserts given dataset instance and returns a Result instance on success.
+ * The callee should not modify any content of the dataset instance.
*
* @param dataset A dataset instance
* @return An instance of Result
* instance. The result instance then provides methods to iterate over all
* found entries.
*
+ * The callee should not modify any content of the criteria instance.
+ *
* @param critera Search critera
* @return A result instance
* @throws java.io.IOException If any IO error occurs
--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcore.database.backend.datasource;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Map;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
+import org.mxchange.jcore.database.backend.BaseDatabaseBackend;
+import org.mxchange.jcore.database.backend.DatabaseBackend;
+import org.mxchange.jcore.database.frontend.DatabaseFrontend;
+import org.mxchange.jcore.database.result.Result;
+import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException;
+
+/**
+ * A database backend to a data source
+ *
+ * @author Roland Haeder
+ */
+public class DataSourceDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
+ /**
+ * Connection instance
+ */
+ private static Connection connection;
+
+ /**
+ * Prepared statement for total row count
+ */
+ private PreparedStatement totalRows;
+
+ /**
+ * Constructor with table name
+ *
+ * @param frontend An instance of the frontend
+ * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the requested driver is not supported
+ */
+ public DataSourceDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
+
+ // Get table name
+ String tableName = frontend.getTableName();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("tableName={0}", tableName)); //NOI18N
+
+ // Now that the driver is there, set the table name
+ this.setTableName(tableName);
+
+ // Set frontend
+ this.setFrontend(frontend);
+ }
+
+ @Override
+ public void connectToDatabase () throws SQLException, NamingException {
+ // Trace message
+ this.getLogger().trace("CALLED!");
+
+ // Is the connection given?
+ if (connection == null) {
+ // Initial context
+ Context context = new InitialContext();
+
+ // Get data source instance
+ DataSource source = (DataSource) context.lookup(this.getProperty("database.datasource.name"));
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("source={0}", source));
+
+ // Now as all access data is stored in data source, get a connection from it
+ connection = source.getConnection();
+
+ // Debug log
+ this.getLogger().debug(MessageFormat.format("connection={0}", connection));
+ }
+
+ // Debug message
+ this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
+
+ // Set prepared statement
+ this.totalRows = connection.prepareStatement(String.format("SELECT COUNT(`%s`) AS `cnt` FROM `%s` LIMIT 1", this.getFrontend().getIdName(), this.getTableName())); //NOI18N
+
+ // Trace message
+ this.getLogger().trace("EXIT!");
+ }
+
+ @Override
+ public Result<? extends Storeable> doInsertDataSet (final Map<String, Object> dataset) throws SQLException, IOException {
+ throw new UnsupportedOperationException("Not supported yet: dataset=" + dataset); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Result<? extends Storeable> doSelectByCriteria (final SearchableCriteria critera) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ throw new UnsupportedOperationException("Not supported yet: criteria=" + critera); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void doShutdown () throws SQLException, IOException {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Long getTotalRows () throws IOException, SQLException {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+}