在AIX机器上使用proc*c

来源:互联网 发布:示剑网络 编辑:程序博客网 时间:2024/05/30 04:51

以前在Linux机器上移植到AIX机器上就编译不过, 赶紧改动下。

 

【AIX机器上makefile】

### My first *.cpp   make in AIX  2009-11-26 by tan
include $(ORACLE_HOME)/precomp/lib/env_precomp.mk

# ORACLE_HOME=/oracle/product/10.2.0

## /oracle/product/10.2.0/precomp/admin/pcscfg.cfg
#ys_include=(/usr/include)
#ltype=short


PRECOMPHOME=$$ORACLE_HOME/precomp/public

PROCINCLUDE=include=.  /
include=${ORACLE_HOME}/precomp/syshdr /
include=${ORACLE_HOME}/precomp/public /
include=${ORACLE_HOME}/rdbms/public /
include=${ORACLE_HOME}/rdbms/demo /
include=${ORACLE_HOME}/plsql/public /
include=${ORACLE_HOME}/network/public /
include=$(ORACLE_HOME)/precomp/lib/env_precomp.mk


ORALIBPATH=$(ORACLE_HOME)/lib
ORALIBS=-lclntsh  -ldl -lm -lnsl ##-lsqlora8

CC = xlc -q64   ## 注意:大写'C' for c++
CC_FLAGS = -g  # -bnoquiet

ORACLE_INCLUDE = $(PRECOMPHOME) /
  $(ORACLE_HOME)/rdbms/demo /
   $(ORACLE_HOME)/otrace/public /
    $(ORACLE_HOME)/rdbms/public /
    $(ORACLE_HOME)/network/public /
    $(ORACLE_HOME)/plsql/public /

 

INCLUDE= -I.   -I/usr/include -I/usr/vacpp/include  -I${ORACLE_HOME}/precomp/public /
   -I${ORACLE_HOME}/rdbms/public /
   -I${ORACLE_HOME}/rdbms/demo /
   -I${ORACLE_HOME}/plsql/public
   ##-I${ORACLE_HOME}/network/public
  

LIBPATH=-L/usr/vacpp/lib -L/usr/lib -L/lib  -L$(ORALIBPATH) $(ORALIBS)

all : connectDb

connectDb : connectDb.o 
 $(CC)   -I${INCLUDE}   -o connectDb  connectDb.o   $(LIBPATH)


connectDb.o :  connectDb.pc 
 proc PARSE=NONE INAME=connectDb.pc ONAME=connectDb.c  ${PROCINCLUDE}  USERID=crmcn_demo/crmcn_demo@devdb81  IRECLEN=512 MODE=ANSI DYNAMIC=ANSI  THREADS=YES  SQLCHECK=SEMANTICS
 $(CC) $(CC_FLAGS) ${INCLUDE}   connectDb.c    
 ##cp  connectDb.o  $(BIN_PATH)/connectDb.o


clean:
 /rm -rf   *.bak *.txt *.log *.o  *.lis  *.bak   *.c  connectDb  t*  run

 

 

[注意]

pc文件中的注释要用/**/, 而不能用//.

声明变量要放到函数模块的前部, 它的前面不能有其他语句。

 

【代码】

EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;

/* connectDb.pc */ 
#include "connectDb.h" 

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sqlca.h>
#include <sqlda.h>
#include <sqlcpr.h>

/**
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>

**/

 

EXEC ORACLE OPTION (RELEASE_CURSOR = YES);

EXEC SQL BEGIN DECLARE SECTION;
 #define MAX_VAR_LEN  200
 #define MAX_NAME_LEN 31
 #define DATA_ROW_NUM 100
 #define DATA_COL_NUM 20
 
 #define THREAD_NUMS   1   /* thread numbers */

 char  SQLSTATE[6];
 long  SQLCODE;
 

 sql_context  ctx[THREAD_NUMS]; 
 
EXEC SQL END DECLARE SECTION;


