demo.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Invercargill.Expressions;
  4. using InvercargillSql;
  5. using InvercargillSql.Orm;
  6. using InvercargillSql.Orm.Projections;
  7. using InvercargillSql.Migrations;
  8. using InvercargillSql.Dialects;
  9. /**
  10. * Invercargill-Sql ORM Demo Application
  11. *
  12. * This demo showcases the main features of the Invercargill-Sql ORM:
  13. * - Database migrations
  14. * - Entity mapping and CRUD operations
  15. * - Projection queries (simple, join, aggregate)
  16. */
  17. public int main(string[] args) {
  18. print("=== Invercargill-Sql ORM Demo ===\n\n");
  19. // Allow optional connection string argument
  20. string connection_string = args.length > 1
  21. ? args[1]
  22. : "sqlite::memory:";
  23. try {
  24. // Create and open database connection
  25. var conn = ConnectionFactory.create_and_open(connection_string);
  26. var dialect = new SqliteDialect();
  27. var registry = new TypeRegistry();
  28. // --- Running Migrations ---
  29. print("--- Running Migrations ---\n");
  30. run_migrations(conn, dialect);
  31. // --- Registering Entity Mappers ---
  32. print("\n--- Registering Entity Mappers ---\n");
  33. register_entities(registry, conn, dialect);
  34. // --- Registering Projection Definitions ---
  35. print("Registering projection definitions...\n");
  36. register_projections(registry);
  37. // Create session with registry
  38. var session = new OrmSession(conn, registry, dialect);
  39. // --- Inserting Data ---
  40. print("\n--- Inserting Data ---\n");
  41. insert_sample_data(session);
  42. // --- Querying Entities ---
  43. print("\n--- Querying Entities ---\n");
  44. demonstrate_entity_queries(session);
  45. // --- Updating Data ---
  46. print("\n--- Updating Data ---\n");
  47. demonstrate_update(session);
  48. // --- Deleting Data ---
  49. print("\n--- Deleting Data ---\n");
  50. demonstrate_delete(session);
  51. // --- Querying Projections ---
  52. print("\n--- Querying Projections ---\n");
  53. demonstrate_projection_queries(session);
  54. print("\n=== Demo Complete ===\n");
  55. conn.close();
  56. return 0;
  57. } catch (Error e) {
  58. printerr("\n=== Demo failed: %s ===\n", e.message);
  59. return 1;
  60. }
  61. }
  62. void run_migrations(Connection conn, SqlDialect dialect) throws SqlError {
  63. var runner = new MigrationRunner(conn, dialect);
  64. // Register migrations
  65. runner.register_migration(new V001_CreateUsers());
  66. runner.register_migration(new V002_CreateProducts());
  67. runner.register_migration(new V003_CreateOrders());
  68. // Run all pending migrations
  69. runner.migrate_to_latest();
  70. // Get applied migrations count
  71. var applied = runner.get_applied_migrations();
  72. print("Applied %d migrations\n", (int)applied.length);
  73. }
  74. void register_entities(TypeRegistry registry, Connection conn, SqlDialect dialect) throws SqlError {
  75. // Register User entity with schema introspection
  76. registry.register_with_schema<User>("users", conn, dialect, User.configure_mapper);
  77. print("Registered User entity\n");
  78. // Register Product entity with schema introspection
  79. registry.register_with_schema<Product>("products", conn, dialect, Product.configure_mapper);
  80. print("Registered Product entity\n");
  81. // Register Order entity with schema introspection
  82. registry.register_with_schema<Order>("orders", conn, dialect, Order.configure_mapper);
  83. print("Registered Order entity\n");
  84. }
  85. void register_projections(TypeRegistry registry) throws ProjectionError {
  86. // Register projections using static configure_projection methods
  87. var user_summary_builder = new ProjectionBuilder<UserSummary>(registry);
  88. UserSummary.configure_projection(user_summary_builder);
  89. registry.register_projection<UserSummary>(user_summary_builder.build());
  90. var order_detail_builder = new ProjectionBuilder<OrderDetail>(registry);
  91. OrderDetail.configure_projection(order_detail_builder);
  92. registry.register_projection<OrderDetail>(order_detail_builder.build());
  93. var sales_report_builder = new ProjectionBuilder<SalesReport>(registry);
  94. SalesReport.configure_projection(sales_report_builder);
  95. registry.register_projection<SalesReport>(sales_report_builder.build());
  96. }
  97. void insert_sample_data(OrmSession session) throws SqlError {
  98. // Insert users
  99. var alice = new User();
  100. alice.name = "Alice";
  101. alice.email = "alice@example.com";
  102. alice.age = 30;
  103. alice.is_active = true;
  104. session.insert(alice);
  105. print("Inserted user: %s (ID: %lld)\n", alice.name, alice.id);
  106. var bob = new User();
  107. bob.name = "Bob";
  108. bob.email = "bob@example.com";
  109. bob.age = 22;
  110. bob.is_active = true;
  111. session.insert(bob);
  112. print("Inserted user: %s (ID: %lld)\n", bob.name, bob.id);
  113. var charlie = new User();
  114. charlie.name = "Charlie";
  115. charlie.email = "charlie@example.com";
  116. charlie.age = 35;
  117. charlie.is_active = false;
  118. session.insert(charlie);
  119. print("Inserted user: %s (ID: %lld)\n", charlie.name, charlie.id);
  120. // Insert products
  121. var widget = new Product();
  122. widget.name = "Widget";
  123. widget.category = "Electronics";
  124. widget.price = 29.99;
  125. widget.stock = 100;
  126. session.insert(widget);
  127. print("Inserted product: %s (ID: %lld)\n", widget.name, widget.id);
  128. var gadget = new Product();
  129. gadget.name = "Gadget";
  130. gadget.category = "Electronics";
  131. gadget.price = 49.99;
  132. gadget.stock = 50;
  133. session.insert(gadget);
  134. print("Inserted product: %s (ID: %lld)\n", gadget.name, gadget.id);
  135. var doohickey = new Product();
  136. doohickey.name = "Doohickey";
  137. doohickey.category = "Accessories";
  138. doohickey.price = 9.99;
  139. doohickey.stock = 200;
  140. session.insert(doohickey);
  141. print("Inserted product: %s (ID: %lld)\n", doohickey.name, doohickey.id);
  142. // Insert orders
  143. int order_count = 0;
  144. // Alice orders 2 Widgets
  145. var order1 = create_order(alice.id, widget.id, 2, 59.98, "completed");
  146. session.insert(order1);
  147. order_count++;
  148. // Alice orders 1 Gadget
  149. var order2 = create_order(alice.id, gadget.id, 1, 49.99, "completed");
  150. session.insert(order2);
  151. order_count++;
  152. // Bob orders 3 Doohickeys
  153. var order3 = create_order(bob.id, doohickey.id, 3, 29.97, "pending");
  154. session.insert(order3);
  155. order_count++;
  156. // Charlie orders 1 Widget and 2 Gadgets
  157. var order4 = create_order(charlie.id, widget.id, 1, 29.99, "completed");
  158. session.insert(order4);
  159. order_count++;
  160. var order5 = create_order(charlie.id, gadget.id, 2, 99.98, "completed");
  161. session.insert(order5);
  162. order_count++;
  163. print("Inserted %d orders\n", order_count);
  164. }
  165. Order create_order(int64 user_id, int64 product_id, int64 quantity, double total, string status) {
  166. var order = new Order();
  167. order.user_id = user_id;
  168. order.product_id = product_id;
  169. order.quantity = quantity;
  170. order.total = total;
  171. order.status = status;
  172. order.created_at = new DateTime.now_utc();
  173. return order;
  174. }
  175. void demonstrate_entity_queries(OrmSession session) throws SqlError {
  176. // Query all users
  177. var all_users = session.query<User>().materialise();
  178. print("All users: %d\n", (int)all_users.length);
  179. // Query users over 25
  180. var users_over_25 = session.query<User>()
  181. .where(expr("age > $0", new NativeElement<int?>(25)))
  182. .materialise();
  183. print("Users over 25: %d\n", (int)users_over_25.length);
  184. // Query active users ordered by age
  185. var active_users = session.query<User>()
  186. .where(expr("is_active == $0", new NativeElement<bool?>(true)))
  187. .order_by(expr("age"))
  188. .materialise();
  189. print("Active users ordered by age: ");
  190. var first = true;
  191. foreach (var user in active_users) {
  192. if (!first) print(", ");
  193. print("%s(%lld)", user.name, user.age);
  194. first = false;
  195. }
  196. print("\n");
  197. // Query with pagination
  198. var paged_users = session.query<User>()
  199. .order_by(expr("id"))
  200. .limit(2)
  201. .offset(1)
  202. .materialise();
  203. print("Users (page 2, size 2): %d\n", (int)paged_users.length);
  204. }
  205. void demonstrate_update(OrmSession session) throws SqlError {
  206. // Find Alice by ID
  207. var alice = session.query<User>()
  208. .where(expr("id == $0", new NativeElement<int64?>(1)))
  209. .first();
  210. if (alice != null) {
  211. // Update Alice's age
  212. alice.age = 31;
  213. session.update(alice);
  214. print("Updated %s's age to %lld\n", alice.name, alice.age);
  215. // Verify the update
  216. var verified = session.query<User>()
  217. .where(expr("id == $0", new NativeElement<int64?>(1)))
  218. .first();
  219. print("Verified update: %s is now %lld\n", verified.name, verified.age);
  220. }
  221. }
  222. void demonstrate_delete(OrmSession session) throws SqlError {
  223. // Find Bob by name (using age as a proxy since string comparison in expressions may not work)
  224. var bob = session.query<User>()
  225. .where(expr("age == $0", new NativeElement<int64?>(22)))
  226. .first();
  227. if (bob != null) {
  228. print("Deleted user: %s\n", bob.name);
  229. session.delete(bob);
  230. // Verify deletion
  231. var remaining = session.query<User>().materialise();
  232. print("Remaining users: %d\n", (int)remaining.length);
  233. }
  234. }
  235. void demonstrate_projection_queries(OrmSession session) throws SqlError, ProjectionError {
  236. // Simple projection - User summaries
  237. print("\n--- User Summaries ---\n");
  238. var user_summaries = session.query<UserSummary>()
  239. .order_by(expr("user_name"))
  240. .materialise();
  241. foreach (var summary in user_summaries) {
  242. print(" [%lld] %s <%s>\n", summary.user_id, summary.user_name, summary.email);
  243. }
  244. print("Total user summaries: %d\n", (int)user_summaries.length);
  245. // Join projection - Order details
  246. print("\n--- Order Details ---\n");
  247. var order_details = session.query<OrderDetail>()
  248. .order_by(expr("order_id"))
  249. .materialise();
  250. foreach (var detail in order_details) {
  251. print(" [%lld] %s ordered %dx %s ($%.2f) - %s\n",
  252. detail.order_id, detail.user_name, detail.quantity,
  253. detail.product_name, detail.total, detail.status);
  254. }
  255. print("Total order details: %d\n", (int)order_details.length);
  256. // Aggregate projection - Sales by category
  257. print("\n--- Sales by Category ---\n");
  258. var sales_reports = session.query<SalesReport>()
  259. .order_by_desc(expr("total_revenue"))
  260. .materialise();
  261. foreach (var report in sales_reports) {
  262. print(" %s: %lld orders, $%.2f revenue, $%.2f avg\n",
  263. report.category, report.total_orders,
  264. report.total_revenue, report.avg_order_value);
  265. }
  266. print("Total categories: %d\n", (int)sales_reports.length);
  267. // Projection with where clause
  268. print("\n--- Completed Orders ---\n");
  269. var completed_orders = session.query<OrderDetail>()
  270. .where(expr("status == $0", new NativeElement<string>("completed")))
  271. .materialise();
  272. print("Completed orders: %d\n", (int)completed_orders.length);
  273. }