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 TypeMethodDescriptionboolean
Inserts 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.JSONObject
addToJSON
(org.json.JSONObject obj) Add columns of a Record to an existing JSON object.clear()
Erases all the column information associated with aRecord
instance.void
close()
Closes any open prepared statements against this record.boolean
columnExists
(String cname) Tests if a given column exists.void
Copy all columns from rec to 'this'void
copyCorresponding
(Record fromRec) Copy all columns fromfromRec
tothis
that have the same column names and typesvoid
delete()
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 theBoolean
value of the named column.byte[]
getByteArray
(String cname) Return thebyte[]
value of the named column.Return theCharacter
value of the named column.Returns theConnection
instance associated to thisRecord
instance.int
getDateAsInt
(String cname) Return the date in an int formatted as YYYYMMDD for the named column.getDateOnly
(String cname) Return thejava.util.Date
value of the named column.getDateTime
(String cname) Return thejava.util.Date
value of the named column.long
getDateTimeMS
(String cname) Return the date/time value as the number of milliseconds since 1970 UTC.Return theDouble
value of the named column.Return theFloat
value of the named column.Return theInteger
value of the named column.Return theLong
value of the named column.Return theShort
value of the named column.Return theString
value of the named column.Returns the name of the table associated to thisRecord
instance.long
Return thelong
value of the named column.getZonedDateTime
(String cname) Return theZonedDateTime
value 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.JSONObject
toJSON()
Convert a record into a JSON object.static org.json.JSONArray
toJSONArray
(List<Record> recs) Utility method to convert a list of Record's into a JSON array of JSON objects.void
update()
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 theBoolean
value of the named column.- Parameters:
cname
-- Throws:
SQLException
-
columnExists
Tests if a given column exists. Returnstrue
of the column exists, andfalse
if it does not.- Parameters:
cname
-- Returns:
- Throws:
SQLException
-
getShort
Return theShort
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getInt
Return theInteger
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getLong
Return theLong
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getFloat
Return theFloat
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getDouble
Return theDouble
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getDateOnly
Return thejava.util.Date
value of the named column. Just a date and no time. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getDateAsInt
Return the date in an int formatted as YYYYMMDD for the named column. A0
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- date format YYYYMMDD
- Throws:
SQLException
- See Also:
-
getDateTime
Return thejava.util.Date
value of the named column. If the column had timezone information, it is stripped off. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getZonedDateTime
Return theZonedDateTime
value of the named column. Time zone information is preserved. Anull
is returned onnull
valued 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. A0
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getTime
Return thelong
value of the named column. Only time information is returned. A0
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- time in milliseconds
- Throws:
SQLException
- See Also:
-
getString
Return theString
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getChar
Return theCharacter
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
getByteArray
Return thebyte[]
value of the named column. Anull
is returned onnull
valued columns.- Parameters:
cname
-- Returns:
- Throws:
SQLException
- See Also:
-
clear
Erases all the column information associated with aRecord
instance.- 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 fromfromRec
tothis
that 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:
close
in interfaceAutoCloseable
-
getConnection
Returns theConnection
instance associated to thisRecord
instance.- Returns:
-
getTableName
Returns the name of the table associated to thisRecord
instance.- 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:
-