OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
odbcxx.h
Go to the documentation of this file.
1 /*
2  * FUNCTION:
3  * ODBC driver -- developed/tested with both iODBC http://www.iodbc.org
4  * and with unixODBC
5  *
6  * HISTORY:
7  * Copyright (c) 2002,2008 Linas Vepstas <linas@linas.org>
8  * created by Linas Vepstas March 2002
9  * ported to C++ March 2008
10  *
11  * LICENSE:
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Affero General Public License v3 as
14  * published by the Free Software Foundation and including the exceptions
15  * at http://opencog.org/wiki/Licenses
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU Affero General Public License
23  * along with this program; if not, write to:
24  * Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
28 #ifndef _OPENCOG_PERSISTENT_ODBC_DRIVER_H
29 #define _OPENCOG_PERSISTENT_ODBC_DRIVER_H
30 
31 #include <stack>
32 #include <string>
33 
34 #include <sql.h>
35 #include <sqlext.h>
36 
41 class ODBCRecordSet;
42 
44 {
45  friend class ODBCRecordSet;
46  private:
47  std::string dbname;
48  std::string username;
50  SQLHENV sql_henv;
51  SQLHDBC sql_hdbc;
52  std::stack<ODBCRecordSet *> free_pool;
53 
55 
56  public:
57  ODBCConnection(const char * dbname,
58  const char * username,
59  const char * authentication);
61 
62  bool connected(void) const;
63 
64  ODBCRecordSet *exec(const char *);
65  void extract_error(const char *);
66 };
67 
69 {
70  friend class ODBCConnection;
71  private:
73  SQLHSTMT sql_hstmt;
74 
75  int ncols;
76  int arrsize;
77  char **column_labels;
79  char **values;
80  int *vsizes;
81 
82  void alloc_and_bind_cols(int ncols);
85 
86  void get_column_labels(void);
87  int get_col_by_name (const char *);
88 
89  public:
90  // rewind the cursor to the start
91  void rewind(void);
92 
93  int fetch_row(void); // return non-zero value if there's another row.
94  const char * get_value(const char * fieldname);
95 
96  // call this, instead of the destructor,
97  // when done with this instance.
98  void release(void);
99 
100  // Calls the callback once for each row.
101  template<class T> bool
102  foreach_row(bool (T::*cb)(void), T *data)
103  {
104  while (fetch_row())
105  {
106  bool rc = (data->*cb) ();
107  if (rc) return rc;
108  }
109  return false;
110  }
111 
112  // Calls the callback once for each column.
113  template<class T> bool
114  foreach_column(bool (T::*cb)(const char *, const char *), T *data)
115  {
116  int i;
117  if (0 > ncols)
118  {
120  }
121 
122  for (i=0; i<ncols; i++)
123  {
124  bool rc = (data->*cb) (column_labels[i], values[i]);
125  if (rc) return rc;
126  }
127  return false;
128  }
129 };
130 
136 inline void escape_single_quotes(std::string &str)
137 {
138  std::string::size_type pos = 0;
139  pos = str.find ('\'', pos);
140  while (pos != std::string::npos)
141  {
142  str.insert(pos, 1, '\'');
143  pos += 2;
144  pos = str.find('\'', pos);
145  }
146 }
147 
150 #endif // _OPENCOG_PERSISTENT_ODBC_DRIVER_H
ODBCRecordSet * exec(const char *)
int ncols
Definition: odbcxx.h:75
int arrsize
Definition: odbcxx.h:76
bool is_connected
Definition: odbcxx.h:49
int get_col_by_name(const char *)
ODBCConnection(const char *dbname, const char *username, const char *authentication)
SQLHENV sql_henv
Definition: odbcxx.h:50
int * vsizes
Definition: odbcxx.h:80
SQLHSTMT sql_hstmt
Definition: odbcxx.h:73
std::stack< ODBCRecordSet * > free_pool
Definition: odbcxx.h:52
char ** column_labels
Definition: odbcxx.h:77
ODBCRecordSet(ODBCConnection *)
void get_column_labels(void)
void alloc_and_bind_cols(int ncols)
const char * get_value(const char *fieldname)
void escape_single_quotes(std::string &str)
Definition: odbcxx.h:136
bool foreach_row(bool(T::*cb)(void), T *data)
Definition: odbcxx.h:102
int * column_datatype
Definition: odbcxx.h:78
std::string username
Definition: odbcxx.h:48
char ** values
Definition: odbcxx.h:79
std::string dbname
Definition: odbcxx.h:47
ODBCConnection * conn
Definition: odbcxx.h:72
bool connected(void) const
int fetch_row(void)
SQLHDBC sql_hdbc
Definition: odbcxx.h:51
bool foreach_column(bool(T::*cb)(const char *, const char *), T *data)
Definition: odbcxx.h:114
void release(void)
void extract_error(const char *)
void rewind(void)
ODBCRecordSet * get_record_set(void)