/* oracle database at machine 's address  */
char g_oracle_db_ipaddr[20] = "192.168.202.253";

 

/***************************************************/
 char  username[] = "crmcn_demo";
 char  password[] = "crmcn_demo";
 char  dbname[] = "devdb81";
/***************************************************/

int g_disconnect_flag = 0;

typedef struct stru_one_row {
 char one_row[DATA_COL_NUM][MAX_VAR_LEN];
} stru_one_row ;

void   enableThreads(void);
void   release_db();

int   db_connect(sql_context ctx, char *username, char *password, char *server);
void   db_close(sql_context ctx);
int   sql_process(sql_context ctx, char *psql);
int   table_isExist(sql_context ctx, char *ptablename);
int   init_context_and_connectDB(sql_context *ctx, char *username, char *password, char *servername);
void   free_context(sql_context *ctx);
int   isConnected(sql_context ctx);
int   create_TRIGGER_CDR_MAIN(sql_context ctx, char *ptime);
int   create_trigger_cdr_siglink_xxx(sql_context ctx, char *ptime);
int   isExist_Object(sql_context ctx, char *objectname);

stru_one_row *  process_only_select(sql_context ctx, char *psql, int *nCol, int *nRow);

 

static stru_one_row *  process_out(sql_context ctx, int *nCol, int *nRow);

static int   do_re_connect();
static int   ispingok(char *pIpAddr);

static void   test_sql_process();
static void    test_select();

int main(void)
{  
 test_sql_process(); 
 return 0; 
}


void test_select()

 static char sz_sql[] = "select id from  mytest ";
 int nRow = 0, nCol = 0;
 int i, j;
 int k;
 stru_one_row *p_mana_row = NULL;

 enableThreads();
  
 if( 1 != init_context_and_connectDB(&ctx[1], username, password, dbname) )
 { fprintf(stderr, "connect error/n");
  free_context(&ctx[1]);
  exit(1);
 }
 else
 {test_sql_process();
  fprintf(stderr, "db connect success/n");
 }

 for (k = 0; k < 10;  k++)
 {  
  p_mana_row = process_only_select(ctx[1],  sz_sql, &nCol, &nRow);
  if( NULL == p_mana_row  )
  { fprintf(stderr, "NULL == p_mana_row, k = [%d] /n", k);
   continue;
  }   
  
  if(0 == nRow)
  { printf("=====================no data!!!/n");
  }

  for(i = 0; i < nRow; i++)
  { for(j = 0; j < nCol; j++)
   { printf("%10s",p_mana_row[i].one_row[j]);    
   }
   printf("/n");
  }
  
  if( NULL != p_mana_row  )
  { free(p_mana_row);      
   p_mana_row = NULL;
  } 
 
 }
 
 release_db();
 
 db_close(ctx[1]);    
 free_context(&ctx[1]);
}

 

void test_sql_process()
{
 char sz_sql[256] ;
 memset(sz_sql, 0, sizeof(sz_sql) );
 snprintf(sz_sql, sizeof(sz_sql), "%s", "insert into mytest values(3, 'ccc')" );
 printf("SQL : %s/n", sz_sql);
 
 enableThreads();
 
 if( 1 != init_context_and_connectDB(&ctx[0], username, password, dbname) )
 { fprintf(stderr, "connect error/n");
  free_context(&ctx[1]);
  exit(1);
 }
 else
 {
  fprintf(stderr, "db connect success/n");
 }
 
 fprintf(stderr, "rtn==================%d/n", sql_process(ctx[0], sz_sql) );
 
 release_db();
 
 db_close(ctx[0]);    
 free_context(&ctx[0]);

}


void enableThreads(void)
{
 EXEC SQL ENABLE THREADS;
}

void release_db()
{ /** release db before close connection **/
 EXEC SQL WHENEVER SQLERROR CONTINUE;
 EXEC SQL ROLLBACK WORK RELEASE;
}

