1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Horde_Db_Adapter_Postgresql_Column extends Horde_Db_Adapter_Base_Column
25: {
26: 27: 28:
29:
30: 31: 32: 33:
34: const MONEY_COLUMN_TYPE_OID = 790;
35:
36:
37: 38: 39:
40: public static $moneyPrecision = 19;
41:
42: 43: 44:
45: protected static $_hasEmptyStringDefault = array('binary', 'string', 'text');
46:
47:
48: 49: 50: 51: 52: 53: 54:
55: public function __construct($name, $default, $sqlType=null, $null=true)
56: {
57: parent::__construct($name, $this->_extractValueFromDefault($default), $sqlType, $null);
58: }
59:
60: 61: 62: 63:
64: protected function _simplifiedType($fieldType)
65: {
66: switch (true) {
67:
68: case preg_match('/^(?:real|double precision)$/', $fieldType):
69: return 'float';
70:
71: case preg_match('/^money$/', $fieldType):
72: return 'decimal';
73:
74: case preg_match('/^(?:character varying|bpchar)(?:\(\d+\))?$/', $fieldType):
75: return 'string';
76:
77: case preg_match('/^bytea$/', $fieldType):
78: return 'binary';
79:
80: case preg_match('/^timestamp with(?:out)? time zone$/', $fieldType):
81: return 'datetime';
82: case preg_match('/^interval$/', $fieldType):
83: return 'string';
84:
85: case preg_match('/^(?:point|line|lseg|box|"?path"?|polygon|circle)$/', $fieldType):
86: return 'string';
87:
88: case preg_match('/^(?:cidr|inet|macaddr)$/', $fieldType):
89: return 'string';
90:
91: case preg_match('/^bit(?: varying)?(?:\(\d+\))?$/', $fieldType):
92: return 'string';
93:
94: case preg_match('/^xml$/', $fieldType):
95: return 'string';
96:
97: case preg_match('/^\D+\[\]$/', $fieldType):
98: return 'string';
99:
100: case preg_match('/^oid$/', $fieldType):
101: return 'integer';
102: }
103:
104:
105: return parent::_simplifiedType($fieldType);
106: }
107:
108: 109: 110:
111: protected function ($default)
112: {
113: switch (true) {
114:
115: case preg_match('/\A-?\d+(\.\d*)?\z/', $default):
116: return $default;
117:
118: case preg_match('/\A\'(.*)\'::(?:character varying|bpchar|text)\z/m', $default, $matches):
119: return $matches[1];
120:
121: case preg_match('/\AE\'(.*)\'::(?:character varying|bpchar|text)\z/m', $default, $matches):
122:
123: return preg_replace('/\\(\d\d\d)/', '$1.oct.chr', $matches[1]);
124:
125: case preg_match('/\A\'(.*)\'::bytea\z/m', $default, $matches):
126: return $matches[1];
127:
128: case preg_match('/\A\'(.+)\'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/', $default, $matches):
129: return $matches[1];
130: case preg_match('/\A\'(.*)\'::interval\z/', $default, $matches):
131: return $matches[1];
132:
133: case $default == 'true':
134: return true;
135: case $default == 'false':
136: return false;
137:
138: case preg_match('/\A\'(.*)\'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/', $default, $matches):
139: return $matches[1];
140:
141: case preg_match('/\A\'(.*)\'::(?:cidr|inet|macaddr)\z/', $default, $matches):
142: return $matches[1];
143:
144: case preg_match('/\AB\'(.*)\'::"?bit(?: varying)?"?\z/', $default, $matches):
145: return $matches[1];
146:
147: case preg_match('/\A\'(.*)\'::xml\z/m', $default, $matches):
148: return $matches[1];
149:
150: case preg_match('/\A\'(.*)\'::"?\D+"?\[\]\z/', $default, $matches):
151: return $matches[1];
152:
153: case preg_match('/\A-?\d+\z/', $default, $matches):
154: return $matches[1];
155: default:
156:
157:
158: return null;
159: }
160: }
161:
162: 163: 164: 165: 166:
167: public function binaryToString($value)
168: {
169: if (is_resource($value)) {
170: $string = stream_get_contents($value);
171: fclose($value);
172: return $string;
173: }
174:
175: return preg_replace_callback("/(?:\\\'|\\\\\\\\|\\\\\d{3})/", array($this, 'binaryToStringCallback'), $value);
176: }
177:
178: 179: 180:
181: public function binaryToStringCallback($matches)
182: {
183: if ($matches[0] == '\\\'') {
184: return "'";
185: } elseif ($matches[0] == '\\\\\\\\') {
186: return '\\';
187: }
188:
189: return chr(octdec(substr($matches[0], -3)));
190: }
191:
192:
193: 194: 195:
196:
197: 198: 199: 200:
201: protected function ($sqlType)
202: {
203: if (preg_match('/^bigint/i', $sqlType)) {
204: return 8;
205: }
206: if (preg_match('/^smallint/i', $sqlType)) {
207: return 2;
208: }
209: return parent::_extractLimit($sqlType);
210: }
211:
212: 213: 214: 215:
216: protected function ($sqlType)
217: {
218: if (preg_match('/^money/', $sqlType)) {
219: return self::$moneyPrecision;
220: }
221: return parent::_extractPrecision($sqlType);
222: }
223:
224: 225: 226: 227:
228: protected function ($sqlType)
229: {
230: if (preg_match('/^money/', $sqlType)) {
231: return 2;
232: }
233: return parent::_extractScale($sqlType);
234: }
235:
236: }
237: