Guest User

Untitled

a guest
Nov 4th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import ballerina/config;
  2. import ballerina/io;
  3. import ballerina/log;
  4. import ballerina/mysql;
  5.  
  6. @final string QUERY_GET_EMPLOYEE_INFORMATION ="SELECT emp_no,birth_date,first_name,last_name,gender,hire_date FROM employees WHERE emp_no= ?";
  7. // sql statement to retreive data
  8. public type EmployeeInformation record { //Record which match columns in the table
  9.  
  10. int emp_no;
  11. string birth_date;
  12. string first_name;
  13. string last_name;
  14. string gender;
  15. string hire_date;
  16. };
  17.  
  18. endpoint mysql:Client employeeDBEndpoint { // mysql endpoint
  19. host: config:getAsString("HOST"), // read database host
  20. port: config:getAsInt("PORT"), // read database port
  21. name: config:getAsString("DB_NAME"), // name of database schema
  22. username: config:getAsString("USERNAME"), // Credential to access database
  23. password: config:getAsString("PASSWORD"),
  24. dbOptions: { "useSSL": false },
  25. poolOptions: { maximumPoolSize: config:getAsInt("POOL_SIZE")}
  26. };
  27.  
  28. public type EmployeeInformationDetails object {
  29. public function getEmployeeInformation(int employeeid) returns json;
  30.  
  31. };
  32.  
  33. function EmployeeInformationDetails::getEmployeeInformation(int employeeid) returns json {
  34.  
  35. log:printInfo("Getting Employee information from Database");
  36. var empInfo = employeeDBEndpoint->select(QUERY_GET_EMPLOYEE_INFORMATION, EmployeeInformation, employeeid); // This will execute the query and
  37. // return a EmployeeInformation record
  38. json err;
  39. match empInfo {
  40. table result => { // result will be a table type
  41. match <json>result{ // cast it to json
  42. json details => {
  43. return details;
  44. }
  45. error e => {
  46. err = getJsonError(e); // return json type error if error occur in converting
  47. }
  48. }
  49. }
  50. error e => {
  51. err = getJsonError(e);
  52. }
  53. }
  54. return err;
  55. }
  56.  
  57. function getJsonError(error e) returns json { // error to json converter.
  58. json err = {
  59. message: e.message
  60. };
  61. return err;
  62. }
Add Comment
Please, Sign In to add comment