/* Oracle re connect when disconnect */
int do_re_connect()

 int nReconnect_success_flag = 1;
 int i; 
 
 while(1)
 {  
   if( -3113 == g_disconnect_flag || -1012 == g_disconnect_flag  )
   { /* try PING ORACLE DB, 0: ok, -1: fail */     
    if(-1 == ispingok(g_oracle_db_ipaddr))
    { continue;
    }
   
    
    for( i = 0; i < THREAD_NUMS; i++)
    { 
     db_close(ctx[i]);    
     free_context(&ctx[i]);
     nReconnect_success_flag &= init_context_and_connectDB(&ctx[i],  username, password, dbname);    
    }     
    
    if(1 != nReconnect_success_flag )
    { 
    #ifdef  DEBUG
     fprintf(stderr, "reconnect failure/n");
    #endif
    
     
     continue;
    }
    else
    { 
    #ifdef  DEBUG
     fprintf(stderr, "reconnect success/n");
    #endif
       
     g_disconnect_flag = 0;     
     break;
    }     
   }   
 } 
 return nReconnect_success_flag;
}


int isConnected(sql_context ctx)
{ /** Return :  1 , connected;  0, disconnect; **/
 EXEC SQL BEGIN DECLARE SECTION;
  int ncount = 0; 
 EXEC SQL END DECLARE SECTION; 
 
 EXEC SQL CONTEXT USE :ctx;
 EXEC SQL SELECT 1 INTO :ncount FROM dual;
 return ncount;
}


/************************************************************
* Function: db_connect
* Description: Logs on to the database as USERNAME/PASSWORD
* return 1:  true;  0: false;
************************************************************/
int  db_connect(sql_context ctx, char *username, char *password, char *server)

 
 EXEC SQL BEGIN DECLARE SECTION;
  char user[20], pass[20], serv[20];
 EXEC SQL END DECLARE SECTION;
 
 if(NULL==username || NULL==password  || NULL==server)
 { 
 #ifdef  DEBUG
     fprintf(stderr, "db_connect: input args error/n");
 #endif
    
  return 0;
 }

 strncpy(user, username, sizeof(user) );
 strncpy(pass, password, sizeof(pass) );
 strncpy(serv, server, sizeof(serv) ); 

 EXEC SQL WHENEVER SQLERROR GOTO connect_error;
 
 EXEC SQL CONTEXT USE :ctx;
 EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :serv;

 #ifdef  DEBUG
   fprintf(stderr, "Connected Success to ORACLE server: %s  as user %s./n", serv, user);
 #endif 
 return 1;

connect_error:
    #ifdef  DEBUG
   fprintf(stderr, "Connected Failure to ORACLE server: %s  as user %s./n", serv, user);
 #endif
    return 0;
}

/***************************************************
* Function: db_close
* Description: This routine logs off the database
***************************************************/
void db_close(sql_context ctx)
{
  EXEC SQL WHENEVER SQLERROR GOTO  db_close_error;
  EXEC SQL CONTEXT USE :ctx;
  EXEC SQL COMMIT WORK RELEASE;
 #ifdef  DEBUG
   fprintf(stderr, "DB Close!/n");
#endif
  return;
 
 db_close_error:    
 #ifdef DEBUG
   fprintf(stderr,  "/nERROR: %.*s/nSQLSTATE==%s, SQLCODE==%ld/n",
    sqlca.sqlerrm.sqlerrml,  sqlca.sqlerrm.sqlerrmc, SQLSTATE, SQLCODE);
#endif
  return;
}


