Package org.kissweb.database
Class Record
java.lang.Object
org.kissweb.database.Record
- All Implemented Interfaces:
AutoCloseable
Instances of this class represent a single row in a table or a row in a result set.
There are two ways of getting instances of this class. The first way is by doing a select against the database. In that case, a
where
There are two ways of getting instances of this class. The first way is by doing a select against the database. In that case, a
Record or list of Record's
is returned. The other way is to create a new row or record for insertion into the database.
In that case, one should create the new Record instance as follows:
Record rec = db.newRecord('myTable');
where
db is a Connection instance.-
Method Summary
Modifier and TypeMethodDescriptionbooleanInserts a new row into the database by creating an executing an SQL statement.Works like addRecord() except that it returns the value of the serial/primary key column that the database used for the new record.org.json.JSONObjectaddToJSON(org.json.JSONObject obj) Add columns of a Record to an existing JSON object.clear()Erases all the column information associated with aRecordinstance.voidclose()Closes any open prepared statements against this record.booleancolumnExists(String cname) Tests if a given column exists.voidCopy all columns from rec to 'this'voidcopyCorresponding(Record fromRec) Copy all columns fromfromRectothisthat have the same column names and typesvoiddelete()Performs an SQL delete on the record.Get the value of a column as anObject.Returns a map of all the columns in the record.getBoolean(String cname) Return theBooleanvalue of the named column.byte[]getByteArray(String cname) Return thebyte[]value of the named column.Return theCharactervalue of the named column.Returns theConnectioninstance associated to thisRecordinstance.intgetDateAsInt(String cname) Return the date in an int formatted as YYYYMMDD for the named column.getDateOnly(String cname) Return thejava.util.Datevalue of the named column.getDateTime(String cname) Return thejava.util.Datevalue of the named column.longgetDateTimeMS(String cname) Return the date/time value as the number of milliseconds since 1970 UTC.Return theDoublevalue of the named column.Return theFloatvalue of the named column.Return theIntegervalue of the named column.Return theLongvalue of the named column.Return theShortvalue of the named column.Return theStringvalue of the named column.Returns the name of the table associated to thisRecordinstance.longReturn thelongvalue of the named column.getZonedDateTime(String cname) Return theZonedDateTimevalue of the named column.Set the value of a column in the record.setDateOnly(String name, int dat) Set the date portion of the Date only (no time info) value of a column in the record.setDateOnly(String name, Date val) Set the date portion of the Date only (no time info) value of a column in the record.setDateTime(String name, long val) Set the date and time value of a column in the record.setDateTime(String name, ZonedDateTime val) Set the date and time value of a column in the record.setDateTime(String name, Date val) Set the date and time value of a column in the record.Set the time value in millisecondsorg.json.JSONObjecttoJSON()Convert a record into a JSON object.static org.json.JSONArraytoJSONArray(List<Record> recs) Utility method to convert a list of Record's into a JSON array of JSON objects.voidupdate()Performs an SQL update on the record.
-
Method Details
-
set
Set the value of a column in the record. This method should not be used with dates or times.- Parameters:
name- the column nameval- the value to set. Can be any type.- Returns:
- See Also:
-
setDateOnly
Set the date portion of the Date only (no time info) value of a column in the record.- Parameters:
name- the column nameval- the value to set.- Returns:
- See Also:
-
setDateOnly
Set the date portion of the Date only (no time info) value of a column in the record.- Parameters:
name- the column namedat- the value to set. Format is YYYYMMDD- Returns:
- See Also:
-
setTime
Set the time value in milliseconds- Parameters:
name- the column namedat- the value to set in milliseconds- Returns:
- See Also:
-
setDateTime
Set the date and time value of a column in the record.- Parameters:
name- the column nameval- the value to set.- Returns:
- See Also:
-
setDateTime
Set the date and time value of a column in the record. This is especially useful for columns with timezone information.
If the underlying database does not actually store a timezone (like PostgreSQL) then this method will fail.- Parameters:
name- the column nameval- the value to set.- Returns:
- See Also:
-
setDateTime
Set the date and time value of a column in the record.- Parameters:
name- the column nameval- number ov milliseconds since 1970 UTC- Returns:
- See Also:
-
get
Get the value of a column as anObject. Other methods that get expected types are typically used over this method.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getBoolean
Return theBooleanvalue of the named column.- Parameters:
cname-- Throws:
SQLException
-
columnExists
Tests if a given column exists. Returnstrueof the column exists, andfalseif it does not.- Parameters:
cname-- Returns:
- Throws:
SQLException
-
getShort
Return theShortvalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getInt
Return theIntegervalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getLong
Return theLongvalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getFloat
Return theFloatvalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getDouble
Return theDoublevalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getDateOnly
Return thejava.util.Datevalue of the named column. Just a date and no time. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getDateAsInt
Return the date in an int formatted as YYYYMMDD for the named column. A0is returned onnullvalued columns.- Parameters:
cname-- Returns:
- date format YYYYMMDD
- Throws:
SQLException- See Also:
-
getDateTime
Return thejava.util.Datevalue of the named column. If the column had timezone information, it is stripped off. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getZonedDateTime
Return theZonedDateTimevalue of the named column. Time zone information is preserved. Anullis returned onnullvalued columns.
If the underlying database does not actually store a timezone (like PostgreSQL) then this method will fail.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getDateTimeMS
Return the date/time value as the number of milliseconds since 1970 UTC. A0is returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getTime
Return thelongvalue of the named column. Only time information is returned. A0is returned onnullvalued columns.- Parameters:
cname-- Returns:
- time in milliseconds
- Throws:
SQLException- See Also:
-
getString
Return theStringvalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getChar
Return theCharactervalue of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
getByteArray
Return thebyte[]value of the named column. Anullis returned onnullvalued columns.- Parameters:
cname-- Returns:
- Throws:
SQLException- See Also:
-
clear
Erases all the column information associated with aRecordinstance.- Returns:
- this
-
update
Performs an SQL update on the record. This is done by creating an actual update statement and executing it against the database. It does not affect any cursors.
This method is smart. It only updates the fields that have changed, and if none have, it does nothing.- Throws:
SQLException- See Also:
-
delete
Performs an SQL delete on the record. This is done by creating an actual update statement and executing it against the database. It does not affect any cursors.- Throws:
SQLException- See Also:
-
addRecordAutoInc
Works like addRecord() except that it returns the value of the serial/primary key column that the database used for the new record.- Returns:
- an Object - should be cast to (short), (int), or (long) depending on the serial type
- Throws:
SQLException- See Also:
-
addRecord
Inserts a new row into the database by creating an executing an SQL statement. It does not affect any cursors.- Returns:
- Throws:
SQLException- See Also:
-
copyCorresponding
Copy all columns fromfromRectothisthat have the same column names and types- Parameters:
fromRec- the record to be copied from- Throws:
SQLException
-
copy
Copy all columns from rec to 'this'- Parameters:
rec- the record to be copied from
-
close
public void close()Closes any open prepared statements against this record. It is not normally needed since this class implements the AutoCloseable interface.- Specified by:
closein interfaceAutoCloseable
-
getConnection
Returns theConnectioninstance associated to thisRecordinstance.- Returns:
-
getTableName
Returns the name of the table associated to thisRecordinstance.- Returns:
-
getAllColumns
Returns a map of all the columns in the record.- Returns:
-
toJSON
public org.json.JSONObject toJSON()Convert a record into a JSON object.- Returns:
-
addToJSON
public org.json.JSONObject addToJSON(org.json.JSONObject obj) Add columns of a Record to an existing JSON object.- Returns:
-
toJSONArray
Utility method to convert a list of Record's into a JSON array of JSON objects.- Parameters:
recs-- Returns:
-