#ifndef __SFJP_ROAST__db__sql_HPP__
#define __SFJP_ROAST__db__sql_HPP__
-#include <string>
-#include <vector>
-#include <map>
+#include "roast/db/sql/sql.hpp"
-#define _ROAST_SQL_DEFULT_QUERY_BUFFER_LENGTH (256)
-#define _ROAST_SQL_DEFULT_QUERY_BUFFER_LENGTH_LONG (512)
-
-namespace roast
-{
- namespace sql
- {
- typedef ::std::string
- _query_t, query_t, sql_query, _sql_query;
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // CREATE TABLE
-
- // create_column_info
- typedef struct _table_column_info
- {
- ::std::string name;
- ::std::string type;
- unsigned int length;
- bool is_unsigned;
- bool is_zerofill;
- bool is_null_permit;
- bool is_primary;
- bool is_unique;
- ::std::string default;
- ::std::string mix;
- ::std::string max;
- ::std::string foreign_table;
- ::std::string foreign_column;
- bool auto_increment;
- ::std::string comment;
- ::std::string extensions;
-
- _table_column_info()
- {
- length = 0;
-
- is_unsigned = false;
- is_zerofill = false;
- is_null_permit = true;
- is_primary = false;
- is_unique = false;
-
- auto_increment = false;
- }
- }
- table_column_info;
-
- // create_table_columns
- typedef ::std::vector<table_column_info> create_table_columns;
-
- //////////////////
- // gen_create_table_query()
-
- _query_t gen_create_table_query(const char* name, const create_table_columns& columns)
- {
- _query_t q;
- q.resize(_ROAST_SQL_DEFULT_QUERY_BUFFER_LENGTH);
- q = "CREATE TABLE ";
-
- q += name;
- //q += ' ';
- q += " (";
-
- for(size_t i=0; i<columns.size(); i++)
- {
- const table_column_info &column = columns[i];
- if ( i != 0 )
- q += ',';
-
- q += column.name;
- q += ' ';
- q += column.type;
-
- if ( column.length != 0 )
- {
- q += '(';
- char work[32];
- sprintf(work,"%u", column.length);
- q += work;
- q += ')';
- }
- }
- q += ");";
-
- return q;
- };
-
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // INSERT INTO
-
- // insert_into_map
- typedef ::std::map<::std::string,::std::string> insert_into_map;
-
- ////////////////////
-
- // gen_insert_into_query()
- _query_t gen_insert_into_query(const char* table_name, const insert_into_map& iimap)
- {
- _query_t q;
- q.resize(_ROAST_SQL_DEFULT_QUERY_BUFFER_LENGTH);
- q = "INSERT INTO ";
-
- q += table_name;
- q += " (";
-
- ///////////////////////////////////
- insert_into_map::const_iterator it=iimap.begin();
- for(; it != iimap.end(); it++)
- {
- const char* key = it->first.c_str();
-
- if ( it != iimap.begin() )
- q += ',';
- q += key;
- }
- q += ") VALUES (";
-
- ///////////////////////////////////
- it=iimap.begin();
- for(; it != iimap.end(); it++)
- {
- const char* value = it->second.c_str();
-
- if ( it != iimap.begin() )
- q += ',';
- q += value;
- }
- q += ");";
-
- return q;
- };
-
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // SELECT
-
- // select_table_reference
- typedef struct
- {
- ::std::string name;
-
- }
- select_table_reference;
-
- // select_columns
- typedef ::std::vector<::std::string> select_columns;
-
- // select_as
- typedef struct
- {
- ::std::string from;
- ::std::string to;
- }
- select_as;
-
- // select_order_by
- typedef struct _select_order_by
- {
- ::std::string name;
- bool is_desc;
-
- _select_order_by()
- {
- is_desc = false;
- }
- }
- select_order_by;
-
- ///////////////////////////////////////////
-
- // select_info
- typedef struct _select_info
- {
- //::std::string column;
- select_columns columns;
- ::std::vector<select_table_reference> tables;
- ::std::vector<select_as> as;
- ::std::string where;
- ::std::vector<::std::string> group_by;
- ::std::string having;
- ::std::vector<select_order_by> order_by;
-
- _select_info()
- {
- }
- ////////////////////////////////////////////
-
- void add_column(const char* column_name)
- {
- columns.push_back(column_name);
- }
-
- void set_where(const char* where_in)
- {
- where = where_in;
- }
-
- void add_table(const char* table_name)
- {
- select_table_reference tblref;
- tblref.name = table_name;
- tables.push_back(tblref);
- }
-
- void add_as(const char* from, const char* to)
- {
- select_as _as;
- _as.from = from;
- _as.to = to;
- as.push_back(_as);
- }
-
- void set_having(const char* hav)
- {
- having = hav;
- }
-
- void add_group_by(const char* group)
- {
- group_by.push_back(group);
- }
-
- void add_order_by(const char* name, bool is_desc=false)
- {
- select_order_by od;
- od.name = name;
- od.is_desc = is_desc;
- order_by.push_back(od);
- }
- }
- select_info;
-
- /////////////////////////////////////////
- // gen_select_query()
-
- _query_t gen_select_query(const select_info& sel)
- {
- size_t i;
- _query_t q;
- q.resize(_ROAST_SQL_DEFULT_QUERY_BUFFER_LENGTH_LONG);
- q = "SELECT ";
-
- ///////////////////////////////////////////////////////////////
-
- // columns
- const select_columns &columns = sel.columns;
- if ( columns.size() == 0 )
- {
- q += "* ";
- }
- else
- {
- const ::std::vector<::std::string> &columns = sel.columns;
- for(i=0; i<columns.size(); i++)
- {
- q += columns[i];
- q += ' ';
- }
- }
-
- ///////////////////////////////////////////////////////////////
-
- // FROM
- q += "FROM ";
- for(i=0; i<sel.tables.size(); i++)
- {
- const select_table_reference& ref = sel.tables[i];
-
- q += ref.name;
- q += ' ';
- }
-
- // WHERE
- if ( sel.where.length() != 0 )
- {
- q += "WHERE ";
- q += sel.where;
- q += ' ';
- }
-
- // GROUP BY
- for(i=0; i<sel.group_by.size(); i++)
- {
- if ( i == 0 )
- q += "GROUP BY ";
- else
- q += ',';
- q += sel.group_by[i];
- }
- q += ' ';
-
- // HAVING
- if ( sel.having.length() != 0 )
- {
- q += "HAVING ";
- q += sel.having;
- q += ' ';
- }
-
- // ORDER BY
- for(i=0; i<sel.order_by.size(); i++)
- {
- if ( i == 0 )
- q += "ORDER BY ";
- else
- q += ',';
- q += sel.order_by[i].name;
- if ( sel.order_by[i].is_desc )
- q += " DESC";
- }
- q += ' ';
-
- /*
- ///////////////////////////////////
- insert_into_map::const_iterator it=iimap.begin();
- for(; it != iimap.end(); it++)
- {
- const char* key = it->first.c_str();
-
- if ( it != iimap.begin() )
- q += ',';
- q += key;
- }
- q += ") VALUES (";
-
- ///////////////////////////////////
- it=iimap.begin();
- for(; it != iimap.end(); it++)
- {
- const char* value = it->second.c_str();
-
- if ( it != iimap.begin() )
- q += ',';
- q += value;
- }
- q += ");";
- */
-
- return q;
- };
-
- ///////////////////////////////////////////////////////////////////////////
- }
-}
-
-#endif//__SFJP_ROAST__db__sqlite_HPP__
+#endif//__SFJP_ROAST__db__sql_HPP__