doc_and_tools/tools/sdk/include/db/database.h

71 lines
2.8 KiB
C++

// database.h: simple database interface
//
// Author: Gongbing
//
// Date: 2020-09-11
#pragma once
#ifndef _INCLUDED_REF_
#define _INCLUDED_REF_
#include "../ref/ref.h"
#endif
#include <OAIdl.h> // for VARIANT
#define MAX_TABLE_FIELD_NAME_LEN 20
namespace db_util
{
enum // Driver type
{
DBDRIVER_ODBC_DBASE = 533,
DBDRIVER_ODBC_EXCEL = 790,
DBDRIVER_ODBC_ACCESS = 25,
DBDRIVER_ODBC_VFDB,
DBDRIVER_ODBC_VFTABLE,
DBDRIVER_FILE_SQLITE = 1000,
};
typedef struct _table_fields
{
wchar_t name[MAX_TABLE_FIELD_NAME_LEN];
VARIANT default_val; // the field 'type' is defining the field type too.
// NOW support VT_BOOL, VT_BSTR, VT_DATE, VT_I1(to VT_I8), VT_R4, VT_R8
// VT_DATE use function VariantTimeToSystemTime to convert !!!
int length; // 0 is not set, always for TEXT field
bool unique;
bool not_null;
bool primary_key;
bool foreign_key;
bool use_default_val; // whether use the 'default_val' in table as the field's default value
}TBLFLD;
__declspec(novtable) struct IRecordset : public ref_util::IRef
{
COM_API_DECLARE(int, get_rows(void)); // get count of records
COM_API_DECLARE(int, get_cols(void)); // get count of record fields
COM_API_DECLARE(bool, get_value(int row, int col, bool* boolean));
COM_API_DECLARE(bool, get_value(int row, int col, int* integer));
COM_API_DECLARE(bool, get_value(int row, int col, double* decimal));
COM_API_DECLARE(bool, get_value(int row, int col, wchar_t* buf, int* words/*[in] - words in buf; [out] - real length of the value. you should provide greater buffer if it was greater than in*/));
COM_API_DECLARE(bool, get_value(int row, int col, inter_module_data::set_data set_val/*data: pointed to an unicode string*/, void* param));
};
__declspec(novtable) struct IDatabase : public ref_util::IRef
{
COM_API_DECLARE(int, open(const wchar_t* db_name, const wchar_t* user_name = 0, const wchar_t* password = 0));
COM_API_DECLARE(int, create_table(const wchar_t* table_name, db_util::TBLFLD* fields, int fields_num, int *composed_primary_key_index = NULL));
COM_API_DECLARE(int, close(void));
COM_API_DECLARE(int, execute_sql(const wchar_t* sql, IRecordset** record = 0));
COM_API_DECLARE(int, get_error_msg(inter_module_data::set_data set_msg/*data: pointed to an unicode string*/, void* param)); // return error code
};
// exports ...
PORT_API(bool) add_database(const wchar_t* db_name, const wchar_t* db_file, int driver_type);
PORT_API(bool) del_database(const wchar_t* db_name, int driver_type = DBDRIVER_ODBC_ACCESS);
PORT_API(IDatabase*) create_database_instance(int driver_type = DBDRIVER_ODBC_ACCESS);
PORT_API(bool) create_table_sql(const wchar_t* table_name, TBLFLD* fields, int fields_num
, inter_module_data::set_data set_msg/*data: pointed to an unicode string*/, void* param
, int *composed_primary_key_index = NULL); // -1 is over
};