Logo
~Database~
~ C++ ~
~Contact~

Query.h

Go to the documentation of this file.
00001 /*
00002  **     Query.h
00003  **
00004  **     Published / author: 2001-01-04 / grymse@telia.com
00005  **/
00006 /*
00007 Copyright (C) 2001 Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #ifndef _QUERY_H
00025 #define _QUERY_H
00026 #ifndef _WIN32
00027 // win32 project only, will not build on unix/linux
00028 #include "fejk.h"
00029 #endif
00030 
00031 #include <string>
00032 #include <map>
00033 
00034 #ifdef ODBCW_NAMESPACE
00035 namespace ODBCW_NAMESPACE {
00036 #endif
00037 
00038 
00039 // xml create options (method ReturnRowAsXML)
00040 #define DQ_XML_ENDTAG 1
00041 //#define DQ_XML_XMLWRAP 2 -- replaced by dq_xml_begin / _end...
00042 #define DQ_XML_CDATASTRINGS 4
00043 #define DQ_XML_TYPEINFO 8
00044 #define DQ_XML_APPEND 16
00045 #define DQ_XML_BEGIN 32
00046 #define DQ_XML_END 64
00047 
00048 
00049 // ODBC Catalog functions
00050 // Use as only parameter with get_result()
00051 
00052 #define SQLGET_CATALOGS 1
00053 #define SQLGET_SCHEMAS 2
00054 #define SQLGET_TABLES 3
00055 #define SQLGET_TABLETYPES 4
00056 #define SQLGET_COLUMNS 5
00057 #define SQLGET_SPECIALCOLUMNS 6
00058 #define SQLGET_PRIMARYKEYS 7
00059 #define SQLGET_VIEWS 8
00060 
00061 
00062 // ODBC length definitions
00063 #define STR_LEN 128+1 // odbc
00064 #define REM_LEN 254+1 // odbc
00065 #define BUF_LEN 512 // DSQLHRES
00066 
00069 typedef union
00070 {
00071         bool b;
00072         char c;
00073         unsigned char byte;
00074         short s;
00075         int i; // not used
00076         long l;
00077         float f;
00078         double d;
00079         _int64 bigint;
00080         SQL_DATE_STRUCT date;
00081         SQL_TIME_STRUCT time;
00082         SQL_TIMESTAMP_STRUCT timestamp;
00083         SQL_NUMERIC_STRUCT numeric;
00084         SQLGUID guid;
00085         SQL_INTERVAL_STRUCT interval;
00086         char String[BUF_LEN];
00087 } SQLUNIONBUFFER;
00088 
00089 
00090 typedef struct sqlhcolstruct
00091 {
00092         struct sqlhcolstruct *next;
00093         SQLCHAR szColumnName[STR_LEN];
00094         SQLSMALLINT nNameLengthPtr;
00095         char szTypeName[STR_LEN];
00096         SQLSMALLINT nDataTypePtr;
00097         SQLUINTEGER nColumnSizePtr;
00098         SQLSMALLINT nDecimalDigitsPtr;
00099         SQLSMALLINT nNullablePtr;
00100         SQLSMALLINT nCType;
00101         SQLUNIONBUFFER *buffer;
00102         SQLINTEGER cbBuffer;
00103         char *tmpstr;
00104 } SQLHCOL;
00105 
00106 
00107 class DSQLHRES
00108 {
00109 public:
00110         DSQLHRES(int); // number of cols to constructor
00111         ~DSQLHRES();
00112 
00113         void add_col(SQLHCOL *);
00114 
00115         SQLSMALLINT m_num_cols;
00116         SQLHCOL **m_cols;
00117 
00118 private:
00119         SQLHCOL *m_colbase;
00120         int m_qcols;
00121 };
00122 
00123 
00124 
00125 class Database;
00126 class Session;
00127 
00129 class Query
00130 {
00131 public:
00133         Query(Database& dbin);
00136         Query(Database& dbin,const std::string& sql);
00137         virtual ~Query();
00138 
00140         bool Connected();
00142         Database& GetDatabase() const;
00144         const std::string& GetLastQuery();
00145 
00148         short execute(const std::string& sql);
00149 
00151         bool get_result(const std::string& sql,char **attrs = NULL);
00152 
00154         void free_result();
00157         bool fetch_row();
00159         long insert_id();
00161         long num_rows();
00163         int num_cols();
00165         const std::string& GetError();
00167         int GetErrno();
00168 
00170         bool is_null(int x);
00171         bool is_null(const std::string& x);
00172         bool is_null();
00173 
00175         const char *get_string(const std::string& sql);
00177         long get_count(const std::string& sql);
00179         double get_num(const std::string& sql);
00180 
00181         const char *getstr(const std::string& x);
00184         const char *getstr(int x);
00185         const char *getstr();
00186 
00187         long getval(const std::string& x);
00190         long getval(int x);
00191         long getval();
00192 
00193         _int64 getbigint(const std::string& x);
00196         _int64 getbigint(int x);
00197         _int64 getbigint();
00198 
00199         double getnum(const std::string& x);
00202         double getnum(int x);
00203         double getnum();
00204 
00206         SQL_INTERVAL_STRUCT * getinterval(const std::string& x);
00207         SQL_INTERVAL_STRUCT * getinterval(int x);
00208         SQL_INTERVAL_STRUCT * getinterval();
00209 
00210         bool GetColumnInfo(short,SQLHCOL& );
00211         bool get_result(SQLSMALLINT func);
00212         bool get_result(SQLSMALLINT func, const std::string& catalog, const std::string& schema, const std::string& tablename);
00213         void ReturnRowAsXML(std::string& ,const std::string& = "",long = DQ_XML_ENDTAG);
00214         bool GotMore();
00215         bool getcol(std::string& colname,std::string& coltype,int& width,int& dec,int& nullable);
00216         bool getcol(int x,std::string& colname,std::string& coltype,int& width,int& dec,int& nullable);
00217 
00218 private:
00219         bool odbc_store_result();
00220         void error(const std::string& text);
00221         //
00222         Database& m_db;
00223         Session *m_odb;
00224         SQLHSTMT m_hstmt; // %!
00225         DSQLHRES *m_hres;
00226         short m_currentcolumn; // colcount......... duh
00227         long m_results; // num_rows() after query
00228         long m_fetchrows; // 0..m_results-1
00229         int m_qcols; // number of columns in result
00230         int m_currentcol;
00231         std::string m_tmpstr;
00232         std::string m_last_query;
00233         std::string m_sql_state;
00234         std::string m_errtext;
00235         std::map<std::string,int> m_nmap;
00236 };
00237 
00238 
00239 #ifdef ODBCW_NAMESPACE
00240 } // namespace ODBCW_NAMESPACE {
00241 #endif
00242 #endif // _QUERY_H
Page, code, and content Copyright (C) 2006 by Anders Hedström