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();
}