/* This Function not process Select ...
 return: 1: Success,  0: Fail 
*/
int sql_process(sql_context ctx, char *psql)


  
 EXEC SQL BEGIN DECLARE SECTION;
  VARCHAR szSql_stat[4096];
 EXEC SQL END   DECLARE SECTION;
 
 
 if(NULL == psql || strlen(psql) < 1)
 { 
  return 0;
 }
 if( (0 == strncmp(psql, "select", 6)) || (0 == strncmp(psql, "SELECT", 6)) )
 { 
 #ifdef  DEBUG
   fprintf(stderr, "this not process SELECT..., Please check SQL/n");
 #endif
  return 0; 
 } 
 
 if ( strlen(psql) >= sizeof(szSql_stat.arr) -1 )
 {
 #ifdef  DEBUG
   fprintf(stderr, "input sql too long/n");
 #endif
 
  return 0;
 }
 
 
 { 
  if( !strncmp(psql, "create table", 12) || !strncmp(psql, "CREATE TABLE", 12) )
  { strcpy((void *)szSql_stat.arr, psql);
  }
  else if( !strncmp(psql, "create", 6) || !strncmp(psql, "CREATE", 6) )
  { strcpy((void *)szSql_stat.arr, psql);
  }
  else if( !strncmp(psql, "drop table", 10) || !strncmp(psql, "DROP TABLE", 10) )
  { strcpy((void *)szSql_stat.arr, psql);
  }
  else if( !strncmp(psql, "drop", 4) || !strncmp(psql, "DROP", 4) )
  { strcpy((void *)szSql_stat.arr, psql);
  }
  else if( !strncmp(psql, "truncate table", 14) || !strncmp(psql, "TRUNCATE TABLE", 14) )
  { strcpy((void *)szSql_stat.arr, psql);
  }
  else if( !strncmp(psql, "call", 4) || !strncmp(psql, "CALL", 4) )
  {   strcpy((void *)szSql_stat.arr, psql);
  }
  else
  { strcpy((void *)szSql_stat.arr, "begin  ");
   strcat((void *)szSql_stat.arr, psql);
   strcat((void *)szSql_stat.arr, "; end;");
  }
  szSql_stat.len = (unsigned short )strlen((void *)szSql_stat.arr);  
 }

 EXEC SQL CONTEXT USE :ctx;
 EXEC SQL WHENEVER SQLERROR  CONTINUE; 
 EXEC SQL EXECUTE  IMMEDIATE :szSql_stat;

 if( SQLCODE )
 { 

  fprintf(stderr, "/nERROR :szSQL==[ %s ], %.*s/nSQLSTATE==%s, SQLCODE==%ld/n",
    psql, sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc, SQLSTATE, SQLCODE);

  
  /* check if want to reconnecdt db */
  if( -1012 == SQLCODE || -3113 == SQLCODE || -3114 == SQLCODE )
  { g_disconnect_flag  = SQLCODE;
   do_re_connect();
  }
  else
  { EXEC SQL ROLLBACK ;
   return 0;
  }  
 }
 else
 { EXEC SQL COMMIT; 
  return 1;
 }
 return 1;
}

/* ptime: "20070410"
 return:  -1 --error
    0  --table not exist
    1  --success
*/
int create_TRIGGER_CDR_MAIN(sql_context ctx, char *ptime)
{

 
 char szTriggerName[31] = "TRI_CDR_MAIN_";
 char szTableName[31]   = "T_CDR_MAIN_";
 char szsql[1024];

 static char sztmp[] = " FOR EACH ROW WHEN (new.cdr_type=1) "
       "DECLARE v_calling   t_ag_number.agnumber%type; "
       "BEGIN select agnumber into v_calling from t_ag_number "
       "where tdmid = :new.tdmid and ag_ip = :new.calling_ip_port; "
       " :new.calling := v_calling; "
       " EXCEPTION WHEN OTHERS THEN  :new.calling := ' '; END ; ";
       
 if(NULL == ptime) return 0;
 
 
 /*   modify by tan 20070620
 CREATE OR REPLACE TRIGGER TRI_CDR_MAIN_20070620
 BEFORE INSERT OR UPDATE OF CDR_TYPE ON T_CDR_MAIN_20070620
 
   FOR EACH ROW WHEN (new.cdr_type=1)
   DECLARE v_calling   t_ag_number.agnumber%type;
   BEGIN select agnumber into v_calling from t_ag_number  where tdmid = :new.tdmid  and   ag_ip = :new.calling_ip_port;
 :new.calling := v_calling;
    EXCEPTION WHEN OTHERS THEN  :new.calling := ' '; END ; 
 */
 
 memset(szsql, 0, 1024);
 strcat(szTriggerName, ptime);
 strcat(szTableName, ptime);

 if( 1 == isExist_Object(ctx, szTableName) && 0 == isExist_Object(ctx, szTriggerName) )
 {
  
  strcpy(szsql, "CREATE OR REPLACE TRIGGER ");
  strcat(szsql, szTriggerName);
  strcat(szsql, " BEFORE INSERT OR UPDATE OF CDR_TYPE ON ");
  strcat(szsql, szTableName);
  strcat(szsql, sztmp);
   
  return sql_process(ctx , szsql) ;
 }
 return 0;
}

