一直以來,筆者都在不停尋找一種更人性化的數據庫訪問方式(並不是說默認的方式不好,而是有時候的確在模塊化設計中不太方便)。
後來有幸在php中找到codeigniter的ActiveReord,詳細參考這篇文章: 抽離CodeIgniter的數據庫訪問類!
然而c++卻始終用著最原始的方式,昨天趁著項目要用的機會,在網上搜索了好久,總算讓我找到兩套c++的數據庫訪問框架:
soci
litesql
兩套代碼我都拿下來看了一下,litesql實現了一套完整的代碼自動生成,功能強大,但是也很重;soci相對要輕量,但是同樣也實現了數據結構到數據庫表的映射。本人還是比較喜歡輕量的東西,所以最終選擇了soci。經過這兩天的試用,感覺非常不錯。
官方的文檔也很詳細,所以這裡就用我寫的單元測試代碼來做一下簡單的講解:
首先創建庫表:
create database soci; CREATE TABLE `tb_test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) default "", `sex` int(11) default 0, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ); create database soci; CREATE TABLE `tb_test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) default "", `sex` int(11) default 0, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) );
1.簡單的select單條記錄
TEST(soci,select_one) { try { session sql(mysql, "host=localhost db=soci user=dantezhu"); indicator ind; string name = "dandan"; int sex; sql << "select sex from tb_test where name = :name", into(sex, ind), use(name); ASSERT_EQ(ind, i_ok) << name; } catch (exception const &e) { FAIL()<<e.what(); } } TEST(soci,select_one) { try { session sql(mysql, "host=localhost db=soci user=dantezhu"); indicator ind; string name = "dandan"; int sex; sql << "select sex from tb_test where name = :name", into(sex, ind), use(name); ASSERT_EQ(ind, i_ok) << name; } catch (exception const &e) { FAIL()<<e.what(); } }
select的結果,如果成功則ind會為i_ok,同值sex被賦值;如果失敗則反之
2.簡單的select多條記錄
TEST(soci,select_multi2) { try { session sql(mysql, "db=soci user=dantezhu"); indicator ind; int count; sql << "select count(*) from tb_test", into(count, ind); ASSERT_EQ(ind, i_ok) << count; if (count == 0) { SUCCEED(); return; } int sex = 1; vector<string> vec_name(count); vector<int> vec_sex(count); sql << "select name,sex from tb_test where sex = :sex", into(vec_name), into(vec_sex), use(sex); } catch (exception const &e) { FAIL()<<e.what(); } } TEST(soci,select_multi2) { try { session sql(mysql, "db=soci user=dantezhu"); indicator ind; int count; sql << "select count(*) from tb_test", into(count, ind); ASSERT_EQ(ind, i_ok) << count; if (count == 0) { SUCCEED(); return; } int sex = 1; vector<string> vec_name(count); vector<int> vec_sex(count); sql << "select name,sex from tb_test where sex = :sex", into(vec_name), into(vec_sex), use(sex); } catch (exception const &e) { FAIL()<<e.what(); } }
與select單條記錄唯一的區別即,into()的參數是一個vector。其實用多個vector這種方式並不是一個很好的選擇,後面會介紹基於數據結構的方式。
3.簡單的insert
TEST(soci,insert_exist) { try { session sql(mysql, "db=soci user=dantezhu"); string name = "dandan"; int sex = 1; sql << "insert into tb_test(name, sex) values(:name, :sex)", use(name), use(sex); } catch (exception const &e) { SUCCEED()<<e.what(); } } TEST(soci,insert_exist) { try { session sql(mysql, "db=soci user=dantezhu"); string name = "dandan"; int sex = 1; sql << "insert into tb_test(name, sex) values(:name, :sex)", use(name), use(sex); } catch (exception const &e) { SUCCEED()<<e.what(); } }
insert,update,delete都有兩個同樣的問題:
a)affect_rows(操作的行數)沒有辦法返回
b)操作的id無法知道,尤其對於insert的主鍵是自增的情況下,無法知道插入的主鍵的值是多少。
update和delete都與insert相似,這裡就不再多說。
接下來是這個框架的很重要的一個特性,即數據庫表與數據結構綁定:
首先我們需要定義一個結構體,並告知soci怎麼讓列名和數據結構的字段對應起來:
struct Person { int id; std::string name; int sex; }; namespace soci { template<> struct type_conversion<Person> { typedef values base_type; static void from_base(values const & v, indicator /* ind */, Person & p) { p.id = v.get<int>("id"); p.name = v.get<std::string>("name"); p.sex = v.get<int>("sex"); } static void to_base(const Person & p, values & v, indicator & ind) { v.set("id", p.id); v.set("name", p.name); v.set("sex", p.sex); ind = i_ok; } }; } struct Person { int id; std::string name; int sex; }; namespace soci { template<> struct type_conversion<Person> { typedef values base_type; static void from_base(values const & v, indicator /* ind */, Person & p) { p.id = v.get<int>("id"); p.name = v.get<std::string>("name"); p.sex = v.get<int>("sex"); } static void to_base(const Person & p, values & v, indicator & ind) { v.set("id", p.id); v.set("name", p.name); v.set("sex", p.sex); ind = i_ok; } }; }
關於
template<> struct type_conversion<Person> template<> struct type_conversion<Person>
這裡,官方的文檔是是有誤的,我查了好長時間,按照上面的寫法來寫即可。
1.用數據結構來select
TEST(soci,select_obj_one) { try { session sql(mysql, "db=soci user=dantezhu"); indicator ind; int count; sql << "select count(*) from tb_test", into(count, ind); ASSERT_EQ(ind, i_ok) << count; string name = "dandan"; Person p; sql << "select id,name,sex from tb_test where name = :name", into(p, ind), use(name); ASSERT_EQ(ind, i_ok) << name; if (sql.got_data()) { cout<< p.id << "," << p.name << "," << p.sex << endl; } } catch (exception const &e) { FAIL()<<e.what(); } } TEST(soci,select_obj_one) { try { session sql(mysql, "db=soci user=dantezhu"); indicator ind; int count; sql << "select count(*) from tb_test", into(count, ind); ASSERT_EQ(ind, i_ok) << count; string name = "dandan"; Person p; sql << "select id,name,sex from tb_test where name = :name", into(p, ind), use(name); ASSERT_EQ(ind, i_ok) << name; if (sql.got_data()) { cout<< p.id << "," << p.name << "," << p.sex << endl; } } catch (exception const &e) { FAIL()<<e.what(); } }
2.用數據結構來進行insert
TEST(soci,insert_obj_noexist) { try { session sql(mysql, "db=soci user=dantezhu"); Person p = { 0, "niuniu", 2 }; sql << "insert into tb_test(name, sex) values(:name, :sex)", use(p); } catch (exception const &e) { FAIL()<<e.what(); } } TEST(soci,insert_obj_noexist) { try { session sql(mysql, "db=soci user=dantezhu"); Person p = { 0, "niuniu", 2 }; sql << "insert into tb_test(name, sex) values(:name, :sex)", use(p); } catch (exception const &e) { FAIL()<<e.what(); } }
整個就是這樣~~下面是文中代碼文件的下載路徑:
http://code.google.com/p/vimercode/source/browse/#svn%2Ftrunk%2Fsoci_test
另外,雖然python下的mysql訪問也算比較簡單,但還是想知道是否有更Pythonic的庫或接口,如果有朋友知道,歡迎不吝告知。