Skip to content

Commit cbb94b3

Browse files
committed
cleanup
1 parent d5f4e13 commit cbb94b3

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

main.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55

66
setup_logging('stdout')
77

8-
# conn_str = os.getenv("DB_CONNECTION_STRING")
9-
conn_str = "Server=Saumya;DATABASE=master;UID=sa;PWD=HappyPass1234;Trust_Connection=yes;TrustServerCertificate=yes;"
10-
8+
conn_str = os.getenv("DB_CONNECTION_STRING")
119
conn = connect(conn_str)
1210

1311
# conn.autocommit = True
1412

1513
cursor = conn.cursor()
16-
cursor.execute("DROP TABLE IF EXISTS test_decimal")
17-
cursor.execute("CREATE TABLE test_decimal (val DECIMAL(38, 10))")
18-
cursor.execute("INSERT INTO test_decimal (val) VALUES (?)", (decimal.Decimal('1234567890.1234567890'),))
19-
cursor.commit()
20-
print("Inserted value")
21-
cursor.execute("SELECT val FROM test_decimal")
22-
row = cursor.fetchone()
23-
print(f"Fetched value: {row[0]}")
24-
print(f"Type of fetched value: {type(row[0])}")
25-
assert row[0] == decimal.Decimal('1234567890.1234567890')
14+
cursor.execute("SELECT database_id, name from sys.databases;")
15+
rows = cursor.fetchall()
16+
17+
for row in rows:
18+
print(f"Database ID: {row[0]}, Name: {row[1]}")
19+
20+
cursor.close()
21+
conn.close()

mssql_python/cursor.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def _get_numeric_data(self, param):
216216
precision = exponent * -1
217217
scale = exponent * -1
218218

219-
# TODO: Revisit this check, do we want this restriction?
220219
if precision > 38:
221220
raise ValueError(
222221
"Precision of the numeric value is too high - "
@@ -231,30 +230,24 @@ def _get_numeric_data(self, param):
231230
# strip decimal point from param & convert the significant digits to integer
232231
# Ex: 12.34 ---> 1234
233232
int_str = ''.join(str(d) for d in digits_tuple)
234-
235-
# Apply exponent to get the unscaled integer string
236233
if exponent > 0:
237234
int_str = int_str + ('0' * exponent)
238235
elif exponent < 0:
239-
# if exponent negative and abs(exponent) > num_digits we padded precision above
240-
# for the integer representation we pad leading zeros
241236
if -exponent > num_digits:
242237
int_str = ('0' * (-exponent - num_digits)) + int_str
243238

244-
# Edge: if int_str becomes empty (Decimal('0')), make "0"
245239
if int_str == '':
246240
int_str = '0'
247241

248-
# Convert decimal base-10 string -> python int, then to 16 little-endian bytes
249-
big_int = int(int_str) # Python big int is arbitrary precision
242+
# Convert decimal base-10 string to python int, then to 16 little-endian bytes
243+
big_int = int(int_str)
250244
byte_array = bytearray(16) # SQL_MAX_NUMERIC_LEN
251245
for i in range(16):
252246
byte_array[i] = big_int & 0xFF
253247
big_int >>= 8
254248
if big_int == 0:
255249
break
256250

257-
# numeric_data.val should be bytes (pybindable). Ensure a bytes object of length 16.
258251
numeric_data.val = bytes(byte_array)
259252
return numeric_data
260253

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,8 +2052,8 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
20522052
throw std::runtime_error(MakeParamMismatchErrorStr(info.paramCType, paramIndex));
20532053
}
20542054
NumericData decimalParam = element.cast<NumericData>();
2055-
LOG("Received numeric parameter at [%zu]: precision=%d, scale=%d, sign=%d, val=%lld",
2056-
i, decimalParam.precision, decimalParam.scale, decimalParam.sign, decimalParam.val);
2055+
LOG("Received numeric parameter at [%zu]: precision=%d, scale=%d, sign=%d, val=%s",
2056+
i, decimalParam.precision, decimalParam.scale, decimalParam.sign, decimalParam.val.c_str());
20572057
SQL_NUMERIC_STRUCT& target = numericArray[i];
20582058
std::memset(&target, 0, sizeof(SQL_NUMERIC_STRUCT));
20592059
target.precision = decimalParam.precision;

0 commit comments

Comments
 (0)