File: classODBC.h

package info (click to toggle)
unixodbc 2.2.11-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,292 kB
  • ctags: 12,410
  • sloc: ansic: 116,624; cpp: 29,333; sh: 16,966; makefile: 2,961; lex: 241; yacc: 182; perl: 141; sed: 16; sql: 1
file content (204 lines) | stat: -rw-r--r-- 4,733 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**************************************************
 *
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under GPL 18.FEB.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey         - pharvey@codebydesign.com
 **************************************************/
#ifndef classODBC_included
#define classODBC_included

#include "classNode.h"
#include "classCanvas.h"
class classDrivers ;
class classDataSources ;
#include <qstring.h>
#include <qcursor.h>
#include <qnamespace.h>
#include <qmessagebox.h>
#include <qlistview.h>
#include <sqlext.h>

#define MAX_COLUMN_WIDTH 1024

// ******************************
//  Common Utility Classes
// ******************************

// Use to display ODBC error messages
void my_msgBox( const QString & className,
             const QString & actionName,
             SQLRETURN rc   = 0,
             SQLHENV  hEnv  = NULL,
             SQLHDBC  hDbc  = NULL,
             SQLHSTMT hStmt = NULL,
             const QString & moreInfo = "",
             QMessageBox::Icon icon = QMessageBox::Critical ) ;

// Cursor scoper class
class CursorScoper: public QWidget
{
 public:
  CursorScoper( QWidget * w ) : w_(w)
    { w_->setCursor(waitCursor) ; }
 ~CursorScoper()
    { w_->unsetCursor()         ; }
 private:
   QWidget *w_ ;
} ;

// SQL ENV scoper class
class EnvironmentScoper
{
 public:
  EnvironmentScoper( const QString & className = "" ) : hEnv_( NULL ) , className_( className )
  {
    SQLRETURN nReturn ;
    if (!SQL_SUCCEEDED(nReturn=SQLAllocEnv( &hEnv_ ) ) )
      my_msgBox( className_, "SQLAllocEnv", nReturn ) ;
  }

  HENV operator()() const
  {
    return hEnv_ ;
  }

 ~EnvironmentScoper()
  {
    SQLRETURN nReturn ;
    if (hEnv_)
      if (!SQL_SUCCEEDED(nReturn=SQLFreeEnv( hEnv_ ) ) )
        my_msgBox( className_, "SQLFreeEnv", nReturn ) ;
    hEnv_ = NULL ;
  }

 private:
  HENV hEnv_ ;
  QString className_ ;

} ;

// SQL DBC scoper class
class ConnectionScoper
{
 public:
  ConnectionScoper( HENV hEnv, const QString & className = "" ) : hDbc_( NULL ) , className_( className ), active_( false )
  {
    SQLRETURN nReturn ;
    if (!SQL_SUCCEEDED(nReturn=SQLAllocConnect( hEnv, &hDbc_ ) ) )
      my_msgBox( className_, "SQLAllocConnect", nReturn, hEnv ) ;
  }

  HDBC operator()() const
  {
    return hDbc_ ;
  }

  bool connect( const QString & dataSourceName, const QString & uid, const QString & pwd )
  {
    SQLRETURN nReturn ;
    if (!active_ && !SQL_SUCCEEDED(nReturn=SQLConnect( hDbc_, (SQLCHAR*)dataSourceName.data(), SQL_NTS, (SQLCHAR*)uid.data(), SQL_NTS, (SQLCHAR*)pwd.data(), SQL_NTS ) ) )
      my_msgBox( className_, "SQLConnect", nReturn, NULL, hDbc_, NULL, dataSourceName ) ;
    else
      active_ = true ;

    return active_ ;
  }

  void disconnect()
  {
    SQLRETURN nReturn ;
    if (active_ && !SQL_SUCCEEDED(nReturn=SQLDisconnect( hDbc_ ) ) )
        my_msgBox( className_, "SQLDisconnect", nReturn, NULL, hDbc_ ) ;
    active_ = false ;
  }

  bool active() const
  {
    return active_ ;
  }

 ~ConnectionScoper()
  {
    SQLRETURN nReturn ;
    if (hDbc_)
    {
      if (active_ && !SQL_SUCCEEDED(nReturn=SQLDisconnect( hDbc_ ) ) )
        my_msgBox( className_, "SQLDisconnect", nReturn, NULL, hDbc_ ) ;

      if (!SQL_SUCCEEDED(nReturn=SQLFreeConnect( hDbc_ ) ) )
        my_msgBox( className_, "SQLFreeConnect", nReturn, NULL, hDbc_ ) ;

      hDbc_ = NULL ;
      active_ = false ;
    }
  }

 private:
  HDBC hDbc_ ;
  bool active_ ;
  QString className_ ;

} ;

// Statement scoper class
class StatementScoper
{
 public:
  StatementScoper( HDBC hDbc, const QString & className = "" ) : hStmt_( NULL ) , className_( className )
  {
    SQLRETURN nReturn ;
    if (!SQL_SUCCEEDED(nReturn=SQLAllocStmt( hDbc, &hStmt_ ) ) )
      my_msgBox( className_, "SQLAllocStmt", nReturn, NULL, hDbc ) ;
  }

  HSTMT operator()() const
  {
    return hStmt_ ;
  }

  ~StatementScoper()
  {
    SQLRETURN nReturn ;
    if (hStmt_)
      if (!SQL_SUCCEEDED(nReturn=SQLFreeStmt( hStmt_, SQL_DROP ) ) )
        my_msgBox( className_, "SQLFreeStmt", nReturn, NULL, NULL, hStmt_ ) ;
    hStmt_ = NULL ;
  }

 private:
  HSTMT hStmt_ ;
  QString className_ ;

} ;

// ******************************
// ******************************

class classODBC: public classNode
{
public:
    enum DSType
    {
      User,
      System
    };

    classODBC( QListView *pParent, classCanvas *pCanvas );
   ~classODBC() {}

    void setOpen( bool );
    void selectionChanged ( QListViewItem * );

private:
    classDrivers      *pDrivers;
    classDataSources  *pDataSourcesUser;
    classDataSources  *pDataSourcesSystem;

};
#endif