Package org.kissweb.database
Class Command
java.lang.Object
org.kissweb.database.Command
- All Implemented Interfaces:
AutoCloseable
This class represents a single statement or command against a database. Of course, each Connection may have
many Command instances in play at a time. New Command instances may by obtained via the Connection class as follows:
where
Command cmd = db.newCommand();
where
db is a Connection instance.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This closes the Command instance.booleanExecute non-select statement.booleanReturnstrueif there are any records matching the given SQL statement andfalseotherwise.Fetch all (but no more thanmax) records and then close the Kiss cursor.Fetch all (but no more thanmax) records and then close the Kiss cursor.Fetch all the records and close the Kiss cursor.org.json.JSONArrayfetchAllJSON(int page, int max, String sql, Object... args) This method is the same asfetchAllexcept that it returns a JSON array of the records.org.json.JSONArrayfetchAllJSON(int max, String sql, Object... args) This method is the same asfetchAllexcept that it returns a JSON array of the records.org.json.JSONArrayfetchAllJSON(String sql, Object... args) This method is the same asfetchAllexcept that it returns the list of records as a JSON array of JSON objects where each object represents a column.Read in the first record and then close the Kiss cursor.org.json.JSONObjectfetchOneJSON(String sql, Object... args) This method is the same asfetchOneexcept that it returns a JSON object.org.json.JSONObjectfetchOneJSON(org.json.JSONObject obj, String sql, Object... args) This method is the same asfetchOneexcept that it adds the columns to en existing JSON object.Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row.Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row.Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row.
-
Method Details
-
execute
Execute non-select statement. This is useful, for example, for UPDATE, INSERT, and DELETE SQL statements.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArrayList) that represents the parameters rather than an in-line list of parameters.- Parameters:
sql- the sql statement with ? parametersargs- the parameter values- Returns:
- false is a normal result
- Throws:
SQLException- See Also:
-
query
Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row. This is useful when a large number of records is possible and fetching all into memory at one time is unneeded. This can save a significant amount of memory since only one record is in memory at a time.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArrayList) that represents the parameters rather than an in-line list of parameters.- Parameters:
sql- the sql statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionIOException- See Also:
-
query
Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row.
The maximum number of records returned is given bymax. This is useful when a large number of records is possible and fetching all into memory at one time is unneeded. This can save a significant amount of memory since only one record is in memory at a time.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArrayList) that represents the parameters rather than an in-line list of parameters.- Parameters:
max-sql- the sql statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionIOException- See Also:
-
query
Execute a select statement returning a Kiss Cursor (not a database cursor) that may be used to obtain each subsequent row. This version is used for paging results. The total result set is broken down intomaxsized pages (starting at zero) You can then choose which group ofmaxrecords wanted.
Thepageparameter selects the desired page of results (starting at zero).
The maximum number of records returned is given bymax. This is useful when a large number of records is possible and fetching all into memory at one time is unneeded. This can save a significant amount of memory since only one record is in memory at a time.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArrayList) that represents the parameters rather than an in-line list of parameters.- Parameters:
page- starting at zeromax-sql- the sql statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionIOException- See Also:
-
fetchOne
Read in the first record and then close the Kiss cursor. The record can be updated or deleted if it was a single-table select and the primary key was selected.
Adding code to the SQL statement telling the database to limit its result set to one record doesn't affect the result but it can make the query significantly faster.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArray) that represents the parameters rather than an in-line list of parameters.- Parameters:
sql- SQL statement with ? parametersargs- the parameter values- Returns:
- the Record or null if none
- Throws:
SQLExceptionException- See Also:
-
fetchOneJSON
This method is the same asfetchOneexcept that it returns a JSON object.- Parameters:
sql-args-- Returns:
- the JSON object or null if no record
- Throws:
SQLExceptionException- See Also:
-
fetchOneJSON
public org.json.JSONObject fetchOneJSON(org.json.JSONObject obj, String sql, Object... args) throws Exception This method is the same asfetchOneexcept that it adds the columns to en existing JSON object.- Parameters:
obj- the JSON object that is to be added tosql-args-- Returns:
- the JSON object passed in
- Throws:
SQLExceptionException- See Also:
-
fetchAll
Fetch all the records and close the Kiss cursor. The records can be updated or deleted if there was a single-table select and the primary key was selected.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArray) that represents the parameters rather than an in-line list of parameters.
If no records are found, an empty list is returned.- Parameters:
sql- SQL statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionException- See Also:
-
fetchAllJSON
This method is the same asfetchAllexcept that it returns the list of records as a JSON array of JSON objects where each object represents a column.- Parameters:
sql-args-- Returns:
- Throws:
SQLExceptionException- See Also:
-
fetchAll
Fetch all (but no more thanmax) records and then close the Kiss cursor. The records can be updated or deleted if there was a single-table select and the primary key was selected.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArray) that represents the parameters rather than an in-line list of parameters.
If no records are found, an empty list is returned.- Parameters:
max-sql- SQL statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionException- See Also:
-
fetchAll
Fetch all (but no more thanmax) records and then close the Kiss cursor.
pagedetermine which block ofmaxrecords to read.pagestarts at zero and is used for paging output.
The records can be updated or deleted if there was a single-table select and the primary key was selected.
The SQL string may contain parameters indicated by the '?' character. A variable number of arguments to this method are used to fill those parameters. Each argument gets applied to each '?' parameter in the same order as they appear in the SQL statement. An SQL prepared statement is used.
This method normally takes a variable argument list representing the consecutive parameters. However, this method also accepts a single argument (which must be anArray) that represents the parameters rather than an in-line list of parameters.
If no records are found, an empty list is returned.- Parameters:
page- starting at zeromax-sql- SQL statement with ? parametersargs- the parameter values- Returns:
- Throws:
SQLExceptionException- See Also:
-
fetchAllJSON
This method is the same asfetchAllexcept that it returns a JSON array of the records.- Parameters:
max-sql-args-- Returns:
- Throws:
SQLExceptionException- See Also:
-
fetchAllJSON
public org.json.JSONArray fetchAllJSON(int page, int max, String sql, Object... args) throws Exception This method is the same asfetchAllexcept that it returns a JSON array of the records.- Parameters:
max-sql-args-- Returns:
- Throws:
SQLExceptionException- See Also:
-
exists
Returnstrueif there are any records matching the given SQL statement andfalseotherwise.- Parameters:
sql-args-- Returns:
- Throws:
Exception
-
close
public void close()This closes the Command instance. This need not be done manually since this class implements the AutoCloseable interface.- Specified by:
closein interfaceAutoCloseable
-