|
1 | | - |
2 | 1 | import asyncio |
3 | | -import async_gaussdb |
4 | 2 | import os |
| 3 | +import async_gaussdb |
5 | 4 |
|
6 | 5 | # ----------------------------------------------------------------------------- |
7 | 6 | # Scenario: Configuring SSL using DSN (Data Source Name) |
|
14 | 13 | # ~/.postgresql/root.crt (Linux/Mac) or %APPDATA%\postgresql\root.crt (Windows) |
15 | 14 | CERTS = os.path.join(os.path.dirname(__file__), '../tests/certs') |
16 | 15 | SSL_CERT_FILE = os.path.join(CERTS, 'server.cert.pem') |
| 16 | + |
| 17 | + |
17 | 18 | async def main(): |
18 | 19 | # ------------------------------------------------------------------------- |
19 | 20 | # Constructing the DSN Connection String |
20 | 21 | # Format: gaussdb://user:password@host:port/database?param=value |
21 | | - # |
| 22 | + # |
22 | 23 | # Key Parameters: |
23 | 24 | # 1. sslmode=verify-ca -> Verifies the server's certificate signature. |
24 | 25 | # (Use 'verify-full' to also verify the hostname) |
25 | 26 | # 2. sslrootcert=... -> Explicitly tells the driver where the CA file is. |
26 | 27 | # ------------------------------------------------------------------------- |
27 | | - |
| 28 | + |
28 | 29 | dsn = ( |
29 | 30 | f"gaussdb://testuser:Test%40123@127.0.0.1:5432/postgres" |
30 | 31 | f"?sslmode=verify-ca&sslrootcert={SSL_CERT_FILE}" |
31 | 32 | ) |
32 | 33 |
|
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 | + ) |
34 | 38 |
|
35 | 39 | try: |
36 | 40 | # 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 |
38 | 42 | # contains all the necessary configuration. |
39 | 43 | conn = await async_gaussdb.connect(dsn) |
40 | | - |
| 44 | + |
41 | 45 | print("SSL Connection Successful (via sslmode)!") |
42 | 46 | print(f" Encryption Status: {conn._protocol.is_ssl}") |
43 | 47 |
|
44 | 48 | # --------------------------------------------------------------------- |
45 | 49 | # Core Tasks (Drop -> Create -> Insert -> Update -> Select) |
46 | 50 | # --------------------------------------------------------------------- |
47 | | - |
| 51 | + |
48 | 52 | # 1. Clean up old data |
49 | 53 | drop_table_sql = "DROP TABLE IF EXISTS test" |
50 | 54 | print(f"\n[Executing] {drop_table_sql}") |
51 | 55 | await conn.execute(drop_table_sql) |
52 | | - |
| 56 | + |
53 | 57 | # 2. Create new table |
54 | 58 | create_table_sql = ( |
55 | 59 | "CREATE TABLE test (id serial PRIMARY KEY, num integer, data text)" |
56 | 60 | ) |
57 | 61 | print(f"\n[Executing] {create_table_sql}") |
58 | 62 | await conn.execute(create_table_sql) |
59 | | - |
| 63 | + |
60 | 64 | # 3. Insert Data (Using $1, $2 placeholders for async driver) |
61 | 65 | insert_data_sql = "INSERT INTO test (num, data) VALUES ($1, $2)" |
62 | 66 | print(f"\n[Executing] {insert_data_sql}") |
63 | | - |
| 67 | + |
64 | 68 | 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") |
66 | 71 | print(" -> Inserted 2 rows.") |
67 | | - |
| 72 | + |
68 | 73 | # 4. Update Data |
69 | 74 | update_data_sql = "UPDATE test SET data = 'gaussdb' WHERE num = 2" |
70 | 75 | print(f"\n[Executing] {update_data_sql}") |
71 | 76 | await conn.execute(update_data_sql) |
72 | 77 | print(" -> Update complete.") |
73 | | - |
| 78 | + |
74 | 79 | # 5. Select and Verify |
75 | 80 | select_sql = "SELECT * FROM test ORDER BY id" |
76 | 81 | print(f"\n[Executing] {select_sql}") |
77 | 82 | rows = await conn.fetch(select_sql) |
78 | | - |
| 83 | + |
79 | 84 | print("\n--- Query Results ---") |
80 | 85 | for row in rows: |
81 | 86 | print(f"ID: {row['id']} | Num: {row['num']} | Data: {row['data']}") |
82 | 87 |
|
83 | 88 | except Exception as e: |
84 | 89 | 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 | + |
87 | 93 | finally: |
88 | 94 | if 'conn' in locals(): |
89 | 95 | print("\nClosing connection...") |
90 | 96 | await conn.close() |
91 | 97 | print("✅ Connection closed.") |
92 | 98 |
|
| 99 | + |
93 | 100 | if __name__ == "__main__": |
94 | 101 | # Check for file existence just for this tutorial to be helpful |
95 | 102 | 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}") |
97 | 105 | print(" (The code will attempt to connect, but will likely fail)") |
98 | | - |
| 106 | + |
99 | 107 | asyncio.run(main()) |
0 commit comments