📄 informix.ec
字号:
} } break; case SQLVCHAR: case SQLCHAR: EXEC SQL get descriptor :sel_desc VALUE :i :char_data = DATA, :ind = INDICATOR; if(ind ==-1) break; char_data[psel_data->col_defs[i-1].size] =0; delspaces(char_data); strcpy(psel_data->pbufs[i-1], char_data); break; default: printf("db_sel_fetch: unkown type:%d\n", type); } } return 1;err_ret: EXEC SQL whenever sqlerror continue; db_err("db_sel_fetch"); return -1;}/* db_sel_free: 释放资源 */void db_sel_free(DB_SEL_DATA *psel_data){ int i; EXEC SQL BEGIN DECLARE SECTION; char sel_stmt[15]; char sel_cursor[15]; char sel_desc[15]; EXEC SQL END DECLARE SECTION; if(psel_data->stmt_num >=0) { sprintf(sel_stmt, "stmt%d", psel_data->stmt_num); EXEC SQL free :sel_stmt; } if(psel_data->cursor_num >=0) { sprintf(sel_cursor, "curs%d", psel_data->cursor_num); EXEC SQL free :sel_cursor; } if(psel_data->desc_num >=0) { sprintf(sel_desc, "desc%d", psel_data->desc_num); EXEC SQL deallocate descriptor :sel_desc; } if(psel_data->pbufs) for(i =0; i<psel_data->col_count; i++) { if(psel_data->pbufs[i]) free(psel_data->pbufs[i]); } psel_data->pbufs =NULL;}/* db_err: 数据库错误 */void db_err(char *pre){ static char message1[256], message[200]; int sql_num, isam_num, msg_num; if((sql_num = sqlca.sqlcode) >= 0) return; isam_num = sqlca.sqlerrd[1]; msg_num = rgetmsg(sql_num, message, sizeof(message)); g_db_rc =sql_num; sprintf(message1, "%s:%s, sql error code = %d, ISAM error code =%d", pre, message, sql_num, isam_num);#ifndef NO_LOG log_err_no_warning(message1);#endif}/* db_bind_alloc: 分配bind需要的buf */int db_bind_alloc(DB_BIND_DATA *pbind_data){ int i, j; if(pbind_data ==NULL) return -1; if((pbind_data->rows =(DB_ROW_DATA *)malloc(sizeof(DB_ROW_DATA)*pbind_data->row_count)) ==NULL) {#ifndef NO_LOG log_err_no_warning("db_bind_alloc: malloc rows failed!\n");#endif return -1; } memset(pbind_data->rows, 0, sizeof(DB_ROW_DATA)*pbind_data->row_count); for(i =0; i<pbind_data->row_count; i++) { if((pbind_data->rows[i].cols =(DB_COL_DATA *)malloc(sizeof(DB_COL_DATA)*pbind_data->col_count)) ==NULL) {#ifndef NO_LOG log_err_no_warning("db_bind_alloc: malloc cols failed!\n");#endif return -1; } memset(pbind_data->rows[i].cols, 0, sizeof(DB_COL_DATA)*pbind_data->col_count); for(j =0; j<pbind_data->col_count; j++) { if((pbind_data->rows[i].cols[j].pvalue =(char *)malloc(pbind_data->col_defs[j].size+1)) ==NULL) {#ifndef NO_LOG log_err_no_warning("db_bind_alloc: malloc col pvalue failed!\n");#endif return -1; } memset(pbind_data->rows[i].cols[j].pvalue, 0, pbind_data->col_defs[j].size+1); } } return 0;}/* db_bind_free: 释放bind分配的buf */void db_bind_free(DB_BIND_DATA *pbind_data){ int i, j; if(pbind_data ==NULL || pbind_data->rows ==NULL) return; for(i =0; i<pbind_data->row_count; i++) { if(pbind_data->rows[i].cols ==NULL) break; for(j =0; j<pbind_data->col_count; j++) { if(pbind_data->rows[i].cols[j].pvalue !=NULL) { free(pbind_data->rows[i].cols[j].pvalue); pbind_data->rows[i].cols[j].pvalue =NULL; } } free(pbind_data->rows[i].cols); pbind_data->rows[i].cols =NULL; } free(pbind_data->rows); pbind_data->rows =NULL;}/* db_bind_exec: 执行批量数据插入,更新. 返回成功的行数 */int db_bind_exec(DB_BIND_DATA *pbind_data, int count, int f_err_continue){ int i, count_success =0; char temp[200]; char *func ="unkown function"; EXEC SQL BEGIN DECLARE SECTION; int j, col_count; char sqlstm[256]; short bind_type; int int_data; short short_data; float sm_float_data; double float_data; datetime year to second dt_data; char char_data[256]; short char_len; EXEC SQL END DECLARE SECTION; if(pbind_data ==NULL) return -1; if(count > pbind_data->row_count) count =pbind_data->row_count; EXEC SQL whenever sqlerror goto err_ret; switch(pbind_data->type) { case BIND_TYPE_INSERT: sprintf(sqlstm, "insert into %s values(", pbind_data->table_name); for(i =0; i<pbind_data->col_count; i++) { if(i) strcat(sqlstm, ","); strcat(sqlstm, "?"); } strcat(sqlstm, ")"); break; case BIND_TYPE_UPDATE: sprintf(sqlstm, "update %s set ", pbind_data->table_name); for(i =0; i<pbind_data->col_count; i++) { if(i) strcat(sqlstm, ","); strcat(sqlstm, pbind_data->col_defs[i].name); strcat(sqlstm, "="); strcat(sqlstm, "?"); } strcat(sqlstm, ")"); break; default: return -1; } col_count =pbind_data->col_count; func ="prepare sqlstm"; EXEC SQL prepare bind_stmt from :sqlstm; func ="allocate desc"; EXEC SQL allocate descriptor 'bind_desc'; func ="declare bind_cursor"; EXEC SQL declare bind_cursor cursor for bind_stmt; func ="open bind_cursor"; EXEC SQL open bind_cursor; func ="set desc count"; EXEC SQL set descriptor 'bind_desc' COUNT=:col_count; func ="desc bind_stmt"; EXEC SQL describe bind_stmt using sql descriptor "bind_desc";/* 如果有错误时继续,则处理SQLCODE[0],填充rows[i].rc */ EXEC SQL whenever sqlerror continue; /*db_begin_trans();*/ count_success =0; for(i =0; i <count; i++) { for(j =1; j<=pbind_data->col_count; j++) { switch(pbind_data->col_defs[j-1].type) { case SQLT_STR: strcpy(char_data, pbind_data->rows[i].cols[j-1].pvalue); bind_type =SQLCHAR; func ="set desc value"; char_len =strlen(char_data)+1; EXEC SQL set descriptor 'bind_desc' value :j type =:bind_type, length =:char_len, data =:char_data; break; case SQLT_FLT: float_data = *(double *)pbind_data->rows[i].cols[j-1].pvalue; bind_type =SQLFLOAT; func ="set desc value"; EXEC SQL set descriptor 'bind_desc' value :j type =:bind_type, data =:float_data; break; case SQLT_INT: int_data =*(int *)pbind_data->rows[i].cols[j-1].pvalue; bind_type =SQLINT; func ="set desc value"; EXEC SQL set descriptor 'bind_desc' value :j type =:bind_type, data =:int_data; break; case SQLT_DATETIME: dtcvasc(pbind_data->rows[i].cols[j-1].pvalue, &dt_data); bind_type =SQLDTIME; func ="set desc value"; EXEC SQL set descriptor 'bind_desc' value :j type =:bind_type, data =:dt_data; break; default: sprintf(temp, "bind row%d,col%d %s:unkown type:%d, size=%d", i, j-1, pbind_data->col_defs[j-1].name, pbind_data->col_defs[j-1].type, pbind_data->col_defs[j-1].size); log_err_no_warning(temp); EXEC SQL free bind_stmt; EXEC SQL free bind_cursor; EXEC SQL deallocate descriptor 'bind_desc'; return -1; } /* end switch */ if(sqlca.sqlcode <0) { pbind_data->rows[i].rc =sqlca.sqlcode; db_err("db_bind_exec set col"); } } sprintf(temp, "db_bind_exec put row %d", i); func =temp; EXEC SQL put bind_cursor using sql descriptor 'bind_desc'; func ="null";printf("i:%d, sqlcode:%d,%d, 0:%c 1:%c sqlerrd=%d, %d, %d, %d, %d, %d\n", i, sqlca.sqlcode, SQLCODE, SQLSTATE[0], SQLSTATE[1], sqlca.sqlerrd[0], sqlca.sqlerrd[1], sqlca.sqlerrd[2], sqlca.sqlerrd[3], sqlca.sqlerrd[4], sqlca.sqlerrd[5]);/*if(i %100 ==0) sleep(2);*/ if(sqlca.sqlcode !=0) { pbind_data->rows[i].rc =sqlca.sqlcode; db_err(temp); if(!f_err_continue) break; } else count_success++; } /*db_commit();*/ EXEC SQL free bind_stmt; EXEC SQL free bind_cursor; if(sqlca.sqlcode <0) { db_err("free bind_cursor"); } EXEC SQL deallocate descriptor 'bind_desc'; return count_success;err_ret: db_err(func); EXEC SQL free bind_stmt; EXEC SQL free bind_cursor; EXEC SQL deallocate descriptor 'bind_desc'; EXEC SQL whenever sqlerror continue;printf(sqlstm);printf("\n"); return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -