CMssqlColumnSchema.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * CMssqlColumnSchema class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @author Christophe Boulain <Christophe.Boulain@gmail.com>
  7. * @link http://www.yiiframework.com/
  8. * @copyright 2008-2013 Yii Software LLC
  9. * @license http://www.yiiframework.com/license/
  10. */
  11. /**
  12. * CMssqlColumnSchema class describes the column meta data of a MSSQL table.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @author Christophe Boulain <Christophe.Boulain@gmail.com>
  16. * @package system.db.schema.mssql
  17. */
  18. class CMssqlColumnSchema extends CDbColumnSchema
  19. {
  20. /**
  21. * Initializes the column with its DB type and default value.
  22. * This sets up the column's PHP type, size, precision, scale as well as default value.
  23. * @param string $dbType the column's DB type
  24. * @param mixed $defaultValue the default value
  25. */
  26. public function init($dbType, $defaultValue)
  27. {
  28. if ($defaultValue=='(NULL)')
  29. {
  30. $defaultValue=null;
  31. }
  32. parent::init($dbType, $defaultValue);
  33. }
  34. /**
  35. * Extracts the PHP type from DB type.
  36. * @param string $dbType DB type
  37. */
  38. protected function extractType($dbType)
  39. {
  40. if(strpos($dbType,'float')!==false || strpos($dbType,'real')!==false)
  41. $this->type='double';
  42. elseif(strpos($dbType,'bigint')===false && (strpos($dbType,'int')!==false || strpos($dbType,'smallint')!==false || strpos($dbType,'tinyint')))
  43. $this->type='integer';
  44. elseif(strpos($dbType,'bit')!==false)
  45. $this->type='boolean';
  46. else
  47. $this->type='string';
  48. }
  49. /**
  50. * Extracts the default value for the column.
  51. * The value is typecasted to correct PHP type.
  52. * @param mixed $defaultValue the default value obtained from metadata
  53. */
  54. protected function extractDefault($defaultValue)
  55. {
  56. if($this->dbType==='timestamp' )
  57. $this->defaultValue=null;
  58. else
  59. parent::extractDefault(str_replace(array('(',')',"'"), '', $defaultValue));
  60. }
  61. /**
  62. * Extracts size, precision and scale information from column's DB type.
  63. * We do nothing here, since sizes and precisions have been computed before.
  64. * @param string $dbType the column's DB type
  65. */
  66. protected function extractLimit($dbType)
  67. {
  68. }
  69. /**
  70. * Converts the input value to the type that this column is of.
  71. * @param mixed $value input value
  72. * @return mixed converted value
  73. */
  74. public function typecast($value)
  75. {
  76. if($this->type==='boolean')
  77. return $value ? 1 : 0;
  78. else
  79. return parent::typecast($value);
  80. }
  81. }