Home | Previous Page | Next Page   Database Access > Executing SQL Statements > Executing SQL Statements >

Executing Basic SQL Statements

The mi_exec( ) function provides the simplest way to send a basic SQL statement to the database server for execution. A basic SQL statement is one that does not need to be prepared. That is, the statement does not execute many times in the DataBlade API module or it does not contain input parameters. To send a basic SQL statement to the database server for execution, take the following steps:

The database server parses the statement string, optimizes it, executes it, and sends back the statement results.

Assembling a Statement String

The mi_exec( ) function passes the SQL statement to the database server as a statement string, which is a text representation of the SQL statement. To execute a statement with mi_exec( ), the statement string must include the entire SQL statement; that is, it cannot contain any input parameters.

You can assemble this statement string in the following ways:

Figure 32. Assembling a SELECT Statement from a Character String
mi_string stmt_txt[30];
mi_string fld_name[15];
...
stcopy("select ", stmt_txt);
fld_name = obtain_fldname(...); 
stcat(fld_name, stmt_txt);
stcat("from customer where customer_num = 101", stmt_txt);
...
mi_exec(conn, stmt_txt, MI_QUERY_BINARY);
Figure 33. Using a Variable to Assemble a SELECT Statement
mi_string stmt_txt[30];
mi_integer cust_num;
...
stcopy("select company from customer where customer_num = ",
   stmt_txt);
cust_num = obtain_custnum(...); 
stcat(cust_num, stmt_txt);
...
stmt_desc = mi_exec(conn, stmt_txt, MI_QUERY_BINARY);

The statement string can contain multiple SQL statements. Each SQL statement must be terminated with the semicolon (;) symbol. For more information, see Executing Multiple SQL Statements.

Sending an SQL Statement

The mi_exec( ) function is for the execution of basic SQL statements, both queries and other valid SQL statements. In a DataBlade API module, use the following DataBlade API functions to execute a basic SQL statement.

Step in Execution of Basic SQL Statement DataBlade API Function
Send the basic SQL statement to the database server for execution and open any cursor required mi_exec( )
Release statement resources mi_query_finish( ),
mi_query_interrupt( )

Once the database server executes the statement that mi_exec( ) sends, the statement becomes the current statement. The current statement is the most recent SQL statement on the connection. Only one statement per connection is current. The database server sends back the results of the current statement, including whether the current statement was successful.

The mi_exec( ) function creates an implicit statement descriptor to hold the information about the current statement. The following table summarizes the memory operations for an implicit statement descriptor.

Memory Duration Memory Operation Function Name
Not allocated from memory-duration pools Constructor mi_exec( )
Destructor mi_query_finish( )

Table 54 lists the DataBlade API accessor functions for the implicit statement descriptor that mi_exec( ) creates.

Table 54. Accessor Functions for an Implicit Statement Descriptor
Statement-Descriptor Information DataBlade API Accessor Function
The name of the SQL statement that is the current statement mi_result_command_name( )
A row descriptor for the columns in the current statement mi_get_row_desc_without_row( )

From the row descriptor, you can use the row-descriptor accessor functions to obtain information about a particular column (see Table 22).

You obtain the status of the current statement with the mi_get_result( ) function. For more information, see Processing Statement Results.

Tip:
The return value that the mi_exec( ) function returns does not indicate the success of the current statement. It indicates if mi_exec( ) was able to successfully send the statement to the database server.

When mi_exec( ) executes a query, it performs the following additional steps:

  1. Opens an implicit cursor to hold the query rows
  2. Reads the query rows into the open cursor
The Implicit Row Cursor

When mi_exec( ) executes a query, it automatically opens an implicit cursor to hold the resulting rows. This cursor is associated with the current statement and is stored as part of the connection descriptor. Therefore, only one cursor per connection can be current. For more information, see Queries and Implicit Cursors.

Tip:
If the implicit cursor that mi_exec( ) creates does not adequately meet the needs of your DataBlade API module, you can use the mi_open_prepared_statement( ) function to define other types of cursors. For more information, see Defining an Explicit Cursor.

When the mi_exec( ) function successfully fetches the query results into the cursor, the cursor position points to the first row of the cursor, and the mi_get_result( ) function returns a status of MI_ROWS to indicate that the cursor contains rows.

You can access these rows one at a time with the mi_next_row( ) function. Each access obtains the row to which the cursor position points. After each access to the cursor, the cursor position moves to the next row. For more information, see Retrieving Query Data.

Control Modes for Query Data

The data that the database server returns for a query can be in one of two control modes:

