- 论坛徽章:
- 0
|
请问各位老鸟能不能提供一个和ORACLE数据库打交道得程序呢?
下面这个够复杂的,不过功能比较强大。
摘自:如何跨越数据库平台。
全文: http://cwqu.3322.org/itf_db.htm
- #include <itf_db.h>;
- #include <stdio.h>;
- #include <tbase.h>;
- #include <sqlca.h>;
- #include <sqlda.h>;
- #include <oraca.h>;
- EXEC ORACLE OPTION (ORACA=YES);
- #include <ctype.h>;
- #include <unistd.h>;
- int vabs(int id)
- {
- if(id<0) return 0-id;
- else
- return id;
- }
- typedef struct
- {
- int Initlized;
- char DbName[100];
- char UserName[100];
- char PassWord[100];
- }DbMgrStru;
- extern "C"
- {
- void sqlclu(SQLDA*);
- struct SQLDA * sqlald(int, unsigned int, unsigned int);
- void sqlnul(unsigned short*, unsigned short*, int*);
- };
- DbMgrStru DbMgr={0};
- int _ShowRunSql_Switch=1;
- /* 连接数据库 */
- int DbConnect(char * dbname,char * username,char * password)
- {
- EXEC SQL BEGIN DECLARE SECTION;
- char * pusername=username;
- char * ppassword=password;
- EXEC SQL END DECLARE SECTION;
- strcpy(DbMgr.UserName,pusername);
- strcpy(DbMgr.PassWord,ppassword);
- EXEC SQL CONNECT :pusername IDENTIFIED BY :ppassword;
- if(sqlca.sqlcode!=0)
- printf("connect to database error : sqlcode is %d u: %s p: %s\n",sqlca.sqlcode,pusername,ppassword);
- puts("\n");
- puts("**************************************************************");
- printf(" SIG ORA ITF IS STARTING: MaxConnection: %d\n",1);
- puts("**************************************************************");
- puts("\n");
- return 1;
- }
- int DbBeginTran(DBDataMgr * ddm)
- {
- DbExecuteSql(ddm,"begintran");
- return 1;
- }
- int DbCommit(DBDataMgr * ddm)
- {
- DbExecuteSql(ddm,"commit");
- return 1;
- }
- int DbRollback(DBDataMgr * ddm)
- {
- DbExecuteSql(ddm,"rollback");
- return 1;
- }
- int DbDisconnect()
- {
- DbExecuteSql(NULL,"commit");
- EXEC SQL COMMIT WORK RELEASE;
- return 1;
- }
- #define MAX_ITEMS 140
- #define MAX_VNAME_LEN 130
- #define MAX_INAME_LEN 130
- char * GetErrorMsg()
- {
- static char buf[1024];
- sprintf(buf,"\n%.*s\n", sqlca.sqlerrm.sqlerrml,
- sqlca.sqlerrm.sqlerrmc);
- return buf;
- }
- int DbShowError()
- {
- printf("sqlerr: sqlcode is '%d' errmsg: '%s'\n",
- sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- int DbBatchInsert(DBDataMgr * ddm)
- {
- SQLDA * bind_dp=NULL;
- SQLDA * select_dp=NULL;
- int id;
- char value[120];
- int preallocateflag=1;
- char sqlcmd[2048];
- int haveerror=0;
- EXEC SQL BEGIN DECLARE SECTION;
- int mtsid=0;
- char * psqlcmd=sqlcmd;
- EXEC SQL END DECLARE SECTION;
- struct RowListStru * row,* next;
- ColListStru * col;
- char info[120];
- int valsize;
- sprintf(sqlcmd,"insert into %s( ",ddm->;TableName);
- for(id=0;id<ddm->;FieldCount;id++)
- {
- strcat(sqlcmd,ddm->;FieldName[id]);
- if(id!=ddm->;FieldCount-1)
- strcat(sqlcmd,", ");
- };
- strcat(sqlcmd,") values(");
- for(id=0;id<ddm->;FieldCount;id++)
- {
- sprintf(value,":v%d",id);
- strcat(sqlcmd,value);
- if(id!=ddm->;FieldCount-1)
- strcat(sqlcmd,", ");
- };
- strcat(sqlcmd,")");
- printf("To run sqlcmd is '%s'\n",sqlcmd);
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,0,"");
- if ((bind_dp=sqlald(MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for bind descriptor.");
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for bind descriptor.");
- haveerror=1;
- goto err_ret_lab;
- }
- for (id = 0; id < MAX_ITEMS; id++)
- {
- bind_dp->;I[id] = NULL;
- bind_dp->;V[id] = NULL;
- }
- if ((select_dp=sqlald (MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for select descriptor.");
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for select descriptor.");
- haveerror=1;
- goto err_ret_lab;
- }
- select_dp->;N = MAX_ITEMS;
- for (id = 0; id < MAX_ITEMS; id++)
- {
- bind_dp->;I[id] = (short *) IMem.Malloc(sizeof(short));
- bind_dp->;V[id] = (char *) IMem.Malloc(1);
- select_dp->;I[id] = (short *) IMem.Malloc(sizeof(short));
- select_dp->;V[id] = (char *) IMem.Malloc(1);
- }
- bind_dp->;N = MAX_ITEMS;
- EXEC SQL PREPARE S1 FROM :psqlcmd;
- if(sqlca.sqlcode!=0)
- {
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab;
- };
- EXEC SQL DECLARE C1 CURSOR FOR S1;
- if(sqlca.sqlcode!=0)
- {
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab;
- };
- EXEC SQL DESCRIBE BIND VARIABLES FOR S1 INTO bind_dp;
- if(sqlca.sqlcode!=0)
- {
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab;
- };
- if (bind_dp->;F < 0)
- {
- printf ("\nToo many bind variables (%d), maximum is %d\n.",-bind_dp->;F, MAX_ITEMS);
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,-2001,"Too many bind variables ");
- haveerror=1;
- goto err_ret_lab;
- }
- bind_dp->;N = bind_dp->;F;
- row = ddm->;Head;
- while(1)
- {
- if(row==NULL)
- break;
- for(id=0;id<ddm->;FieldCount;id++)
- {
- col=row->;Col + id;
- valsize = (int)strlen(col->;Value);
- bind_dp->;L[id] = valsize;
- IMem.Free(bind_dp->;V[id]);
- bind_dp->;V[id] = (char *) IMem.Malloc((bind_dp->;L[id] + 1));
- strncpy(bind_dp->;V[id], col->;Value, valsize);
- if ((strncmp(bind_dp->;V[id], "NULL", 4) == 0) ||
- (strncmp(bind_dp->;V[id], "null", 4) == 0) || valsize==0)
- *bind_dp->;I[id] = -1;
- else
- *bind_dp->;I[id] = 0;
- bind_dp->;T[id] = 1;
- };
- EXEC SQL OPEN C1 USING DESCRIPTOR bind_dp;
- if(sqlca.sqlcode!=0)
- {
- if(ddm!=NULL)
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- printf("sqlerr: sqlcode is '%d' errmsg: '%s'\n",sqlca.sqlcode,GetErrorMsg());
- for(id=0;id<ddm->;FieldCount;id++)
- {
- col=row->;Col + id;
- valsize = (int)strlen(col->;Value);
- printf("id: %2d col: '%s' size: %d value: '%s'\n",id,ddm->;FieldName[id],valsize,col->;Value);
- };
- EXEC SQL ROLLBACK;
- haveerror=1;
- goto err_ret_lab;
- //return 0;
- };
- next = row->;Next;
- row = next;
- };
- EXEC SQL CLOSE C1;
- err_ret_lab:
- for (id = 0; id < MAX_ITEMS; id++)
- {
- if (bind_dp->;V[id] != NULL)
- IMem.Free(bind_dp->;V[id]);
- if(bind_dp->;I[id] !=NULL)
- IMem.Free(bind_dp->;I[id]);
- if (select_dp->;V[id] != NULL)
- IMem.Free(select_dp->;V[id]);
- if(select_dp->;I[id] != NULL)
- IMem.Free(select_dp->;I[id]);
- }
- if(bind_dp!=NULL)
- sqlclu(bind_dp);
- if(select_dp!=NULL)
- sqlclu(select_dp);
- if(haveerror)
- return 0;
- return 1;
- }
- int DbExecuteSql(DBDataMgr * ddm,char * sqlcmd)
- {
- SQLDA * bind_dp=NULL;
- SQLDA * select_dp=NULL;
- int id;
- int null_ok, precision, scale;
- char value[8192],fname[120],head[300],fieldtype;
- int mtsid=0,preallocateflag=1,fieldsize=0,fielddecsize=0;
- EXEC SQL BEGIN DECLARE SECTION;
- char * psqlcmd=sqlcmd;
- EXEC SQL END DECLARE SECTION;
- int haveerror=0;
- int isnumber[200];
- if(ddm!=NULL)
- {
- DD_Free(ddm);
- DD_Initlize(ddm);
- };
- if(_ShowRunSql_Switch)
- printf("want to run sql: '%s'\n",sqlcmd);
- sscanf(sqlcmd,"%s",head);
- scale = (int)strlen(head);
- IString.Allt(head);
- for(id=0;id<scale;id++)
- head[id]=toupper(head[id]);
- if(!strcmp(head,"BEGINTRAN"))
- return 1;
- DD_SetSqlCode(ddm,0,"");
- if ((strncmp(head,"SELECT",6)!=0))
- {
- DD_SetRecordCount(ddm,0);
- DD_SetFieldCount(ddm,0);
- EXEC SQL EXECUTE IMMEDIATE :psqlcmd;
- if(vabs(sqlca.sqlcode)==1403) sqlca.sqlcode=0;
- if(vabs(sqlca.sqlcode)==1405) sqlca.sqlcode=0;
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- if(sqlca.sqlcode!=0)
- DbShowError();
- return 1;
- };
- if ((bind_dp=sqlald(MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for bind descriptor.");
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for bind descriptor.");
- haveerror=1;
- goto err_ret_lab_sql;
- }
- for (id = 0; id < MAX_ITEMS; id++)
- {
- bind_dp->;I[id] = NULL;
- bind_dp->;V[id] = NULL;
- };
- if ((select_dp=sqlald (MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for select descriptor.");
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for select descriptor.");
- haveerror=1;
- goto err_ret_lab_sql;
- }
- select_dp->;N = MAX_ITEMS;
- for (id = 0; id < MAX_ITEMS; id++)
- {
- bind_dp->;I[id] = (short *) IMem.Malloc(sizeof(short),"binddp_i");
- bind_dp->;V[id] = (char *) IMem.Malloc(1,"binddp_v");
- select_dp->;I[id] = (short *) IMem.Malloc(sizeof(short),"selectdp_i");
- select_dp->;V[id] = (char *) IMem.Malloc(1,"selectdp_v");
- }
- bind_dp->;N = MAX_ITEMS;
- EXEC SQL PREPARE S0 FROM :psqlcmd;
- if(sqlca.sqlcode!=0)
- {
- printf("prepare error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab_sql;
- };
- EXEC SQL DECLARE C0 CURSOR FOR S0;
- if(sqlca.sqlcode!=0)
- {
- printf("declare error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab_sql;
- };
- EXEC SQL DESCRIBE BIND VARIABLES FOR S0 INTO bind_dp;
- if(sqlca.sqlcode!=0)
- {
- printf("describe error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab_sql;
- };
- if (bind_dp->;F < 0)
- {
- printf ("\nToo many bind variables (%d), maximum is %d\n.",-bind_dp->;F, MAX_ITEMS);
- DD_SetSqlCode(ddm,-2001,"Too many bind variables ");
- haveerror=1;
- goto err_ret_lab_sql;
- }
- bind_dp->;N = bind_dp->;F;
- EXEC SQL OPEN C0 USING DESCRIPTOR bind_dp;
- /* 无记录 */
- if(vabs(sqlca.sqlcode)==1403) sqlca.sqlcode=0;
- /* 提取的值为空 */
- if(vabs(sqlca.sqlcode)==1405) sqlca.sqlcode=0;
- if(sqlca.sqlcode!=0)
- {
- printf("open error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab_sql;
- };
- select_dp->;N = MAX_ITEMS;
- EXEC SQL DESCRIBE SELECT LIST FOR S0 INTO select_dp;
- if (select_dp->;F < 0)
- {
- printf ("\nToo many select - list items (%d), maximum is %d\n",
- -(select_dp->;F), MAX_ITEMS);
- DD_SetSqlCode(ddm,-2001,"Too many select - list items");
- haveerror=1;
- goto err_ret_lab_sql;
- }
- if(sqlca.sqlcode!=0)
- {
- printf("describe select list error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- haveerror=1;
- goto err_ret_lab_sql;
- };
- DD_SetRecordCount(ddm,0);
- DD_SetFieldCount(ddm,select_dp->;F);
- select_dp->;N = select_dp->;F;
- for (id = 0; id < select_dp->;F; id++)
- {
- isnumber[id]=0;
- sqlnul ((unsigned short *)&(select_dp->;T[id]), (unsigned short *)&(select_dp->;T[id]), &null_ok);
- switch (select_dp->;T[id])
- {
- /* 这里是字符类型 */
- case 1 :
- break;
- /* 这里是数值类型,要确定小数点,强行转为字符型了 */
- case 2 :
- isnumber[id]=1;
- select_dp->;T[id] = 1;
- select_dp->;L[id] = 100;
- precision = 50;
- scale = 6;
- #ifdef MODIFIED_BY_PROGRAM_NOTE
- sqlprc (&(select_dp->;L[id]), &precision, &scale);
- if (precision == 0) precision = 40;
- if (scale >; 0)
- select_dp->;L[id] = sizeof(float);
- else
- select_dp->;L[id] = sizeof(int);
- #endif
- break;
- /* LONG类型 */
- case 8 :
- select_dp->;L[id] = 240;
- break;
- /* ROWID 类型 */
- case 11 :
- select_dp->;L[id] = 18;
- break;
- /* 日期类型 */
- case 12 :
- select_dp->;L[id] = 9;
- break;
- /* RAW 类型 */
- case 23 :
- break;
- /* LONG RAW 类型 */
- case 24 :
- select_dp->;L[id] = 240;
- break;
- }
- if (select_dp->;T[id] != 2) /* 不是数字类型 */
- {
- fieldtype = 'C';
- IMem.Free(select_dp->;V[id]);
- select_dp->;V[id] = (char *) IMem.Malloc(select_dp->;L[id] + 1,"selectdp_vrel");
- //select_dp->;V[id] = (char *) realloc(select_dp->;V[id],select_dp->;L[id] + 1);
- }
- else
- {
- fieldtype = 'N';
- IMem.Free(select_dp->;V[id]);
- select_dp->;V[id] = (char *) IMem.Malloc(select_dp->;L[id] + 1,"selectdp_vrel");
- //select_dp->;V[id] = (char *) realloc(select_dp->;V[id],select_dp->;L[id]);
- };
- /* 不是LONGRAW 与数字类型,均转为字符类型 */
- if (select_dp->;T[id] != 24 && select_dp->;T[id] != 2)
- select_dp->;T[id] = 1;
- /* 数字类型,判断小数点 */
- if (select_dp->;T[id] == 2)
- if (scale >; 0)
- select_dp->;T[id] = 4;
- else
- select_dp->;T[id] = 3;
- /* 字段名称长度要限制 */
- if((int)strlen(select_dp->;S[id])>;4096)
- *(select_dp->;S[id] + 4096) = 0;
- /* 取字段名称 */
- strcpy(value,select_dp->;S[id]);
- IString.Allt(value);
- sscanf(value,"%s",fname);
- /*
- printf("Field %d is '%s' type %d\n",id,fname,select_dp->;T[id]);
- */
- DD_SetFieldName(ddm,id,fname);
- DD_SetFieldType(ddm,id,fieldtype);
- DD_SetFieldSize(ddm,id,123);
- DD_SetFieldDecSize(ddm,id,789);
- }
- while(1)
- {
- if(sqlca.sqlcode!=0)
- {
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- DbShowError();
- break;
- };
- EXEC SQL FETCH C0 USING DESCRIPTOR select_dp;
- /* 无记录了 */
- if(vabs(sqlca.sqlcode)==1403)
- break;
- /* 提取的值为空 */
- if(vabs(sqlca.sqlcode)==1405) sqlca.sqlcode=0;
- /* 有错误了 */
- if(vabs(sqlca.sqlcode)!=0)
- {
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- DbShowError();
- break;
- };
- /* printf("select_dp_F is %d\n",select_dp->;F); */
- DD_AddRow(ddm);
- for (id = 0; id < select_dp->;F; id++)
- {
- if (*select_dp->;I[id] < 0)
- {
- if (select_dp->;T[id] == 4)
- {
- sprintf (value,"%-*c ",(int)select_dp->;L[id]+3, ' ');
- }
- else
- {
- sprintf (value,"%-*c ",(int)select_dp->;L[id], ' ');
- };
- }
- else
- {
- if (select_dp->;T[id] == 3)
- {
- sprintf (value,"%*d ", (int)select_dp->;L[id],*(int *)select_dp->;V[id]);
- }
- else
- if (select_dp->;T[id] == 4)
- {
- sprintf (value,"%*.2f ", (int)select_dp->;L[id],*(float *)select_dp->;V[id]);
- }
- else
- {
- if(select_dp->;L[id]>;1500)
- {
- memcpy(value,select_dp->;V[id],1500);
- value[1500]=0;
- }
- else
- sprintf (value,"%-*.*s ", (int)select_dp->;L[id],(int)select_dp->;L[id], select_dp->;V[id]);
- }
- };
- if(isnumber[id])
- IString.Allt(value);
- else
- IString.AlltRight(value);
- DD_SetValue(ddm,id,value);
- /*
- printf("field %2d type: %d value is '%s' - mtsid: %d - %d\n",
- id,select_dp->;T[id],value,mtsid,thr_self());
- */
- }
- }
- EXEC SQL CLOSE C0;
- err_ret_lab_sql:
- for (id = 0; id < MAX_ITEMS; id++)
- {
- if (bind_dp->;V[id] != NULL)
- IMem.Free(bind_dp->;V[id]);
- if (bind_dp->;I[id] != NULL)
- IMem.Free(bind_dp->;I[id]);
- if (select_dp->;V[id] != NULL)
- IMem.Free(select_dp->;V[id]);
- if(select_dp->;I[id] != NULL)
- IMem.Free(select_dp->;I[id]);
- }
- if(bind_dp!=NULL)
- sqlclu(bind_dp);
- if(select_dp!=NULL)
- sqlclu(select_dp);
- if(haveerror)
- return 0;
- return 1;
- }
- int DbSelectRow(DBDataMgr * ddm,char * sqlcmd,int (* data_handle)(void *,void *),void * dbioptr,void * ass_ptr)
- {
- SQLDA * bind_dp;
- SQLDA * select_dp;
- int id;
- int null_ok, precision, scale;
- char value[8192],fname[120],head[300];
- int mtsid=0,preallocateflag=1;
- EXEC SQL BEGIN DECLARE SECTION;
- char * psqlcmd=sqlcmd;
- EXEC SQL END DECLARE SECTION;
- int isnumber[200];
- if(ddm!=NULL)
- {
- DD_Free(ddm);
- DD_Initlize(ddm);
- };
- if(_ShowRunSql_Switch)
- printf("want to run sql: '%s'\n",sqlcmd);
- sscanf(sqlcmd,"%s",head);
- scale = (int)strlen(head);
- IString.Allt(head);
- for(id=0;id<scale;id++)
- head[id]=toupper(head[id]);
- if(!strcmp(head,"BEGINTRAN"))
- return 1;
- DD_SetSqlCode(ddm,0,"");
- if ((strncmp(head,"SELECT",6)!=0))
- return 0;
- if ((bind_dp=sqlald(MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for bind descriptor.");
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for bind descriptor.");
- return 0;
- }
- if ((select_dp=sqlald (MAX_ITEMS,MAX_VNAME_LEN,MAX_INAME_LEN)) == (SQLDA *) 0)
- {
- printf("Cannot allocate memory for select descriptor.");
- DD_SetSqlCode(ddm,-2001,"Cannot allocate memory for select descriptor.");
- return 0;
- }
- select_dp->;N = MAX_ITEMS;
- for (id = 0; id < MAX_ITEMS; id++)
- {
- bind_dp->;I[id] = (short *) IMem.Malloc(sizeof(short));
- bind_dp->;V[id] = (char *) IMem.Malloc(1);
- select_dp->;I[id] = (short *) IMem.Malloc(sizeof(short));
- select_dp->;V[id] = (char *) IMem.Malloc(1);
- }
- bind_dp->;N = MAX_ITEMS;
- EXEC SQL PREPARE S0 FROM :psqlcmd;
- if(sqlca.sqlcode!=0)
- {
- printf("prepare error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- EXEC SQL DECLARE C5 CURSOR FOR S0;
- if(sqlca.sqlcode!=0)
- {
- printf("declare error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- EXEC SQL DESCRIBE BIND VARIABLES FOR S0 INTO bind_dp;
- if(sqlca.sqlcode!=0)
- {
- printf("describe error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- if (bind_dp->;F < 0)
- {
- printf ("\nToo many bind variables (%d), maximum is %d\n.",-bind_dp->;F, MAX_ITEMS);
- DD_SetSqlCode(ddm,-2001,"Too many bind variables ");
- return 0;
- }
- bind_dp->;N = bind_dp->;F;
- EXEC SQL OPEN C5 USING DESCRIPTOR bind_dp;
- /* 无记录 */
- if(vabs(sqlca.sqlcode)==1403) sqlca.sqlcode=0;
- /* 提取的值为空 */
- if(vabs(sqlca.sqlcode)==1405) sqlca.sqlcode=0;
- if(sqlca.sqlcode!=0)
- {
- printf("open error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- select_dp->;N = MAX_ITEMS;
- EXEC SQL DESCRIBE SELECT LIST FOR S0 INTO select_dp;
- if (select_dp->;F < 0)
- {
- printf ("\nToo many select - list items (%d), maximum is %d\n",
- -(select_dp->;F), MAX_ITEMS);
- DD_SetSqlCode(ddm,-2001,"Too many select - list items");
- return 0;
- }
- if(sqlca.sqlcode!=0)
- {
- printf("describe select list error %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- return 0;
- };
- DD_SetRecordCount(ddm,0);
- DD_SetFieldCount(ddm,select_dp->;F);
- select_dp->;N = select_dp->;F;
- for (id = 0; id < select_dp->;F; id++)
- {
- sqlnul ((unsigned short *)&(select_dp->;T[id]), (unsigned short *)&(select_dp->;T[id]), &null_ok);
- isnumber[id]=0;
- switch (select_dp->;T[id])
- {
- /* 这里是字符类型 */
- case 1 :
- break;
- /* 这里是数值类型,要确定小数点,强行转为字符型了 */
- case 2 :
- isnumber[id]=1;
- select_dp->;T[id] = 1;
- select_dp->;L[id] = 100;
- precision = 50;
- scale = 6;
- #ifdef MODIFIED_BY_PROGRAM_NOTE
- sqlprc (&(select_dp->;L[id]), &precision, &scale);
- if (precision == 0) precision = 40;
- if (scale >; 0)
- select_dp->;L[id] = sizeof(float);
- else
- select_dp->;L[id] = sizeof(int);
- #endif
- break;
- /* LONG类型 */
- case 8 :
- select_dp->;L[id] = 240;
- break;
- /* ROWID 类型 */
- case 11 :
- select_dp->;L[id] = 18;
- break;
- /* 日期类型 */
- case 12 :
- select_dp->;L[id] = 9;
- break;
- /* RAW 类型 */
- case 23 :
- break;
- /* LONG RAW 类型 */
- case 24 :
- select_dp->;L[id] = 240;
- break;
- }
- IMem.Free(select_dp->;V[id]);
- if (select_dp->;T[id] != 2) /* 不是数字类型 */
- select_dp->;V[id] = (char *) IMem.Malloc(select_dp->;L[id] + 1);
- else
- select_dp->;V[id] = (char *) IMem.Malloc(select_dp->;L[id]);
- /* 不是LONGRAW 与数字类型,均转为字符类型 */
- if (select_dp->;T[id] != 24 && select_dp->;T[id] != 2)
- select_dp->;T[id] = 1;
- /* 数字类型,判断小数点 */
- if (select_dp->;T[id] == 2)
- if (scale >; 0)
- select_dp->;T[id] = 4;
- else
- select_dp->;T[id] = 3;
- /* 字段名称长度要限制 */
- if((int)strlen(select_dp->;S[id])>;4096)
- *(select_dp->;S[id] + 4096) = 0;
- /* 取字段名称 */
- strcpy(value,select_dp->;S[id]);
- if(isnumber[id])
- IString.Allt(value);
- else
- IString.AlltRight(value);
- sscanf(value,"%s",fname);
- /*
- printf("Field %d is '%s' type %d\n",id,fname,select_dp->;T[id]);
- */
- DD_SetFieldName(ddm,id,fname);
- }
- while(1)
- {
- if(sqlca.sqlcode!=0)
- {
- printf("1-sqlcode is %d\n",sqlca.sqlcode);
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- break;
- };
- EXEC SQL FETCH C5 USING DESCRIPTOR select_dp;
- /* 无记录了 */
- if(vabs(sqlca.sqlcode)==1403)
- break;
- /* 提取的值为空 */
- if(vabs(sqlca.sqlcode)==1405) sqlca.sqlcode=0;
- /* 有错误了 */
- if(vabs(sqlca.sqlcode)!=0)
- {
- DD_SetSqlCode(ddm,sqlca.sqlcode,GetErrorMsg());
- printf("2-sqlcode is %d\n",sqlca.sqlcode);
- break;
- };
- /* printf("select_dp_F is %d\n",select_dp->;F); */
- if(ddm->;RecordCount==0)
- DD_AddRow(ddm);
- for (id = 0; id < select_dp->;F; id++)
- {
- if (*select_dp->;I[id] < 0)
- {
- if (select_dp->;T[id] == 4)
- {
- sprintf (value,"%-*c ",(int)select_dp->;L[id]+3, ' ');
- }
- else
- {
- sprintf (value,"%-*c ",(int)select_dp->;L[id], ' ');
- };
- }
- else
- {
- if (select_dp->;T[id] == 3)
- {
- sprintf (value,"%*d ", (int)select_dp->;L[id],*(int *)select_dp->;V[id]);
- }
- else
- if (select_dp->;T[id] == 4)
- {
- sprintf (value,"%*.2f ", (int)select_dp->;L[id],*(float *)select_dp->;V[id]);
- }
- else
- {
- if(select_dp->;L[id]>;1500)
- {
- memcpy(value,select_dp->;V[id],1500);
- value[1500]=0;
- }
- else
- sprintf (value,"%-*.*s ", (int)select_dp->;L[id],(int)select_dp->;L[id], select_dp->;V[id]);
- }
- };
- IString.Allt(value);
- DD_SetValue(ddm,id,value);
- }
- if(data_handle!=NULL)
- if(!data_handle(dbioptr,ass_ptr)) break;
- }
- EXEC SQL CLOSE C5;
- for (id = 0; id < MAX_ITEMS; id++)
- {
- if (bind_dp->;V[id] != (char *) 0)
- IMem.Free(bind_dp->;V[id]);
- IMem.Free(bind_dp->;I[id]);
- if (select_dp->;V[id] != (char *) 0)
- IMem.Free(select_dp->;V[id]);
- IMem.Free(select_dp->;I[id]);
- }
- sqlclu(bind_dp);
- sqlclu(select_dp);
- return 1;
- }
复制代码 |
|