/* ptime: "20070410"
 return:  -1 --error
    0  --table not exist
    1  --success
*/
int create_trigger_cdr_siglink_xxx(sql_context ctx, char *ptime)
{
 
 
 char szTriggerName[31] = "TRI_CDR_SIGLINK_";
 char szTableName[31]   = "T_CDR_SIGLINK_";
 char szsql[1024];
 


/*
 static char sztmp[] = " REFERENCING NEW AS New OLD AS Old "
       "FOR EACH ROW  "
       "DECLARE "
        "v_sig_from T_NODE_IP.IP%type; "
        "v_sig_to   T_NODE_IP.IP%type; "
       "BEGIN "
        "SELECT n.NODE_NAME INTO  v_sig_from  FROM T_NODE_IP  i, T_NODE n "
        "where i.IP = :new.SIG_FROM  AND   i.NODE_ID = n.NODE_ID; "

        "SELECT n.NODE_NAME INTO  v_sig_to  FROM T_NODE_IP  i, T_NODE n "
        "where i.IP = :new.SIG_TO  AND i.NODE_ID = n.NODE_ID; "

        ":new.SIG_FROM  := v_sig_from;   :new.SIG_TO    := v_sig_to; "
              
        "EXCEPTION WHEN OTHERS THEN  :new.SIG_FROM  := ' '; :new.SIG_TO    := ' '; "
       "END; ";  
       */
       
 static char sztmp[] = " REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW "       
       "DECLARE "
        "v_sig_from  T_NODE_IP.IP%type; "
        "v_sig_to  T_NODE_IP.IP%type; "
        "v_party_from   T_NODE_IP.NODE_ID%type; "
        "v_party_to     T_NODE_IP.NODE_ID%type; "
        "v_count    number; "
       "BEGIN "
        " v_party_from := -1;   v_party_to := -2;  v_count    := 0;"
        "  select count(1) into v_count from t_node_ip where ip = :NEW.SIG_FROM; "
        " if v_count = 1 then "
         "SELECT n.NODE_NAME, i.NODE_ID INTO  v_sig_from, v_party_from "
         "FROM T_NODE_IP  i, T_NODE n "
         "where i.IP = :NEW.SIG_FROM  AND   i.NODE_ID = n.NODE_ID; "
         ":NEW.SIG_FROM := v_sig_from; :NEW.PARTY_FROM := v_party_from;"
        " end if; "
      " v_count    := 0; "
           " select count(1) into v_count from t_node_ip where ip = :NEW.SIG_TO;"
         " if v_count = 1 then "
        "SELECT n.NODE_NAME, i.NODE_ID  INTO  v_sig_to,  v_party_to "
        "FROM T_NODE_IP  i, T_NODE n  "
        "where i.IP = :NEW.SIG_TO  AND i.NODE_ID = n.NODE_ID; "
        
        ":NEW.SIG_TO    := v_sig_to;  :NEW.PARTY_TO  := v_party_to;"
       " end if;    "               
      "EXCEPTION WHEN OTHERS THEN NULL;  END; "; 
      
      
      if(NULL == ptime) return 0;

 /*   modify by tan 20070625
  CREATE OR REPLACE TRIGGER TRI_CDR_SIGLINK_20070620
  BEFORE INSERT  ON T_CDR_SIGLINK_20070620
  REFERENCING NEW AS New OLD AS Old
  FOR EACH ROW
  DECLARE
   v_sig_from     T_NODE_IP.IP%type;
   v_sig_to       T_NODE_IP.IP%type;  
  BEGIN
   SELECT n.NODE_NAME INTO  v_sig_from  FROM T_NODE_IP  i, T_NODE n
   where i.IP = :new.SIG_FROM  AND   i.NODE_ID = n.NODE_ID;
 
   SELECT n.NODE_NAME INTO  v_sig_to  FROM T_NODE_IP  i, T_NODE n
   where i.IP = :new.SIG_TO  AND   i.NODE_ID = n.NODE_ID;

   :new.SIG_FROM  := v_sig_from; 
   :new.SIG_TO    := v_sig_to;
       
   EXCEPTION WHEN OTHERS THEN  :new.SIG_FROM  := ' '; :new.SIG_TO    := ' ';
  END;
 */
 
 memset(szsql, 0, 1024);
 strcat(szTriggerName, ptime);
 strcat(szTableName, ptime); 
 
 if( 1 == isExist_Object(ctx, szTableName) && 0 == isExist_Object(ctx, szTriggerName) )
 {
  
  strcpy(szsql, "CREATE OR REPLACE TRIGGER ");
  strcat(szsql, szTriggerName);
  strcat(szsql, " BEFORE INSERT  ON ");
  strcat(szsql, szTableName);
  strcat(szsql, sztmp);
  
  return sql_process(ctx , szsql) ;
 }
 return 0;
}

