运行时初始化
在 SmartESQL 嵌入式 C 应用程序中,SmartEDB 数据库的 SQL 接口通常被称为 SQL 引擎,其数据类型为 database_t,并通过调用 mcoapi_create_engine()
函数创建。在创建 SQL 引擎之前,必须先初始化运行时环境并注册错误处理程序。
接下来,需指定内存设备和数据库参数,最后完成数据库的打开与连接操作。有关如何定义内存设备和数据库参数的详细说明,请参内存设备和打开参数页面。
以下代码片段展示了单用户应用程序在使用纯内存数据库时所遵循的这些步骤。
int main(int argc, char** argv)
{
MCO_RET rc;
mco_db_h db;
mco_device_t dev;
mco_db_params_t db_params;
database_t engine;
/* set fatal error handler */
mco_error_set_handler(SmartEDB_error_handler);
/* start SmartEDB runtime */
mco_runtime_start();
/* setup memory device as a plain conventional memory region */
dev.type = MCO_MEMORY_CONV; /* set the device as a conventional memory device */
dev.assignment = MCO_MEMORY_ASSIGN_DATABASE; /* assign the device as main database memory */
dev.size = DATABASE_SIZE; /* set the device size */
dev.dev.conv.ptr = (void*)malloc( DATABASE_SIZE ); /* allocate database memory */
if ( !dev.dev.conv.ptr )
{
SmartEDB_error_handler(MCO_E_NOMEM);
}
/* initialize and customize the database parameters */
mco_db_params_init ( &db_params ); /* initialize the params with default values */
db_params.mem_page_size = MEMORY_PAGE_SIZE; /* set page size for the in memory part */
db_params.disk_page_size = 0; /* set page size to zero to disable disk use */
db_params.db_max_connections = 1; /* set total number of database connections */
db_params.db_log_type = REDO_LOG; /* set transaction log type */
/* open a database on the device with given params */
rc = mco_db_open_dev(db_name, c_apidb_get_dictionary(), &dev, 1, &db_params );
if ( MCO_S_OK == rc )
{
/* connect to the database by name */
rc = mco_db_connect( db_name, &db );
if ( MCO_S_OK == rc )
{
...
}
}
/* Close the database, stop the SmartEDB runtime and free the database memory */
rc = mco_db_close(db_name);
mco_runtime_stop();
free( dev.dev.conv.ptr );
}
当 SmartEDB 运行时初始化完成并且建立了数据库连接,就可以通过调用 mcoapi_create_engine()
函数创建一个指向数据库(类型为 database_t
)的 SQL 接口。
rc = mco_db_connect( db_name, &db );
if ( MCO_S_OK == rc )
{
database_t engine;
rc = (MCO_RET)mcoapi_create_engine(db, &engine);
if ( MCO_S_OK == rc ) {
add_person( engine, "John Smith", "unknown", "Team leader", 75000, 80.3f, "Steve Smith");
show_persons( engine, "John Smith");
}
}
然后,可以通过调用 mcosql_execute_query()
和 mcosql_execute_statement()
来执行 SQL 语句,如以下来自 add_person()
和 show_persons()
函数的代码片段所示:
MCO_RET add_person(database_t engine, char const* name, char const* address, char const* position,
uint4 salary, float rating, char const* manager)
{
MCO_RET rc = MCO_S_OK;
...
rc = (MCO_RET)mcosql_execute_statement(engine, NULL, NULL,
"insert into Person (name,address,position,salary,rating,manager) values (%s,%s,%s,%u,%f,%p)",
name, address, position, salary, rating, manager );
...
}
MCO_RET show_persons( database_t engine, char* name_pattern )
{
MCO_RET rc = MCO_S_OK;
data_source_t data_source = NULL;
...
rc = (MCO_RET)mcosql_execute_query(engine, NULL, &data_source,
"select name,position,salary,rating,manager,address from Person where name like %s",
name_pattern );
...
}
有关初始化和销毁 SQL 引擎的更多信息,请参阅 C API SQL 引擎初始化页面。