Class SQLDataDescriptor
- All Implemented Interfaces:
DataDescriptor
SQL DataDescriptor
SQL DataDescriptor stores data in an SQL database and retrieves them from there using native SQL statements specified by the developer. When creating the DataDescriptor (either programmatically or from an XML file), you have to specify the SQL statements used by the DataDescriptor to fetch, save, modify or delete data. Non-specified statements result in an error. If you specify SelectStatements, then you can perform read operations only through the DataDescriptor, all other write or delete operations result in an error and are not performed.
You can specify any SQL statement for the DataDescriptor that is supported by the database, or you can use the
record’s attribute values in the specified SQL statements:
Example:
If you want to use the record’s ID in the WHERE part of the UPDATE statement and you want to update the rest of the data,
you can do it in the following way:
update table_name set column1 = :column1, column2 = :column2 where id = :_id;
Attributes can be used in SQL statements by attaching a colon character before the attribute name. Consequently, they are used as parameters in the specified SQL statement. The framework automatically passes the corresponding record value to these parameters when performing the query.
- Since:
- 4.0
- Author:
- JBStrap
- See Also:
-
Field Summary
Fields inherited from class com.jbstrap.core.meta.BaseDataDescriptor
access, columns, domainClass, name
-
Constructor Summary
ConstructorDescriptionSQLDataDescriptor
(String name) Creates a DataDescriptor with the specified name. -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a copy of the DataDescriptorlong
countData
(SQLTransaction transaction, Client client, Criteria criteria) Gets the number of the record in the database.find
(SQLTransaction transaction, Client client, Criteria criteria, List<Order> orders, int startRow, int endRow) Initiates a query by using the DataDescriptor.findById
(SQLTransaction transaction, Client client, Object... id) Fetches a record from the database using the record’s unique ID.Gets the SQL statement specified for an SQL DataDescriptor and used to delete a record.Gets the SQL statement used to insert a new record.Gets the SELECT statement specified for the SQL DataDescriptor.Gets the SQL statement specified for the SQL DataDescriptor and used to insert a record.Initiates a transaction with the DataDescriptor type.remove
(SQLTransaction transaction, Client client, List<Record> records) Initiates a delete operation by using a DataDescriptor.
Use the method to delete one or more records from the database using a DataDescriptor.save
(SQLTransaction transaction, Client client, List<Record> records) Initiates a save operation using a DataDescriptor.
Use the method to save one or more records to the database using the DataDescriptor.setDeleteStatement
(String deleteStatement) Sets an SQL statement used by the SQL DataDescriptor to delete a record.setInsertStatement
(String insertStatement) Sets the INSERT statement used to insert records in the database by the SQL DataDescriptor.setSelectStatement
(String selectStatement) Sets the SELECT statement specified for the SQL DataDescriptor.setUpdateStatement
(String updateStatement) Sets an UPDATE SQL statement for the SQL DataDescriptor.Methods inherited from class com.jbstrap.core.meta.BaseDataDescriptor
addColumn, addColumns, addParam, countData, countData, fetchData, fetchData, fetchData, fetchData, fetchData, fetchData, find, find, find, find, find, find, find, find, find, find, find, find, findById, findById, getAccess, getColumn, getColumns, getDefaultFilterCriteria, getName, getParam, getPrimaryKeyName, getPrimaryKeyNames, getRead, getWrite, isFilterable, remove, remove, remove, remove, remove, remove, remove, remove, remove, save, save, save, save, save, save, save, save, save, setAccess, setDefaultFilterCriteria, setParam, setRead, setWrite, sortColumn
-
Constructor Details
-
SQLDataDescriptor
Creates a DataDescriptor with the specified name.- Parameters:
name
- Unique SQL DataDescriptor name- Throws:
NullPointerException
- Thrown ifnull
is specified for the DataDescriptor name
-
-
Method Details
-
getSelectStatement
Gets the SELECT statement specified for the SQL DataDescriptor.- Returns:
- SQL statement used by the DataDescriptor or
null
if no SELECT statement was specified
-
setSelectStatement
Sets the SELECT statement specified for the SQL DataDescriptor. The specified SQL statement is used by the DataDescriptor to fetch data if you use a DataDescriptor to fetch data from the database.- Parameters:
selectStatement
- The SQL statement used for the query Ifnull
is specified, you cannot use a DataDescriptor to fetch data from the database.- Returns:
- SQL DataDescriptor instance
-
getInsertStatement
Gets the SQL statement used to insert a new record.- Returns:
- An INSERT statement specified for the DataDescriptor or
null
if there was no INSERT statement specified
-
setInsertStatement
Sets the INSERT statement used to insert records in the database by the SQL DataDescriptor. The specified SQL statement uses the DataDescriptor to create the new record. In the specified SQL statement, you can use the attributes in the record. Specify these attributes in the following format::attribute_name
.- Parameters:
insertStatement
- The SQL statement used by the DataDescriptor to insert data Ifnull
is specified, you cannot insert a new record into the database by using a DataDescriptor.- Returns:
- SQL DataDescriptor instance
-
getUpdateStatement
Gets the SQL statement specified for the SQL DataDescriptor and used to insert a record.- Returns:
- An UPDATE SQL statement specified for the DataDescriptor or
null
if there was no UPDATE SQL statement specified for the DataDescriptor
-
setUpdateStatement
Sets an UPDATE SQL statement for the SQL DataDescriptor. The specified SQL statement is used by The DataDescriptor to update a record. In the specified SQL statement, you can use the attributes in the record. Specify these attributes in the following format::attribute_name
.- Parameters:
updateStatement
- The SQL statement used by the DataDescriptor to update a record Ifnull
is specified, you cannot update a record using a DataDescriptor.- Returns:
- SQL DataDescriptor instance
-
getDeleteStatement
Gets the SQL statement specified for an SQL DataDescriptor and used to delete a record.- Returns:
- A DELETE statement specified for the DataDescriptor or
null
if there was no DELETE statement specified
-
setDeleteStatement
Sets an SQL statement used by the SQL DataDescriptor to delete a record. In the specified SQL statement, you can use the attributes in the record. Specify these attributes in the following format::attribute_name
.- Parameters:
deleteStatement
- The SQL statement used by the DataDescriptor to delete a record- Returns:
- SQL DataDescriptor instance
-
find
public List<Record> find(SQLTransaction transaction, Client client, Criteria criteria, List<Order> orders, int startRow, int endRow) throws FindException, DataAccessException Initiates a query by using the DataDescriptor.- Specified by:
find
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Parameters:
transaction
- Transaction object used to initiated the query Ifnull
is specified, the query does not use a transaction (it takes place outside of a transaction).client
- The client instance initiating the query Ifnull
is specified, user access to the data is not checked.startRow
- The index of the first rowendRow
- The index of the last rowcriteria
-Criteria
instance used as a filter criterion Ifnull
is specified, the query fetches all records from the database without filtering.orders
- A list containingOrder
objects used to the order the query results Ifnull
or an empty list is passed, the query results are not ordered.- Returns:
- A list of
Record
instances returned by the query - Throws:
FindException
- Occurs if an error was encountered during the queryDataAccessException
- Occurs if the user in the specified client has no access to read the data
-
countData
public long countData(SQLTransaction transaction, Client client, Criteria criteria) throws RowCountException, DataAccessException Gets the number of the record in the database.- Specified by:
countData
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Parameters:
transaction
- Transaction object used to initiated the query Ifnull
is specified, the query does not use a transaction (it takes place outside of a transaction).client
- The client instance initiating the query Ifnull
is specified, user access to the data is not checked.criteria
-Criteria
instance used as a filter criterion Ifnull
is specified, the query fetches all records from the database without filtering.- Returns:
- The number of records corresponding to the specified criteria If no records correspond to the specified criteria, the method returns 0.
- Throws:
RowCountException
- Occurs if an error was encountered during the execution of the SQL instruction that does the counting.DataAccessException
- Occurs if the logged in user has no read access to the DataDescriptor
-
save
public List<Record> save(SQLTransaction transaction, Client client, List<Record> records) throws SaveException, DataAccessException Initiates a save operation using a DataDescriptor.
Use the method to save one or more records to the database using the DataDescriptor.- Specified by:
save
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Parameters:
transaction
- Transaction object used to initiated the query Ifnull
is specified, the query does not use a transaction (it takes place outside of a transaction).client
- The client instance initiating the query Ifnull
is specified, user access to the data is not checked.records
- A list of records to be saved- Returns:
- The saved record
- Throws:
SaveException
- Occurs if an error was encountered during the save.DataAccessException
- Occurs if the user on the specified client has no permission to save the record to the DataDescriptor.
-
remove
public List<Record> remove(SQLTransaction transaction, Client client, List<Record> records) throws RemoveException, DataAccessException Initiates a delete operation by using a DataDescriptor.
Use the method to delete one or more records from the database using a DataDescriptor.- Specified by:
remove
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Parameters:
transaction
- Transaction object used to initiated the query Ifnull
is specified, the query does not use a transaction (it takes place outside of a transaction).client
- The client instance initiating the query Ifnull
is specified, user access (and whether the user has rights to write the data) to the data is not checked.records
- A list of records to be deleted- Throws:
RemoveException
- Occurs if an error was encountered during the removal processDataAccessException
- Occurs if the user on the specified client has no access to write the data of the DataDescripor.
-
findById
public Record findById(SQLTransaction transaction, Client client, Object... id) throws FindException, DataAccessException Fetches a record from the database using the record’s unique ID.- Specified by:
findById
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Parameters:
transaction
- Transaction object used to initiated the query Ifnull
is specified, the query does not use a transaction (it takes place outside of a transaction).client
- The client instance initiating the query Ifnull
is specified, user access to the data is not checked.id
- Record ID- Throws:
FindException
- Occurs if an error was encountered during the queryDataAccessException
- Occurs if the user in the specified client has no access to query the data.
-
openTransaction
Initiates a transaction with the DataDescriptor type.- Specified by:
openTransaction
in interfaceDataDescriptor
- Specified by:
openTransaction
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Returns:
- New database transaction object
-
copy
Creates a copy of the DataDescriptor- Specified by:
copy
in interfaceDataDescriptor
- Overrides:
copy
in classBaseDataDescriptor<SQLDataDescriptor,
SQLTransaction> - Returns:
- A new DataDescriptor, that is a perfect copy of the DataDescriptor
-