package de.nplusc.izc.tools.IOtools;

import de.nplusc.izc.tools.baseTools.Tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

public class AdvancedDBWriter
{

    Connection cn = null;
    ResultSet rs = null;

    public AdvancedDBWriter(String host, String db, String user, String pw)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            this.cn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + db + "", user, pw);
        }
        catch (ClassNotFoundException | SQLException e)
        {
        }
    }

    public Object[] querySIngleColumn(String Query)
    {
        this.rs = null;
        LinkedList masterList = new LinkedList();

        Tools.DebugHelperPrint(Query, true, "Toolkit.enableCoreDebug");
        try
        {
            Statement st = this.cn.createStatement();
            this.rs = st.executeQuery(Query);
            if (this.rs != null)
            {
                while (this.rs.next())
                {
                    try
                    {
                        masterList.add(this.rs.getObject(1));
                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
                this.rs.close();
                return masterList.toArray(new Object[0]);
            }

        }
        catch (Exception e)
        {
            return null;
        }
        return null;
    }

    public Object querySingleCell(String Query)
    {
        this.rs = null;
        Tools.DebugHelperPrint(Query, true, "Toolkit.enableCoreDebug");
        try
        {
            Statement st = this.cn.createStatement();
            this.rs = st.executeQuery(Query);
            if (this.rs != null)
            {
                if (this.rs.next())
                {
                    try
                    {
                        return this.rs.getObject(1);
                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
                this.rs.close();
            }
        }
        catch (Exception e)
        {
            return null;
        }
        return null;
    }

    public Object[] querySingleRow(String Query)
    {
        this.rs = null;
        try
        {
            Statement st = this.cn.createStatement();
            this.rs = st.executeQuery(Query);
            return querySingleRow(Query, this.rs.getMetaData().getColumnCount(), true);
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public Object[] querySingleRow(String Query, int cols)
    {
        return querySingleRow(Query, cols, false);
    }

    private Object[] querySingleRow(String Query, int cols, boolean delivRS)
    {
        if (!delivRS)
        {
            rs = null;
        }
        LinkedList masterList = new LinkedList();

        Tools.DebugHelperPrint(Query, true, "Toolkit.enableCoreDebug");
        try
        {
            Statement st = null;
            if (!delivRS)
            {
                st = cn.createStatement();
                rs = st.executeQuery(Query);
            }
            if (rs != null)
            {
                while (rs.next())
                {
                    try
                    {
                        for (int i = 1; i <= cols; i++)
                        {
                            masterList.add(rs.getObject(i));
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        return null;
                    }
                }
                rs.close();
                return masterList.toArray(new Object[0]);
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public Object[][] queryTable(String Query)
    {
        rs = null;
        try
        {
            Statement st = this.cn.createStatement();
            this.rs = st.executeQuery(Query);
            return queryTable(Query, this.rs.getMetaData().getColumnCount(), true);
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return (Object[][]) null;
    }

    public Object[][] queryTable(String Query, int cols)
    {
        return queryTable(Query, cols, false);
    }

    private Object[][] queryTable(String Query, int cols, boolean delivRS)
    {
        if (!delivRS)
        {
            this.rs = null;
        }
        LinkedList RowList = new LinkedList();
        LinkedList masterList = new LinkedList();

        Tools.DebugHelperPrint(Query, true, "Toolkit.enableCoreDebug");
        try
        {
            Statement st = null;
            if (!delivRS)
            {
                st = this.cn.createStatement();
            }
            this.rs = st.executeQuery(Query);
            if (this.rs != null)
            {
                while (this.rs.next())
                {
                    try
                    {
                        for (int i = 1; i <= cols; i++)
                        {
                            RowList.add(this.rs.getObject(i));
                        }
                    }
                    catch (Exception e)
                    {
                        return (String[][]) null;
                    }
                }
                this.rs.close();
                masterList.add(RowList.toArray(new Object[0]));
            }
        }
        catch (Exception e)
        {
            return null;
        }
        Object[] retvaltmp = ((Object[][]) masterList.toArray(new String[0][0]))[0];
        int sizetmp = retvaltmp.length;
        int sizet = sizetmp / cols;
        Object[][] retvals = new String[sizet][cols];
        for (int i = 0; i < sizet; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                retvals[i][j] = retvaltmp[(i * cols + j)];
            }
        }

        return retvals;
    }

    public boolean write(String query)
    {
        try
        {
            return write(query, false);
        }
        catch (Exception e)
        {
        }
        return false;
    }

    public boolean write(String query, boolean passthroughException)
            throws Exception
    {
        return write(query, passthroughException, false);
    }

    public boolean write(String query, boolean passthroughException, boolean erroutwhenzeroedited)
            throws Exception
    {
        boolean ret = true;
        Tools.DebugHelperPrint(query, true, "Toolkit.enableCoreDebug");
        try
        {
            Statement st = this.cn.createStatement();
            int rs = st.executeUpdate(query);
            if (rs == 0)
            {
                ret = false;
            }
        }
        catch (Exception e)
        {
            e.getMessage();
            e.printStackTrace();

            Tools.DebugHelperPrint("FF", true, "Toolkit.enableFinerDebug");
            ret = false;
        }
        if ((passthroughException) && (!ret))
        {
            throw new Exception();
        }

        return ret;
    }

    public void closeConn()
    {
        try
        {
            this.cn.close();
        }
        catch (SQLException ex)
        {
            Tools.DebugHelperPrint("DB.close()->fail", true, "Toolkit.enableCoreDebug");
        }
    }

    public int getColCount()
    {
        try
        {
            ResultSetMetaData md = this.rs.getMetaData();
            return md.getColumnCount();
        }
        catch (SQLException ex)
        {
        }
        return -1;
    }

    public String[] getHeadlines()
    {
        try
        {
            ResultSetMetaData md = this.rs.getMetaData();
            int mc = md.getColumnCount();
            String[] ret = new String[mc];
            for (int i = 0; i < mc; i++)
            {
                ret[i] = md.getColumnName(i);
            }
            return ret;
        }
        catch (SQLException ex)
        {
            Tools.DebugHelperPrint("Fikk", true, "Toolkit.enableCoreDebug");
        }
        return null;
    }
}

/* Location:           C:\Users\LH\Documents\NetBeansProjects\ToolKit\ToolKit.jar
 * Qualified Name:     de.nplusc.izc.tools.IOtools.DBWriter
 * JD-Core Version:    0.6.2
 */