raw_controller.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. class RawController extends AppController
  3. {
  4. protected $pdo;
  5. protected function before_filter()
  6. {
  7. View::select(null, null);
  8. header('Content-type: application/json');
  9. $this->pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  10. PDO::ATTR_PERSISTENT => true
  11. ));
  12. }
  13. public function index()
  14. {
  15. $statement = $this->pdo->query( 'SELECT id,randomNumber FROM World WHERE id = '. mt_rand(1, 10000) );
  16. echo json_encode($statement->fetch(PDO::FETCH_ASSOC));
  17. }
  18. public function queries($count = 1)
  19. {
  20. $count = min(max($count, 1), 500);
  21. $res = $this->pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
  22. $worlds = [];
  23. for ($i = 0; $i < $count; ++$i) {
  24. $res->execute([mt_rand(1, 10000)]);
  25. $worlds[] = $res->fetch(PDO::FETCH_ASSOC);
  26. }
  27. echo json_encode($worlds);
  28. }
  29. public function update($count = 1)
  30. {
  31. $count = min(max($count, 1), 500);
  32. $worlds = [];
  33. $sth = $this->pdo->prepare('SELECT * FROM World WHERE id = ?');
  34. $updateSth = $this->pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
  35. for ($i = 0; $i < $count; ++$i) {
  36. $id = mt_rand(1, 10000);
  37. $randomNumber = mt_rand(1, 10000);
  38. $sth->execute(array($id));
  39. $row = ['id' => $id, 'randomNumber' => $updateSth->fetchColumn()];
  40. $row['randomNumber'] = $randomNumber;
  41. $updateSth->execute([$randomNumber, $id]);
  42. $worlds[] = $row;
  43. }
  44. echo json_encode($worlds);
  45. }
  46. }