将ORACLE数据库的表转换为C++类与结构体的想法及实现

曾经很多年一年在C++下用otlv4连接数据库,非常厌倦了写那些无聊的:增、删、改、查的SQL语句,偶然的机会,项目中用到JAVA要所以就接触了hibernet,突然发现,原来可以不用写SQL的,只需要通过工具,就直接将table转换成了结构体了,而且操作也非常的简单,故有此想法写了一个小工具,方便C++操作数据库,而把个人关注点放到需求上,而不是看那些偶心的SQL语句。


思路:

通过otlv4开发库,根据:select * from table得到该表的所有字段,根据字段的定义,将字段类型转换成C++或C相应的类型,一般都为double和std::string,即在hpp文件中生成一个class struct_表名的类,主要用于存储数据,同时,生成一个相应的类:class class_表名,这类中主要为操作方法,增、删、改、查,通过相应的方法,传入相应的struct_表名即可以完成操作。


模板文件

将ORACLE数据库的表转换为C++类与结构体的想法及实现_第1张图片

@define_hpp_head
//#ifndef __hpp_template_HPP__
//#define __hpp_template_HPP__




#ifndef USE_LOG4CPP
# define LOG ::printf
#else
log4cpp::Category& sub = log4cpp::Category::getInstance(std::string("sub1"));
# define LOG sub.info
#endif


#include "pool.hpp"
#include <iostream>
#include <vector>
#include <sstream>


@declare_namespace_begin


@declare_struct_name
{
public:
/*
@declare_struct_member_note
*/
@declare_struct_member

public:
const std::string str()
{
std::stringstream ss;
ss << " Debug information: " << std::endl
@declare_struct_stringstream
;
return ss.str();
}


};


//std::ostream& operator << (std::ostream& o, const struct_hpp_template& rhs)
@decleare_struct_operator_in
{
o << " Debug information:" << std::endl
@declare_struct_stringstream_in
;
return o;
}


@declare_class_name
{
private:
otl_stream astream_;


public:
bool eof()
{
return astream_.eof(); 
}


public:
// void operator >> (struct_hpp_template& one)
@declare_class_operator_stream_name
{
if (!astream_.eof())
{
@declare_class_operator_stream_variable
astream_ >> one.reserved_field_xxxx;
}
}

public:
// int function_select(const struct_hpp_template& one)
@declare_function_select_name
{
const char* SQL = 
"SELECT "
@declare_function_select_fields
// " FROM hpp_template"
@declare_function_select_from
" WHERE 1=1 "
@declare_function_select_where
"  AND 1=1";


try
{
Framework::database db;
astream_.close();    
astream_.open(100, SQL, *db);


@declare_function_select_where_bind

astream_.flush();
}
catch(otl_exception& p)              
{                                    
LOG("message   = %s\n", p.msg);    
LOG("statement = %s\n", p.stm_text);
LOG("sqlstate  = %s\n", p.sqlstate);
LOG("var_info  = %s\n", p.var_info);


throw p;
}                                      
return 0;
}


public:
@declare_function_insert_name_vector
{
const char* SQL = 
// "INSERT INTO hpp_template"
@declare_function_insert_table
" ("
@declare_function_insert_fields
" ) "
"VALUES "
" ("
@declare_function_insert_values
" )";

try
{
Framework::database db;      
otl_stream o(100, SQL, *db);


@declare_function_insert_for_begin
@declare_function_insert_for_bind
@declare_function_insert_for_end
o.flush();
return o.get_rpc();
}
catch(otl_exception& p)              
{                                    
LOG("message   = %s\n", p.msg);    
LOG("statement = %s\n", p.stm_text);
LOG("sqlstate  = %s\n", p.sqlstate);
LOG("var_info  = %s\n", p.var_info);


throw p;
}                                      
return 0;
}

public:
// int function_insert(const struct_hpp_template& one)
@declare_function_insert_name
{
const char* SQL = 
// "INSERT INTO hpp_template"
@declare_function_insert_table
" ("
@declare_function_insert_fields
" ) "
"VALUES "
" ("
@declare_function_insert_values
" )";


try
{
Framework::database db;      
otl_stream o(100, SQL, *db);


@declare_function_insert_bind

o.flush();  
return o.get_rpc();
}
catch(otl_exception& p)              
{                                    
LOG("message   = %s\n", p.msg);    
LOG("statement = %s\n", p.stm_text);
LOG("sqlstate  = %s\n", p.sqlstate);
LOG("var_info  = %s\n", p.var_info);


throw p;
}                                      
return 0;
}


public:
// int function_update(const struct_hpp_template& newOne, const struct_hpp_template& oldOne)
@declare_function_update_name
{
const char* SQL = 
// "UPDATE hpp_template SET"
@declare_function_update_table
@declare_function_update_fields
" WHERE "
"  1=1"
@declare_function_update_where
"  AND 1=1";


try
{
Framework::database db;      
otl_stream o(100, SQL, *db);

@declare_function_insert_values_new
//--------------------------------------------
@declare_function_insert_values_old

o.flush();  
return o.get_rpc();
}
catch(otl_exception& p)              
{                                    
LOG("message   = %s\n", p.msg);    
LOG("statement = %s\n", p.stm_text);
LOG("sqlstate  = %s\n", p.sqlstate);
LOG("var_info  = %s\n", p.var_info);


throw p;
}                                      
return 0;
}


public:
// int function_delete(const struct_hpp_template& oldOne)
@declare_function_delete_name
{
const char* SQL = 
// "DELETE FROM hpp_template"
@declare_function_delete_table
" WHERE 1=1 "
@declare_function_delete_where
"  AND 1=1";


try
{
Framework::database db;      
otl_stream o(100, SQL, *db);


@declare_function_delete_where_bind


o.flush();  
return o.get_rpc();
}
catch(otl_exception& p)              
{                                    
LOG("message   = %s\n", p.msg);    
LOG("statement = %s\n", p.stm_text);
LOG("sqlstate  = %s\n", p.sqlstate);
LOG("var_info  = %s\n", p.var_info);


throw p;
}                                      
return 0;
}
};


@declare_namespace_end


#endif // end define hpp_template


你可能感兴趣的:(oracle,C++)