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