Run the basic usage example
()
| 37 | |
| 38 | |
| 39 | def main() -> int: |
| 40 | """Run the basic usage example""" |
| 41 | print("=== GraphLite SDK Basic Usage Example ===\n") |
| 42 | |
| 43 | # Use temporary directory for demo |
| 44 | db_path = tempfile.mkdtemp(prefix="graphlite_sdk_example_") |
| 45 | |
| 46 | try: |
| 47 | # 1. Open a database |
| 48 | print("1. Opening database...") |
| 49 | db = GraphLite.open(db_path) |
| 50 | print(f" Database opened at {db_path}\n") |
| 51 | |
| 52 | # 2. Create a session |
| 53 | print("2. Creating session...") |
| 54 | session = db.session("admin") |
| 55 | print(" Session created for user 'admin'\n") |
| 56 | |
| 57 | # 3. Execute DDL statements |
| 58 | print("3. Creating schema and graph...") |
| 59 | session.execute("CREATE SCHEMA IF NOT EXISTS /example") |
| 60 | session.execute("SESSION SET SCHEMA /example") |
| 61 | session.execute("CREATE GRAPH IF NOT EXISTS social") |
| 62 | session.execute("SESSION SET GRAPH social") |
| 63 | print(" Schema and graph created\n") |
| 64 | |
| 65 | # 4. Insert data using transactions |
| 66 | print("4. Inserting data with transaction...") |
| 67 | with session.transaction() as tx: |
| 68 | tx.execute("INSERT (p:Person {name: 'Alice', age: 30})") |
| 69 | tx.execute("INSERT (p:Person {name: 'Bob', age: 25})") |
| 70 | tx.execute("INSERT (p:Person {name: 'Charlie', age: 35})") |
| 71 | tx.execute("INSERT (p:Person {name: 'David', age: 28})") |
| 72 | tx.execute("INSERT (p:Person {name: 'Eve', age: 23})") |
| 73 | tx.execute("INSERT (p:Person {name: 'Frank', age: 40})") |
| 74 | tx.commit() |
| 75 | print(" Inserted 6 persons\n") |
| 76 | |
| 77 | # 5. Query data directly |
| 78 | print("5. Querying data...") |
| 79 | result = session.query("MATCH (p:Person) RETURN p.name as name, p.age as age") |
| 80 | print(f" Found {len(result.rows)} persons:") |
| 81 | for row in result.rows: |
| 82 | name = row.get("name") |
| 83 | age = row.get("age") |
| 84 | if name is not None and age is not None: |
| 85 | print(f" - Name: {name}, Age: {age}") |
| 86 | print() |
| 87 | |
| 88 | # 6. Use query builder |
| 89 | print("6. Using query builder...") |
| 90 | result = (session.query_builder() |
| 91 | .match_pattern("(p:Person)") |
| 92 | .where_clause("p.age > 25") |
| 93 | .return_clause("p.name as name, p.age as age") |
| 94 | .order_by("p.age DESC") |
| 95 | .execute()) |
| 96 | print(f" Found {len(result.rows)} persons over 25:") |
no test coverage detected