📄 record.java
字号:
*/
private int saveWithInsert (Connection connection)
throws DataSetException, ConnectionException
{
PreparedStatement stmt = null;
try
{
stmt = connection.prepareStatement (getSaveString());
int ps = 1;
for (int i = 1; i <= size(); i++)
{
Value val = getValue (i);
if (! isValueClean(i) &&
! schema.getColumn(i).readOnly())
{
val.setPreparedStatementValue (stmt, ps++);
}
}
int ret = stmt.executeUpdate();
if (((TableDataSet) parentDataSet).isRefreshOnSave())
{
refresh (parentDataSet.connection());
}
else
{
// Marks all of the values clean since they have now been saved
markRecordClean();
}
setSaveType (Enums.AFTERINSERT);
if (ret > 1)
throw new ConnectionException ("There were " + ret + " rows inserted with this records key value.");
return ret;
}
catch (SQLException e1)
{
throw new ConnectionException (e1);
}
finally { try
{
if (stmt != null)
stmt.close();
}
catch (SQLException e2)
{
throw new ConnectionException (e2);
}
} }
/**
Saves the data in this Record to the database with an UPDATE statement
@return SQL UPDATE statement
*/
private int saveWithUpdate (Connection connection)
throws DataSetException, ConnectionException
{
PreparedStatement stmt = null;
try
{
stmt = connection.prepareStatement (getSaveString());
int ps = 1;
for (int i = 1; i <= size(); i++)
{
Value val = getValue (i);
if (! isValueClean(i) &&
! schema.getColumn(i).readOnly())
{
val.setPreparedStatementValue (stmt, ps++);
}
}
for (int i = 1; i <= parentDataSet.getKeyDef ().size(); i++)
{
//Value val = getValue (parentDataSet.getKeyDef ().getAttrib(i));
Value val = getOrigValue (parentDataSet.getKeyDef ().getAttrib(i));
if (val.isNull())
throw new DataSetException ("You cannot execute an update with a null value for a KeyDef.");
val.setPreparedStatementValue (stmt, ps++);
}
int ret = stmt.executeUpdate();
if (((TableDataSet) parentDataSet).isRefreshOnSave())
{
refresh (parentDataSet.connection());
}
else
{
// Marks all of the values clean since they have now been saved
markRecordClean();
}
setSaveType (Enums.AFTERUPDATE);
if (ret > 1)
throw new SQLException ("There were " + ret + " rows updated with this records key value.");
return ret;
}
catch (SQLException e1)
{
throw new ConnectionException (e1);
}
finally { try
{
if (stmt != null)
stmt.close();
}
catch (SQLException e2)
{
throw new ConnectionException (e2);
}
} }
/**
sets the internal save type as one of the defined privates (ie: ZOMBIE)
*/
void setSaveType (int type)
{
this.saveType = type;
}
/**
gets the internal save type as one of the defined privates (ie: ZOMBIE)
*/
int getSaveType ()
{
return (this.saveType);
}
/**
sets the value at pos with a byte[]
*/
public Record setValue (int pos, byte[] value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a double
*/
public Record setValue (int pos, double value) throws DataSetException
{
this.values[pos].setValue (new Double (value));
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a float
*/
public Record setValue (int pos, float value) throws DataSetException
{
this.values[pos].setValue (new Float (value));
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a int
*/
public Record setValue (int pos, int value) throws DataSetException
{
this.values[pos].setValue (new Integer (value));
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a long
*/
public Record setValue (int pos, long value) throws DataSetException
{
this.values[pos].setValue (new Long (value));
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a Value
*/
public Record setValue (int pos, Value value) throws DataSetException
{
this.values[pos].setValue (value.getValue());
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a String
*/
public Record setValue (int pos, String value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a BigDecimal
*/
public Record setValue (int pos,
BigDecimal value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a java.sql.Date
*/
public Record setValue (int pos,
java.sql.Date value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a java.sql.Time
*/
public Record setValue (int pos,
java.sql.Time value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a java.sql.Timestamp
*/
public Record setValue (int pos,
java.sql.Timestamp value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a java.util.Date
*/
public Record setValue (int pos,
java.util.Date value) throws DataSetException
{
this.values[pos].setValue (value);
markValueDirty (pos);
return this;
}
/**
sets the value at pos with a boolean
*/
public Record setValue (int pos, boolean value) throws DataSetException
{
this.values[pos].setValue (new Boolean (value));
markValueDirty (pos);
return this;
}
/**
sets the value at column name with a byte[]
*/
public Record setValue (String columnName,
byte[] value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a double
*/
public Record setValue (String columnName,
double value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a float
*/
public Record setValue (String columnName,
float value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a int
*/
public Record setValue (String columnName,
int value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a long
*/
public Record setValue (String columnName,
long value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a Value
*/
public Record setValue (String columnName,
Value value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a String
*/
public Record setValue (String columnName,
String value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a BigDecimal
*/
public Record setValue (String columnName,
BigDecimal value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a java.sql.Date
*/
public Record setValue (String columnName,
java.sql.Date value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a java.sql.Time
*/
public Record setValue (String columnName,
java.sql.Time value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a java.sql.Timestamp
*/
public Record setValue (String columnName,
java.sql.Timestamp value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a java.util.Date
*/
public Record setValue (String columnName,
java.util.Date value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at column name with a boolean
*/
public Record setValue (String columnName,
boolean value) throws DataSetException
{
setValue (schema.index(columnName), value);
return this;
}
/**
sets the value at pos with a NULL
*/
public Record setValueNull (int pos) throws DataSetException
{
if (pos == 0)
throw new DataSetException ("Value position must be greater than 0.");
else if (pos > size())
throw new DataSetException ("Value position is greater than number of values.");
this.values[pos].setValue (null);
markValueDirty (pos);
return this;
}
/**
sets the value at column name with a NULL
*/
public Record setValueNull (String columnName) throws DataSetException
{
if (columnName == null || columnName.length() == 0)
throw new DataSetException ("You must specify a column name!");
setValueNull(schema.index(columnName));
return this;
}
/**
the number of columns in this object
@return the number of columns in this object
*/
public int size()
{
return numberOfColumns;
}
/**
whether or not this Record is to be saved with an SQL delete statement
@return true if saved with delete
*/
public boolean toBeSavedWithDelete()
{
return (this.saveType == Enums.DELETE) ? true : false;
}
/**
whether or not this Record is to be saved with an SQL insert statement
@return true if saved with insert
*/
public boolean toBeSavedWithInsert()
{
return (this.saveType == Enums.INSERT) ? true : false;
}
/**
whether or not this Record is to be saved with an SQL update statement
@return true if saved with update
*/
public boolean toBeSavedWithUpdate()
{
return (this.saveType == Enums.UPDATE) ? true : false;
}
/**
* This returns a representation of this Record
* @return java.lang.String
*/
public String toString ()
{
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
PrintWriter out = new PrintWriter (bout);
out.print ("{");
try
{
for (int i = 1; i <= size (); i++)
{
out.print ("'" + getValue (i) + "'");
if (i < size ())
out.print (',');
}
}
catch (IOException ioe)
{}
out.print ("}");
out.flush ();
return bout.toString ();
}
/**
Unmarks a record that has been marked for deletion.
<P>
WARNING: You must reset the save type before trying to save this record again.
@see #markForUpdate()
@see #markForInsert()
@see #markToBeDeleted()
*/
public Record unmarkToBeDeleted() throws DataSetException
{
if (this.saveType == Enums.ZOMBIE)
throw new DataSetException ("This record has already been deleted!");
setSaveType (Enums.UNKNOWN);
return this;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -