agirstatool/src/org/april/agirstatool/cli/SQLUtils.java

273 lines
5.6 KiB
Java
Raw Normal View History

2020-01-05 17:24:43 +01:00
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of AgirStatool, simple key value database.
*
* AgirStatool is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* AgirStatool 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with AgirStatool. If not, see <http://www.gnu.org/licenses/>.
*/
package org.april.agirstatool.cli;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
2020-01-29 00:51:45 +01:00
import java.time.LocalDateTime;
import java.time.ZoneOffset;
2020-01-05 17:24:43 +01:00
import org.apache.commons.lang3.StringUtils;
import org.april.agirstatool.core.AgirStatoolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class SQLUtils.
*/
public final class SQLUtils
{
private static Logger logger = LoggerFactory.getLogger(SQLUtils.class);
/**
2020-01-22 19:37:28 +01:00
* Instantiates a new SQL utils.
2020-01-05 17:24:43 +01:00
*/
private SQLUtils()
{
}
/**
* Close quietly.
*
* @param connection
* the connection
*/
public static void closeQuietly(final Connection connection)
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
}
/**
* Close quietly.
*
* @param connection
* the connection
* @param statement
* the statement
* @param resultSet
* the result set
*/
public static void closeQuietly(final Connection connection, final Statement statement, final ResultSet resultSet)
{
//
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
//
if (statement != null)
{
try
{
statement.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
//
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
}
/**
* Close quietly.
*
* @param statement
* the statement
* @param resultSet
* the result set
*/
public static void closeQuietly(final Statement statement, final ResultSet resultSet)
{
//
if (statement != null)
{
try
{
statement.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
//
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
}
/**
2020-01-22 19:37:28 +01:00
* Gets the connexion.
*
* @param serverUrl
* the server url
* @param databaseName
* the database name
* @param login
* the login
* @param password
* the password
* @return the connexion
2020-01-05 17:24:43 +01:00
* @throws AgirStatoolException
2020-01-22 19:37:28 +01:00
* the agir statool exception
2020-01-05 17:24:43 +01:00
*/
public static Connection getConnexion(final String serverUrl, final String databaseName, final String login, final String password) throws AgirStatoolException
{
Connection result;
try
{
if (StringUtils.isBlank(serverUrl))
{
throw new IllegalArgumentException("Undefined server URL.");
}
else if (StringUtils.isBlank(databaseName))
{
throw new IllegalArgumentException("Undefined database name.");
}
else if (StringUtils.isBlank(login))
{
throw new IllegalArgumentException("Undefined database login.");
}
else
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
result = DriverManager.getConnection(serverUrl + databaseName + "?useUnicode=true&amp;amp;characterEncoding=utf8", login, password);
}
}
catch (SQLException exception)
{
throw new AgirStatoolException("Database connection failed: " + exception.getMessage(), exception);
}
catch (InstantiationException exception)
{
throw new AgirStatoolException("Database connection failed: " + exception.getMessage(), exception);
}
catch (IllegalAccessException exception)
{
throw new AgirStatoolException("Database connection failed: " + exception.getMessage(), exception);
}
catch (ClassNotFoundException exception)
{
throw new AgirStatoolException("Database connection failed: " + exception.getMessage(), exception);
}
//
return result;
}
2020-01-22 19:37:28 +01:00
/**
* Gets the nullable long.
*
* @param resultSet
* the result set
* @param index
* the index
* @return the nullable long
* @throws SQLException
* the SQL exception
*/
2020-01-05 17:24:43 +01:00
public static Long getNullableLong(final ResultSet resultSet, final int index) throws SQLException
{
Long result;
if (resultSet.getObject(index) == null)
{
result = null;
}
else
{
result = resultSet.getLong(index);
}
//
return result;
}
2020-01-29 00:51:45 +01:00
/**
* To date time.
*
* @param source
* the source
* @return the local date time
*/
public static LocalDateTime toLocalDateTime(final java.sql.Timestamp source)
{
LocalDateTime result;
if (source == null)
{
result = null;
}
else
{
long seconds = source.getTime() / 1000;
long nanos = (source.getTime() - seconds * 1000) * 1000000;
result = LocalDateTime.ofEpochSecond(seconds, (int) nanos, ZoneOffset.UTC);
}
//
return result;
}
2020-01-05 17:24:43 +01:00
}