Skip to content

Commit c915452

Browse files
committed
cleanup
1 parent ee5ceaf commit c915452

4 files changed

Lines changed: 25 additions & 86 deletions

File tree

main.py

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,17 @@
55

66
setup_logging('stdout')
77

8-
# conn_str = os.getenv("DB_CONNECTION_STRING")
9-
# conn = connect(conn_str)
8+
conn_str = os.getenv("DB_CONNECTION_STRING")
9+
conn = connect(conn_str)
1010

11-
# # conn.autocommit = True
11+
# conn.autocommit = True
1212

13-
# cursor = conn.cursor()
14-
# cursor.execute("SELECT database_id, name from sys.databases;")
15-
# rows = cursor.fetchall()
13+
cursor = conn.cursor()
14+
cursor.execute("SELECT database_id, name from sys.databases;")
15+
rows = cursor.fetchall()
1616

17-
# for row in rows:
18-
# print(f"Database ID: {row[0]}, Name: {row[1]}")
17+
for row in rows:
18+
print(f"Database ID: {row[0]}, Name: {row[1]}")
1919

20-
# cursor.close()
21-
# conn.close()
22-
23-
conn_str = "Server=Saumya;DATABASE=master;UID=sa;PWD=HappyPass1234;Trust_Connection=yes;TrustServerCertificate=yes;"
24-
db_connection = connect(conn_str)
25-
db_connection.autocommit = True
26-
values = ["Ω" * 4100, "漢" * 5000]
27-
cursor = db_connection.cursor()
28-
cursor.execute("CREATE TABLE #pytest_nvarcharmax (col NVARCHAR(MAX))")
29-
db_connection.commit()
30-
31-
# --- use executemany for inserts ---
32-
cursor.executemany(
33-
"INSERT INTO #pytest_nvarcharmax VALUES (?)",
34-
[(v,) for v in values],
35-
)
36-
db_connection.commit()
37-
38-
# --- fetchall ---
39-
cursor.execute("SELECT col FROM #pytest_nvarcharmax ORDER BY LEN(col)")
40-
rows = [r[0] for r in cursor.fetchall()]
41-
assert rows == sorted(values, key=len)
42-
43-
# --- fetchone ---
44-
cursor.execute("SELECT col FROM #pytest_nvarcharmax ORDER BY LEN(col)")
45-
r1 = cursor.fetchone()[0]
46-
r2 = cursor.fetchone()[0]
47-
assert {r1, r2} == set(values)
48-
assert cursor.fetchone() is None
49-
50-
# --- fetchmany ---
51-
cursor.execute("SELECT col FROM #pytest_nvarcharmax ORDER BY LEN(col)")
52-
53-
batch = cursor.fetchmany(1)
54-
assert set(r[0] for r in batch).issubset(set(values))
55-
56-
remaining = []
57-
while True:
58-
rows = cursor.fetchmany(1)
59-
if not rows:
60-
break
61-
remaining.extend(rows)
62-
63-
all_fetched = [r[0] for r in batch + remaining]
64-
assert set(all_fetched) == set(values)
65-
66-
67-
# --- cleanup ---
68-
cursor.execute("DROP TABLE #pytest_nvarcharmax")
69-
db_connection.commit()
20+
cursor.close()
21+
conn.close()

