⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 record.java

📁 Town是一个100% 纯Java API
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.workingdogs.town;

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

/*
Town, a Java JDBC abstraction layer
Copyright (C) 1999  Serge Knystautas, Jon S. Stevens

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*/
/**
A Record represents a row in the database. It contains a collection of
<a href="Value.html">Values</A> which are the individual contents of each column in the row.

@author Jon S. Stevens <A HREF="mailto:jon@working-dogs.com">jon@working-dogs.com</A>
@author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
@version 1.0
*/
public class Record
{
    /** an array of Value objects, this is 1 based*/
    private Value values[];
	/** an array of Value objects, 1 based, storing the original keydef values in case they've been changed*/
	private Value keydefValues[];
    /** a 1 To 1 relationship between Values and whether they are clean or not */
    private boolean isClean[];
    /** the parent DataSet for this Record */
    private DataSet parentDataSet;
    /** number of columns in this Record */
    private int numberOfColumns;
    /** this is the state of this record */
    private int saveType = 0;
    /** a saved copy of the schema for this Record */
    private Schema schema;
    /** the status of a record */
    //private boolean status;
    /**
       Creates a new Record and sets the parent dataset to the passed in value.
     */
    protected Record(DataSet ds) throws DataSetException,
    ConnectionException
    {
        this (ds, true);
    }
    /**
        Creates a new Record and sets the parent dataset to the passed in value.
      */
    protected Record(DataSet ds,
            boolean readData) throws DataSetException, ConnectionException
    {
        parentDataSet = ds;
        initializeRecord();
        if (readData)
            createValues(parentDataSet.resultSet());
        else
            createValues (null);
    }
    /**
       *
       * Creates the value objects for this Record. It is 1 based
       *
       * @exception   DataSetException
       * @exception   ConnectionException
       */
    private void createValues(ResultSet rs) throws DataSetException,
    ConnectionException
    {
        for (int i = 1; i <= size(); i++)
        {
            Value val = new Value (rs, i, schema.getColumn(i).typeEnum());
            this.values[i] = val;
        }
				
		if (parentDataSet instanceof TableDataSet)
		{
	        KeyDef kd = parentDataSet.getKeyDef();
			for (int i = 1; i <= kd.size(); i++)
			{
				String attrib = kd.getAttrib(i);
				int index = schema.index (attrib);
				this.keydefValues[i] = (Value)this.values[index].clone ();
			}
		}
    }
    private Hashtable getAffectedColumns() throws DataSetException
    {
        Hashtable affectedColumns = new Hashtable (size());
        for (int i = 1; i <= size(); i++)
        {
            if (isValueClean(i) == false)
                affectedColumns.put ((Object) new Integer(i),
                        (Object) schema.getColumns()[i].name());
        }
        return affectedColumns;
    }
    /**
      * Get the value as a BigDecimal
      * @return java.math.BigDecimal
      */
    public BigDecimal getAsBigDecimal (int index) throws DataSetException
    {
        return getValue (index).asBigDecimal ();
    }
    /**
      * Get the value as a BigDecimal
      * @return java.math.BigDecimal
      */
    public BigDecimal getAsBigDecimal (int index,
            int scale) throws DataSetException
    {
        return getValue (index).asBigDecimal (scale);
    }
    /**
      * Get the value as a BigDecimal
      * @return java.math.BigDecimal
      */
    public BigDecimal getAsBigDecimal (String column)
            throws DataSetException
    {
        return getValue (column).asBigDecimal ();
    }
    /**
      * Get the value as a BigDecimal
      * @return java.math.BigDecimal
      */
    public BigDecimal getAsBigDecimal (String column,
            int scale) throws DataSetException
    {
        return getValue (column).asBigDecimal (scale);
    }
    /**
      * Get the value as a boolean
      * @return boolean
      */
    public boolean getAsBoolean (int index) throws DataSetException
    {
        return getValue (index).asBoolean ();
    }
    /**
      * Get the value as a boolean
      * @return boolean
      */
    public boolean getAsBoolean (String column) throws DataSetException
    {
        return getValue (column).asBoolean ();
    }
    /**
      * Get the value as a byte
      * @return byte
      */
    public byte getAsByte (int index) throws DataSetException
    {
        return getValue (index).asByte ();
    }
    /**
      * Get the value as a byte
      * @return byte
      */
    public byte getAsByte (String column) throws DataSetException
    {
        return getValue (column).asByte ();
    }
    /**
      * Get the value as bytes
      * @return a byte array
      */
    public byte[] getAsBytes (int index) throws DataSetException
    {
        return getValue (index).asBytes ();
    }
    /**
      * Get the value as bytes
      * @return a byte array
      */
    public byte[] getAsBytes (String column) throws DataSetException
    {
        return getValue (column).asBytes ();
    }
    /**
      * Get the value as a Date
      * @return a java.sql.Date
      */
    public java.sql.Date getAsDate (int index) throws DataSetException
    {
        return getValue (index).asDate ();
    }
    /**
      * Get the value as a Date
      * @return a java.sql.Date
      */
    public java.sql.Date getAsDate (String column) throws DataSetException
    {
        return getValue (column).asDate ();
    }
    /**
      * Get the value as a Double
      * @return a double
      */
    public double getAsDouble (int index) throws DataSetException
    {
        return getValue (index).asDouble ();
    }
    /**
      * Get the value as a Double
      * @return a double
      */
    public double getAsDouble (String column) throws DataSetException
    {
        return getValue (column).asDouble ();
    }
    /**
      * Get the value as a float
      * @return a float
      */
    public float getAsFloat (int index) throws DataSetException
    {
        return getValue (index).asFloat ();
    }
    /**
      * Get the value as a float
      * @return a float
      */
    public float getAsFloat (String column) throws DataSetException
    {
        return getValue (column).asFloat ();
    }
    /**
      * Get the value as an int
      * @return an int
      */
    public int getAsInt (int index) throws DataSetException
    {
        return getValue (index).asInt ();
    }
    /**
      * Get the value as an int
      * @return an int
      */
    public int getAsInt (String column) throws DataSetException
    {
        return getValue (column).asInt ();
    }
    /**
      * Get the value as a long
      * @return a long
      */
    public long getAsLong (int index) throws DataSetException
    {
        return getValue (index).asLong ();
    }
    /**
      * Get the value as a long
      * @return a long
      */
    public long getAsLong (String column) throws DataSetException
    {
        return getValue (column).asLong ();
    }
    /**
      * Get the value as a short
      * @return a short
      */
    public short getAsShort (int index) throws DataSetException
    {
        return getValue (index).asShort ();
    }
    /**
      * Get the value as a short
      * @return a short
      */
    public short getAsShort (String column) throws DataSetException
    {
        return getValue (column).asShort ();
    }
    /**
      * Get the value as a String
      * @return a String
      */
    public String getAsString (int index) throws DataSetException
    {
        return getValue (index).asString ();
    }
    /**
      * Get the value as a String
      * @return a String
      */
    public String getAsString (String column) throws DataSetException
    {
        return getValue (column).asString ();
    }
    /**
      * Get the value as a Time
      * @return a Time
      */
    public Time getAsTime (int index) throws DataSetException
    {
        return getValue (index).asTime ();
    }
    /**
      * Get the value as a Time
      * @return a Time
      */
    public Time getAsTime (String column) throws DataSetException
    {
        return getValue (column).asTime ();
    }
    /**
      * Get the value as a Timestamp
      * @return a Timestamp
      */
    public Timestamp getAsTimestamp (int index) throws DataSetException
    {
        return getValue (index).asTimestamp ();
    }
    /**
      * Get the value as a Timestamp
      * @return a Timestamp
      */
    public Timestamp getAsTimestamp (String column) throws DataSetException
    {
        return getValue (column).asTimestamp ();
    }
    /**
      * Get the value as a java.util.Date
      * @return a java.util.Date
      */
    public java.util.Date getAsUtilDate (int index) throws DataSetException
    {
        return getValue (index).asUtilDate ();
    }
    /**
      * Get the value as a java.util.Date
      * @return a java.util.Date
      */
    public java.util.Date getAsUtilDate (String column)
            throws DataSetException
    {
        return getValue (column).asUtilDate ();
    }
    /**
        Gets the DataSet for this Record

        @return the DataSet for this Record
      */
    public DataSet getDataSet()
    {
        return parentDataSet;
    }
    /**
        Builds the SQL DELETE statement for this Record
        @return SQL DELETE statement
      */
    private String getDeleteSaveString() throws DataSetException,
    ConnectionException
    {
        KeyDef kd = parentDataSet.getKeyDef();
        if (kd == null || kd.size() == 0)
        {
            throw new DataSetException ("You must specify KeyDef attributes for this TableDataSet in order to delete a Record.");
        }

        StringBuffer iss1 = new StringBuffer (256);

        boolean comma = false;
        for (int i = 1; i <= kd.size(); i++)
        {
            if (! comma)
            {
                iss1.append (kd.getAttrib(i));
                iss1.append (" = ?");
                comma = true;
            }
            else
            {
                iss1.append (" AND ");
                iss1.append (kd.getAttrib(i));
                iss1.append (" = ? ");
            }
        }
        return "DELETE FROM " + parentDataSet.getTableName () +
                " WHERE " + iss1.toString();
    }
    /**
        Builds the SQL INSERT statement for this Record
        @return SQL INSERT statement
      */
    private String getInsertSaveString() throws DataSetException,
    ConnectionException
    {
        StringBuffer iss1 = new StringBuffer (256);
        StringBuffer iss2 = new StringBuffer (256);

        boolean comma = false;
        for (int i = 1; i <= size(); i++)
        {
            Value val = getValue (i);
            if (! isValueClean(i) &&
                    ! schema.getColumn (i).readOnly())
            {
                if (! comma)
                {
                    iss1.append (schema.getColumn(i).name());
                    iss2.append ("?");
                    comma = true;
                }
                else
                {
                    iss1.append (", " + schema.getColumn(i).name());
                    iss2.append (", ?");
                }
            }
        }
        return "INSERT INTO " + parentDataSet.getTableName () + " ( " +
                iss1.toString() + " ) VALUES ( " + iss2.toString() + " )";
    }

    /**
       * This builds the SELECT statement in order to refresh the contents of
       * this Record. It depends on a valid KeyDef to exist and it must have been
       * created with a TableDataSet.
       *
       * @return     the SELECT string
       * @exception   DataSetException
       */
    public String getRefreshQueryString() throws DataSetException,
    ConnectionException
    {
        if (parentDataSet.getKeyDef() == null ||
                parentDataSet.getKeyDef ().size() == 0)
            throw new DataSetException ("You can only perform a getRefreshQueryString on a TableDataSet that was created with a KeyDef.");
        else if (parentDataSet instanceof QueryDataSet)
            throw new DataSetException ("You can only perform a getRefreshQueryString on Records created with a TableDataSet.");

        StringBuffer iss1 = new StringBuffer (256);
        StringBuffer iss2 = new StringBuffer (256);
        boolean comma = false;

        for (int i = 1; i <= size(); i++)
        {
            if (! comma)
            {
                iss1.append (schema.getColumn(i).name());
                comma = true;
            }
            else
            {
                iss1.append (", ");
                iss1.append (schema.getColumn(i).name());
            }
        }

        comma = false;

        for (int i = 1; i <= parentDataSet.getKeyDef ().size(); i++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -