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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostgresqlTest.php 3.1KB

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