acl/lib_acl_cpp/samples/win_dbservice/win_dbserviceDlg.cpp

348 lines
7.3 KiB
C++
Raw Normal View History

// win_dbserviceDlg.cpp : ʵ<><CAB5><EFBFBD>ļ<EFBFBD>
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
#include "acl_cpp/db/db_service_sqlite.hpp"
#include "acl_cpp/db/db_handle.hpp"
#include "acl_cpp/db/db_sqlite.hpp"
#include "win_dbservice.h"
#include "win_dbserviceDlg.h"
#include ".\win_dbservicedlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////////
class myquery : public acl::db_query
{
public:
myquery(int id) : id_(id)
{
}
~myquery()
{
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿڣ<D3BF><DAA3><EFBFBD> SQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void on_error(acl::db_status status)
{
(void) status;
printf(">>on error, id: %d\r\n", id_);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿڣ<D3BF><DAA3><EFBFBD> SQL <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void on_ok(const acl::db_rows* rows, int affected)
{
if (rows)
printf(">>on ok, id: %d, rows->legnth: %u, group_name: %s\r\n",
id_, rows->length(), (*(*rows)[0])["group_name"]);
else
printf(">>on ok, id: %d, affected: %d\r\n",
id_, affected);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿڣ<D3BF><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void destroy()
{
printf(">> myquery destroy now\r\n");
delete this;
}
protected:
private:
int id_;
};
static acl::string __dbfile("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.db");
2014-11-19 00:25:21 +08:00
const char* CREATE_TBL =
"create table group_tbl\r\n"
"(\r\n"
"group_name varchar(128) not null,\r\n"
"uvip_tbl varchar(32) not null default 'uvip_tbl',\r\n"
"access_tbl varchar(32) not null default 'access_tbl',\r\n"
"access_week_tbl varchar(32) not null default 'access_week_tbl',\r\n"
"access_month_tbl varchar(32) not null default 'access_month_tbl',\r\n"
"update_date date not null default '1970-1-1',\r\n"
"disable integer not null default 0,\r\n"
"add_by_hand integer not null default 0,\r\n"
"class_level integer not null default 0,\r\n"
"primary key(group_name, class_level)\r\n"
")";
static bool tbl_create(acl::db_handle& db)
{
if (db.tbl_exists("group_tbl"))
return (true);
if (db.sql_update(CREATE_TBL) == false)
{
printf("sql error\r\n");
return (false);
}
else
{
printf("create table ok\r\n");
return (true);
}
}
static bool create_db(void)
{
acl::db_sqlite db(__dbfile);
if (db.open() == false)
{
printf("open dbfile: %s error\r\n", __dbfile.c_str());
return (false);
}
db.show_conf();
return (tbl_create(db));
}
//////////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ó<EFBFBD><C3B3>򡰹<EFBFBD><F2A1B0B9>ڡ<EFBFBD><DAA1>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD> CAboutDlg <20>Ի<EFBFBD><D4BB><EFBFBD>
2014-11-19 00:25:21 +08:00
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// <20>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV ֧<><D6A7>
2014-11-19 00:25:21 +08:00
// ʵ<><CAB5>
2014-11-19 00:25:21 +08:00
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// Cwin_dbserviceDlg <20>Ի<EFBFBD><D4BB><EFBFBD>
2014-11-19 00:25:21 +08:00
Cwin_dbserviceDlg::Cwin_dbserviceDlg(CWnd* pParent /*=NULL*/)
: CDialog(Cwin_dbserviceDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
server_ = NULL;
handle_ = new acl::aio_handle(acl::ENGINE_WINMSG);
}
Cwin_dbserviceDlg::~Cwin_dbserviceDlg()
{
if (server_)
delete server_;
delete handle_;
}
void Cwin_dbserviceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(Cwin_dbserviceDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_ADD_DATA, OnBnClickedAddData)
ON_BN_CLICKED(IDC_GET_DATA, OnBnClickedGetData)
ON_BN_CLICKED(IDC_DELETE_DATA, OnBnClickedDeleteData)
END_MESSAGE_MAP()
// Cwin_dbserviceDlg <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
BOOL Cwin_dbserviceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// <20><>\<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...\<5C><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ϵͳ<CFB5>˵<EFBFBD><CBB5>С<EFBFBD>
2014-11-19 00:25:21 +08:00
// IDM_ABOUTBOX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD>Χ<EEB7B6>ڡ<EFBFBD>
2014-11-19 00:25:21 +08:00
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// <20><><EFBFBD>ô˶Ի<CBB6><D4BB><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EAA1A3>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>ǶԻ<C7B6><D4BB><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><DCBD>Զ<EFBFBD>
// ִ<>д˲<D0B4><CBB2><EFBFBD>
SetIcon(m_hIcon, TRUE); // <20><><EFBFBD>ô<EFBFBD>ͼ<EFBFBD><CDBC>
SetIcon(m_hIcon, FALSE); // <20><><EFBFBD><EFBFBD>Сͼ<D0A1><CDBC>
2014-11-19 00:25:21 +08:00
ShowWindow(SW_MINIMIZE);
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD> DOS <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
AllocConsole();
FILE* fp = freopen("CONOUT$","w+t",stdout);
// <20>򿪿<EFBFBD><F2BFAABF><EFBFBD> DOS <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
acl::open_dos();
logger_open("dbservice.log", "dbservice", "all:1");
if (create_db() == false)
printf(">>create table error\r\n");
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD> WIN32 <20><>Ϣģʽ<C4A3><CABD>IPC<50><43>ʽ
2014-11-19 00:25:21 +08:00
server_ = new acl::db_service_sqlite("DB_TEST", __dbfile, 2, 2, true);
if (server_->open(handle_) == false)
{
printf("open db service failed\r\n");
delete server_;
server_ = NULL;
}
return TRUE; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ؼ<CBBF><D8BC>Ľ<EFBFBD><C4BD><EFBFBD><E3A3AC><EFBFBD>򷵻<EFBFBD> TRUE
2014-11-19 00:25:21 +08:00
}
void Cwin_dbserviceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƹ<EFBFBD>ͼ<EFBFBD><EFBFBD><EAA1A3><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ĵ<EFBFBD>/<2F><>ͼģ<CDBC>͵<EFBFBD> MFC Ӧ<>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
// <20><EFBFBD>ɿ<EFBFBD><C9BF><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ɡ<EFBFBD>
2014-11-19 00:25:21 +08:00
void Cwin_dbserviceDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // <20><><EFBFBD>ڻ<EFBFBD><DABB>Ƶ<EFBFBD><C6B5><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// ʹͼ<CAB9><CDBC><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><D0BE><EFBFBD>
2014-11-19 00:25:21 +08:00
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
2014-11-19 00:25:21 +08:00
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//<2F><><EFBFBD>û<EFBFBD><C3BB>϶<EFBFBD><CFB6><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱϵͳ<CFB5><CDB3><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>ȡ<EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
2014-11-19 00:25:21 +08:00
HCURSOR Cwin_dbserviceDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD>
2014-11-19 00:25:21 +08:00
void Cwin_dbserviceDlg::OnBnClickedAddData()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (server_ == NULL)
{
MessageBox("db not opened yet!");
return;
}
acl::string sql;
myquery* query;
for (int i = 0; i < 1000; i++)
{
query = new myquery(i);
sql.format("insert into group_tbl('group_name', 'uvip_tbl')"
" values('<27>й<EFBFBD><D0B9><EFBFBD>-%d', 'test')", i);
2014-11-19 00:25:21 +08:00
server_->sql_update(sql.c_str(), query);
}
}
// <20><>ѯ<EFBFBD><D1AF><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD>
2014-11-19 00:25:21 +08:00
void Cwin_dbserviceDlg::OnBnClickedGetData()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (server_ == NULL)
{
MessageBox("db not opened yet!");
return;
}
acl::string sql;
myquery* query;
for (int i = 0; i < 1000; i++)
{
query = new myquery(i);
sql.format("select * from group_tbl"
" where group_name='<27>й<EFBFBD><D0B9><EFBFBD>-%d'"
2014-11-19 00:25:21 +08:00
" and uvip_tbl='test'", i);
server_->sql_select(sql.c_str(), query);
}
}
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD>
2014-11-19 00:25:21 +08:00
void Cwin_dbserviceDlg::OnBnClickedDeleteData()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (server_ == NULL)
{
MessageBox("db not opened yet!");
return;
}
acl::string sql;
myquery* query;
for (int i = 0; i < 1000; i++)
{
query = new myquery(i);
sql.format("delete from group_tbl"
" where group_name='<27>й<EFBFBD><D0B9><EFBFBD>-%d'"
2014-11-19 00:25:21 +08:00
" and uvip_tbl='test'", i);
server_->sql_update(sql.c_str(), query);
}
}