|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface RDBMSView
View for Relational Database Management Systems.
This View is an interface that represents a collection of data items
organized as a set of formal-ly-described tables. You can take advantage of
this View to query and update with SQL statements relational databases like
Oracle, MySQL, DB2, etc. Two basic operations are allowed here:
INSERT, UPDATE, or DELETE statement or
an SQL statement that returns nothing, such as an SQL DDL statement (for
example: CREATE TABLE).SELECT statement, to retrieve data from the
relational database.
// Get an instance of a RDBMS View interface.
RDBMSView view = (RDBMSView) datastoreService.getView(...);
// Connect the Data Store.
view.connect();
// Get an instance of a RDBMS View interface.
RDBMSView view = (RDBMSView) datastoreService.getView(...);
// Connect the Data Store.
view.connect();
// Begin a transaction in the database management system.
view.beginTransaction();
// Get an instance of a RDBMS View interface.
RDBMSView view = (RDBMSView) datastoreService.getView(...);
// Connect the Data Store.
view.connect();
// Begin a transaction in the database management system.
view.beginTransaction();
// Create the SQL statement to execute.
String sql = "INSERT INTO HOME_USERS (ID, NAME) VALUES (1, 'John Wood')";
// Run the SQL update statement.
view.executeUpdate(sql, null);
// SQL statement to create first user.
String sql1 = "INSERT INTO HOME_USERS (ID, NAME) VALUES (1, 'John Wood')";
// SQL statement to create second user.
String sql2 = "INSERT INTO HOME_USERS (ID, NAME) VALUES (2, 'James Sharpe')";
// SQL statement to create third user.
String sql3 = "INSERT INTO HOME_USERS (ID, NAME) VALUES (3, 'Sofia Green')";
// Execute three SQL update statements at once.
view.executeUpdate(sql1 + ";" + sql2 + ";" + sql3, new Character(';'));
create-user.sql, delete-user.sql,
update-user.sql, etc. Later on we can read these text files with
a Provider (Warework recommends you to use the FileText Provider for this
task) and use this Provider to read the statements for a View. Remember that
you can define a default Provider for a View when you associate a View to a
Data Store:
// Add a View and link a Provider to it.
datastoreService.addView("sample-datastore", SampleViewImpl.class, "sql-provider", null);
sql-provider Provider now can be used in the View as the
default Provider to read text files from a specific directory. Let us say we
have the following content for create-user.sql:
INSERT INTO HOME_USERS (ID, NAME) VALUES (1, 'John Wood')
sql-provider is the default Provider in a RDBMS View, we can
read the content of this file with the following code:
// Read the content of 'create-user.sql' and execute it.
view.executeUpdateByName("create-user", null, null);
sql-provider.sql-provider reads the content of
create-user.sql and returns it (as a String
object).create-user.sql in the Data Store.
INSERT INTO HOME_USERS (ID, NAME) VALUES (${USER_ID}, ${USER_NAME})
// Values for variables.
Hashtable values = new Hashtable();
// Set variables for the update statement.
values.put("USER_ID", new Integer(3));
values.put("USER_NAME", "Ian Sharpe");
// Create a new user in the database.
view.executeUpdateByName("create-user", values, null);
INSERT INTO HOME_USERS (ID, NAME) VALUES (${USER_ID}, ${USER_NAME});
INSERT INTO ACTIVE_USERS (ID, NAME) VALUES (${USER_ID});
// Values for variables.
Hashtable values = new Hashtable();
// Set variables for the update statement.
values.put("USER_ID", new Integer(3));
values.put("USER_NAME", "Ian Sharpe");
// Create a new user in the database.
view.executeUpdateByName("create-user", values, new Character(';'));
// Commits changes in the Database Management System.
view.commit();
// Cancel latest update operations.
view.rollback();
// Execute the statement to retrieve some data.
Object result = view.executeQuery("SELECT * FROM HOME_USERS", -1, -1);
ResultSet object. Anyway, check out the specific type returned
by the implementation class of this View because each Data Store may provide
a different object as result.
// Get the first 10 rows.
Object result = view.executeQuery("SELECT * FROM HOME_USERS", 1, 10);
// Get rows from 11 to 20.
Object result = view.executeQuery("SELECT * FROM HOME_USERS", 2, 10);
// Get rows from 21 to 26.
Object result = view.executeQuery("SELECT * FROM HOME_USERS", 3, 10);
SELECT * FROM HOME_USERS
sql-provider still is our default Provider, we can read the
script like this:
// Read the content of 'list-users.sql' and execute it in the database.
Object result = view.executeQueryByName("list-users", null, -1, -1);
SELECT * FROM HOME_USERS A WHERE A.ID = ${USER_ID}
// Values for variables.
Hashtable values = new Hashtable();
// Set variables to filter the query.
values.put("USER_ID", new Integer(8375));
// Read 'list-users.sql', replace variables and execute the final statement.
Object result = view.executeQueryByName("list-users", values, -1, -1);
// Get the second page with no more than 10 registries in it.
Object result = view.executeQueryByName("list-users", null, 2, 10);
// Close the connection with the Data Store.
view.disconnect();
| Method Summary | |
|---|---|
Object |
executeQuery(String statement,
int page,
int pageRows)
Executes an SQL query statement in the Relational Database Management System. |
Object |
executeQueryByName(String name,
Hashtable values,
int page,
int pageRows)
Loads a String that represents an SQL query (typically a static SQL SELECT statement) from the default Provider defined for this
View (or the next View in the stack of Views of the Data Store) and
executes it in the Relational Database Management System. |
void |
executeUpdate(String statement,
Character delimiter)
Executes a set of SQL update statements in the Relational Database Management System. |
void |
executeUpdateByName(String name,
Hashtable values,
Character delimiter)
Reads a set of SQL update statements in the default Provider defined for this View (or the next View in the stack of Views of the Data Store) and executes them in the Relational Database Management System. |
| Methods inherited from interface com.warework.service.datastore.view.DBMSView |
|---|
beginTransaction, commit, rollback |
| Methods inherited from interface com.warework.service.datastore.view.DataStoreView |
|---|
connect, disconnect, isConnected |
| Method Detail |
|---|
Object executeQuery(String statement,
int page,
int pageRows)
throws ClientException
statement - SQL statement, typically a static SQL SELECT
statement, to retrieve data from the relational database.page - Number of pages which hold the number of rows that you want in
the result of the database (check out pageRows
argument). Set this argument to -1 to avoid
pages.pageRows - Number of rows that you want in the result of the database.
Set this argument to -1 to retrieve every row.
ClientException
Object executeQueryByName(String name,
Hashtable values,
int page,
int pageRows)
throws ClientException
SELECT statement) from the default Provider defined for this
View (or the next View in the stack of Views of the Data Store) and
executes it in the Relational Database Management System.
name - Name of the query statement to load from a Provider.values - Map where the keys represent variable names in the
query-string loaded from the Provider and the values those
that will replace the variables. Every variable must be inside
'${' and '}' so the variable CAR must be in this query-string
as '${CAR}'. Pass null to this parameter to make
no changes in the query loaded.page - Number of pages which hold the number of rows that you want in
the result of the database (check out pageRows
argument). Set this argument to -1 to avoid
pages.pageRows - Number of rows that you want in the result of the database.
Set this argument to -1 to retrieve every row.
ClientException
void executeUpdate(String statement,
Character delimiter)
throws ClientException
statement - SQL statement, which may be an INSERT,
UPDATE, or DELETE statement or an
SQL statement that returns nothing, such as an SQL DDL
statement (for example: CREATE TABLE).delimiter - Character that separates each statement (for example: ';').
Use null to indicate that just one statement is
passed to the database.
ClientException
void executeUpdateByName(String name,
Hashtable values,
Character delimiter)
throws ClientException
name - Name of the statement to load in the Provider.values - Map where the keys represent variable names in the
update-string loaded from the Provider and the values those
that will replace the variables. Every variable must be inside
'${' and '}' so the variable CAR must be in this
statement-string as '${CAR}'. Pass null to this
parameter to make no changes in the statement loaded.delimiter - Character that separates each statement (i.e.: ';'). Use
null to indicate that just one statement is
passed to the database.
ClientException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||