Skip to content

Commit 266d764

Browse files
authored
Merge pull request #21 from zhanghaitao3/master
Fix code formatting to comply with flake8 standards
2 parents 2bb2823 + e5a5d83 commit 266d764

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

examples/demo.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
import asyncio
32
import async_gaussdb
3+
44
# -----------------------------------------------------------------------------
55
# Database Connection Configuration
66
# -----------------------------------------------------------------------------
@@ -12,11 +12,13 @@
1212
'port': 8000
1313
}
1414

15+
1516
async def main():
1617
print(f"Connecting to GaussDB at {DB_CONFIG['host']}:{DB_CONFIG['port']}...")
17-
18+
1819
# 1. Establish Connection
19-
# async_gaussdb automatically handles openGauss/GaussDB specific protocols (e.g., SHA256 auth)
20+
# async_gaussdb automatically handles openGauss/GaussDB specific protocols
21+
# (e.g., SHA256 auth)
2022
conn = await async_gaussdb.connect(**DB_CONFIG)
2123
print("✅ Connection established successfully!")
2224

@@ -41,18 +43,18 @@ async def main():
4143

4244
# ---------------------------------------------------------------------
4345
# Step 3: Insert Data
44-
# Note: Async drivers for Postgres/GaussDB typically use $1, $2 placeholders
45-
# instead of %s used in standard synchronous drivers.
46+
# Note: Async drivers for Postgres/GaussDB typically use $1, $2
47+
# placeholders instead of %s used in standard synchronous drivers.
4648
# ---------------------------------------------------------------------
4749
insert_data_sql = "INSERT INTO test (num, data) VALUES ($1, $2)"
48-
50+
4951
# Preparing sample data
5052
data_to_insert = [
5153
(1, 'initial_data'),
52-
(2, 'data_to_be_updated'), # This row (num=2) will be updated later
54+
(2, 'data_to_be_updated'), # This row (num=2) will be updated later
5355
(3, 'other_data')
5456
]
55-
57+
5658
print(f"\n[Executing] {insert_data_sql}")
5759
for num, data in data_to_insert:
5860
await conn.execute(insert_data_sql, num, data)
@@ -72,24 +74,25 @@ async def main():
7274
# ---------------------------------------------------------------------
7375
select_sql = "SELECT * FROM test ORDER BY id"
7476
print(f"\n[Executing] {select_sql}")
75-
77+
7678
# fetch() returns a list of Record objects
7779
rows = await conn.fetch(select_sql)
78-
80+
7981
print("\n--- Query Results ---")
8082
for row in rows:
8183
# Access data by column name or index
8284
print(f"ID: {row['id']} | Num: {row['num']} | Data: {row['data']}")
83-
85+
8486
except Exception as e:
85-
print(f"\n❌ An error occurred: {e}")
87+
print(f"\nAn error occurred: {e}")
8688
finally:
8789
# ---------------------------------------------------------------------
8890
# Close Connection
8991
# ---------------------------------------------------------------------
9092
print("\nClosing connection...")
9193
await conn.close()
92-
print("✅ Connection closed.")
94+
print("Connection closed.")
95+
9396

9497
if __name__ == "__main__":
9598
asyncio.run(main())

examples/ssl_demo.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
import asyncio
3-
import async_gaussdb
42
import os
3+
import async_gaussdb
54

65
# -----------------------------------------------------------------------------
76
# Scenario: Configuring SSL using DSN (Data Source Name)
@@ -14,86 +13,95 @@
1413
# ~/.postgresql/root.crt (Linux/Mac) or %APPDATA%\postgresql\root.crt (Windows)
1514
CERTS = os.path.join(os.path.dirname(__file__), '../tests/certs')
1615
SSL_CERT_FILE = os.path.join(CERTS, 'server.cert.pem')
16+
17+
1718
async def main():
1819
# -------------------------------------------------------------------------
1920
# Constructing the DSN Connection String
2021
# Format: gaussdb://user:password@host:port/database?param=value
21-
#
22+
#
2223
# Key Parameters:
2324
# 1. sslmode=verify-ca -> Verifies the server's certificate signature.
2425
# (Use 'verify-full' to also verify the hostname)
2526
# 2. sslrootcert=... -> Explicitly tells the driver where the CA file is.
2627
# -------------------------------------------------------------------------
27-
28+
2829
dsn = (
2930
f"gaussdb://testuser:Test%40123@127.0.0.1:5432/postgres"
3031
f"?sslmode=verify-ca&sslrootcert={SSL_CERT_FILE}"
3132
)
3233