/* Check one object if existed */
int isExist_Object(sql_context ctx, char *objectname)
{


 EXEC SQL BEGIN DECLARE SECTION;
  VARCHAR szobjectname[50];
  int  nexist;
 EXEC SQL END   DECLARE SECTION;


 if( NULL == objectname )
 {
  return -1;
 }
 
 strcpy((void *)szobjectname.arr, objectname);
 szobjectname.len = (unsigned short)strlen((void *)szobjectname.arr);
 
/*EXEC SQL CONTEXT USE :ctx;

 EXEC SQL CALL FUNC_OBJECT_ISEXIST(:szobjectname) INTO :nexist;
*/
 return nexist;
}

stru_one_row * process_only_select(sql_context ctx, char *psql, int *nCol, int *nRow)
{ /***  This Function only process select .....  **/


  int ncursor_1_open = 0;
 stru_one_row  *p_mang_row = NULL;
 
 EXEC SQL BEGIN DECLARE SECTION;
  char sql_stat[2048];
 EXEC SQL END   DECLARE SECTION;
 
 
 if(NULL == psql)
 { 
 #ifdef  DEBUG
   fprintf(stderr, "process_only_select: Please check input args/n");
 #endif
  return NULL;
 }
 if( (0 != strncmp(psql, "select", 6)) && (0 != strncmp(psql, "SELECT", 6)) )
 { 
 #ifdef  DEBUG
   fprintf(stderr, "process_only_select: This Function only process select ....., Please Check SQL./n");
 #endif
  return NULL;
 }

 

 
 EXEC SQL WHENEVER SQLERROR CONTINUE;
 EXEC SQL CONTEXT USE :ctx;  

 EXEC SQL ALLOCATE DESCRIPTOR 'output_descriptor';

 { 
  strcpy(sql_stat, psql);

  if( !ncursor_1_open)
  { EXEC SQL PREPARE S FROM :sql_stat; 
   
   if (SQLCODE != 0)
   {    
   #ifdef DEBUG
    fprintf(stderr, "/nERROR :szSQL==[ %s ], %.*s/nSQLSTATE==%s, SQLCODE==%ld/n",
    psql, sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc, SQLSTATE, SQLCODE);
   #endif
   
    EXEC SQL WHENEVER SQLERROR CONTINUE;
 
    EXEC SQL ROLLBACK;
    EXEC SQL DEALLOCATE DESCRIPTOR 'output_descriptor';
    
    if (NULL != p_mang_row)
    {
      free(p_mang_row);
      p_mang_row = NULL;      
     }
    return NULL;
   }
  
   EXEC SQL DECLARE  cursor_1  CURSOR FOR S; 
   EXEC SQL OPEN  cursor_1;
   ncursor_1_open = 1; 
      
   if (NULL == (p_mang_row =  process_out(ctx, nCol, nRow )) )
   {
    /* Close the cursor. */
     EXEC SQL WHENEVER SQLERROR CONTINUE;
    
    EXEC SQL CLOSE  cursor_1;
    ncursor_1_open = 0;
    EXEC SQL DEALLOCATE DESCRIPTOR 'output_descriptor';
    return NULL;
   }   
  }

  if(ncursor_1_open)
  { 
   EXEC SQL WHENEVER SQLERROR CONTINUE;
   EXEC SQL CLOSE  cursor_1;
   ncursor_1_open = 0;
  }
 }
 
 EXEC SQL DEALLOCATE DESCRIPTOR 'output_descriptor'; 
 
 return p_mang_row;
}

 

