source: Include/Classes/System/Data/Odbc/Odbc.ab@ 97

Last change on this file since 97 was 1, checked in by (none), 18 years ago
File size: 7.5 KB
Line 
1' odbc.sbp
2
3#ifndef _INC_SQLEXT
4#include <api_sqlext.sbp>
5#endif
6
7
8
9'===========================================================
10' IOdbcConnection インターフェイス
11
12Dim IID_IOdbcConnection = [&HE0FC6F3C,&HDD4C,&H40f6,[&H82,&H8F,&H42,&H90,&H67,&H27,&H9C,&HDC]] As GUID
13Interface IOdbcConnection
14 Inherits IUnknown
15 Function Connect(lpszDataSource As LPSTR) As SQLRETURN
16 Function Disconnect() As SQLRETURN
17 Function CreateCommand() As *COdbcCommand
18End Interface
19
20Function OdbcConnection_CreateInstance() As *IOdbcConnection
21 Dim pobj_OdbcConnection As *COdbcConnection
22 pobj_OdbcConnection=New COdbcConnection()
23
24 pobj_OdbcConnection->QueryInterface(IID_IOdbcConnection,VarPtr(OdbcConnection_CreateInstance))
25End Function
26
27' ここまで
28'===========================================================
29
30
31
32Class COdbcConnection
33 m_ref As Long '参照カウンタ
34
35 hEnv As SQLHENV '環境ハンドル
36 hDbc As SQLHDBC '接続ハンドル
37
38Public
39 Sub COdbcConnection()
40 '環境ハンドルを取得
41 SQLAllocHandle( SQL_HANDLE_ENV, 0 , hEnv )
42 SQLSetEnvAttr ( hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3 As SQLPOINTER, 0 )
43
44 '接続ハンドルを取得
45 SQLAllocHandle( SQL_HANDLE_DBC, hEnv, hDbc )
46 End Sub
47 Sub ~COdbcConnection()
48 '切断
49 Disconnect()
50
51 '接続ハンドルを解放
52 SQLFreeHandle( SQL_HANDLE_DBC, hDbc )
53
54 '環境ハンドルを解放
55 SQLFreeHandle( SQL_HANDLE_ENV, hEnv )
56 End Sub
57
58
59 Function GetEnvHandle() As SQLHENV
60 GetEnvHandle = hEnv
61 End Function
62 Function GetDbcHandle() As SQLHDBC
63 GetDbcHandle = hDbc
64 End Function
65
66
67
68 '-------------------------------------------------------
69 ' COMインターフェイス用IUnknownメソッド
70
71 Virtual Function QueryInterface(ByRef riid As IID, ppvObj As *VoidPtr) As HRESULT
72 If IsEqualIID(riid,IID_IOdbcConnection)<>0 or IsEqualIID(riid,IID_IUnknown)<>0 Then
73 Set_LONG_PTR(ppvObj,VarPtr(This) As LONG_PTR)
74 AddRef()
75 QueryInterface=S_OK
76 Else
77 Set_LONG_PTR(ppvObj,0)
78 QueryInterface=E_NOINTERFACE
79 End If
80 End Function
81
82 Virtual Function AddRef() As DWord
83 m_ref++
84 AddRef=m_ref
85 End Function
86 Virtual Function Release() As DWord
87 m_ref--
88 If m_ref=0 Then
89 Delete VarPtr(This)
90 End If
91 Release=m_ref
92 End Function
93
94 ' ここまで
95 '-------------------------------------------------------
96
97
98 '-------------------------------------------------------
99 ' 接続を確立
100 '-------------------------------------------------------
101 Virtual Function Connect(lpszDataSource As LPSTR) As SQLRETURN
102 Dim szConnStrOut[ELM(1024)] As Byte
103 Dim cbConnStrOut As SQLSMALLINT
104
105 'データベースへ接続する
106 Dim ret As SQLRETURN
107 ret=SQLDriverConnect(hDbc,
108 0,
109 lpszDataSource,
110 SQL_NTS,
111 szConnStrOut,
112 1024,
113 cbConnStrOut,
114 SQL_DRIVER_COMPLETE_REQUIRED)
115
116 Connect=ret
117 End Function
118
119
120 '-------------------------------------------------------
121 ' 切断
122 '-------------------------------------------------------
123 Virtual Function Disconnect() As SQLRETURN
124 Disconnect = SQLDisconnect(hDbc)
125 End Function
126
127
128 '--------------------------------
129 ' コマンド インスタンスを生成
130 '--------------------------------
131
132 Virtual Function CreateCommand() As *COdbcCommand
133 Dim pobj_Command As *COdbcCommand
134 pobj_Command=New COdbcCommand(VarPtr(This))
135
136 Return pobj_Command
137 End Function
138
139/*
140 Function ExecDirect(lpszSQL As LPSTR) As SQLRETURN
141 '前回のステートメントハンドルを解放
142 StmtFreeHandle()
143
144 'ステートメントハンドルを取得する
145 SQLAllocHandle( SQL_HANDLE_STMT, hDbc, hStmt )
146
147 'SQL文を発行
148 ExecDirect = SQLExecDirect( hStmt, lpszSQL, SQL_NTS )
149 End Function
150
151 Function Prepare(lpszSQL As LPSTR) As SQLRETURN
152 '前回のステートメントハンドルを解放
153 StmtFreeHandle()
154
155 'ステートメントハンドルを取得する
156 SQLAllocHandle( SQL_HANDLE_STMT, hDbc, hStmt )
157
158 Prepare = SQLPrepare( hStmt, lpszSQL, SQL_NTS ) 'SQL文を設定する
159 End Function
160
161 Function Execute() As SQLRETURN
162 Execute = SQLExecute( hStmt )
163 End Function*/
164End Class
165
166
167
168Class COdbcCommand
169Public
170 pobj_Connection As *COdbcConnection
171 hStmt As SQLHSTMT
172 CommandText As String
173
174 Sub COdbcCommand(pobj_Connection As *COdbcConnection)
175 This.pobj_Connection=pobj_Connection
176 End Sub
177 Sub ~COdbcCommand()
178 If hStmt Then
179 SQLFreeHandle( SQL_HANDLE_STMT, hStmt )
180 hStmt=0
181 End If
182 End Sub
183
184
185 Function ExecuteReader() As *COdbcDataReader
186 'DataReaderを構築
187 Dim pobj_DataReader As *COdbcDataReader
188 pobj_DataReader=New COdbcDataReader(VarPtr(This))
189
190 Return pobj_DataReader
191 End Function
192
193 Function Release() As DWord
194 Delete VarPtr(This)
195 End Function
196End Class
197
198
199Class COdbcData
200Public
201 lpszName As LPSTR
202 iType As SQLSMALLINT
203 iSize As SQLINTEGER
204
205 lpszValue As LPSTR
206
207 Sub COdbcData(lpszName As LPSTR, iType As SQLSMALLINT, iSize As SQLINTEGER)
208 This.lpszName=malloc(lstrlen(lpszName)+1)
209 lstrcpy(This.lpszName,lpszName)
210
211 This.iType=iType
212
213 This.iSize=iSize
214
215 lpszValue=malloc(iSize+1)
216 End Sub
217 Sub ~COdbcData()
218 free(lpszName)
219 lpszName=0
220
221 free(lpszValue)
222 lpszValue=0
223 End Sub
224End Class
225
226
227Class COdbcDataReader
228 pobj_Command As *COdbcCommand
229
230 Item As **COdbcData
231 iColNum As SQLSMALLINT
232
233 bFirstRead As BOOL
234
235 hStmt As SQLHSTMT
236
237Public
238
239 Sub COdbcDataReader(pobj_Command As *COdbcCommand)
240 This.pobj_Command=pobj_Command
241
242
243 'ステートメントハンドルを取得する
244 SQLAllocHandle( SQL_HANDLE_STMT,
245 pobj_Command->pobj_Connection->GetDbcHandle(),
246 hStmt )
247
248 'SQL文を発行
249 SQLExecDirect( hStmt, pobj_Command->CommandText, SQL_NTS )
250
251
252 '列数を取得
253 SQLNumResultCols(hStmt, iColNum)
254
255 '列データ格納領域を確保
256 Item=malloc(iColNum*SizeOf(*COdbcData))
257
258 Dim i As SQLSMALLINT
259 Dim szColName[255] As Byte
260 Dim acc_bytes As SQLSMALLINT
261 Dim iType As SQLSMALLINT, iSize As SQLINTEGER, scale As SQLSMALLINT, nullable As SQLSMALLINT
262 Dim displaysize As SQLINTEGER
263 For i=0 To ELM(iColNum)
264 '列属性を取得
265 SQLDescribeCol(hStmt,
266 i+1,
267 szColName,255,acc_bytes,
268 iType,
269 iSize,
270 scale,
271 nullable)
272
273 '文字列に換算したときの長さを取得
274 SQLColAttributes (hStmt,
275 i+1, SQL_COLUMN_DISPLAY_SIZE, NULL, 0,
276 ByVal NULL, displaysize)
277
278 If displaysize<lstrlen(szColName) Then
279 iSize=lstrlen(szColName)+1
280 Else
281 iSize=displaysize+1
282 End If
283
284 '単一データを生成
285 Item[i]=New COdbcData(szColName,iType,iSize)
286
287 'バインド
288 Dim idummy As SQLLEN
289 SQLBindCol(hStmt, i+1, SQL_CHAR, ByVal Item[i]->lpszValue, iSize, idummy)
290 Next
291
292 bFirstRead=1
293 End Sub
294 Sub ~COdbcDataReader()
295 Dim i As Long
296 For i=0 To ELM(iColNum)
297 Delete Item[i]
298 Next
299 free(Item)
300 Item=0
301
302 If hStmt Then
303 SQLFreeHandle( SQL_HANDLE_STMT, hStmt )
304 hStmt=0
305 End If
306 End Sub
307
308 Function GetFieldType(i As Long) As SQLSMALLINT
309 If i>=iColNum Then Return 0
310 Return Item[i]->iType
311 End Function
312
313 Function GetName(i As Long) As LPSTR
314 If i>=iColNum Then Return 0
315 Return Item[i]->lpszName
316 End Function
317
318 Function GetStrPtr(i As Long) As LPSTR
319 If i>=iColNum Then Return 0
320 Return Item[i]->lpszValue
321 End Function
322
323 Function Read() As BOOL
324 Dim ret As SQLRETURN
325 If bFirstRead Then
326 '先頭行へ移動する
327 SQLFetchScroll( hStmt, SQL_FETCH_FIRST, 0 )
328
329 ret = SQLFetch( hStmt )
330 If (ret = SQL_NO_DATA) Then Return 0
331
332 bFirstRead=0
333 Else
334 '次の行へ移動する
335 ret = SQLFetchScroll( hStmt, SQL_FETCH_NEXT, 0 )
336 If (ret = SQL_NO_DATA) Then Return 0
337 End If
338
339 Return 1
340 End Function
341
342 Virtual Function Release() As DWord
343 Delete VarPtr(This)
344 End Function
345End Class
Note: See TracBrowser for help on using the repository browser.