api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PostgresqlTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Mevdschee\PhpCrudApi\Tests;
  3. class PostgresqlTest extends Tests
  4. {
  5. /**
  6. * Gets the name of the database engine
  7. *
  8. * @return string Name of the database engine
  9. */
  10. public function getEngineName()
  11. {
  12. return 'PostgreSQL';
  13. }
  14. /**
  15. * Connects to the Database
  16. *
  17. * @return object Database connection
  18. */
  19. public function connect($config)
  20. {
  21. $e = function ($v) {
  22. return str_replace(array('\'','\\'), array('\\\'','\\\\'), $v);
  23. };
  24. $hostname = $e($config['hostname']);
  25. $database = $e($config['database']);
  26. $username = $e($config['username']);
  27. $password = $e($config['password']);
  28. $connectionString = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
  29. return pg_connect($connectionString);
  30. }
  31. /**
  32. * Disconnects from the Database
  33. *
  34. * @return boolean Success
  35. */
  36. public function disconnect($db)
  37. {
  38. return pg_close($db);
  39. }
  40. /**
  41. * Checks the version of the Database
  42. *
  43. * @return void
  44. */
  45. public function checkVersion($db)
  46. {
  47. $major = 9;
  48. $minor = 1;
  49. $version = pg_version();
  50. $v = explode('.', $version['server']);
  51. if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
  52. die("Detected PostgreSQL $v[0].$v[1], but only $major.$minor and up are supported\n");
  53. }
  54. }
  55. /**
  56. * Gets the capabilities of the Database
  57. *
  58. * @return int Capabilites
  59. */
  60. public function getCapabilities($db)
  61. {
  62. $capabilities = 0;
  63. $major = 9;
  64. $minor = 4;
  65. $version = pg_version();
  66. $v = explode('.', $version['server']);
  67. if ($v[0]>$major || ($v[0]==$major && $v[1]>=$minor)) {
  68. $capabilities |= self::JSON;
  69. }
  70. $extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
  71. foreach ($extensions as $extension) {
  72. if ($extension['extname'] === 'postgis') {
  73. $capabilities |= self::GIS;
  74. }
  75. }
  76. return $capabilities;
  77. }
  78. /**
  79. * Seeds the database for this connection
  80. *
  81. * @return void
  82. */
  83. public function seedDatabase($db, $capabilities)
  84. {
  85. $fixture = __DIR__.'/data/blog_postgresql.sql';
  86. $contents = file_get_contents($fixture);
  87. if (!($capabilities & self::GIS)) {
  88. $contents = preg_replace('/(geometry)( NOT)? NULL/i', 'text\2 NULL', $contents);
  89. $contents = preg_replace('/ST_GeomFromText/i', 'concat', $contents);
  90. $contents = preg_replace('/CREATE EXTENSION IF NOT EXISTS postgis;/i', '', $contents);
  91. }
  92. if (!($capabilities & self::JSON)) {
  93. $contents = preg_replace('/JSONB? NOT NULL/i', 'text NOT NULL', $contents);
  94. }
  95. $queries = preg_split('/;\s*\n/', $contents);
  96. array_pop($queries);
  97. foreach ($queries as $i=>$query) {
  98. if (!pg_query($db, $query.';')) {
  99. $i++;
  100. die("Loading '$fixture' failed on statemement #$i with error:\n".print_r(pg_last_error($db), true)."\n");
  101. }
  102. }
  103. }
  104. }