stru_one_row * process_out(sql_context ctx, int *nCol, int *nRow)
{
 stru_one_row   *p_many_row = NULL;
 int i, num = 0, nLen = DATA_ROW_NUM; 

 EXEC SQL BEGIN DECLARE SECTION; 
  int  output_count,  occurs, type, len;
  short indi;
  char data[MAX_VAR_LEN], name[MAX_NAME_LEN];
 EXEC SQL END   DECLARE SECTION;

 output_count = 0;
 
 EXEC SQL CONTEXT USE :ctx;
 EXEC SQL DESCRIBE OUTPUT S USING DESCRIPTOR 'output_descriptor';
 EXEC SQL GET DESCRIPTOR 'output_descriptor' :output_count = COUNT;
 type = 12;
 len = MAX_VAR_LEN;

 for(i=0; i < output_count; i++)
 {
  occurs = i+1;
  EXEC SQL SET DESCRIPTOR 'output_descriptor' VALUE :occurs TYPE = :type, LENGTH = :len;
  EXEC SQL GET DESCRIPTOR 'output_descriptor' VALUE :occurs :name = NAME; 
 }

 EXEC SQL WHENEVER NOT FOUND DO BREAK;
 
 if(output_count > DATA_COL_NUM)
 { 
 #ifdef DEBUG 
  fprintf(stderr, " COL value is too big!/n");
 #endif
 
  return NULL;
 }
 *nCol = output_count;

 if( NULL == (p_many_row=calloc(sizeof(stru_one_row), DATA_ROW_NUM)) )
 {
 #ifdef DEBUG
  perror("calloc fail!");
  fprintf(stderr, "calloc: %s ,Errno==%d/n", strerror(errno), errno);
 #endif
 
  return NULL;
 }

 for(;;)
 { 
  EXEC SQL FETCH cursor_1 INTO DESCRIPTOR 'output_descriptor';

  for(i=0; i < output_count ; i++)
  {
   occurs = i+1;

   EXEC SQL GET DESCRIPTOR 'output_descriptor' VALUE :occurs :data = DATA, :indi = INDICATOR;

    if(-1 == indi)
    {
     memcpy(p_many_row[num].one_row[i], " ", 1 );
    }
    else
    {   
     memcpy(p_many_row[num].one_row[i], data, strlen(data) );    
    }
  }
  num++; 

  if( num >= nLen )
  { 
   nLen += DATA_ROW_NUM ;   
   if( NULL == (p_many_row=realloc(p_many_row, nLen * sizeof(stru_one_row)) )  )
   {
   #ifdef DEBUG
    perror("realloc fail!");
    fprintf(stderr, "realloc: %s ,Errno==%d/n", strerror(errno), errno);
   #endif
   
    return NULL;
   }
  } 
 }  
 *nRow = num;
 return (p_many_row);
}