mssql_python/cursor.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -998,19 +998,19 @@ def execute(
998998
self.is_stmt_prepared = [False]
999999

10001000
log('debug', "Executing query: %s", operation)
1001-
# for i, param in enumerate(parameters):
1002-
# log('debug',
1003-
# """Parameter number: %s, Parameter: %s,
1004-
# Param Python Type: %s, ParamInfo: %s, %s, %s, %s, %s""",
1005-
# i + 1,
1006-
# param,
1007-
# str(type(param)),
1008-
# parameters_type[i].paramSQLType,
1009-
# parameters_type[i].paramCType,
1010-
# parameters_type[i].columnSize,
1011-
# parameters_type[i].decimalDigits,
1012-
# parameters_type[i].inputOutputType,
1013-
# )
1001+
for i, param in enumerate(parameters):
1002+
log('debug',
1003+
"""Parameter number: %s, Parameter: %s,
1004+
Param Python Type: %s, ParamInfo: %s, %s, %s, %s, %s""",
1005+
i + 1,
1006+
param,
1007+
str(type(param)),
1008+
parameters_type[i].paramSQLType,
1009+
parameters_type[i].paramCType,
1010+
parameters_type[i].columnSize,
1011+
parameters_type[i].decimalDigits,
1012+
parameters_type[i].inputOutputType,
1013+
)
10141014

10151015
ret = ddbc_bindings.DDBCSQLExecute(
10161016
self.hstmt,
@@ -1701,7 +1701,6 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
17011701
if paraminfo.isDAE:
17021702
any_dae = True
17031703

1704-
# If any DAE parameter exists, fall back to row-by-row execution
17051704
if any_dae:
17061705
log('debug', "DAE parameters detected. Falling back to row-by-row execution with streaming.")
17071706
for row in seq_of_parameters:
@@ -1759,7 +1758,6 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
17591758
# Reset input sizes after execution
17601759
self._reset_inputsizes()
17611760

1762-
17631761
def fetchone(self) -> Union[None, Row]:
17641762
"""
17651763
Fetch the next row of a query result set.

mssql_python/pybind/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ target_compile_definitions(ddbc_bindings PRIVATE
272272

273273
# Add warning level flags for MSVC
274274
if(MSVC)
275-
target_compile_options(ddbc_bindings PRIVATE /W4 )
275+
target_compile_options(ddbc_bindings PRIVATE /W4 /WX)
276276
endif()
277277

278278
# Add macOS-specific string conversion fix

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,8 +1997,7 @@ SQLRETURN SQLExecuteMany_wrap(const SqlHandlePtr statementHandle,
19971997
const std::wstring& query,
19981998
const py::list& columnwise_params,
19991999
const std::vector<ParamInfo>& paramInfos,
2000-
size_t paramSetSize)
2001-
{
2000+
size_t paramSetSize) {
20022001
SQLHANDLE hStmt = statementHandle->get();
20032002
SQLWCHAR* queryPtr;
20042003

@@ -2008,22 +2007,17 @@ SQLRETURN SQLExecuteMany_wrap(const SqlHandlePtr statementHandle,
20082007
#else
20092008
queryPtr = const_cast<SQLWCHAR*>(query.c_str());
20102009
#endif
2011-
20122010
RETCODE rc = SQLPrepare_ptr(hStmt, queryPtr, SQL_NTS);
20132011
if (!SQL_SUCCEEDED(rc)) return rc;
20142012

2015-
// Check if any param is DAE/streaming
20162013
bool hasDAE = false;
20172014
for (const auto& p : paramInfos) {
20182015
if (p.isDAE) {
20192016
hasDAE = true;
20202017
break;
20212018
}
20222019
}
2023-
20242020
if (!hasDAE) {
2025-
LOG("No DAE parameters detected. Proceeding with fast array execution.");
2026-
// Fast path: no streaming, bind entire array
20272021
std::vector<std::shared_ptr<void>> paramBuffers;
20282022
rc = BindParameterArray(hStmt, columnwise_params, paramInfos, paramSetSize, paramBuffers);
20292023
if (!SQL_SUCCEEDED(rc)) return rc;
@@ -2034,8 +2028,6 @@ SQLRETURN SQLExecuteMany_wrap(const SqlHandlePtr statementHandle,
20342028
rc = SQLExecute_ptr(hStmt);
20352029
return rc;
20362030
} else {
2037-
LOG("DAE parameters detected. Falling back to row-by-row execution with streaming.");
2038-
// Fallback: row-by-row execution with streaming
20392031
size_t rowCount = columnwise_params.size();
20402032
for (size_t rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
20412033
py::list rowParams = columnwise_params[rowIndex];
@@ -2045,8 +2037,6 @@ SQLRETURN SQLExecuteMany_wrap(const SqlHandlePtr statementHandle,
20452037
if (!SQL_SUCCEEDED(rc)) return rc;
20462038

20472039
rc = SQLExecute_ptr(hStmt);
2048-
2049-
// Handle DAE streaming
20502040
while (rc == SQL_NEED_DATA) {
20512041
SQLPOINTER token;
20522042
rc = SQLParamData_ptr(hStmt, &token);
@@ -2055,7 +2045,6 @@ SQLRETURN SQLExecuteMany_wrap(const SqlHandlePtr statementHandle,
20552045
py::object* py_obj_ptr = reinterpret_cast<py::object*>(token);
20562046
if (!py_obj_ptr) return SQL_ERROR;
20572047

2058-
// Support string/binary streaming
20592048
if (py::isinstance<py::str>(*py_obj_ptr)) {
20602049
std::string data = py_obj_ptr->cast<std::string>();
20612050
SQLLEN data_len = static_cast<SQLLEN>(data.size());

0 commit comments

Comments
 (0)