package hc.adlrunner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import hc.adl.IFunctionCallback;

public class ADLSQL implements IFunctionCallback {
	public enum ADLSQLFunctions {
		open, exec, query, close, load
	}

	private Connection c;

	public ADLSQL() {
	}

	public Object FunctionCallback(String funcName, Vector<Object> vParams) {
		ADLSQLFunctions function = ADLSQLFunctions.valueOf(funcName);
		try {
			switch (function) {
			case close:
				c.close();
				return true;
			case exec: {
				Statement s = c.createStatement();
				s.execute((String) vParams.get(0));
			}
			case open: {
				this.c = DriverManager.getConnection((String) vParams.get(0));
				return true;
			}
			case query: {
				Statement s = c.createStatement();
				ResultSet rs = s.executeQuery((String) vParams.get(0));
				String[] foo = new String[10];
				try {
					rs.next();
					for (int i = 0; i < foo.length; i++) {
						foo[i] = rs.getString(i + 1);
						System.out.println(foo[i]);
					}
				} catch (Throwable t) {
				}
				return foo;
			}
			case load: {
				return Class.forName((String) vParams.get(0));
			}
			}
		} catch (SQLException e) {
			return e;
		} catch (ClassNotFoundException e) {
			return e;
		}
		return "Funktion nicht gefunden";
	}

}
