00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #ifndef ODBCPP_RECORD
00024 #define ODBCPP_RECORD
00025 
00026 #include        "statement.h"
00027 #include        <map>
00028 #include        <sqlucode.h>
00029 
00030 namespace odbcpp
00031 {
00032 
00033 
00034 
00035 
00045 template<class T>
00046 class buffer : public object
00047 {
00048 public:
00059         buffer(size_t size = 1) : f_ptr(new T[size]) {}
00060 
00066         ~buffer() { delete [] f_ptr; }
00067 
00079         T *get() const { return const_cast<T *>(f_ptr); }
00080 
00081 private:
00086         T *                     f_ptr;
00087 };
00089 typedef buffer<char>            buffer_char_t;
00090 
00091 
00092 
00093 class record_base : public object
00094 {
00095 public:
00096                                 record_base();
00097                                 record_base(const record_base& base);
00098         virtual                 ~record_base() {}
00099 
00100         record_base&            operator = (const record_base& base);
00101 
00102         
00103         virtual bool            is_dynamic() const = 0;
00104         bool                    is_bound() const { return f_statement; }
00105 
00106         virtual void            bind(statement& stmt);
00107         void                    unbind() { f_statement.reset(); }
00108         virtual void            finalize() {}
00109 
00110 protected:
00111         smartptr<statement>     f_statement;
00112 
00113 private:
00114         virtual void            bind_impl() = 0;
00115 };
00116 
00117 
00118 class record : public record_base
00119 {
00120 public:
00121         virtual bool            is_dynamic() const { return false; }
00122 
00123 protected:
00124         
00125         bool                    empty() const { return f_bind_by_name.empty() && f_bind_by_col.empty(); }
00126         size_t                  size() const { return f_bind_by_name.size() + f_bind_by_col.size(); }
00127 
00128         
00129         void                    bind(const std::string& name, std::string& str, bool *is_null = 0);
00130         void                    bind(SQLSMALLINT col, std::string& str, bool *is_null = 0);
00131         void                    bind(const std::string& name, std::wstring& str, bool *is_null = 0);
00132         void                    bind(SQLSMALLINT col, std::wstring& str, bool *is_null = 0);
00133 
00134         
00135         void                    bind(const std::string& name, SQLCHAR& tiny_int, bool *is_null = 0);    
00136         void                    bind(SQLSMALLINT col, SQLCHAR& tiny_int, bool *is_null = 0);
00137         void                    bind(const std::string& name, SQLSCHAR& tiny_int, bool *is_null = 0);
00138         void                    bind(SQLSMALLINT col, SQLSCHAR& tiny_int, bool *is_null = 0);
00139 
00140         void                    bind(const std::string& name, SQLSMALLINT& small_int, bool *is_null = 0);
00141         void                    bind(SQLSMALLINT col, SQLSMALLINT& small_int, bool *is_null = 0);
00142         void                    bind(const std::string& name, SQLUSMALLINT& small_int, bool *is_null = 0);
00143         void                    bind(SQLSMALLINT col, SQLUSMALLINT& small_int, bool *is_null = 0);
00144 
00145         void                    bind(const std::string& name, SQLINTEGER& integer, bool *is_null = 0);
00146         void                    bind(SQLSMALLINT col, SQLINTEGER& integer, bool *is_null = 0);
00147         void                    bind(const std::string& name, SQLUINTEGER& integer, bool *is_null = 0);
00148         void                    bind(SQLSMALLINT col, SQLUINTEGER& integer, bool *is_null = 0);
00149 
00150         void                    bind(const std::string& name, SQLBIGINT& big_int, bool *is_null = 0);
00151         void                    bind(SQLSMALLINT col, SQLBIGINT& big_int, bool *is_null = 0);
00152         void                    bind(const std::string& name, SQLUBIGINT& big_int, bool *is_null = 0);
00153         void                    bind(SQLSMALLINT col, SQLUBIGINT& big_int, bool *is_null = 0);
00154 
00155         
00156         void                    bind(const std::string& name, SQLREAL& real, bool *is_null = 0);        
00157         void                    bind(SQLSMALLINT col, SQLREAL& real, bool *is_null = 0);
00158 
00159         void                    bind(const std::string& name, SQLFLOAT& dbl, bool *is_null = 0);        
00160         void                    bind(SQLSMALLINT col, SQLFLOAT& dbl, bool *is_null = 0);
00161 
00162         
00163         void                    bind(const std::string& name, SQLCHAR *binary, SQLLEN length, bool *is_null = 0);
00164         void                    bind(SQLSMALLINT col, SQLCHAR *binary, SQLLEN length, bool *is_null = 0);
00165 
00166         
00167         void                    bind(const std::string& name, SQL_DATE_STRUCT& date, bool *is_null = 0);
00168         void                    bind(SQLSMALLINT col, SQL_DATE_STRUCT& date, bool *is_null = 0);
00169         void                    bind(const std::string& name, SQL_TIME_STRUCT& time, bool *is_null = 0);
00170         void                    bind(SQLSMALLINT col, SQL_TIME_STRUCT& time, bool *is_null = 0);
00171         void                    bind(const std::string& name, SQL_TIMESTAMP_STRUCT& timestamp, bool *is_null = 0);
00172         void                    bind(SQLSMALLINT col, SQL_TIMESTAMP_STRUCT& timestamp, bool *is_null = 0);
00173         void                    bind(const std::string& name, SQL_NUMERIC_STRUCT& numeric, bool *is_null = 0);
00174         void                    bind(SQLSMALLINT col, SQL_NUMERIC_STRUCT& numeric, bool *is_null = 0);
00175         void                    bind(const std::string& name, SQLGUID& guid, bool *is_null = 0);
00176         void                    bind(SQLSMALLINT col, SQLGUID& guid, bool *is_null = 0);
00177 
00178 private:
00179         struct bind_info_t {
00180                                 bind_info_t() :
00181                                         
00182                                         f_col(0),
00183                                         f_target_type(SQL_UNKNOWN_TYPE),
00184                                         f_data(NULL),
00185                                         f_size(0),
00186                                         f_fetch_size(0),
00187                                         f_is_null(0),
00188                                         f_string(NULL)
00189                                         
00190                                 {
00191                                 }
00192 
00193                 std::string             f_name;         
00194                 SQLSMALLINT             f_col;          
00195                 SQLSMALLINT             f_target_type;  
00196                 SQLPOINTER              f_data;         
00197                 SQLULEN                 f_size;         
00198                 SQLLEN                  f_fetch_size;   
00199                 bool *                  f_is_null;      
00200                 smartptr<buffer_char_t> f_data_buffer;  
00201                 union {
00202                         std::string *   f_string;       
00203                         std::wstring *  f_wstring;      
00204                 };
00205         };
00207         typedef std::map<const std::string, bind_info_t *>      bind_info_name_map_t;
00209         typedef std::pair<const std::string, bind_info_t *>     bind_info_name_t;
00211         typedef std::map<const SQLSMALLINT, bind_info_t *>      bind_info_col_map_t;
00213         typedef std::pair<const SQLSMALLINT, bind_info_t *>     bind_info_col_t;
00214 
00215         virtual void            bind_impl();
00216         virtual void            finalize();
00217         void                    finalize_info(bind_info_t *info);
00218 
00219         bind_info_name_map_t    f_bind_by_name;
00220         bind_info_col_map_t     f_bind_by_col;
00221 };
00222 
00223 
00224 
00225 class dynamic_record : public record_base
00226 {
00227 public:
00228         virtual bool            is_dynamic() const { return true; }
00229 
00230         
00231         bool                    empty() const { return f_bind_by_name.empty() && f_bind_by_col.empty(); }
00232         size_t                  size() const { return f_bind_by_col.size(); }
00233         const std::string&      column_name(SQLSMALLINT col) const;
00234         SQLSMALLINT             column_number(const std::string& name) const;
00235 
00236         
00237         bool                    exists(const std::string& name);
00238         SQLSMALLINT             get_type(const std::string& name) const;
00239         SQLSMALLINT             get_type(SQLSMALLINT col) const;
00240         SQLSMALLINT             get_decimal_digits(const std::string& name) const;
00241         SQLSMALLINT             get_decimal_digits(SQLSMALLINT col) const;
00242         SQLSMALLINT             get_is_null(const std::string& name) const;
00243         SQLSMALLINT             get_is_null(SQLSMALLINT col) const;
00244         SQLLEN                  get_size(const std::string& name) const;
00245         SQLLEN                  get_size(SQLSMALLINT col) const;
00246 
00247         
00248         void                    get(const std::string& name, std::string& str) const;
00249         void                    get(SQLSMALLINT col, std::string& str) const;
00250         void                    get(const std::string& name, std::wstring& str) const;
00251         void                    get(SQLSMALLINT col, std::wstring& str) const;
00252 
00253         
00254         void                    get(const std::string& name, SQLCHAR& tiny_int) const;  
00255         void                    get(SQLSMALLINT col, SQLCHAR& tiny_int) const;
00256         void                    get(const std::string& name, SQLSCHAR& tiny_int) const;
00257         void                    get(SQLSMALLINT col, SQLSCHAR& tiny_int) const;
00258 
00259         void                    get(const std::string& name, SQLSMALLINT& small_int) const;
00260         void                    get(SQLSMALLINT col, SQLSMALLINT& small_int) const;
00261         void                    get(const std::string& name, SQLUSMALLINT& small_int) const;
00262         void                    get(SQLSMALLINT col, SQLUSMALLINT& small_int) const;
00263 
00264         void                    get(const std::string& name, SQLINTEGER& integer) const;
00265         void                    get(SQLSMALLINT col, SQLINTEGER& integer) const;
00266         void                    get(const std::string& name, SQLUINTEGER& integer) const;
00267         void                    get(SQLSMALLINT col, SQLUINTEGER& integer) const;
00268 
00269         void                    get(const std::string& name, SQLBIGINT& big_int) const;
00270         void                    get(SQLSMALLINT col, SQLBIGINT& big_int) const;
00271         void                    get(const std::string& name, SQLUBIGINT& big_int) const;
00272         void                    get(SQLSMALLINT col, SQLUBIGINT& big_int) const;
00273 
00274         
00275         void                    get(const std::string& name, SQLREAL& real) const;      
00276         void                    get(SQLSMALLINT col, SQLREAL& real) const;
00277 
00278         void                    get(const std::string& name, SQLFLOAT& dbl) const;      
00279         void                    get(SQLSMALLINT col, SQLFLOAT& dbl) const;
00280 
00281         
00282         SQLULEN                 get(const std::string& name, SQLCHAR *binary, SQLLEN length) const;
00283         SQLULEN                 get(SQLSMALLINT col, SQLCHAR *binary, SQLLEN length) const;
00284 
00285         
00286         void                    get(const std::string& name, SQL_DATE_STRUCT& date) const;
00287         void                    get(SQLSMALLINT col, SQL_DATE_STRUCT& date) const;
00288         void                    get(const std::string& name, SQL_TIME_STRUCT& time) const;
00289         void                    get(SQLSMALLINT col, SQL_TIME_STRUCT& time) const;
00290         void                    get(const std::string& name, SQL_TIMESTAMP_STRUCT& timestamp) const;
00291         void                    get(SQLSMALLINT col, SQL_TIMESTAMP_STRUCT& timestamp) const;
00292         void                    get(const std::string& name, SQL_NUMERIC_STRUCT& numeric) const;
00293         void                    get(SQLSMALLINT col, SQL_NUMERIC_STRUCT& numeric) const;
00294         void                    get(const std::string& name, SQLGUID& guid) const;
00295         void                    get(SQLSMALLINT col, SQLGUID& guid) const;
00296 
00297 private:
00298         struct bind_info_t {
00299                                         bind_info_t() :
00300                                                 
00301                                                 f_col(0),
00302                                                 f_target_type(0),
00303                                                 f_bind_type(0),
00304                                                 f_decimal_digits(0),
00305                                                 
00306                                                 f_size(0),
00307                                                 f_fetch_size(0)
00308                                         {
00309                                         }
00310 
00311                 std::string             f_name;         
00312                 SQLSMALLINT             f_col;          
00313                 SQLSMALLINT             f_target_type;  
00314                 SQLSMALLINT             f_bind_type;    
00315                 SQLSMALLINT             f_decimal_digits;       
00316                 smartptr<buffer_char_t> f_data;         
00317                 SQLULEN                 f_size;         
00318                 SQLLEN                  f_fetch_size;   
00319         };
00321         typedef std::map<const std::string, bind_info_t *>      bind_info_name_map_t;
00323         typedef std::pair<const std::string, bind_info_t *>     bind_info_name_t;
00325         typedef std::vector<bind_info_t *>                      bind_info_col_vector_t;
00326 
00327         virtual void            bind_impl();
00328         const bind_info_t *     find_column(const std::string& name, SQLSMALLINT target_type, bool except_null = false) const;
00329         const bind_info_t *     find_column(SQLSMALLINT col, SQLSMALLINT target_type, bool except_null = false) const;
00330         const bind_info_t *     verify_column(const bind_info_t *info, SQLSMALLINT target_type, bool except_null) const;
00331 
00332         bind_info_name_map_t    f_bind_by_name;
00333         bind_info_col_vector_t  f_bind_by_col;          
00334 };
00335 
00336 
00337 
00338 }       
00339 
00340 #endif          // #ifndef ODBCPP_RECORD