CPgsqlColumnSchema.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * CPgsqlColumnSchema class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CPgsqlColumnSchema class describes the column meta data of a PostgreSQL table.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.db.schema.pgsql
  15. * @since 1.0
  16. */
  17. class CPgsqlColumnSchema extends CDbColumnSchema
  18. {
  19. /**
  20. * Extracts the PHP type from DB type.
  21. * @param string $dbType DB type
  22. */
  23. protected function extractType($dbType)
  24. {
  25. if(strpos($dbType,'[')!==false || strpos($dbType,'char')!==false || strpos($dbType,'text')!==false)
  26. $this->type='string';
  27. elseif(strpos($dbType,'bool')!==false)
  28. $this->type='boolean';
  29. elseif(preg_match('/(real|float|double)/',$dbType))
  30. $this->type='double';
  31. elseif(preg_match('/(integer|oid|serial|smallint)/',$dbType))
  32. $this->type='integer';
  33. else
  34. $this->type='string';
  35. }
  36. /**
  37. * Extracts the default value for the column.
  38. * The value is typecasted to correct PHP type.
  39. * @param mixed $defaultValue the default value obtained from metadata
  40. */
  41. protected function extractDefault($defaultValue)
  42. {
  43. if($defaultValue==='true')
  44. $this->defaultValue=true;
  45. elseif($defaultValue==='false')
  46. $this->defaultValue=false;
  47. elseif(strpos($defaultValue,'nextval')===0)
  48. $this->defaultValue=null;
  49. elseif(preg_match('/^\'(.*)\'::/',$defaultValue,$matches))
  50. $this->defaultValue=$this->typecast(str_replace("''","'",$matches[1]));
  51. elseif(preg_match('/^-?\d+(\.\d*)?$/',$defaultValue,$matches))
  52. $this->defaultValue=$this->typecast($defaultValue);
  53. // else is null
  54. }
  55. }