Skip to content

Commit cb53741

Browse files
committed
restored another file
1 parent fc8f1f1 commit cb53741

1 file changed

Lines changed: 32 additions & 34 deletions

File tree

mssql_python/testing_ddbc_bindings.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Licensed under the MIT license.
44
This module provides functions to test DDBC bindings.
55
"""
6-
76
import ctypes
87
import datetime
98
import os
@@ -27,15 +26,13 @@ def alloc_handle(handle_type, input_handle):
2726
"""
2827
Allocate a handle for the given handle type and input handle.
2928
"""
30-
handle = ctypes.c_void_p()
31-
result_alloc = ddbc_bindings.DDBCSQLAllocHandle(
29+
result_alloc, handle = ddbc_bindings.DDBCSQLAllocHandle(
3230
handle_type,
33-
input_handle,
34-
ctypes.cast(ctypes.pointer(handle), ctypes.c_void_p).value,
31+
input_handle
3532
)
3633
if result_alloc < 0:
3734
print(
38-
"Error:", ddbc_bindings.DDBCSQLCheckError(handle_type, handle.value, result_alloc)
35+
"Error:", ddbc_bindings.DDBCSQLCheckError(handle_type, handle, result_alloc)
3936
)
4037
raise RuntimeError(f"Failed to allocate handle. Error code: {result_alloc}")
4138
return handle
@@ -45,10 +42,10 @@ def free_handle(handle_type, handle):
4542
"""
4643
Free the handle for the given handle type and handle.
4744
"""
48-
result_free = ddbc_bindings.DDBCSQLFreeHandle(handle_type, handle.value)
45+
result_free = ddbc_bindings.DDBCSQLFreeHandle(handle_type, handle)
4946
if result_free < 0:
5047
print(
51-
"Error:", ddbc_bindings.DDBCSQLCheckError(handle_type, handle.value, result_free)
48+
"Error:", ddbc_bindings.DDBCSQLCheckError(handle_type, handle, result_free)
5249
)
5350
raise RuntimeError(f"Failed to free handle. Error code: {result_free}")
5451

@@ -60,12 +57,12 @@ def ddbc_sql_execute(
6057
Execute an SQL statement using DDBC bindings.
6158
"""
6259
result_execute = ddbc_bindings.DDBCSQLExecute(
63-
stmt_handle.value, query, params, param_info_list, is_stmt_prepared, use_prepare
60+
stmt_handle, query, params, param_info_list, is_stmt_prepared, use_prepare
6461
)
6562
if result_execute < 0:
6663
print(
6764
"Error: ",
68-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle.value, result_execute),
65+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle, result_execute),
6966
)
7067
raise RuntimeError(f"Failed to execute query. Error code: {result_execute}")
7168
return result_execute
@@ -79,12 +76,12 @@ def fetch_data_onebyone(stmt_handle):
7976
ret_fetch = 1
8077
while ret_fetch != SQL_NO_DATA:
8178
row = []
82-
ret_fetch = ddbc_bindings.DDBCSQLFetchOne(stmt_handle.value, row)
79+
ret_fetch = ddbc_bindings.DDBCSQLFetchOne(stmt_handle, row)
8380
if ret_fetch < 0:
8481
print(
8582
"Error: ",
8683
ddbc_bindings.DDBCSQLCheckError(
87-
SQL_HANDLE_STMT, stmt_handle.value, ret_fetch
84+
SQL_HANDLE_STMT, stmt_handle, ret_fetch
8885
),
8986
)
9087
raise RuntimeError(f"Failed to fetch data. Error code: {ret_fetch}")
@@ -100,12 +97,12 @@ def fetch_data_many(stmt_handle):
10097
rows = []
10198
ret_fetch = 1
10299
while ret_fetch != SQL_NO_DATA:
103-
ret_fetch = ddbc_bindings.DDBCSQLFetchMany(stmt_handle.value, rows, 10)
100+
ret_fetch = ddbc_bindings.DDBCSQLFetchMany(stmt_handle, rows, 10)
104101
if ret_fetch < 0:
105102
print(
106103
"Error: ",
107104
ddbc_bindings.DDBCSQLCheckError(
108-
SQL_HANDLE_STMT, stmt_handle.value, ret_fetch
105+
SQL_HANDLE_STMT, stmt_handle, ret_fetch
109106
),
110107
)
111108
raise RuntimeError(f"Failed to fetch data. Error code: {ret_fetch}")
@@ -117,11 +114,11 @@ def fetch_data_all(stmt_handle):
117114
Fetch all data using DDBC bindings.
118115
"""
119116
rows = []
120-
ret_fetch = ddbc_bindings.DDBCSQLFetchAll(stmt_handle.value, rows)
117+
ret_fetch = ddbc_bindings.DDBCSQLFetchAll(stmt_handle, rows)
121118
if ret_fetch != SQL_NO_DATA:
122119
print(
123120
"Error: ",
124-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle.value, ret_fetch),
121+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle, ret_fetch),
125122
)
126123
raise RuntimeError(f"Failed to fetch data. Error code: {ret_fetch}")
127124
return rows
@@ -132,28 +129,28 @@ def fetch_data(stmt_handle):
132129
Fetch data using DDBC bindings.
133130
"""
134131
rows = []
135-
column_count = ddbc_bindings.DDBCSQLNumResultCols(stmt_handle.value)
132+
column_count = ddbc_bindings.DDBCSQLNumResultCols(stmt_handle)
136133
print("Number of columns = " + str(column_count))
137134
while True:
138-
result_fetch = ddbc_bindings.DDBCSQLFetch(stmt_handle.value)
135+
result_fetch = ddbc_bindings.DDBCSQLFetch(stmt_handle)
139136
if result_fetch == SQL_NO_DATA:
140137
break
141138
if result_fetch < 0:
142139
print(
143140
"Error: ",
144141
ddbc_bindings.DDBCSQLCheckError(
145-
SQL_HANDLE_STMT, stmt_handle.value, result_fetch
142+
SQL_HANDLE_STMT, stmt_handle, result_fetch
146143
),
147144
)
148145
raise RuntimeError(f"Failed to fetch data. Error code: {result_fetch}")
149146
if column_count > 0:
150147
row = []
151-
result_get_data = ddbc_bindings.DDBCSQLGetData(stmt_handle.value, column_count, row)
148+
result_get_data = ddbc_bindings.DDBCSQLGetData(stmt_handle, column_count, row)
152149
if result_get_data < 0:
153150
print(
154151
"Error: ",
155152
ddbc_bindings.DDBCSQLCheckError(
156-
SQL_HANDLE_STMT, stmt_handle.value, result_get_data
153+
SQL_HANDLE_STMT, stmt_handle, result_get_data
157154
),
158155
)
159156
raise RuntimeError(f"Failed to get data. Error code: {result_get_data}")
@@ -166,11 +163,11 @@ def describe_columns(stmt_handle):
166163
Describe columns using DDBC bindings.
167164
"""
168165
column_names = []
169-
result_describe = ddbc_bindings.DDBCSQLDescribeCol(stmt_handle.value, column_names)
166+
result_describe = ddbc_bindings.DDBCSQLDescribeCol(stmt_handle, column_names)
170167
if result_describe < 0:
171168
print(
172169
"Error: ",
173-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle.value, result_describe),
170+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_STMT, stmt_handle, result_describe),
174171
)
175172
raise RuntimeError(f"Failed to describe columns. Error code: {result_describe}")
176173
return column_names
@@ -180,11 +177,11 @@ def connect_to_db(dbc_handle, connection_string):
180177
"""
181178
Connect to the database using DDBC bindings.
182179
"""
183-
result_connect = ddbc_bindings.DDBCSQLDriverConnect(dbc_handle.value, 0, connection_string)
180+
result_connect = ddbc_bindings.DDBCSQLDriverConnect(dbc_handle, 0, connection_string)
184181
if result_connect < 0:
185182
print(
186183
"Error: ",
187-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_DBC, dbc_handle.value, result_connect),
184+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_DBC, dbc_handle, result_connect),
188185
)
189186
raise RuntimeError(f"SQLDriverConnect failed. Error code: {result_connect}")
190187

@@ -348,26 +345,27 @@ def add_numeric_param(params, param_infos, param):
348345

349346
if __name__ == "__main__":
350347
# Allocate environment handle
351-
env_handle = alloc_handle(SQL_HANDLE_ENV, 0)
352-
348+
env_handle = alloc_handle(SQL_HANDLE_ENV, None)
349+
353350
# Set the DDBC version environment attribute
354351
result_set_env = ddbc_bindings.DDBCSQLSetEnvAttr(
355-
env_handle.value, SQL_ATTR_DDBC_VERSION, SQL_OV_DDBC3_80, 0
352+
env_handle, SQL_ATTR_DDBC_VERSION, SQL_OV_DDBC3_80, 0
356353
)
357354
if result_set_env < 0:
358355
print(
359356
"Error: ",
360-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_ENV, env_handle.value, result_set_env),
357+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_ENV, env_handle, result_set_env),
361358
)
362359
raise RuntimeError(
363360
f"Failed to set DDBC version attribute. Error code: {result_set_env}"
364361
)
365362

366363
# Allocate connection handle
367-
dbc_handle = alloc_handle(SQL_HANDLE_DBC, env_handle.value)
364+
dbc_handle = alloc_handle(SQL_HANDLE_DBC, env_handle)
368365

369366
# Fetch the connection string from environment variables
370367
connection_string = os.getenv("DB_CONNECTION_STRING")
368+
371369
if not connection_string:
372370
raise EnvironmentError(
373371
"Environment variable 'DB_CONNECTION_STRING' is not set or is empty."
@@ -378,7 +376,7 @@ def add_numeric_param(params, param_infos, param):
378376
print("Connection successful!")
379377

380378
# Allocate connection statement handle
381-
stmt_handle = alloc_handle(SQL_HANDLE_STMT, dbc_handle.value)
379+
stmt_handle = alloc_handle(SQL_HANDLE_STMT, dbc_handle)
382380

383381
ParamInfo = ddbc_bindings.ParamInfo
384382
"""
@@ -441,16 +439,16 @@ def add_numeric_param(params, param_infos, param):
441439
print(row)
442440
else:
443441
print("No columns to fetch data from.")
444-
ret_fetch = ddbc_bindings.DDBCSQLMoreResults(stmt_handle.value)
442+
ret_fetch = ddbc_bindings.DDBCSQLMoreResults(stmt_handle)
445443

446444
# Free the statement handle
447445
free_handle(SQL_HANDLE_STMT, stmt_handle)
448446
# Disconnect from the data source
449-
result_disconnect = ddbc_bindings.DDBCSQLDisconnect(dbc_handle.value)
447+
result_disconnect = ddbc_bindings.DDBCSQLDisconnect(dbc_handle)
450448
if result_disconnect < 0:
451449
print(
452450
"Error: ",
453-
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_DBC, dbc_handle.value, result_disconnect),
451+
ddbc_bindings.DDBCSQLCheckError(SQL_HANDLE_DBC, dbc_handle, result_disconnect),
454452
)
455453
raise RuntimeError(
456454
f"Failed to disconnect from the data source. Error code: {result_disconnect}"

0 commit comments

Comments
 (0)