Table 55 shows the format of different data types in the two control modes.

Table 55. Control Modes for Data
Type of Data Text Representation Binary Representation
Character Null-terminated string Varying-length structure: mi_lvarchar
Date "mm/dd/yyyy"

Nondefault locale: End-user date format

Integer number of days since December 31, 1899

(DATE, mi_date)

Date/time "yyyy-mm-dd HH:MM:SS"

Nondefault locale: End-user date and time format

dtime_t

(DATETIME, mi_datetime)

Interval "yyyy-mm"

"dd HH:MM:SS"

Nondefault locale: End-user date and time format

intrvl_t

(INTERVAL, mi_interval)

Integer Integer value as a string:

thousands separator = ","

Nondefault locale: End-user numeric format

Internal format:
  • Two-byte integer
    (SMALLINT, mi_smallint)
  • Four-byte integer
    (INTEGER, mi_integer)
  • Eight-byte integer: ifx_int8_t
    (INT8, mi_int8)
Decimal Fixed-point value as a string:

thousands separator = ","
decimal separator = "."

Nondefault locale: End-user numeric format

dec_t

(DECIMAL, mi_decimal)

Monetary Fixed-point value as a string:

thousands separator = ","
decimal separator = "."
currency symbol = "$"

Nondefault locale: End-user monetary format

dec_t

(MONEY, mi_money)

Floating-point Floating-point value as a string:

thousands separator = ","
decimal separator = "."

Nondefault locale: End-user numeric format

Internal format:
  • single-precision floating point (SMALLFLOAT, mi_real)
  • double-precision floating point (FLOAT, mi_double_precision)
Boolean "t" or "T"

"f" or "F"

MI_TRUE, MI_FALSE

(BOOLEAN, mi_boolean)

Smart large object Text representation of the LO handle
(obtained with mi_lo_to_string( ))
LO handle

(CLOB, BLOB; MI_LO_HANDLE)

Row type Unnamed row type:

"ROW(fld_value1, fld_value2, ...)"

Named row type:

"row_type(fld_value1, fld_value2, ...)"

Row structure

(ROW, named row type; MI_ROW)

Collection type "SET{elmnt_value, elmnt_value, ...}"

"MULTISET{elmnt_value, elmnt_value, ...}"

"LIST{elmnt_value, elmnt_value, ...}"

Collection structure

(SET, LIST, MULTISET; MI_COLLECTION)

Varying-length opaque type External format of opaque type
(as returned by output support function)
Varying-length structure: mi_bitvarying
(which contains the internal C data type)
Fixed-length opaque type External format of opaque type
(as returned by output support function)
Internal C data type
Distinct type Text representation of its source data type Binary representation of its source data type

The mi_exec( ) function indicates the control mode of the query with a bit-mask control argument, which is one of the following flags.

Control Mode
Control-Flag Value
Text representation
MI_QUERY_NORMAL
Binary representation
MI_QUERY_BINARY

In the send_statement( ) function (page Example: The send_statement( ) Function), mi_exec( ) sets the control mode of the query data to text representation.

To determine the control mode for query data, use the mi_binary_query( ) function. The mi_binary_query( ) function determines the control mode for data of the current statement.

Example: The send_statement( ) Function

The send_statement( ) function takes an existing open connection and an SQL statement string as arguments and sends the statement to the database server with the mi_exec( ) function. It specifies text representation for the query results.

/* FUNCTION: send_statement( )
 * PURPOSE: To send an SQL statement to the database server for
 *          execution
 *
 * CALLED BY: Called from within a C user-defined function to
 *            execute a basic SQL statement
 */
 
mi_integer
send_statement(MI_CONNECTION *conn, mi_string *stmt)
{
   mi_integer count;
 
   /* Send the statement, specifying results be sent 
    * in their text representation (MI_QUERY_NORMAL)
    */
   if ( MI_ERROR == mi_exec(conn, stmt, MI_QUERY_NORMAL) )
      {
       mi_db_error_raise(conn, MI_EXCEPTION, 
         "mi_exec failed\n");
      }

   /* Get the results of the current statement */
   count = get_results(conn);

   /* Release statement resources */
   if ( mi_query_finish(conn) == MI_ERROR )
      {
      mi_db_error_raise(conn, MI_EXCEPTION, 
         "mi_query_finish failed\n");
      }

   return ( count );
}

The send_statement( ) function calls another user function, get_results( ), to examine the status of the current statement. For the implementation of the get_results( ) function, see Example: The get_results( ) Function.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]