Guest User

Untitled

a guest
Oct 12th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import ballerina/io;
  2. import ballerina/config;
  3. import ballerina/log;
  4. import ballerina/mysql;
  5. import ballerina/sql;
  6.  
  7. endpoint mysql:Client databaseConnector {
  8. host: "localhost",
  9. port: 3306,
  10. name: "github_issues",
  11. username: "root",
  12. password: "password",
  13. poolOptions: { maximumPoolSize:5},
  14. dbOptions: { useSSL: false }
  15. };
  16.  
  17. type Person record {
  18. int id;
  19. int age = -1;
  20. float salary;
  21. string name;
  22. boolean married;
  23. };
  24.  
  25. type Oxder record { //I know... Order messes up the SQL
  26. int personId;
  27. int orderId;
  28. string items;
  29. float amount;
  30. };
  31.  
  32. type OrderDetails record {
  33. int orderId;
  34. string personName;
  35. string items;
  36. float amount;
  37. };
  38.  
  39. public function main() {
  40. var personTableSelectResult = databaseConnector->select("SELECT * FROM Person", Person);
  41. match personTableSelectResult {
  42. table personTable => {
  43. printTable("SELECT operation success", "Person", personTable);
  44.  
  45. var orderTableSelectResult = databaseConnector->select("SELECT * FROM Oxder", Oxder);
  46. match orderTableSelectResult {
  47. table orderTable => {
  48. printTable("SELECT operation success", "Order", orderTable);
  49. table<OrderDetails> joinedTable =
  50. from personTable as tempPersonTbl join orderTable as tempOrderTbl
  51. on tempPersonTbl.id == tempOrderTbl.personId
  52. select tempPersonTbl.name as personName,
  53. tempOrderTbl.orderId as orderId,
  54. tempOrderTbl.items as items,
  55. tempOrderTbl.amount as amount;
  56. printTable("Joined tb", "JOINED", joinedTable);
  57. }
  58. error e => {
  59. log:printError("Error reading ORDER table: " + e.message);
  60. }
  61. }
  62. }
  63. error e => {
  64. log:printError("Error reading PERSON table: " + e.message);
  65. }
  66. }
  67. }
  68.  
  69. function printTable(string stmt, string tableName, table returnedTable) {
  70. var retData = <json>returnedTable;
  71. io:println(stmt);
  72. io:print(tableName);
  73. match retData {
  74. json jsonRes => io:println(io:sprintf("%s", jsonRes));
  75. error e => io:println("Error in table to json conversion");
  76. }
  77. }
Add Comment
Please, Sign In to add comment