- 论坛徽章:
- 15
|
本帖最后由 yulihua49 于 2013-11-01 10:59 编辑
还要写SQL语句?一般的包装器,insert是可以不写语句的。这点不如JAVA的Hibernate。
看看我这个包装器,C的,可以适用于MYSQL,ORACLE,DB2。称为DAU--Data Access Unit
- #include <sqli.h>
- #include <DAU.h>
- #define trans_begin(sql) ___SQL_Transaction__((sql),TRANBEGIN)
- #define trans_commit(sql) ___SQL_Transaction__((sql),TRANCOMMIT)
- #define trans_rollback(sql) ___SQL_Transaction__((sql),TRANROLLBACK)
- #define INTERVAL ((int)(now_usec() - now))
- #include "mytab.tpl"
- #include "mytab.stu"
- static int processDB(T_SQL_Connect *SQL_Connect)
- {
- DAU my_DAU;
- mytable_stu mytab;
- char stmt[4096],tmp[1024],*p;
- INT64 now;
- int ret,i;
- int today=rtoday();
- char *rec=NULL;
- DAU_init(&my_DAU,SQL_Connect,NULL,&mytab,mytable_tpl);//使用mytable表
- //test DAU_select
- p=mk_where("Name",stmt);
- strcpy(mytab.Name,"中国 123");
- // rjultostrfmt(mytab.beg_date,cvtdate("7.24",today),"yyyy\nmm\ndd");
- ret=DAU_select(&my_DAU,stmt,0);
- printf("stmt=%s,ret=%d\n",stmt,ret);
- if(ret>0) while(!DAU_next(&my_DAU)) {
- DAU_pack(&my_DAU,stmt);
- printf("select:%s\n",stmt);
- }
- //test prepare
- p=mk_where("ID",stmt);
- now=now_usec();
- for(i=0;i<46;i++) {
- mytab.ID=i+1;
- ret=DAU_prepare(&my_DAU,stmt);
- if(ret) {
- ShowLog(1,"%s:prepare fault ret=%d err=%d,%s",__FUNCTION__,ret,
- SQL_Connect->Errno,
- SQL_Connect->ErrMsg);
- DAU_free(&my_DAU);
- return ret;
- }
- while(!DAU_next(&my_DAU)) {
- DAU_pack(&my_DAU,tmp);
- printf("%s\n",tmp);
- }
- }
- ShowLog(5,"%s:prepare i=%d,INTERVAL=%d",__FUNCTION__,i,INTERVAL);
- sprintf(stmt,"select max(ID) m from %s",my_DAU.srm.tabname);
- ret=___SQL_Select__(SQL_Connect,stmt,&rec,0);
- if(ret<=0) {
- DAU_free(&my_DAU);
- if(rec) free(rec);
- return 1;
- }
- i=atoi(rec);
- free(rec);
- //test update
- mytab.ID=i;
- rjultostrfmt(mytab.beg_date,rtoday(),YEAR_TO_DAY);
- trans_begin(SQL_Connect);
- mk_where("ID",stmt);
- ret=DAU_update(&my_DAU,stmt);
- trans_commit(SQL_Connect);
- printf("stmt=%s,ret=%d\n",stmt,ret);
- //test delete
- mk_where("ID",stmt);
- ret=DAU_delete(&my_DAU,stmt);
- trans_commit(SQL_Connect);
- printf("stmt=%s,ret=%d\n",stmt,ret);
- //test insert
- rjultostrfmt(mytab.beg_date,rtoday(),YEAR_TO_DAY);
- strcpy(mytab.Name,"中国 .");
- *stmt=0;//may be INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1
- trans_begin(SQL_Connect);
- now=now_usec();
- ret=DAU_insert(&my_DAU,stmt);
- printf("stmt=%s,ret=%d,INTERVAL=%d\n",stmt,ret,INTERVAL);
- trans_commit(SQL_Connect);
- DAU_free(&my_DAU);
- return ret;
- }
- int main(int argc,char *argv[])
- {
- int ret=0;
- T_SQL_Connect *SQL_Connect;
- tzset();
- if(argc>1) {
- ret=envcfg(argv[1]); //打开哪个数据库,在配置文件里。
- if(ret) printf("envcfg=%d\n",ret);
- }
- ret=init_DB_pool();
- if(ret) {
- ShowLog(1,"%s:open DBpool fault,err=%d!",argv[0],ret);
- return 1;
- }
- ret=get_DB_connect(&SQL_Connect,0);
- if(ret) {
- ShowLog(1,"get_DB_connect error %d",ret);
- free_DB_pool();
- return 2;
- }
- ret=mysql_thread_safe();
- printf("mysql_thread_safe=%d\n",ret);
- ret=processDB(SQL_Connect);
- release_DB_connect(&SQL_Connect,0);
- free_DB_pool();
- return 0;
- }
复制代码 |
|