-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_demo.py
More file actions
178 lines (150 loc) · 5.62 KB
/
client_demo.py
File metadata and controls
178 lines (150 loc) · 5.62 KB
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
"""
client_demo.py — ConnectAI Python Connector demo against the mock server.
Demonstrates:
1. Connecting to the mock API
2. Fetching metadata (catalogs, tables)
3. SELECT with fetchall / fetchone / fetchmany
4. cursor.description (column metadata)
5. Parameterized query (pyformat)
6. Batch INSERT via executemany
7. SELECT after INSERT (in-memory state)
8. DELETE and verify empty result
9. Stored procedure call
10. Error handling
"""
import cdata_connect_ai
BASE_URL = "http://localhost:8080/api"
USERNAME = "test@example.com"
PASSWORD = "any_token"
SEP = "=" * 60
SEP2 = "-" * 60
def section(title: str):
print(f"\n{SEP}\n {title}\n{SEP2}")
def connect():
return cdata_connect_ai.connect(
base_url=BASE_URL,
username=USERNAME,
password=PASSWORD,
)
# ---------------------------------------------------------------------------
# 1. Basic SELECT — fetchall
# ---------------------------------------------------------------------------
section("1. Basic SELECT — fetchall")
conn = connect()
cur = conn.cursor()
cur.execute("SELECT * FROM Salesforce1.Salesforce.Contact")
rows = cur.fetchall()
print(f"Rows returned: {len(rows)}")
for row in rows[:3]:
print(" ", row)
conn.close()
# ---------------------------------------------------------------------------
# 2. cursor.description — column metadata
# ---------------------------------------------------------------------------
section("2. cursor.description — column metadata")
conn = connect()
cur = conn.cursor()
cur.execute("SELECT * FROM Salesforce1.Salesforce.Contact")
print(f"{'Column':<20} {'Type':<10} {'Nullable'}")
print(SEP2)
for col in cur.description:
name, type_obj, _, length, precision, scale, nullable = col
print(f"{name:<20} {str(type_obj):<10} {nullable}")
conn.close()
# ---------------------------------------------------------------------------
# 3. fetchone / fetchmany
# ---------------------------------------------------------------------------
section("3. fetchone / fetchmany")
conn = connect()
cur = conn.cursor()
cur.execute("SELECT * FROM Salesforce1.Salesforce.Account")
first = cur.fetchone()
print(f"fetchone(): {first}")
batch = cur.fetchmany(2)
print(f"fetchmany(2): {batch}")
conn.close()
# ---------------------------------------------------------------------------
# 4. Parameterized query (pyformat)
# ---------------------------------------------------------------------------
section("4. Parameterized query (pyformat)")
conn = connect()
cur = conn.cursor()
cur.execute(
"SELECT * FROM PostgreSQL_Prod.public.users WHERE id = %(id)s",
{"id": 1},
)
rows = cur.fetchall()
print(f"Rows with id=1: {rows}")
conn.close()
# ---------------------------------------------------------------------------
# 5. Batch INSERT via executemany + SELECT to verify
# ---------------------------------------------------------------------------
section("5. Batch INSERT via executemany + SELECT to verify")
conn = connect()
cur = conn.cursor()
# Insert 3 beers — pass raw Python values, executemany wraps them with type info
cur.executemany(
"INSERT INTO PostgreSQL2.public.dbapi20test_booze (beer) VALUES (@beer)",
[
{"@beer": "Coopers"},
{"@beer": "Boags"},
{"@beer": "Victoria Bitter"},
],
)
print("Inserted 3 rows via executemany")
cur.execute("SELECT * FROM PostgreSQL2.public.dbapi20test_booze")
rows = cur.fetchall()
print(f"SELECT after INSERT -> {len(rows)} rows:")
for row in rows:
print(" ", row)
conn.close()
# ---------------------------------------------------------------------------
# 6. DELETE and verify empty
# ---------------------------------------------------------------------------
section("6. DELETE + verify empty")
conn = connect()
cur = conn.cursor()
cur.execute("DELETE FROM PostgreSQL2.public.dbapi20test_booze")
print("DELETE executed")
cur.execute("SELECT * FROM PostgreSQL2.public.dbapi20test_booze")
rows = cur.fetchall()
print(f"Rows after DELETE: {len(rows)}")
conn.close()
# ---------------------------------------------------------------------------
# 7. Stored procedure call
# ---------------------------------------------------------------------------
section("7. Stored procedure call")
conn = connect()
cur = conn.cursor()
cur.callproc("PostgreSQL2.public.lowercase_input", ("HELLO WORLD",))
rows = cur.fetchall()
print(f"callproc result: {rows}")
conn.close()
# ---------------------------------------------------------------------------
# 8. Error handling — connection error
# ---------------------------------------------------------------------------
section("8. Error handling")
try:
bad_conn = cdata_connect_ai.connect(
base_url="http://localhost:9999/api",
username="x",
password="y",
max_retries=0,
)
cur = bad_conn.cursor()
cur.execute("SELECT 1")
except cdata_connect_ai.OperationalError as e:
print(f"OperationalError caught (expected): {type(e).__name__}")
except Exception as e:
print(f"Other error: {type(e).__name__}: {e}")
# ---------------------------------------------------------------------------
# 9. Context info
# ---------------------------------------------------------------------------
section("9. Package info")
print(f"cdata_connect_ai version : {cdata_connect_ai.__version__}")
print(f"DB-API level : {cdata_connect_ai.apilevel}")
print(f"Thread safety : {cdata_connect_ai.threadsafety}")
print(f"Param style : {cdata_connect_ai.paramstyle}")
print(f"Type objects : STRING={cdata_connect_ai.STRING}, NUMBER={cdata_connect_ai.NUMBER}, "
f"DATETIME={cdata_connect_ai.DATETIME}, BINARY={cdata_connect_ai.BINARY}")
print(f"\n{SEP}\nAll done.\n{SEP}")