DbModel.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Simps.
  5. *
  6. * @link https://simps.io
  7. * @document https://doc.simps.io
  8. * @license https://github.com/simple-swoole/simps/blob/master/LICENSE
  9. */
  10. namespace App\Model;
  11. use Simps\DB\BaseModel;
  12. class DbModel extends BaseModel
  13. {
  14. public function fortunes()
  15. {
  16. $fortune = [];
  17. $this->pdo->fortune_test = $this->pdo->fortune_test ?? $this->pdo->prepare('SELECT id, message FROM Fortune');
  18. $this->pdo->fortune_test->execute();
  19. $arr = $this->pdo->fortune_test->fetchAll();
  20. foreach ($arr as $row) {
  21. $fortune[$row['id']] = $row['message'];
  22. }
  23. $fortune[0] = 'Additional fortune added at request time.';
  24. \asort($fortune);
  25. return $fortune;
  26. }
  27. public function updates(int $queries = 0)
  28. {
  29. $query_count = 1;
  30. if ($queries > 1) {
  31. $query_count = $queries > 500 ? 500 : $queries;
  32. }
  33. $arr = [];
  34. $this->pdo->updates_test_select = $this->pdo->updates_test_select ?? $this->pdo->prepare(
  35. 'SELECT id, randomNumber FROM World WHERE id = ?'
  36. );
  37. $this->pdo->updates_test_update = $this->pdo->updates_test_update ?? $this->pdo->prepare(
  38. 'UPDATE World SET randomNumber = ? WHERE id = ?'
  39. );
  40. while ($query_count--) {
  41. $id = mt_rand(1, 10000);
  42. $randomNumber = mt_rand(1, 10000);
  43. $this->pdo->updates_test_select->execute([$id]);
  44. $ret = $this->pdo->updates_test_select->fetchAll();
  45. // Store result in array.
  46. $world = ['id' => $id, 'randomNumber' => $ret[0]['randomNumber']];
  47. $world['randomNumber'] = $randomNumber;
  48. $this->pdo->updates_test_update->execute([$randomNumber, $id]);
  49. $arr[] = $world;
  50. }
  51. return \json_encode($arr, JSON_NUMERIC_CHECK);
  52. }
  53. public function db(int $queries = 0)
  54. {
  55. // Read number of queries to run from URL parameter
  56. $query_count = 1;
  57. if ($queries > 1) {
  58. $query_count = $queries > 500 ? 500 : $queries;
  59. }
  60. // Create an array with the response string.
  61. $arr = [];
  62. // Define query
  63. $this->pdo->db_test = $this->pdo->db_test ?? $this->pdo->prepare(
  64. 'SELECT id, randomNumber FROM World WHERE id = ?'
  65. );
  66. // For each query, store the result set values in the response array
  67. while ($query_count--) {
  68. $id = mt_rand(1, 10000);
  69. $this->pdo->db_test->execute([$id]);
  70. $data = $this->pdo->db_test->fetchAll();
  71. // Store result in array.
  72. $arr[] = ['id' => $id, 'randomNumber' => $data[0]['randomNumber']];
  73. }
  74. // Use the PHP standard JSON encoder.
  75. // http://www.php.net/manual/en/function.json-encode.php
  76. if ($queries === -1) {
  77. $arr = $arr[0];
  78. }
  79. return \json_encode($arr, JSON_NUMERIC_CHECK);
  80. }
  81. public function microDb()
  82. {
  83. $id = mt_rand(1, 10000);
  84. $data = $this->get(
  85. "World",
  86. [
  87. 'id',
  88. 'randomNumber'
  89. ],
  90. [
  91. "id" => $id
  92. ]
  93. );
  94. return \json_encode($data, JSON_NUMERIC_CHECK);
  95. }
  96. public function microQueries(int $queries = 0)
  97. {
  98. $query_count = 1;
  99. if ($queries > 1) {
  100. $query_count = $queries > 500 ? 500 : $queries;
  101. }
  102. $arr = [];
  103. while ($query_count--) {
  104. $id = mt_rand(1, 10000);
  105. $data = $this->get(
  106. "World",
  107. [
  108. 'id',
  109. 'randomNumber'
  110. ],
  111. [
  112. "id" => $id
  113. ]
  114. );
  115. // Store result in array.
  116. $arr[] = $data;
  117. }
  118. return \json_encode($arr, JSON_NUMERIC_CHECK);
  119. }
  120. public function microUpdates(int $queries = 0)
  121. {
  122. $query_count = 1;
  123. if ($queries > 1) {
  124. $query_count = $queries > 500 ? 500 : $queries;
  125. }
  126. $arr = [];
  127. while ($query_count--) {
  128. $id = mt_rand(1, 10000);
  129. $randomNumber = mt_rand(1, 10000);
  130. $data = $this->get(
  131. "World",
  132. [
  133. 'id',
  134. 'randomNumber'
  135. ],
  136. [
  137. "id" => $id
  138. ]
  139. );
  140. $world = ['id' => $id, 'randomNumber' => $data['randomNumber']];
  141. $world['randomNumber'] = $randomNumber;
  142. $this->update(
  143. "World",
  144. [
  145. 'randomNumber' => $randomNumber
  146. ],
  147. [
  148. "id" => $id
  149. ]
  150. );
  151. $arr[] = $world;
  152. }
  153. return \json_encode($arr, JSON_NUMERIC_CHECK);
  154. }
  155. }