com.cefriel.template.io.sql.SQLReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapping-template Show documentation
Show all versions of mapping-template Show documentation
A template-based component exploiting Apache Velocity to define
declarative mappings for schema and data transformations.
/*
* Copyright (c) 2019-2024 Cefriel.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.cefriel.template.io.sql;
import com.cefriel.template.io.Reader;
import org.eclipse.rdf4j.query.algebra.Str;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidParameterException;
import java.sql.*;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SQLReader implements Reader {
private final Logger log = LoggerFactory.getLogger(SQLReader.class);
private Connection conn;
private String queryHeader;
private boolean verbose;
private static final Object lock = new Object();
public boolean checkTableExists(String table, Connection connection) {
List tables = new ArrayList<>();
try {
PreparedStatement tablesQueryStatement;
String dbDriver = connection.getMetaData().getDatabaseProductName();
String databaseName = connection.getCatalog();
// Prepare and execute table query based on the database driver
if (dbDriver.equalsIgnoreCase("mysql")) {
tablesQueryStatement = connection.prepareStatement("SELECT table_name FROM information_schema.tables WHERE table_schema = ?;");
tablesQueryStatement.setString(1, databaseName);
} else if (dbDriver.equalsIgnoreCase("postgresql")) {
tablesQueryStatement = connection.prepareStatement("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'");
} else {
throw new IllegalArgumentException("SQLReader does not support " + dbDriver);
}
ResultSet resultSet = tablesQueryStatement.executeQuery();
while (resultSet.next()) {
String tableName = resultSet.getString("table_name");
tables.add(tableName);
}
} catch (SQLException e) {
log.error("Error connecting to the database: " + e.getMessage(), e);
}
return tables.contains(table);
}
public SQLReader(String driver, String url, String database, String username, String password) throws SQLException {
if (!url.contains("jdbc:"))
url = "jdbc:" + driver + "://" + url + "/" + database;
log.info("Connection to database with URL: " + url);
conn = DriverManager.getConnection(url, username, password);
}
public SQLReader(Connection conn) {
this.conn = conn;
}
/**
* Executes a SQL query returning a {@code ResultSet}.
*
* @param query SQL query to be executed
* @return ResultSet for the SQL query executed
*/
public ResultSet executeQuery(String query) {
ResultSet resultSet = null;
if (queryHeader != null) {
query = queryHeader + query;
}
if (verbose) {
log.info("Query executed: \n" + query);
}
try {
synchronized (lock) {
Statement statement = conn.createStatement();
resultSet = statement.executeQuery(query);
}
return resultSet;
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return null;
}
private List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy