package fr.lteconsulting.hexa.server.qpath;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

public class Database
{
	Connection connection;

	DatabaseMetaData databaseMetaData;

	private static final Logger logger = Logger.getLogger( Database.class.getSimpleName() );

	public boolean init( Connection connection )
	{
		logger.info( "Database initialisation" );

		this.connection = connection;

		return true;
	}

	public void term()
	{
		logger.info( "Database term" );

		if( connection == null )
			return;

		databaseMetaData = null;

		try
		{
			connection.close();
		}
		catch( SQLException e )
		{
			e.printStackTrace();
		}

		connection = null;
	}

	public String getCurrentDatabase()
	{
		try
		{
			return connection.getCatalog();
		}
		catch( SQLException e )
		{
			e.printStackTrace();

			return null;
		}
	}

	public DatabaseMetaData getDatabaseMetaData()
	{
		ensureMetadata();

		return databaseMetaData;
	}

	public void startTransaction()
	{
		logger.info( "START TRANSACTION" );
		sql( "START TRANSACTION" );
	}

	public void commit()
	{
		logger.info( "COMMIT" );
		sql( "COMMIT" );
	}

	public void rollback()
	{
		logger.info( "ROLLBACK" );
		sql( "ROLLBACK" );
	}

	public DBResults sql( String sql )
	{
		try
		{
			logger.info( "SQL-SELECT: " + sql );

			Statement stmt = connection.createStatement();

			DBResults res = new DBResults( stmt.executeQuery( sql ), stmt );
			return res;
		}
		catch( SQLException exception )
		{
			String message = "SQLException during call to sql executing statement '" + sql + "' !";

			logger.severe( message );

			exception.printStackTrace();

			throw new DatabaseException( message, exception );
		}
	}

	public int sqlInsert( String sql )
	{
		try
		{
			logger.info( "SQL-INSERT: " + sql );

			PreparedStatement stmt = connection.prepareStatement( sql, Statement.RETURN_GENERATED_KEYS );
			stmt.execute();

			ResultSet res = stmt.getGeneratedKeys();
			if( !res.next() )
				return 0;

			return (int) res.getLong( 1 );
		}
		catch( SQLException exception )
		{
			String message = "SQLException during call to sqlInsert executing statement '" + sql + "' !";

			logger.severe( message );

			exception.printStackTrace();

			throw new DatabaseException( message, exception );
		}
	}

	public int sqlDelete( String sql )
	{
		// there is no technical difference with sqlUpdate,
		// however the semantic is not the same for the user
		return sqlUpdate( sql );
	}

	public int sqlUpdate( String sql )
	{
		try
		{
			logger.info( "SQL-MODIFY: " + sql );

			PreparedStatement stmt = connection.prepareStatement( sql );
			stmt.execute();

			return stmt.getUpdateCount();
		}
		catch( SQLException exception )
		{
			String message = "SQLException during call to sqlUpdate executing statement '" + sql + "' !";

			logger.severe( message );

			exception.printStackTrace();

			throw new DatabaseException( message, exception );
		}
	}

	private void ensureMetadata()
	{
		if( databaseMetaData == null )
		{
			try
			{
				databaseMetaData = connection.getMetaData();
			}
			catch( SQLException e )
			{
				String message = "SQLException during call to ensureMetadata !";

				logger.severe( message );

				e.printStackTrace();

				throw new DatabaseException( message, e );
			}
		}
	}
}

@@ -871,16 +871,61 @@
 try%0A%09%09%7B%0A
+%09%09%09if( connection == null )%0A%09%09%09%09return null;%0A
 %09%09%09retur

fix bug npe