Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class to test Db2 SQL Engine
  5. * Tests use static setUpBeforeClass and tearDownAfterClass because setUp and tearDown
  6. * have problems recreating the test library
  7. */
  8. class DB2iSeriesSQLEngineTest extends PHPUnit_Framework_TestCase
  9. {
  10. protected static $dbUsername = "USERNAME";
  11. protected static $dbPassword = "PASSWORD";
  12.  
  13. protected static $conn;
  14. protected static $toolkitObject;
  15. protected static $testLib = 'DOCTRINE';
  16. protected static $testTable = 'TESTTBL';
  17. protected static $testData = [
  18. ['COD' => 'A', 'DES' => 'DESCA'],
  19. ['COD' => 'B', 'DES' => 'DESCB'],
  20. ['COD' => 'C', 'DES' => 'DESCC'],
  21. ['COD' => 'D', 'DES' => 'DESCD'],
  22. ];
  23.  
  24. public static function setUpBeforeClass()
  25. {
  26. parent::setUpBeforeClass();
  27.  
  28. static::initConnection();
  29. static::initToolkitObject();
  30.  
  31. static::createTestDb();
  32. static::createTestTable();
  33. static::initTestTableData();
  34. }
  35.  
  36. public static function tearDownAfterClass()
  37. {
  38. parent::tearDownAfterClass();
  39. static::deleteTestDb();
  40. }
  41.  
  42. protected static function initConnection()
  43. {
  44. $options = [
  45. 'autocommit' => 1, //DB2_AUTOCOMMIT_ON',
  46. 'i5_naming' => 1, //DB2_I5_NAMING_ON
  47. 'db2_attr_case' => 0, //DB2_CASE_NATURAL
  48. 'i5_date_fmt' => 1, //DB2_I5_FMT_ISO
  49. //'i5_lib' => 'QGPL',
  50. 'i5_libl' => 'QGPL',
  51. 'i5_commit' => 2 //2=READ_COMMITTED 5=DB2_I5_TXN_SERIALIZABLE
  52. ];
  53.  
  54. static::$conn = db2_connect('*LOCAL', static::$dbUsername, static::$dbPassword, $options);
  55. }
  56.  
  57. protected static function execute($sql, $params = [])
  58. {
  59. //echo $sql;
  60. $stmt = @db2_prepare(static::$conn, $sql);
  61. if (!$stmt) {
  62. throw new \Exception(db2_stmt_errormsg());
  63. }
  64.  
  65. if (empty($params)) {
  66. $res = db2_exec(static::$conn, $sql);
  67. } else {
  68. $res = db2_execute($stmt, $params);
  69. }
  70.  
  71. return $res;
  72. }
  73.  
  74. protected static function fetchAll($stmt)
  75. {
  76. $resultSet = [];
  77. while ($row = db2_fetch_assoc($stmt)) {
  78. $resultSet[] = $row;
  79. }
  80. return $resultSet;
  81. }
  82.  
  83. protected static function initToolkitObject()
  84. {
  85. //Toolkit CCSID test
  86. include_once 'iToolkitService.php';
  87.  
  88. static::$toolkitObject = ToolkitService::getInstance(static::$conn, DB2_I5_NAMING_ON);
  89. static::$toolkitObject->setToolkitServiceParams(array(
  90. 'stateless' => true
  91. ));
  92. }
  93.  
  94. protected static function call($command)
  95. {
  96. static::$toolkitObject->CLCommand($command);
  97. }
  98.  
  99. protected static function createTestDb()
  100. {
  101. static::deleteTestDb();
  102. static::execute(sprintf('CREATE COLLECTION %s', static::$testLib));
  103. }
  104.  
  105. protected static function createTestTable()
  106. {
  107. $createStmt = sprintf("
  108. CREATE TABLE %s/%s (
  109. /*ID INTEGER GENERATED BY DEFAULT AS IDENTITY,*/
  110. COD VARCHAR(2) NOT NULL WITH DEFAULT '',
  111. DES NVARCHAR (20)
  112. )
  113. ", static::$testLib, static::$testTable);
  114.  
  115. static::execute($createStmt);
  116. }
  117.  
  118. protected static function initTestTableData()
  119. {
  120. $insertStmt = sprintf("
  121. INSERT INTO %s/%s (COD, DES) VALUES (?, ?)
  122. ", static::$testLib, static::$testTable);
  123.  
  124. foreach (static::$testData as $row) {
  125. static::execute($insertStmt, array_values($row));
  126. }
  127. }
  128.  
  129. protected static function deleteTestDb()
  130. {
  131. /* Disconnect Journal from library */
  132. static::call(sprintf("ENDJRNPF FILE(*ALL) JRN(%s/QSQJRN)", static::$testLib));
  133. /* Delete Journal */
  134. static::call(sprintf("DLTJRN JRN(%s/QSQJRN)", static::$testLib));
  135. /* Delete Journal Receiver */
  136. static::call(sprintf("DLTJRNRCV JRNRCV(%s/QSQJRN*) DLTOPT(*IGNINQMSG)", static::$testLib));
  137.  
  138. /* Base Libl */
  139. // static::call(sprintf("CHGLIBL LIBL(QGPL)"));
  140.  
  141. /* Delete library */
  142. static::call(sprintf("DLTLIB LIB(%s)", static::$testLib));
  143. }
  144.  
  145. /**
  146. * @requires OS AIX
  147. */
  148. public function testOrderBy()
  149. {
  150. $expectedData = array_reverse(static::$testData);
  151.  
  152. $testStmt = sprintf("
  153. SELECT
  154. *
  155. FROM %s/%s
  156. ORDER BY COD DESC
  157. ", static::$testLib, static::$testTable);
  158.  
  159. $stmt = $this->execute($testStmt);
  160.  
  161. $data = $this->fetchAll($stmt);
  162.  
  163. $this->assertEquals($expectedData, $data);
  164. }
  165.  
  166. /**
  167. * This is a false test because nested Order By do not work as expected on IBMi
  168. * @requires OS AIX
  169. */
  170. public function testNestedOrderByIsNotWorkingOnIbmi()
  171. {
  172. $expectedData = static::$testData;// array_reverse(static::$testData);
  173.  
  174. $testStmt = sprintf("
  175. SELECT * FROM (
  176. SELECT
  177. *
  178. FROM %s/%s
  179. ORDER BY COD DESC
  180. ) t1
  181. ", static::$testLib, static::$testTable);
  182.  
  183. $stmt = $this->execute($testStmt);
  184. $data = $this->fetchAll($stmt);
  185.  
  186. $this->assertEquals($expectedData, $data);
  187. }
  188.  
  189. /**
  190. * This is a false test because nested Order By do not work as expected on IBMi
  191. * @requires OS AIX
  192. */
  193. public function testNestedOrderByAlternativeUsingOverStatement()
  194. {
  195. $expectedData = array_reverse(static::$testData);
  196.  
  197. $testStmt = sprintf("
  198. SELECT t1.* FROM (
  199. SELECT %s.*, ROW_NUMBER() OVER(ORDER BY COD DESC) as ROWNUM
  200. FROM %s/%s
  201. ORDER BY COD DESC
  202. ) t1
  203. ", static::$testTable, static::$testLib, static::$testTable);
  204.  
  205. $stmt = $this->execute($testStmt);
  206. $data = $this->fetchAll($stmt);
  207.  
  208. //remove ROWNUM
  209. $data = array_map(function ($el) {
  210. return ['COD' => $el['COD'], 'DES' => $el['DES']];
  211. }, $data);
  212.  
  213. $this->assertEquals($expectedData, $data);
  214. }
  215.  
  216. /**
  217. * This is a false test because LIMIT OFFSET statement is not implemented until 7.1 TR11 and IBM i 7.2 TR3
  218. * @requires OS AIX
  219. * @expectedException \Exception
  220. */
  221. public function testLimitOffsetIsNotWorkingOnCurrentIbmiVersion()
  222. {
  223.  
  224. $testStmt = sprintf("
  225. SELECT * FROM %s/%s LIMIT 2 OFFSET 2
  226. ", static::$testLib, static::$testTable);
  227.  
  228. $this->execute($testStmt);
  229. }
  230.  
  231. /**
  232. * @requires OS AIX
  233. */
  234. public function testLimitOffsetAlternativeUsingRownum()
  235. {
  236.  
  237. $limit = 2;
  238. $offset = 2;
  239.  
  240. $expectedData = array_splice(static::$testData, $limit, $offset);
  241.  
  242. $testStmt = sprintf("
  243. SELECT t1.* FROM (
  244. SELECT %s.*, ROW_NUMBER() OVER(ORDER BY COD ASC) as ROWNUM
  245. FROM %s/%s
  246. ORDER BY COD ASC
  247. ) t1 WHERE ROWNUM BETWEEN %d AND %d
  248. ", static::$testTable, static::$testLib, static::$testTable, $offset + 1, $offset + $limit);
  249.  
  250. $stmt = $this->execute($testStmt);
  251. $data = $this->fetchAll($stmt);
  252.  
  253. //remove ROWNUM
  254. $data = array_map(function ($el) {
  255. return ['COD' => $el['COD'], 'DES' => $el['DES']];
  256. }, $data);
  257.  
  258. $this->assertEquals($expectedData, $data);
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement