
(********************************************************************)
(*                                                                  *)
(*  sql_act.s7i   Generate code for actions for database access.    *)
(*  Copyright (C) 1990 - 1994, 2004 - 2014  Thomas Mertes           *)
(*                                                                  *)
(*  This file is part of the Seed7 compiler.                        *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const ACTION: SQL_BIND_BIGINT        is action "SQL_BIND_BIGINT";
const ACTION: SQL_BIND_BIGRAT        is action "SQL_BIND_BIGRAT";
const ACTION: SQL_BIND_BOOL          is action "SQL_BIND_BOOL";
const ACTION: SQL_BIND_BSTRI         is action "SQL_BIND_BSTRI";
const ACTION: SQL_BIND_DURATION      is action "SQL_BIND_DURATION";
const ACTION: SQL_BIND_FLOAT         is action "SQL_BIND_FLOAT";
const ACTION: SQL_BIND_INT           is action "SQL_BIND_INT";
const ACTION: SQL_BIND_NULL          is action "SQL_BIND_NULL";
const ACTION: SQL_BIND_STRI          is action "SQL_BIND_STRI";
const ACTION: SQL_BIND_TIME          is action "SQL_BIND_TIME";
const ACTION: SQL_CLOSE              is action "SQL_CLOSE";
const ACTION: SQL_CMP_DB             is action "SQL_CMP_DB";
const ACTION: SQL_CMP_STMT           is action "SQL_CMP_STMT";
const ACTION: SQL_COLUMN_BIGINT      is action "SQL_COLUMN_BIGINT";
const ACTION: SQL_COLUMN_BIGRAT      is action "SQL_COLUMN_BIGRAT";
const ACTION: SQL_COLUMN_BOOL        is action "SQL_COLUMN_BOOL";
const ACTION: SQL_COLUMN_BSTRI       is action "SQL_COLUMN_BSTRI";
const ACTION: SQL_COLUMN_DURATION    is action "SQL_COLUMN_DURATION";
const ACTION: SQL_COLUMN_FLOAT       is action "SQL_COLUMN_FLOAT";
const ACTION: SQL_COLUMN_INT         is action "SQL_COLUMN_INT";
const ACTION: SQL_COLUMN_STRI        is action "SQL_COLUMN_STRI";
const ACTION: SQL_COLUMN_TIME        is action "SQL_COLUMN_TIME";
const ACTION: SQL_COMMIT             is action "SQL_COMMIT";
const ACTION: SQL_CPY_DB             is action "SQL_CPY_DB";
const ACTION: SQL_CPY_STMT           is action "SQL_CPY_STMT";
const ACTION: SQL_CREATE_DB          is action "SQL_CREATE_DB";
const ACTION: SQL_CREATE_STMT        is action "SQL_CREATE_STMT";
const ACTION: SQL_DESTR_DB           is action "SQL_DESTR_DB";
const ACTION: SQL_DESTR_STMT         is action "SQL_DESTR_STMT";
const ACTION: SQL_DRIVER             is action "SQL_DRIVER";
const ACTION: SQL_EMPTY_DB           is action "SQL_EMPTY_DB";
const ACTION: SQL_EMPTY_STMT         is action "SQL_EMPTY_STMT";
const ACTION: SQL_EQ_DB              is action "SQL_EQ_DB";
const ACTION: SQL_EQ_STMT            is action "SQL_EQ_STMT";
const ACTION: SQL_ERR_CODE           is action "SQL_ERR_CODE";
const ACTION: SQL_ERR_DB_FUNC        is action "SQL_ERR_DB_FUNC";
const ACTION: SQL_ERR_LIB_FUNC       is action "SQL_ERR_LIB_FUNC";
const ACTION: SQL_ERR_MESSAGE        is action "SQL_ERR_MESSAGE";
const ACTION: SQL_EXECUTE            is action "SQL_EXECUTE";
const ACTION: SQL_FETCH              is action "SQL_FETCH";
const ACTION: SQL_IS_NULL            is action "SQL_IS_NULL";
const ACTION: SQL_NE_DB              is action "SQL_NE_DB";
const ACTION: SQL_NE_STMT            is action "SQL_NE_STMT";
const ACTION: SQL_OPEN               is action "SQL_OPEN";
const ACTION: SQL_PREPARE            is action "SQL_PREPARE";
const ACTION: SQL_STMT_COLUMN_COUNT  is action "SQL_STMT_COLUMN_COUNT";
const ACTION: SQL_STMT_COLUMN_NAME   is action "SQL_STMT_COLUMN_NAME";


const proc: sql_prototypes (inout file: c_prog) is func

  begin
    declareExtern(c_prog, "void        sqlBindBigInt (sqlStmtType, intType, const const_bigIntType);");
    declareExtern(c_prog, "void        sqlBindBigRat (sqlStmtType, intType, const const_bigIntType, const const_bigIntType);");
    declareExtern(c_prog, "void        sqlBindBool (sqlStmtType, intType, boolType);");
    declareExtern(c_prog, "void        sqlBindBStri (sqlStmtType, intType, bstriType);");
    declareExtern(c_prog, "void        sqlBindDuration (sqlStmtType, intType, intType, intType, intType, intType, intType, intType, intType);");
    declareExtern(c_prog, "void        sqlBindFloat (sqlStmtType, intType, floatType);");
    declareExtern(c_prog, "void        sqlBindInt (sqlStmtType, intType, intType);");
    declareExtern(c_prog, "void        sqlBindNull (sqlStmtType, intType);");
    declareExtern(c_prog, "void        sqlBindStri (sqlStmtType, intType, striType);");
    declareExtern(c_prog, "void        sqlBindTime (sqlStmtType, intType, intType, intType, intType, intType, intType, intType, intType, intType);");
    declareExtern(c_prog, "void        sqlClose (databaseType);");
    declareExtern(c_prog, "bigIntType  sqlColumnBigInt (sqlStmtType, intType);");
    declareExtern(c_prog, "void        sqlColumnBigRat (sqlStmtType, intType, bigIntType *, bigIntType * );");
    declareExtern(c_prog, "boolType    sqlColumnBool (sqlStmtType, intType);");
    declareExtern(c_prog, "bstriType   sqlColumnBStri (sqlStmtType, intType);");
    declareExtern(c_prog, "void        sqlColumnDuration (sqlStmtType, intType, intType *, intType *, intType *, intType *, intType *, intType *, intType * );");
    declareExtern(c_prog, "floatType   sqlColumnFloat (sqlStmtType, intType);");
    declareExtern(c_prog, "intType     sqlColumnInt (sqlStmtType, intType);");
    declareExtern(c_prog, "striType    sqlColumnStri (sqlStmtType, intType);");
    declareExtern(c_prog, "void        sqlColumnTime (sqlStmtType, intType, intType *, intType *, intType *, intType *, intType *, intType *, intType *, intType *, boolType * );");
    declareExtern(c_prog, "void        sqlCommit (databaseType);");
    declareExtern(c_prog, "void        sqlCpyDb (databaseType *const, const databaseType);");
    declareExtern(c_prog, "void        sqlCpyDbGeneric (genericType *const, const genericType);");
    declareExtern(c_prog, "void        sqlCpyStmt (sqlStmtType *const, const sqlStmtType);");
    declareExtern(c_prog, "void        sqlCpyStmtGeneric (genericType *const, const genericType);");
    declareExtern(c_prog, "databaseType sqlCreateDb (const databaseType);");
    declareExtern(c_prog, "genericType sqlCreateDbGeneric (const genericType);");
    declareExtern(c_prog, "sqlStmtType sqlCreateStmt (const sqlStmtType);");
    declareExtern(c_prog, "genericType sqlCreateStmtGeneric (const genericType);");
    declareExtern(c_prog, "void        sqlDestrDb (const databaseType);");
    declareExtern(c_prog, "void        sqlDestrDbGeneric (const genericType);");
    declareExtern(c_prog, "void        sqlDestrStmt (const sqlStmtType);");
    declareExtern(c_prog, "void        sqlDestrStmtGeneric (const genericType);");
    declareExtern(c_prog, "intType     sqlDriver (databaseType);");
    declareExtern(c_prog, "intType     sqlErrCode (void);");
    declareExtern(c_prog, "striType    sqlErrDbFunc (void);");
    declareExtern(c_prog, "striType    sqlErrLibFunc (void);");
    declareExtern(c_prog, "striType    sqlErrMessage (void);");
    declareExtern(c_prog, "void        sqlExecute (sqlStmtType);");
    declareExtern(c_prog, "boolType    sqlFetch (sqlStmtType);");
    declareExtern(c_prog, "boolType    sqlIsNull (sqlStmtType, intType);");
    declareExtern(c_prog, "databaseType sqlOpen (intType, const const_striType, const const_striType, const const_striType);");
    declareExtern(c_prog, "sqlStmtType sqlPrepare (databaseType, striType);");
    declareExtern(c_prog, "intType     sqlStmtColumnCount (sqlStmtType);");
    declareExtern(c_prog, "striType    sqlStmtColumnName (sqlStmtType, intType);");
  end func;


const proc: process (SQL_BIND_BIGINT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
  begin
    statement.expr &:= "sqlBindBigInt(";
    process_expr(params[1], statement);
    statement.expr &:= ", ";
    process_expr(params[2], statement);
    statement.expr &:= ", ";
    getAnyParamToExpr(params[3], statement);
    statement.expr &:= ");\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (SQL_BIND_BIGRAT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
  begin
    statement.expr &:= "sqlBindBigRat(";
    process_expr(params[1], statement);
    statement.expr &:= ", ";
    process_expr(params[2], statement);
    statement.expr &:= ", ";
    getAnyParamToExpr(params[3], statement);
    statement.expr &:= ", ";
    getAnyParamToExpr(params[4], statement);
    statement.expr &:= ");\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (SQL_BIND_BOOL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindBool(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_BIND_BSTRI, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
  begin
    statement.expr &:= "sqlBindBStri(";
    process_expr(params[1], statement);
    statement.expr &:= ", ";
    process_expr(params[2], statement);
    statement.expr &:= ", ";
    getAnyParamToExpr(params[3], statement);
    statement.expr &:= ");\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (SQL_BIND_DURATION, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindDuration(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[4], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[5], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[6], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[7], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[8], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[9], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_BIND_FLOAT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindFloat(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_BIND_INT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindInt(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_BIND_NULL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindNull(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_BIND_STRI, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
  begin
    statement.expr &:= "sqlBindStri(";
    process_expr(params[1], statement);
    statement.expr &:= ", ";
    process_expr(params[2], statement);
    statement.expr &:= ", ";
    getAnyParamToExpr(params[3], statement);
    statement.expr &:= ");\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (SQL_BIND_TIME, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlBindTime(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[4], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[5], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[6], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[7], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[8], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[9], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[10], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_CLOSE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlClose(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_CMP_DB, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "ptrCmp(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_CMP_STMT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "ptrCmp(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_BIGINT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_bigint_result(c_expr);
    c_expr.result_expr := "sqlColumnBigInt(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_BIGRAT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlColumnBigRat(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    getAnyParamToExpr(params[4], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (SQL_COLUMN_BOOL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlColumnBool(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_BSTRI, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_bstri_result(c_expr);
    c_expr.result_expr := "sqlColumnBStri(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_DURATION, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlColumnDuration(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[5], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[6], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[7], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[8], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[9], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (SQL_COLUMN_FLOAT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlColumnFloat(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_INT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlColumnInt(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_STRI, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "sqlColumnStri(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (SQL_COLUMN_TIME, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlColumnTime(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[5], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[6], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[7], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[8], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[9], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[10], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[11], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (SQL_COMMIT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlCommit(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_CPY_DB, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    process_expr(params[1], c_param1);
    c_param3.temp_num := c_param1.temp_num;
    process_expr(params[3], c_param3);
    if has_temp_values(c_param3) then
      c_expr.expr &:= "{\n";
      appendWithDiagnostic(c_param1.temp_decls, c_expr);
      appendWithDiagnostic(c_param3.temp_decls, c_expr);
      appendWithDiagnostic(c_param1.temp_assigns, c_expr);
      appendWithDiagnostic(c_param3.temp_assigns, c_expr);
    end if;
    if c_param3.result_expr <> "" then
      c_expr.expr &:= "{\n";
      setDiagnosticLine(c_expr);
      if isNormalVariable(params[1]) then
        c_expr.expr &:= "databaseType old_db=";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ";\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= "=";
      else
        c_expr.expr &:= "databaseType *db_ptr=&(";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ");\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "databaseType old_db=*db_ptr;\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "*db_ptr=";
      end if;
      c_expr.expr &:= c_param3.result_expr;
      c_expr.expr &:= ";\n";
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "sqlDestrDb(old_db);\n";
      c_expr.expr &:= "}\n";
    else
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "sqlCpyDb(&(";
      c_expr.expr &:= c_param1.expr;
      c_expr.expr &:= "), ";
      c_expr.expr &:= c_param3.expr;
      c_expr.expr &:= ");\n";
    end if;
    if has_temp_values(c_param3) then
      appendWithDiagnostic(c_param1.temp_frees, c_expr);
      appendWithDiagnostic(c_param3.temp_frees, c_expr);
      c_expr.expr &:= "}\n";
    end if;
  end func;


const proc: process (SQL_CPY_STMT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    process_expr(params[1], c_param1);
    c_param3.temp_num := c_param1.temp_num;
    process_expr(params[3], c_param3);
    if has_temp_values(c_param3) then
      c_expr.expr &:= "{\n";
      appendWithDiagnostic(c_param1.temp_decls, c_expr);
      appendWithDiagnostic(c_param3.temp_decls, c_expr);
      appendWithDiagnostic(c_param1.temp_assigns, c_expr);
      appendWithDiagnostic(c_param3.temp_assigns, c_expr);
    end if;
    if c_param3.result_expr <> "" then
      c_expr.expr &:= "{\n";
      setDiagnosticLine(c_expr);
      if isNormalVariable(params[1]) then
        c_expr.expr &:= "sqlStmtType old_stmt=";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ";\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= "=";
      else
        c_expr.expr &:= "sqlStmtType *stmt_ptr=&(";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ");\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "sqlStmtType old_stmt=*stmt_ptr;\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "*stmt_ptr=";
      end if;
      c_expr.expr &:= c_param3.result_expr;
      c_expr.expr &:= ";\n";
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "sqlDestrStmt(old_stmt);\n";
      c_expr.expr &:= "}\n";
    else
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "sqlCpyStmt(&(";
      c_expr.expr &:= c_param1.expr;
      c_expr.expr &:= "), ";
      c_expr.expr &:= c_param3.expr;
      c_expr.expr &:= ");\n";
    end if;
    if has_temp_values(c_param3) then
      appendWithDiagnostic(c_param1.temp_frees, c_expr);
      appendWithDiagnostic(c_param3.temp_frees, c_expr);
      c_expr.expr &:= "}\n";
    end if;
  end func;


const proc: process (SQL_DRIVER, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlDriver(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_EQ_DB, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") == (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_EQ_STMT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") == (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_ERR_CODE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlErrCode()";
  end func;


const proc: process (SQL_ERR_DB_FUNC, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "sqlErrDbFunc()";
  end func;


const proc: process (SQL_ERR_LIB_FUNC, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "sqlErrLibFunc()";
  end func;


const proc: process (SQL_ERR_MESSAGE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "sqlErrMessage()";
  end func;


const proc: process (SQL_EXECUTE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "sqlExecute(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (SQL_FETCH, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlFetch(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_IS_NULL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlIsNull(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_NE_DB, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") != (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_NE_STMT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") != (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_OPEN, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_typed_result(resultType(getType(function)), c_expr);
    c_expr.result_expr := "sqlOpen(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[3], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[4], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (SQL_PREPARE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_typed_result(resultType(getType(function)), c_expr);
    c_expr.result_expr := "sqlPrepare(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (SQL_STMT_COLUMN_COUNT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "sqlStmtColumnCount(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (SQL_STMT_COLUMN_NAME, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "sqlStmtColumnName(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;
