CPgsqlColumnSchema.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 size, precision and scale information from column's DB type.
  38. * @param string $dbType the column's DB type
  39. */
  40. protected function extractLimit($dbType)
  41. {
  42. if(strpos($dbType,'('))
  43. {
  44. if (preg_match('/^time.*\((.*)\)/',$dbType,$matches))
  45. {
  46. $this->precision=(int)$matches[1];
  47. }
  48. elseif (preg_match('/\((.*)\)/',$dbType,$matches))
  49. {
  50. $values=explode(',',$matches[1]);
  51. $this->size=$this->precision=(int)$values[0];
  52. if(isset($values[1]))
  53. $this->scale=(int)$values[1];
  54. }
  55. }
  56. }
  57. /**
  58. * Extracts the default value for the column.
  59. * The value is typecasted to correct PHP type.
  60. * @param mixed $defaultValue the default value obtained from metadata
  61. */
  62. protected function extractDefault($defaultValue)
  63. {
  64. if($defaultValue==='true')
  65. $this->defaultValue=true;
  66. elseif($defaultValue==='false')
  67. $this->defaultValue=false;
  68. elseif(strpos($defaultValue,'nextval')===0)
  69. $this->defaultValue=null;
  70. elseif(preg_match('/^\'(.*)\'::/',$defaultValue,$matches))
  71. $this->defaultValue=$this->typecast(str_replace("''","'",$matches[1]));
  72. elseif(preg_match('/^(-?\d+(\.\d*)?)(::.*)?$/',$defaultValue,$matches))
  73. $this->defaultValue=$this->typecast($matches[1]);
  74. // else is null
  75. }
  76. }