/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 com.approximatrix.draco.io.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public abstract class DracoSQL {
	
	private static final String DB_CLASSNAME = "org.sqlite.JDBC";
	private static final String DB_CONNECTION_PREFIX = "jdbc:sqlite:";
	
	protected Connection conn = null;
	
	private DracoSQL() {
		try {
			Class.forName(DB_CLASSNAME);
		} catch(ClassNotFoundException cnfe) {
			System.err.println("Could not initialize DB for load/save");
			cnfe.printStackTrace();
		}
	}
	
	protected DracoSQL(String filename) {
		this();
		try {
			this.conn = DriverManager.getConnection(DB_CONNECTION_PREFIX+filename);
			this.conn.setAutoCommit(false);
		} catch(SQLException se) {
			System.err.println("Error connecting to db: "+filename);
			se.printStackTrace();
		}
	}
	
	public boolean close() {
		try {
			this.conn.close();
		} catch(SQLException se) {
			System.err.println("Could not close db connection");
			se.printStackTrace();
			return false;
		}
		return true;
	}
	
	public abstract String getError();
}
