Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

UNICODE

#include "SQLDBC.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
 
using namespace SQLDBC;

static void parseArgs(int argc, char **argv);

static SQLDBC_Connection *connectDB();

static char* convertToUCS2(char *src);

static void usage();

void exitOnError(SQLDBC_ErrorHndl &err);

int main(int argc, char *argv[])
{
   parseArgs(argc, argv);

   SQLDBC_Connection *conn = connectDB();

   /*
    * Create a new statment object and execute it.
    */
   SQLDBC_Statement *stmt = conn->createStatement();
   
   /*
    * Converts the command to UCS2.
    */
   char *command = convertToUCS2((char*)"SELECT 'Hello UNICODE' from DUAL");

   /*
    * Executes the UNICODE encoded SQL command.
    */
   stmt->execute((const char*)command, SQLDBC_StringEncodingUCS2Native);

   exitOnError(stmt->error());

   /*
    * Get the resultset.
    */  
   SQLDBC_ResultSet *result;
   result = stmt->getResultSet();

   exitOnError(stmt->error());

   /*
    * Fetch the row from the resultset.
    */  
   result->next();

   exitOnError(result->error());

   char szString[30];
   SQLDBC_Length ind;
   /*
    * Get a string value from the column.
    */
   result->getObject(1, SQLDBC_HOSTTYPE_ASCII, szString, &ind, sizeof(szString));

   exitOnError(result->error());

   printf("%s\n", szString);

   return 0;
}

static char *convertToUCS2(char *src) 
{
  int len = strlen(src);
  wchar_t *w=(wchar_t*)calloc(len, sizeof(wchar_t));
  int nc = mbstowcs(w, src, len);
  if ((size_t)-1 == nc) {
    fprintf(stderr, "Conversion to UCS2 failed. Execution stopped.\n");
    exit(1);
  }
  short *s=(SQLDBC_Int2*)calloc(nc+1, sizeof(SQLDBC_Int2));
  int i;
  for(i=0; i<nc; i++) {
    s[i] = w[i];
  }
  free(w);
  return (char*)s;
}

static char* strupper(char *str)
{
    char *p=str;
    while(*p) {
        *p=toupper(*p);
        ++p;
    }
    return str;
}

typedef struct ConnectArgsT {
  char * username; 
  char * password; 
  char * dbname;   
  char * host;     
} ConnectArgsT;

ConnectArgsT connectArgs; 

static char *argv0;       

#ifdef WIN32
#define STRICMP _stricmp
#else
#define STRICMP strcasecmp
#endif

SQLDBC_Connection *connectDB()
{
  char errorText[200];
  SQLDBC_Retcode rc;
  /*
   * Every application has to initialize the SQLDBC library by getting a 
   * reference to the ClientRuntime and calling the SQLDBC_Environment constructor.
   */
  SQLDBC_IRuntime *runtime;
  runtime = SQLDBC::GetClientRuntime(errorText, sizeof(errorText));
  if (!runtime) {
    fprintf(stderr, "Getting instance of the ClientRuntime failed %s\n", errorText);
    usage();
  }
  SQLDBC_Environment *env = new SQLDBC_Environment(runtime);
  /*
   * Create a new connection object and open a session to the database.
   */
  SQLDBC_Connection *conn = env->createConnection();
  printf("Connecting to '%s' on '%s' as user '%s'\n", 
         connectArgs.dbname, connectArgs.host, connectArgs.username);

  rc = conn->connect(connectArgs.host, connectArgs.dbname, 
                     connectArgs.username, connectArgs.password);
  if(SQLDBC_OK != rc) {
    fprintf(stderr, "Can't connect to '%s'.\nERROR: %d:'%s'\n", 
            connectArgs.dbname, conn->error().getErrorCode(), conn->error().getErrorText());
    exit(1);
  }
  return conn;
}

static void parseArgs (int argc, char **argv)
{
  argv0 = strdup(argv[0]);

  /*
   * setting defaults for demo database
   */
  connectArgs.username = strdup("MONA");
  connectArgs.password = strdup("RED");
  connectArgs.dbname = (char*)"HOTELDB";
  connectArgs.host = (char*)"localhost";
  /*
   * use values from command line
   */
  if (argc > 4) {
    connectArgs.host = argv [4];
  }
  if (argc > 3) {
    connectArgs.dbname = argv [3];
  }
  if (argc > 2) {
    connectArgs.password = argv [2];
  }
  if (argc > 1) {
    if (!STRICMP(argv [1], "-h"))
      usage();
    else {
      connectArgs.username = argv [1];
    }
  }
  strupper(connectArgs.username);
  strupper(connectArgs.password);
}

void exitOnError(SQLDBC_ErrorHndl &err) 
{
  if(err) {
    fprintf(stderr, "Execution stopped %d:'%s'", err.getErrorCode(), err.getErrorText());
    exit(1);
  }  
}
  
static void usage() 
{
  char *s = strrchr(argv0, '/');
  if (!s) 
    s = strrchr(argv0, '\\');
  if (s) 
    *s = '\0';
  printf("Syntax: %s [-h] | [<connect_options>]\n"
         "\tconnect_options :: \n"
         "\t\t[ <username> \n"
         "\t\t[ <password>\n"
         "\t\t[ <database_name>\n"
         "\t\t[ <database_server> ]]]]\n"
         "\tCalling %s without any arguments will use user '%s','%s'\n"
         "\t\ton database '%s' at server '%s'\n",
         argv0, argv0, 
         connectArgs.username, connectArgs.password, 
         connectArgs.dbname, connectArgs.host);
  exit(1);
}