//定义各种变量。录制生成的脚本中,这些变量的定义在vdf.h中。
#include "lrd.h"
static LRD_INIT_INFO InitInfo = {LRD_INIT_INFO_EYECAT};
static LRD_DEFAULT_DB_VERSION DBTypeVersion[] =
{
{LRD_DBTYPE_NONE, LRD_DBVERSION_NONE}
};
static unsigned long uliRowsProcessed;
static unsigned long uliFetchedRows;
static LRD_VAR_DESC COUNT_D1 =
{LRD_VAR_DESC_EYECAT, 100, 8, LRD_DBTYPE_ORACLE, {1, 1, 0}, DT_FLT8};
static void FAR * OraEnv1;
static void FAR * OraSvc1;
static void FAR * OraStm1;
static void FAR * OraStm2;
static void FAR * OraDef1;
static void FAR * OraSrv1;
static void FAR * OraSes1;
int i;
double time_elapsed;
merc_timer_handle_t timer;
vuser_init()
{
//初始化数据库环境
lrd_init(&InitInfo, DBTypeVersion);
lrd_initialize_db(LRD_DBTYPE_ORACLE, 3, 0);
lrd_env_init(LRD_DBTYPE_ORACLE, &OraEnv1, 0, 0);
lrd_ora8_handle_alloc(OraEnv1, SVCCTX, &OraSvc1, 0);
lrd_ora8_handle_alloc(OraEnv1, SERVER, &OraSrv1, 0);
lrd_ora8_handle_alloc(OraEnv1, SESSION, &OraSes1, 0);
//设置要连接的数据库
lrd_server_attach(OraSrv1, "GIS", 3, 0, 0);
lrd_ora8_attr_set(OraSes1, USERNAME, "ims", -1, 0);
lrd_ora8_attr_set(OraSes1, PASSWORD, "ims", -1, 0);
//开始连接数据库
lrd_ora8_attr_set_from_handle(OraSvc1, SERVER, OraSrv1, 0, 0);
lrd_ora8_attr_set_from_handle(OraSvc1, SESSION, OraSes1, 0, 0);
lrd_session_begin(OraSvc1, OraSes1, 1, 0, 0);
return 0;
}
Action()
{
//删除数据
lrd_ora8_handle_alloc(OraEnv1, STMT, &OraStm1, 0);
lrd_ora8_stmt(OraStm1, "delete from tb_data_gps t where gpstime "
"between to_date('2010-9-27 00:00:00','yyyy-MM-dd HH24:mi:ss') "
"and to_date('2010-9-27 23:59:59','yyyy-MM-dd HH24:mi:ss')\n", 1, 0, 0);
lrd_ora8_attr_set(OraStm1, PREFETCH_ROWS, "0", -1, 0);
lrd_ora8_attr_set(OraStm1, PREFETCH_MEMORY, "0", -1, 0);
lrd_ora8_exec(OraSvc1, OraStm1, 1, 0, &uliRowsProcessed, 0, 0, 0, 0, 0);
lrd_handle_free(&OraStm1, 0);
lrd_ora8_commit(OraSvc1, 0, 0);
for(i=0; i<100; i++)
{
timer = lr_start_timer();
lr_think_time(60);
//查询数据
lrd_ora8_handle_alloc(OraEnv1, STMT, &OraStm2, 0);
lrd_ora8_stmt(OraStm2, "select count(*) from tb_data_gps t where gpstime "
"between to_date('2010-9-27 00:00:00','yyyy-MM-dd HH24:mi:ss') "
"and to_date('2010-9-27 23:59:59','yyyy-MM-dd HH24:mi:ss')\n", 1, 0, 0); lrd_ora8_attr_set(OraStm2, PREFETCH_ROWS, "0", -1, 0);
lrd_ora8_attr_set(OraStm2, PREFETCH_MEMORY, "0", -1, 0);
lrd_ora8_exec(OraSvc1, OraStm2, 0, 0, &uliRowsProcessed, 0, 0, 0, 0, 0);
//绑定查询结果的每个字段,后续lrd_ora8_fetch才能执行。
lrd_ora8_bind_col(OraStm2, &OraDef1, 1, &COUNT_D1, 0, 0);
//保存第1列第1行单元格的数据到参数Records中,这个函数一般用在fetch之前。
lrd_ora8_save_col(OraStm2, 1, 1, 0, "Records");
//第5个参数为PrintRow1表示打印表格出来(在print.inl文件中定义的用来把表格打出来的函数),为0表示不打印。
lrd_ora8_fetch(OraStm2, -1, 1, &uliFetchedRows, 0, 2, 0, 0);
lrd_handle_free(&OraStm2, 0);
lr_output_message("记录数:%s", lr_eval_string("{Records}"));
time_elapsed = lr_end_timer(timer);
lr_output_message ("花费时间:%g", time_elapsed);
}
return 0;
}
vuser_end()
{
//释放连接数据库
lrd_session_end(OraSvc1, OraSes1, 0, 0); lrd_server_detach(OraSrv1, 0, 0); lrd_handle_free(&OraSrv1, 0);
lrd_handle_free(&OraSvc1, 0);
lrd_handle_free(&OraSes1, 0);
lrd_handle_free(&OraEnv1, 0);
return 0;
}