/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.geoforge.sql.adaptator;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.sqlite.SQLiteConfig;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
public class AdaptatorSqlite
extends AdaptatorAbs
{
public AdaptatorSqlite()
throws Exception
{
super("org.sqlite.JDBC");
}
public static synchronized AdaptatorSqlite s_getInstance()
throws Exception
{
if (_INSTANCE_ == null)
{
_INSTANCE_ = new AdaptatorSqlite();
}
return _INSTANCE_;
}
public int executeUpdate(
String strPathDatabase,
String strCommand)
throws Exception
{
Connection con = null;
Statement stt = null;
try
{
String strUrlDatabase = "jdbc:sqlite:" + strPathDatabase;
SQLiteConfig config = new SQLiteConfig();
config.enableLoadExtension(true);
con = DriverManager.getConnection(
strUrlDatabase,
config.toProperties());
stt = con.createStatement();
stt.executeUpdate("PRAGMA foreign_keys = ON;");
int intRes = stt.executeUpdate(strCommand);
/*System.out.println(
"dev_sqlite_info : " + "intRes=" + intRes +
", strCommand=" + strCommand);*/
if(stt != null)
{
stt.close();
stt = null;
}
if(con != null && !con.isClosed())
{
con.close();
con = null;
}
return intRes;
}
catch (SQLException e)
{
if(stt != null)
{
stt.close();
stt = null;
}
if(con != null && !con.isClosed())
{
con.close();
con = null;
}
String str = e.getMessage();
str += ", working on database : \n" + strPathDatabase +
",\n strCommand = \n" + strCommand;
throw new SQLException(str);
}
}
private static AdaptatorSqlite _INSTANCE_;
}