一、前言:
今天试了下如何用C++类实现接口封装,感觉蛮好 。用于封装的类主要有两个,sqliteStatement类和sqliteWrapper类,是一个老外写的。我看了下源码,主要是对C接口进行了封装,好处自然不用说,可以重用。很佩服老外的技巧,在这里就引用下他们的代码供大家分享下他们的思想。
源代码链接:http://www.adp-gmbh.ch/sqlite/wrapper.html
二、类源码:
1/*
2 sqliteWrapper.h
3
4 Copyright (C) 2004 René Nyffenegger
5
6 This source code is provided 'as-is',without any express or implied
7 warranty. In no event will the author be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications,and to alter it and redistribute it
12 freely,subject to the following restrictions:
13
14 1. The origin of this source code must not be misrepresented; you must not
15 claim that you wrote the original source code. If you use this source code
16 in a product,an acknowledgment in the product documentation would be
17 appreciated but is not required.
18
19 2. Altered source versions must be plainly marked as such,and must not be
20 misrepresented as being the original source code.
21
22 3. This notice may not be removed or altered from any source distribution.
23
24 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
25 */
26
27 #ifndef sqlITE_WRAPPER_H__
28#define sqlITE_WRAPPER_H__
29
30 #include <string>
31 #include <vector>
32
33 #include "sqlite3.h"
34
35class sqliteStatement{
36private:
37// sqliteStatement's ctor only to be called by sqliteWrapper
38 friend class sqliteWrapper;
39 sqliteStatement(std::stringconst& statement,sqlite3* db);
40
41public:
42 sqliteStatement();
43
44enum dataType{
45 INT=sqlITE_INTEGER,
46 FLT=sqlITE_FLOAT,128); line-height:1.5!important">47 TXT=sqlITE_TEXT,128); line-height:1.5!important">48 BLB=sqlITE_BLOB,128); line-height:1.5!important">49 NUL=sqlITE_NULL,128); line-height:1.5!important">50 };
51
52 dataType DataType(int pos_zero_indexed);
53
54int ValueInt(55 std::string ValueString(56
57 sqliteStatement(const sqliteStatement&);58~sqliteStatement();
59
60sqliteStatement& operator=(sqliteStatement const&);61
62bool Bind(int pos_zero_indexed,std::const& value);
63double value);
64int value);
65bool BindNull(66
67bool Execute();
68
69bool NextRow();
70
71 Call Reset if not depending on the NextRow cleaning up.
72 For example for select count(*) statements73bool Reset();
74
75bool RestartSelect();
76
7778void DecreaseRefCounter();79
80int* ref_counter_; not yet used...81 sqlite3_stmt* stmt_;
82 };
83
84class sqliteWrapper {
8586 sqliteWrapper();
87
88bool Open(std::const& db_file);
89
90class ResultRecord {
9192 std::vector<std::string> fields_;
93 };
94
95class ResultTable {
96 friend 9798 ResultTable():ptr_cur_record_(0){}
99
100 std::vector<ResultRecord> records_;
101
102 ResultRecord* next(){
103if (ptr_cur_record_<records_.size()){
104return&(records_[ptr_cur_record_++]);
105 }
106return0;
107 }
108
109110void reset(){
111 records_.clear();
112 ptr_cur_record_=113 }
114
115116 unsigned int ptr_cur_record_;
117 };
118
119bool SelectStmt(std::const& stmt,ResultTable& res);
120bool DirectStatement(std::const& stmt);
121 sqliteStatement* Statement(std::122
123 std::string LastError();
124
125 Transaction related126bool Begin();
127bool Commit();
128bool Rollback();
129
130131
132staticint SelectCallback(void*p_data,255); line-height:1.5!important">int num_fields,255); line-height:1.5!important">char**p_fields,255); line-height:1.5!important">char**p_col_names);
133
134 sqlite3* db_;
135 };
136
137#endif