33-
print(f"Connecting via DSN: ...sslmode=verify-ca&sslrootcert={os.path.basename(SSL_CERT_FILE)}")
34+
print(
35+
f"Connecting via DSN: ...sslmode=verify-ca&"
36+
f"sslrootcert={os.path.basename(SSL_CERT_FILE)}"
37+
)
3438

3539
try:
3640
# Connect to the database
37-
# We do not need to pass a 'ssl=' context object here because the DSN
41+
# We do not need to pass a 'ssl=' context object here because the DSN
3842
# contains all the necessary configuration.
3943
conn = await async_gaussdb.connect(dsn)
40-
44+
4145
print("SSL Connection Successful (via sslmode)!")
4246
print(f" Encryption Status: {conn._protocol.is_ssl}")
4347

4448
# ---------------------------------------------------------------------
4549
# Core Tasks (Drop -> Create -> Insert -> Update -> Select)
4650
# ---------------------------------------------------------------------
47-
51+
4852
# 1. Clean up old data
4953
drop_table_sql = "DROP TABLE IF EXISTS test"
5054
print(f"\n[Executing] {drop_table_sql}")
5155
await conn.execute(drop_table_sql)
52-
56+
5357
# 2. Create new table
5458
create_table_sql = (
5559
"CREATE TABLE test (id serial PRIMARY KEY, num integer, data text)"
5660
)
5761
print(f"\n[Executing] {create_table_sql}")
5862
await conn.execute(create_table_sql)
59-
63+
6064
# 3. Insert Data (Using $1, $2 placeholders for async driver)
6165
insert_data_sql = "INSERT INTO test (num, data) VALUES ($1, $2)"
6266
print(f"\n[Executing] {insert_data_sql}")
63-
67+
6468
await conn.execute(insert_data_sql, 1, "sslmode_demo")
65-
await conn.execute(insert_data_sql, 2, "wait_for_update") # num=2 will be updated
69+
# num=2 will be updated
70+
await conn.execute(insert_data_sql, 2, "wait_for_update")
6671
print(" -> Inserted 2 rows.")
67-
72+
6873
# 4. Update Data
6974
update_data_sql = "UPDATE test SET data = 'gaussdb' WHERE num = 2"
7075
print(f"\n[Executing] {update_data_sql}")
7176
await conn.execute(update_data_sql)
7277
print(" -> Update complete.")
73-
78+
7479
# 5. Select and Verify
7580
select_sql = "SELECT * FROM test ORDER BY id"
7681
print(f"\n[Executing] {select_sql}")
7782
rows = await conn.fetch(select_sql)
78-
83+
7984
print("\n--- Query Results ---")
8085
for row in rows:
8186
print(f"ID: {row['id']} | Num: {row['num']} | Data: {row['data']}")
8287

8388
except Exception as e:
8489
print(f"\nERROR Connection or Execution Failed: {e}")
85-
print(" Hint: Check if 'server.crt' exists and if the server supports SSL.")
86-
90+
print(" Hint: Check if 'server.crt' exists and "
91+
"if the server supports SSL.")
92+
8793
finally:
8894
if 'conn' in locals():
8995
print("\nClosing connection...")
9096
await conn.close()
9197
print("✅ Connection closed.")
9298

99+
93100
if __name__ == "__main__":
94101
# Check for file existence just for this tutorial to be helpful
95102
if not os.path.exists(SSL_CERT_FILE):
96-
print(f"⚠️ WARNING: The certificate file was not found at: {SSL_CERT_FILE}")
103+
print(f"⚠️ WARNING: The certificate file was not found at: "
104+
f"{SSL_CERT_FILE}")
97105
print(" (The code will attempt to connect, but will likely fail)")
98-
106+
99107
asyncio.run(main())

0 commit comments

Comments
 (0)