/* This class was generated by Cartographer
 * Licensing info here.
 */

package dbobjects;

import java.io.*;
import java.math.*;
import java.util.*;
import java.sql.*;

public class MyTable extends DBObject
{

  //
  // Constants
  //

  /**
   * SQL statement for creating new MyTable records.
   */
  private static final String INSERT = "INSERT INTO " +
                                         "t_my_table (c_id,c_name,c_description )" +
                                       "VALUES (?,?,?)";

  /**
   * SQL statement for updating existing MyTable records.
   */
  private static final String UPDATE = "UPDATE t_my_table " +
                                       "SET ";

  /**
   * SQL statement for deleting existing MyTable records.
   */
  private static final String DELETE = "DELETE FROM " +
                                         "t_my_table " +
                                       "WHERE c_id = ? ";

  //
  // Class Fields
  //


  /**
   * 
   */
  private Connection con;


  /**
   * 
   */
  private PreparedStatement stmt;


  /**
   * 
   */
  private DBInteger id;

  /**
   * 
   */
  private boolean idIsDirty;

  /**
   * 
   */
  private DBVarChar name;

  /**
   * 
   */
  private boolean nameIsDirty;

  /**
   * 
   */
  private DBLongVarChar description;

  /**
   * 
   */
  private boolean descriptionIsDirty;



  //
  // Constructors
  //


  /**
   * Default Constructor.
   */
  public MyTable()
  {
    this.id = new DBInteger();
    this.name = new DBVarChar();
    this.description = new DBLongVarChar();
  }


  /**
   * Construct a(n) MyTable object from the given parameters.
   */
  public MyTable(Integer id , String name , String description)
  {
    this.id = new DBInteger(id);
    this.name = new DBVarChar(name);
    this.description = new DBLongVarChar(description);
  }