int table_isExist(sql_context ctx, char *ptablename)
{
 EXEC SQL BEGIN DECLARE SECTION; 
  VARCHAR sztablename[50];
  int  nexist;
 EXEC SQL END   DECLARE SECTION;
 
 if( NULL==ptablename )
 {
  return -1;
 }

 

 strcpy((void *)sztablename.arr, ptablename);
 sztablename.len = (unsigned short)strlen((void *)sztablename.arr);
 
 EXEC SQL CONTEXT USE :ctx;

 /* EXEC SQL CALL ISEXIST_TABLE(:sztablename) INTO :nexist; */

 return nexist;
}

/* return:  1: true;  0: false */
int init_context_and_connectDB(sql_context *ctx, char *username, char *password, char *servername)
{  
 if (NULL == ctx )
 {
  fprintf(stderr, "ctx is NULL/n/n");
 }
 else
 {
  fprintf(stderr, "ctx not NULL/n/n");
 }

 EXEC SQL CONTEXT ALLOCATE :(*ctx);  
 return  db_connect(*ctx,username, password, servername);  
}


void free_context(sql_context *ctx)

 if(ctx != NULL)
 {  
  EXEC SQL CONTEXT FREE :(*ctx);
 
  ctx = NULL;   
 }
}

/* input: pIpAddr --要连接的机器IPaddr
  return: 0 --ok; -1 --not ok
 */
int ispingok(char *pIpAddr)

 char *pFileName = "pingData.txt";
 char *pCmd    = "ping -c3";
 char szstring[256];
 
 FILE *infp = NULL;
 char *p = NULL, *q = NULL;
 char sztemp[10];
 char szline[100];
 int n_ping;
 
  if(NULL == pIpAddr)
 { 
  return -1;
 }
 
 memset(sztemp,   0, 10);
 memset(szline,   0, 100);
 memset(szstring,  0, 256);
 
 sprintf(szstring, "%s %s > %s", pCmd, pIpAddr, pFileName );
 #ifdef DEBUG
 printf("szstring==%s/nlen====%d/n", szstring, (int)strlen(szstring));
 #endif
 
 /* "ping -c3 192.168.202.253 > pingData.txt" 含义:
  ping 一个ip地址,  -c3为ping 3次, 将结果输入pingData.txt文件
 */
 system(szstring); 
  
 /* analyse file:"pingData.txt" */ 
 infp = fopen(pFileName, "r");
 if(NULL == infp)
 { 
 #ifdef DEBUG
  fprintf(stderr, "fopen: %s/n", strerror(errno));
 #endif
  return -1;
 }
 
 while(NULL != fgets(szline, 80, infp))
 { /*look for '%' in ping file*/
  p = strchr(szline, '%');
  if(NULL == p)
   { continue;    
   } 
   
  /* look for number before '%' in ping file ; example: ", 100%" */
  for( q = p-4; q < p ;q++ )
  { q =strchr(q, ' ');
   if(NULL != q)
    { memcpy(sztemp, q+1, p-(q+1));
     n_ping = atoi(sztemp);
     
     break;
    }
  }  
 }
 p = NULL;
 q = NULL;
 
 if(NULL != infp)
 { if(0 != fclose(infp))
  {
  #ifdef DEBUG
   fprintf(stderr, "fclose: %s/n", strerror(errno));
  #endif
   return -1;
  }
 }
 
 if(100 == n_ping)
  { return -1;
  }
 
 return 0;
}

 

原创粉丝点击