mysql-simple.ecl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. IMPORT mysql;
  2. /*
  3. This example illustrates various calls to embdded MySQL code
  4. */
  5. // This is the record structure in ECL that will correspond to the rows in the MySQL dataset
  6. // Note that the default values specified in the fields will be used when a NULL value is being
  7. // returned from MySQL
  8. childrec := RECORD
  9. string name,
  10. integer4 value { default(99999) },
  11. boolean boolval { default(true) },
  12. real8 r8 {default(99.99)},
  13. real4 r4 {default(999.99)},
  14. DATA d {default (D'999999')},
  15. DECIMAL10_2 ddd {default(9.99)},
  16. UTF8 u1 {default(U'9999 ß')},
  17. UNICODE8 u2 {default(U'9999 ßßßß')}
  18. END;
  19. // Some data we will use to initialize the MySQL table
  20. init := DATASET([{'name1', 1, true, 1.2, 3.4, D'aa55aa55', 1234567.89, U'Straße', U'Straße'},
  21. {'name2', 2, false, 5.6, 7.8, D'00', -1234567.89, U'là', U'là'}], childrec);
  22. // Set up the MySQL database
  23. drop() := EMBED(mysql : user('rchapman'),database('test'))
  24. DROP TABLE IF EXISTS tbl1;
  25. ENDEMBED;
  26. create() := EMBED(mysql : user('rchapman'),database('test'))
  27. CREATE TABLE tbl1 ( name VARCHAR(20), value INT, boolval TINYINT, r8 DOUBLE, r4 FLOAT, d BLOB, ddd DECIMAL(10,2), u1 VARCHAR(10), u2 VARCHAR(10) );
  28. ENDEMBED;
  29. // Initialize the MySQL table, passing in the ECL dataset to provide the rows
  30. initialize(dataset(childrec) values) := EMBED(mysql : user('rchapman'),database('test'))
  31. INSERT INTO tbl1 values (?, ?, ?, ?, ?, ?, ?, ?, ?);
  32. ENDEMBED;
  33. // Add an additional row containing NULL values, to test that they are handled properly when read in ECL
  34. initializeNulls() := EMBED(mysql : user('rchapman'),database('test'))
  35. INSERT INTO tbl1 (name) values ('nulls');
  36. ENDEMBED;
  37. // Note that the query string is encoded in utf8
  38. initializeUtf8() := EMBED(mysql : user('rchapman'),database('test'))
  39. INSERT INTO tbl1 values ('utf8test', 1, 1, 1.2, 3.4, 'aa55aa55', 1234567.89, 'Straße', 'Straße');
  40. ENDEMBED;
  41. // Returning a dataset
  42. dataset(childrec) testMySQLDS() := EMBED(mysql : user('rchapman'),database('test'))
  43. SELECT * from tbl1;
  44. ENDEMBED;
  45. // Returning a single row
  46. childrec testMySQLRow() := EMBED(mysql : user('rchapman'),database('test'))
  47. SELECT * from tbl1 LIMIT 1;
  48. ENDEMBED;
  49. // Passing in parameters
  50. childrec testMySQLParms(
  51. string name,
  52. integer value,
  53. boolean boolval,
  54. real8 r8,
  55. real4 r4,
  56. DATA d,
  57. UTF8 u1,
  58. UNICODE8 u2) := EMBED(mysql : user('rchapman'),database('test'))
  59. SELECT * from tbl1 WHERE name=? AND value=? AND boolval=? AND r8=? AND r4=? AND d=? AND u1=? AND u2=?;
  60. ENDEMBED;
  61. // Returning scalars
  62. string testMySQLString() := EMBED(mysql : user('rchapman'),database('test'))
  63. SELECT max(name) from tbl1;
  64. ENDEMBED;
  65. dataset(childrec) testMySQLStringParam(string filter) := EMBED(mysql : user('rchapman'),database('test'))
  66. SELECT * from tbl1 where name = ?;
  67. ENDEMBED;
  68. integer testMySQLInt() := EMBED(mysql : user('rchapman'),database('test'))
  69. SELECT max(value) from tbl1;
  70. ENDEMBED;
  71. boolean testMySQLBool() := EMBED(mysql : user('rchapman'),database('test'))
  72. SELECT max(boolval) from tbl1;
  73. ENDEMBED;
  74. real8 testMySQLReal8() := EMBED(mysql : user('rchapman'),database('test'))
  75. SELECT max(r8) from tbl1;
  76. ENDEMBED;
  77. real4 testMySQLReal4() := EMBED(mysql : user('rchapman'),database('test'))
  78. SELECT max(r4) from tbl1;
  79. ENDEMBED;
  80. data testMySQLData() := EMBED(mysql : user('rchapman'),database('test'))
  81. SELECT max(d) from tbl1;
  82. ENDEMBED;
  83. UTF8 testMySQLUtf8() := EMBED(mysql : user('rchapman'),database('test'))
  84. SELECT max(u1) from tbl1;
  85. ENDEMBED;
  86. UNICODE testMySQLUnicode() := EMBED(mysql : user('rchapman'),database('test'))
  87. SELECT max(u2) from tbl1;
  88. ENDEMBED;
  89. // Passing in AND returning a dataset - this ends up acting a bit like a keyed join...
  90. stringrec := RECORD
  91. string name
  92. END;
  93. stringrec extractName(childrec l) := TRANSFORM
  94. SELF := l;
  95. END;
  96. dataset(childrec) testMySQLDSParam(dataset(stringrec) inrecs) := EMBED(mysql : user('rchapman'),database('test'))
  97. SELECT * from tbl1 where name = ?;
  98. ENDEMBED;
  99. sequential (
  100. drop(),
  101. create(),
  102. initialize(init),
  103. initializeNulls(),
  104. initializeUtf8(),
  105. OUTPUT(testMySQLDS()),
  106. OUTPUT(testMySQLRow().name),
  107. OUTPUT(testMySQLParms('name1', 1, true, 1.2, 3.4, D'aa55aa55', U'Straße', U'Straße')),
  108. OUTPUT(testMySQLString()),
  109. OUTPUT(testMySQLStringParam(testMySqlString())),
  110. OUTPUT(testMySQLInt()),
  111. OUTPUT(testMySQLBool()),
  112. OUTPUT(testMySQLReal8()),
  113. OUTPUT(testMySQLReal4()),
  114. OUTPUT(testMySQLData()),
  115. OUTPUT(testMySQLUtf8()),
  116. OUTPUT(testMySQLUnicode()),
  117. OUTPUT(testMySQLDSParam(PROJECT(init, extractName(LEFT))))
  118. );