Anfang des Inhaltsbereichs

Diese Grafik wird im zugehörigen Text erklärt Beispiel 2 Dokument im Navigationsbaum lokalisieren

HelloUnicodeDB.cpc ist ein Precompiler-Programm, das UNICODE Host-Variablen verwendet (siehe UNICODE in Programmiersprachen, Abschnitt C/C++-Precompiler).

Definition HelloUnicodeDB.cpc

/**********************************************************************

  module     : HelloUnicodeDB.cpc

  ---------------------------------------------------------------------

  responsible : MarcoP

  special area: demonstration program precompiler
  description : demonstration program for unicode features of SAPDB precompiler
  
  The following steps are needed to execute the demo program:
  1. customize the connect data in the embedded SQL program source (line 43 - 46)
     
  2. precompile/compile the embedded SQL program HelloUnicodeDB.cpc
     cpc [-u <userid>,<password> -d <database_name> -n <server_node>] HelloUnicodeDB
     
  3. linking the embedded SQL program HelloUnicodeDB.cpc
     cpclnk HelloUnicodeDB

  last changed: 2000-04-30 17:17
  see also :

  ---------------------------------------------------------------------

  copyright: Copyright by SAP AG, 2001

**********************************************************************/

/*===================================================================*
 *  INCLUDES                                                      *
 *===================================================================*/
#include <stdio.h>
#include <string.h>

/*===================================================================*
 *  DEFINES                                                       *
 *===================================================================*/

#define KNLIDNTFR 63 /* max. number of character for a unicode kernel identifier*/

 /* connect data */
#define USERID     "TEST"       /* username */
#define PASSWD     "TEST"       /* password */
#define SERVERNODE "localhost"  /* computer name */
#define SERVERDB   "TST"        /* name of database*/

/*===================================================================*
 *  MACROS                                                        *
 *===================================================================*/

/*===================================================================*
 *  LOCAL CLASSES, STRUCTURES, TYPES, UNIONS ...                    *
 *===================================================================*/

/*===================================================================*
 *  STATIC/INLINE FUNCTIONS (PROTOTYPES)                            *
 *===================================================================*/

/* print UCS2 string as 7-bit Ascii, replace non-ascii characters with '?'*/
static void printAs7bitAscii(SQLUCS2 *aUCS2Str, int length)
{
   int i;
   for (i = 0; i < length; i++) {
     if (   aUCS2Str[i] == 0x0000
         || aUCS2Str[i] == 0x0020 )
       return;
     if (   aUCS2Str[i] < 0x007f
         && aUCS2Str[i] > 0x0000)
       printf("%c",aUCS2Str[i]);
     else
       printf("?");
   }
}

/* format sql error for output */
static char *FormatSQLError(sqlcatype *sqlca)
{
  static char buffer[512];
#ifndef sql_oracle

  sprintf(buffer, "(%d):%.*s", sqlca->sqlcode,
     sqlca->sqlerrml, sqlca->sqlerrmc);
#else
  sprintf(buffer, "(%d):%.*s", sqlca->sqlcode,
     sqlca->sqlerrm.sqlerrml, sqlca->sqlerrm.sqlerrmc);
#endif
  return(buffer);
}

/*==================================================================*
 *  METHODS                                                      *
 *==================================================================*/

int main(int argc, char **argv)
{
  EXEC SQL BEGIN DECLARE SECTION;
   char *user         = USERID;
   char *pwd          = PASSWD;
   char *servernode    = SERVERNODE;
   char *serverdb      = SERVERDB;
   /* "SELECT TABLENAME FROM DOMAIN.TABLES " encoded in UCS2 */
   SQLUCS2 sqlstmt[36] = {0x0053, 0x0045, 0x004C, 0x0045, 0x0043, 0x0054, 0x0020, 0x0054, 0x0041, 0x0042,
                           0x004C, 0x0045, 0x004E, 0x0041, 0x004D, 0x0045, 0x0020, 0x0046, 0x0052, 0x004F,
                           0x004D, 0x0020, 0x0044, 0x004F, 0x004D, 0x0041, 0x0049, 0x004E, 0x002E, 0x0054,
                           0x0041, 0x0042, 0x004C, 0x0045, 0x0053, 0x0000};
   SQLUCS2 resultstring[64];
  EXEC SQL END DECLARE SECTION;

  /* set connect properties */
  EXEC SQL SET SERVERDB :serverdb ON :servernode;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
  
  /* connect to database */
  EXEC SQL CONNECT :user IDENTIFIED BY :pwd;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
  
  /* parse a unicode sql command and give it a statement name */
  EXEC SQL PREPARE stmt1 FROM :sqlstmt;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
  
  /* declare a cursor for a prepared statement name */
  EXEC SQL DECLARE curs1 CURSOR FOR stmt1;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
  
  /* open the cursor "curs1" */
  EXEC SQL OPEN curs1;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
  
  /* loop over resultset */
  while (sqlca.sqlcode != 100)
  {
    /* fetch a single row into a character hostvariable encoded in UCS2 */
    EXEC SQL FETCH curs1 INTO :resultstring;
    if (sqlca.sqlcode != 0 && sqlca.sqlcode != 100) {
      printf("\n%s\n", FormatSQLError(&sqlca));
    }
    else {
      printAs7bitAscii(resultstring, KNLIDNTFR);
      printf("\n");
    }
  }
  
  /* close the cursor */
  EXEC SQL CLOSE curs1;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }

  /* commit and release the session */
  EXEC SQL COMMIT WORK RELEASE;
  if (sqlca.sqlcode != 0 ) {
    printf("\n%s\n", FormatSQLError(&sqlca));
  }
}


/*===================================================================*
 *  END OF CODE                                                   *
 *===================================================================*/

Ende des Inhaltsbereichs