PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/plugins/db/pgsql/pgsql.dbtools.php

https://bitbucket.org/lipki/interlimo
PHP | 95 lines | 56 code | 13 blank | 26 comment | 12 complexity | 2ba96e37f2368f6c0291aaa4b113e2b9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage db_driver
  5. * @author Laurent Jouanneau
  6. * @contributor Laurent Jouanneau
  7. * @contributor Nicolas Jeudy (patch ticket #99)
  8. * @copyright 2005-2007 Laurent Jouanneau
  9. * @link http://www.jelix.org
  10. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  11. */
  12. /**
  13. *
  14. * @package jelix
  15. * @subpackage db_driver
  16. */
  17. class pgsqlDbTools extends jDbTools {
  18. /*
  19. * retourne la liste des tables
  20. * @return array $tab[] = $nomDeTable
  21. */
  22. protected function _getTableList (){
  23. $results = array ();
  24. $sql = "SELECT tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY tablename";
  25. $rs = $this->_connector->query ($sql);
  26. while ($line = $rs->fetch()){
  27. $results[] = $line->tablename;
  28. }
  29. return $results;
  30. }
  31. /**
  32. * récupère la liste des champs pour une base donnée.
  33. * @return array $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull)
  34. */
  35. protected function _getFieldList ($tableName){
  36. $results = array ();
  37. // get table informations
  38. $sql ='SELECT oid, relhaspkey, relhasindex FROM pg_class WHERE relname = \''.$tableName.'\'';
  39. $rs = $this->_connector->query ($sql);
  40. if (! ($table = $rs->fetch())) {
  41. throw new Exception('dbtools, pgsql: unknow table');
  42. }
  43. $pkeys = array();
  44. // get primary keys informations
  45. if ($table->relhaspkey == 't') {
  46. $sql = 'SELECT indkey FROM pg_index WHERE indrelid = '.$table->oid.' and indisprimary = true';
  47. $rs = $this->_connector->query ($sql);
  48. $pkeys = preg_split("/[\s]+/", $rs->fetch()->indkey);
  49. }
  50. // get field informations
  51. $sql_get_fields = "SELECT t.typname, a.attname, a.attnotnull, a.attnum, a.attlen, a.atttypmod,
  52. a.atthasdef, d.adsrc
  53. FROM pg_type t, pg_attribute a LEFT JOIN pg_attrdef d ON (d.adrelid=a.attrelid AND d.adnum=a.attnum)
  54. WHERE
  55. a.attnum > 0 AND a.attrelid = ".$table->oid." AND a.atttypid = t.oid
  56. ORDER BY a.attnum";
  57. $toReturn=array();
  58. $rs = $this->_connector->query ($sql_get_fields);
  59. while ($line = $rs->fetch ()){
  60. $field = new jDbFieldProperties();
  61. $field->name = $line->attname;
  62. $field->type = preg_replace('/(\D*)\d*/','\\1',$line->typname);
  63. $field->notNull = ($line->attnotnull=='t');
  64. $field->hasDefault = ($line->atthasdef == 't');
  65. $field->default = $line->adsrc;
  66. if(preg_match('/^nextval\(.*\)$/', $line->adsrc)){
  67. $field->autoIncrement=true;
  68. $field->default = '';
  69. }
  70. if(in_array($line->attnum, $pkeys))
  71. $field->primary = true;
  72. if($line->attlen == -1 && $line->atttypmod != -1)
  73. $field->length = $line->atttypmod - 4;
  74. $toReturn[$line->attname]=$field;
  75. }
  76. return $toReturn;
  77. }
  78. public function execSQLScript ($file) {
  79. $sqlQueries=file_get_contents($file);
  80. $this->_connector->query ($sqlQueries);
  81. }
  82. }