/*
 * $Source: /usr/local/cvsroot/erserver/erserver/java/src/com/postgres/replic/tools/desc/AddTable.java,v $
 * $Author: asullivan $ $Revision: 1.1.1.1 $ $Date: 2003/08/26 19:02:25 $
 *
 */

package com.postgres.replic.tools.desc;

import com.postgres.replic.tools.*;
import java.sql.*;

public class AddTable extends GeneratorDescriptor {
    protected String table;
    protected String column;
    protected static final boolean TEST = false;

    public AddTable(
    Connection conn,
    String table,
    String column) throws Exception {
        if (conn == null) {
            throw new Exception ("AddTable:: conn == null");
        }
        if (table == null) {
            throw new Exception ("AddTable:: table == null");
        }
        if (column == null) {
            throw new Exception("AddTable:: column == null");
        }

        this.conn = conn;
        this.table = table;
        this.column = column;

    }

    public void addTable() throws Exception {}

    protected String [] getAttNum(String table, String column) {
        String [] res = null;
        Statement stmt = null;
        ResultSet rs = null;
        int count = T_NO;

        try {
            stmt = getConnection().createStatement();
            String sql = "select pgc.oid, pga.attnum from pg_class pgc" +
            ", pg_attribute pga where pgc.relname = '" + table + "'" +
            " and pgc.oid = pga.attrelid" +
            " and pga.attname = '" + column + "'";
            if (getDebug()) System.out.println("AddTable::getAttNum: sql = " + sql);
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                if (res == null) {
                    res = new String[2];
                }
                res[0] = rs.getString(1);
                res[1] = rs.getString(2);
                if (getDebug())
                    System.out.println("AddTable::getAttNum: res[1] = " + res[1]);
            }
        } catch (Exception e) {
            System.out.println("AddTable::getAttNum" + e.toString());
        } finally {
            if (stmt != null) {
                try { stmt.close(); } catch (Exception e) { };
            }

            if (rs != null) {
                try { rs.close(); } catch (Exception e) { };
            }
        }

        return res;
    }

    protected void insertRserv(
    String systemTable,
    String table,
    String column,
    String oid,
    String attNum) throws Exception {

        Statement stmt = null;
        ResultSet rs = null;

        try {

            stmt = getConnection().createStatement();

            String sql = "insert into " + systemTable + " (tname, cname, reloid, key)" +
            " values ('" + table + "', '" + column + "', " + oid + ", " + attNum + ")";



            if (getDebug()) System.out.println("AddTable::insertRserv: sql = " + sql);
            stmt.executeUpdate(sql);

            //getConnection().commit();
        } catch (Exception e) {

            try { getConnection().rollback(); } catch (Exception ex) { };
            throw new Exception ("AddTable::insertRserv" + e.toString());
        } finally {
            if (stmt != null) {
                try { stmt.close(); } catch (Exception e) { };
            }
            if (rs != null) {
                try { rs.close(); } catch (Exception e) { };
            }
        }
    }
}