  /**
   * Construct a(n) MyTable object from the database given the primary key.
   */
  public MyTable(Integer id)
  {
    try
    {
      con = getConnection();
      stmt = con.prepareStatement("SELECT c_id,c_name,c_description FROM t_my_table WHERE c_id = ? ");
      psSetObject(stmt,1,new DBInteger(id));
      ResultSet rs = stmt.executeQuery();

      rs.next();
      this.id = new DBInteger((Integer)rs.getObject(1));
      this.name = new DBVarChar((String)rs.getObject(2));
      this.description = new DBLongVarChar((String)rs.getObject(3));

      rs.close();
      stmt.close();
      con.close();
    }
    catch(SQLException sqle)
    {
      log("Error creating Email record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }

  }


  //
  // Access Methods
  //


  /**
   * Get the id for the my_table record.
   * @return The id for the my_table record.
   */
  public Integer getId()
  {
    return (Integer)id.getValue();
  }


  /**
   * Set the id for the my_table record.
   * @param id The value to set the id to.
   */
  public void setId(Integer id)
  {
    this.id.setValue((Integer)id);
    this.idIsDirty = true;
  }


  /**
   * Get the name for the my_table record.
   * @return The name for the my_table record.
   */
  public String getName()
  {
    return (String)name.getValue();
  }


  /**
   * Set the name for the my_table record.
   * @param name The value to set the name to.
   */
  public void setName(String name)
  {
    this.name.setValue((String)name);
    this.nameIsDirty = true;
  }


  /**
   * Get the description for the my_table record.
   * @return The description for the my_table record.
   */
  public String getDescription()
  {
    return (String)description.getValue();
  }


  /**
   * Set the description for the my_table record.
   * @param description The value to set the description to.
   */
  public void setDescription(String description)
  {
    this.description.setValue((String)description);
    this.descriptionIsDirty = true;
  }


  /**
   * Get the value of a column from the database column name.
   * @return The value for the given column.
   */
  public Object getColumn(String column)
  {
    if( column.equals("c_id") ) { return getId(); }
    else if( column.equals("c_name") ) { return getName(); }
    else if( column.equals("c_description") ) { return getDescription(); }
    return null;
  }


  /**
   * Set the value of a column from the database column name.
   */
  public void setColumn(String column, Object value)
  {
    if( column.equals("c_id") ) { setId((Integer)value); }
    else if( column.equals("c_name") ) { setName((String)value); }
    else if( column.equals("c_description") ) { setDescription((String)value); }
  }


  //
  // Persistence Methods
  //


  /**
   * Creates a(n) MyTable record in the database - uses default database
   * connection.
   * @return The number of rows inserted.
   */
  public int create()
  {
    int rowCount = 0;

    try
    {
      con = getConnection();

      stmt = con.prepareStatement(INSERT);
      psSetObject(stmt,1,(DBField)id);
      psSetObject(stmt,2,(DBField)name);
      psSetObject(stmt,3,(DBField)description);
      rowCount = stmt.executeUpdate();

      stmt.close();
      con.close();
    }
    catch(SQLException sqle)
    {
      log("Error creating MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }

  /**
   * Creates a(n) MyTable record in the database - uses supplied database
   * connection.
   * @return The number of rows inserted.
   */
  public int create(Connection con)
  {
    int rowCount = 0;

    try
    {
      this.con = con;

      stmt = con.prepareStatement(INSERT);
      psSetObject(stmt,1,(DBField)id);
      psSetObject(stmt,2,(DBField)name);
      psSetObject(stmt,3,(DBField)description);
      rowCount = stmt.executeUpdate();

      stmt.close();
    }
    catch(SQLException sqle)
    {
      log("Error creating MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }

  /**
   * Updates a(n) MyTable record in the database - uses default database
   * connection.
   * @return The number of rows updated.
   */
  public int update()
  {
    int rowCount = 0;
    String query = UPDATE;
    Vector fieldVector = new Vector();

    try
    {
      con = getConnection();

      if( nameIsDirty )
      {
        if( !query.equalsIgnoreCase("UPDATE t_my_table SET ") )
        {
          query += ", ";
        }
        query += "c_name = ?";
        fieldVector.addElement(name);
      }
      if( descriptionIsDirty )
      {
        if( !query.equalsIgnoreCase("UPDATE t_my_table SET ") )
        {
          query += ", ";
        }
        query += "c_description = ?";
        fieldVector.addElement(description);
      }

      query += " WHERE c_id = ? ";
      fieldVector.addElement(id);

      stmt = con.prepareStatement(query);
      for(int i = 0; i < fieldVector.size(); i++)
      {
        psSetObject(stmt,i+1,(DBField)fieldVector.elementAt(i));
      }
      rowCount = stmt.executeUpdate();

      stmt.close();
      con.close();
    }
    catch(SQLException sqle)
    {
      log("Error updating MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }

  /**
   * Updates a(n) MyTable record in the database - uses supplied database
   * connection.
   * @return The number of rows updated.
   */
  public int update(Connection con)
  {
    int rowCount = 0;
    String query = UPDATE;
    Vector fieldVector = new Vector();

    try
    {
      this.con = con;

      if( nameIsDirty )
      {
        if( !query.equalsIgnoreCase("UPDATE t_my_table SET ") )
        {
          query += ", ";
        }
        query += "c_name = ?";
        fieldVector.addElement(name);
      }
      if( descriptionIsDirty )
      {
        if( !query.equalsIgnoreCase("UPDATE t_my_table SET ") )
        {
          query += ", ";
        }
        query += "c_description = ?";
        fieldVector.addElement(description);
      }

      query += " WHERE c_id = ? ";
      fieldVector.addElement(id);

      stmt = con.prepareStatement(query);
      for(int i = 0; i < fieldVector.size(); i++)
      {
        psSetObject(stmt,i+1,(DBField)fieldVector.elementAt(i));
      }
      rowCount = stmt.executeUpdate();

      stmt.close();
    }
    catch(SQLException sqle)
    {
      log("Error updating MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }      

  /**
   * Deletes a(n) MyTable record in the database - uses default database
   * connection.
   * @return The number of rows deleted.
   */
  public int destroy()
  {
    int rowCount = 0;

    try
    {
      con = getConnection();

      stmt = con.prepareStatement(DELETE);
      psSetObject(stmt,1,(DBField)id);
      rowCount = stmt.executeUpdate();

      stmt.close();
      con.close();
    }
    catch(SQLException sqle)
    {
      log("Error deleting MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }

  /**
   * Deletes a(n) MyTable record in the database - uses supplied database
   * connection.
   * @return The number of rows deleted.
   */
  public int destroy(Connection con)
  {
    int rowCount = 0;

    try
    {
      this.con = con;

      stmt = con.prepareStatement(DELETE);
      psSetObject(stmt,1,(DBField)id);
      rowCount = stmt.executeUpdate();

      stmt.close();
    }
    catch(SQLException sqle)
    {
      log("Error deleting MyTable record: " + sqle.getMessage());
      sqle.printStackTrace();
    }
    catch(Exception e)
    {
      log("Error communicating with database: " + e.getMessage());
      e.printStackTrace();
    }
    return rowCount;
  }


  //
  // Utility Methods
  //


  /**
   * @return The string representation of the object.
   */
  public String toString()
  {
    return "" + id.toString() + ":" + name.toString() + ":" + description.toString();
  }// end toString()

}// end class MyTable {}