Scalable Database Server, HiRDB Version 8 UAP Development Guide

[Contents][Index][Back][Next]

18.3.2 SQL coding rule

Organization of this subsection
(1) SQL statement coding rule
(2) Explicitly specifying connection context when using the multi-connection facility
(3) Explicitly specifying an execution environment
(4) Specifying embedded variables
(5) Specifying indicator variables
(6) Exception handling
(7) Static SQL statements and dynamic SQL statements
(8) Reading out the result set of a dynamic cursor
(9) Connecting to and disconnecting from a HiRDB server
(10) Exception generation conditions
(11) Comments and handling of SQL optimization specification

(1) SQL statement coding rule

Each SQL statement must be enclosed between the SQL leading character string (#sql) and the SQL trailing character (;). The SQL statement itself must further be enclosed between curly brackets. Connection class and cursor declarations must also be enclosed between the SQL leading character string and the SQL trailing character.

Table 18-2 shows the SQL statement coding formats.

Table 18-2 SQL statement coding formats

Function Format Purpose
SQL execution
#sql [context] { SQL-statement } ;
Executes an SQL statement. The SQL statements that can be used differ for the standard interface version and the native interface version. For details, see 18.3.3 SQL statements that can be used in SQLJ.
Declaration of an iterator class with column specification
  • Standard interface version
    #sql modifier iterator class-name
    (data-type column-name,...) ;
  • Native interface version
This function cannot be used.
Declares the class to be used for cursor declaration. Cannot be used in a FETCH statement.
Declaration of an iterator class with a position specification
  • Standard interface version
    #sql modifier iterator class-name
    (data-type,...) ;
  • Native interface version
    #sql modifier iterator class-name
    [implements
    JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.
    ForUpdate]
    [with (keyword=value,...)]
       (data-type,...) ;
Declares the class to be used in the cursor declaration. This function is used in a FETCH statement.
Declaration of a connection class
#sql modifier context class-name ;
Declares the class to be used for connection.
Declaration of a cursor
#sql iterator-object = { SELECT-statement } ;
Defines and opens a cursor.
Conversion of a result set
  • Standard interface version
#sql [context] iterator-object
     = {CAST :JDBC-result-set} ;
  • Native interface version
This function cannot be used.
Converts a JDBC result set into one that can be used by SQLJ.

Notes
modifier
Combination of private, public, protected, final, abstract, protected, static, native, synchronized, transient, and volatile.
context
{connection-context|connection-context,execution-context|execution-context}
keyword
holdability or updateColumns
value
true, false, or "column-name-1,column-name-2,..."
data-type
Java data type
column-name
Retrieval item

(2) Explicitly specifying connection context when using the multi-connection facility

When you use the multi-connection facility, insert the connection context surrounded by square brackets between the SQL leading character string and the SQL statement, to explicitly specify the connection to be used. An example follows:

 #sql [connCtx] { DELETE FROM EMP WHERE SAL > 1000};

If no connection context is explicitly specified, the default connection context is assumed.

(3) Explicitly specifying an execution environment

In SQLJ, a user can explicitly specify an execution environment instead of using the default one. To specify an execution environment, insert the execution connection context surrounded by square brackets between the SQL leading character string and the SQL statement.

If SQL statements are simultaneously being executed in multiple threads for a single connection, using separate multiple execution environments can prevent an execution result from being overwritten by another SQL statement. An example follows:

 ExecutionContext execCtx = new ExecutionContext();
 try {
  #sql [execCtx] { DELETE FROM STOCK WHERE PCODE > 1000 };
  System.out.println
   ("removed " + execCtx.getUpdateCount() + "goods");
 }
 catch(SQLException e){
  System.out.println("SQLException has occurred with "+ " exception " + e);
 }

If no execution connection context is explicitly specified, the default execution environment is used.

The values described in the following table are maintained in the execution environments. These values are set using the set<name> method and determined using the get<name> method.

Name Details
MaxRows Maximum number of rows to be returned from a search.
MaxFieldSize Maximum size of data in units of bytes to be returned in columns and OUTPUT variable value.
QueryTimeout Maximum wait time until SQL execution is completed. This is invalid in HiRDB.
UpdateCount Number of updated, inserted, or deleted rows (reference only).
SQLWarnings Correspond to SQLWARN0-SQLWARNF (reference only).

If multi-connection is also specified, the connection context and execution connection context must be specified in that order, delimited by a comma. An example follows:

 #sql [connCtX, execCtx] { DELETE FROM STOCK WHERE PCODE > 1000 };

(4) Specifying embedded variables

In SQLJ, BEGIN DECLARE SECTION for declaring embedded variables is not used.

Any variables, parameters, and object fields can be used as embedded variables. In an SQL statement, a variable is described as ":variable-name" with a colon at the front. The colon can be separated by blank spaces from the variable name.

The IN, OUT, or INOUT parameter of a CALL statement is described as ":{IN|OUT|INOUT}variable-name".

Additionally, in SQLJ, you can use ":(expression)" as an embedded variable. The expression must be enclosed by parentheses. This is a Java method and not an SQL method. An example follows:

#sql { SELECT COL1, COL2 FROM TABLE1 WHERE :(x[--i]) > COL3 };

(5) Specifying indicator variables

SQLJ has no indicator variable. Therefore, to set a null value for an embedded variable, use the Wrapper type defined in the sql.lang package instead of the basic data type. If a null value is received by a Java variable of the basic data type, the SQLNullException exception occurs.

(6) Exception handling

SQLJ cannot handle exceptions from an embedded SQL WHENEVER statement. Therefore, Java exception handling (try...catch) is used instead of WHENEVER. An example follows:

 try{
    #sql { DELETE FROM STOCK WHERE PCODE > 1000 };
 }
 catch(SQLException e){
    System.out.println("SQLCODE:" + e.getErrorCode() +
                       "\nERRMSG:" + e.getMessage() );
 }

If an error occurs during SQL execution, the JDBC exception object (java.sql.SQLException) is issued.

SQLCODE, SQLSTATE, and error messages are stored in exception objects, and their values can be obtained using the getErrorCode, getSQLState, and getMessage methods.

(7) Static SQL statements and dynamic SQL statements

In SQLJ, only static SQL statements can be described. Dynamic SQL statements cannot be described.

To use a dynamic SQL statement, use the JDBC API.

(8) Reading out the result set of a dynamic cursor

You can use a CAST statement to convert and read out the result set of a dynamic cursor created using the JDBC API as the result set of an SQLJ cursor. An example follows:

 
 #sql iterator Employees(String ename, double sal);
 Statement stmt=conn.createStatement();
 String Query="SELECT pname, pcode FROM stock WHERE pcode > 1000";
 ResultSet rs=stmt.executeQuery(query);
 Employees emps;
 #sql emps ={CAST :rs };
 

The CAST statement cannot be used with the native interface version. If the statement is used, a translation error results.

(9) Connecting to and disconnecting from a HiRDB server

The CONNECT and DISCONNECT statements can be used in the native interface version but not in the standard interface version. For both the standard interface and native interface versions, Java instructions can be used to connect to or disconnect from a HiRDB server.

(10) Exception generation conditions

In HiRDB embedded SQL statements, an alarm is issued in the following cases. In contrast, exceptions occur in SQLJ.

(11) Comments and handling of SQL optimization specification

Comments (/*-*/) described between the SQL leading character string and the SQL trailing character are deleted. However, in cursor declaration and SQL statement execution, the SQL optimization specification (/*>>-<<*/) described between curly brackets is not deleted and handled as an SQL statement. All other SQL optimization specifications (/*>>-<<*/) are treated as comments. For details on comments and SQL optimization specifications inside SQL statements, see the manual HiRDB Version 8 SQL Reference.