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_Base_Column
25: {
26: protected $_name;
27: protected $_type;
28: protected $_null;
29: protected $_limit;
30: protected $_precision;
31: protected $_scale;
32: protected $_unsigned;
33: protected $_default;
34: protected $_sqlType;
35: protected $_isText;
36: protected $_isNumber;
37:
38:
39: 40: 41:
42:
43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function __construct($name, $default, $sqlType = null, $null = true)
57: {
58: $this->_name = $name;
59: $this->_sqlType = $sqlType;
60: $this->_null = $null;
61:
62: $this->_limit = $this->_extractLimit($sqlType);
63: $this->_precision = $this->_extractPrecision($sqlType);
64: $this->_scale = $this->_extractScale($sqlType);
65: $this->_unsigned = $this->_extractUnsigned($sqlType);
66:
67: $this->_type = $this->_simplifiedType($sqlType);
68: $this->_isText = $this->_type == 'text' || $this->_type == 'string';
69: $this->_isNumber = $this->_type == 'float' || $this->_type == 'integer' || $this->_type == 'decimal';
70:
71: $this->_default = $this->extractDefault($default);
72: }
73:
74: 75: 76:
77: public function isText()
78: {
79: return $this->_isText;
80: }
81:
82: 83: 84:
85: public function isNumber()
86: {
87: return $this->_isNumber;
88: }
89:
90: 91: 92:
93: public function typeCast($value)
94: {
95: if ($value === null) return null;
96:
97: switch ($this->_type) {
98: case 'string':
99: case 'text':
100: return $value;
101: case 'integer':
102: return strlen($value) ? (int)$value : null;
103: case 'float':
104: return strlen($value) ? (float)$value : null;
105: case 'decimal':
106: return $this->valueToDecimal($value);
107: case 'datetime':
108: case 'timestamp':
109: return $this->stringToTime($value);
110: case 'time':
111: return $this->stringToDummyTime($value);
112: case 'date':
113: return $this->stringToDate($value);
114: case 'binary':
115: return $this->binaryToString($value);
116: case 'boolean':
117: return $this->valueToBoolean($value);
118: default:
119: return $value;
120: }
121: }
122:
123: public function ($default)
124: {
125: return $this->typeCast($default);
126: }
127:
128:
129: 130: 131:
132:
133: 134: 135:
136: public function getName()
137: {
138: return $this->_name;
139: }
140:
141: 142: 143:
144: public function getDefault()
145: {
146: return $this->_default;
147: }
148:
149: 150: 151:
152: public function getType()
153: {
154: return $this->_type;
155: }
156:
157: 158: 159:
160: public function getLimit()
161: {
162: return $this->_limit;
163: }
164:
165: 166: 167:
168: public function precision()
169: {
170: return $this->_precision;
171: }
172:
173: 174: 175:
176: public function scale()
177: {
178: return $this->_scale;
179: }
180:
181: 182: 183:
184: public function isUnsigned()
185: {
186: return $this->_unsigned;
187: }
188:
189: 190: 191:
192: public function isNull()
193: {
194: return $this->_null;
195: }
196:
197: 198: 199:
200: public function getSqlType()
201: {
202: return $this->_sqlType;
203: }
204:
205:
206: 207: 208:
209:
210: 211: 212: 213: 214:
215: public function binaryToString($value)
216: {
217: return (string)$value;
218: }
219:
220: 221: 222: 223:
224: public function stringToDate($string)
225: {
226: if (empty($string) ||
227:
228: preg_replace('/[^\d]/', '', $string) == 0) {
229: return null;
230: }
231:
232: $d = new Horde_Date($string);
233: $d->setDefaultFormat('Y-m-d');
234:
235: return $d;
236: }
237:
238: 239: 240: 241:
242: public function stringToTime($string)
243: {
244: if (empty($string) ||
245:
246: preg_replace('/[^\d]/', '', $string) == 0) {
247: return null;
248: }
249:
250: return new Horde_Date($string);
251: }
252:
253: 254: 255: 256: 257: 258:
259: public function stringToDummyTime($value)
260: {
261: if (empty($string)) {
262: return null;
263: }
264: return $this->stringToTime('2000-01-01 ' . $string);
265: }
266:
267: 268: 269: 270:
271: public function valueToBoolean($value)
272: {
273: if ($value === true || $value === false) {
274: return $value;
275: }
276:
277: $value = strtolower($value);
278: return $value == 'true' || $value == 't' || $value == '1';
279: }
280:
281: 282: 283: 284:
285: public function valueToDecimal($value)
286: {
287: return (float)$value;
288: }
289:
290:
291: 292: 293:
294:
295: 296: 297: 298:
299: protected function ($sqlType)
300: {
301: if (preg_match("/\((.*)\)/", $sqlType, $matches)) {
302: return (int)$matches[1];
303: }
304: return null;
305: }
306:
307: 308: 309: 310:
311: protected function ($sqlType)
312: {
313: if (preg_match("/^(numeric|decimal|number)\((\d+)(,\d+)?\)/i", $sqlType, $matches)) {
314: return (int)$matches[2];
315: }
316: return null;
317: }
318:
319: 320: 321: 322:
323: protected function ($sqlType)
324: {
325: switch (true) {
326: case preg_match("/^(numeric|decimal|number)\((\d+)\)/i", $sqlType):
327: return 0;
328: case preg_match("/^(numeric|decimal|number)\((\d+)(,(\d+))\)/i",
329: $sqlType, $match):
330: return (int)$match[4];
331: }
332: }
333:
334: 335: 336: 337:
338: protected function ($sqlType)
339: {
340: return (boolean)preg_match('/^int.*unsigned/i', $sqlType);
341: }
342:
343: 344: 345: 346:
347: protected function _simplifiedType($fieldType)
348: {
349: switch (true) {
350: case preg_match('/int/i', $fieldType):
351: return 'integer';
352: case preg_match('/float|double/i', $fieldType):
353: return 'float';
354: case preg_match('/decimal|numeric|number/i', $fieldType):
355: return $this->_scale == 0 ? 'integer' : 'decimal';
356: case preg_match('/datetime/i', $fieldType):
357: return 'datetime';
358: case preg_match('/timestamp/i', $fieldType):
359: return 'timestamp';
360: case preg_match('/time/i', $fieldType):
361: return 'time';
362: case preg_match('/date/i', $fieldType):
363: return 'date';
364: case preg_match('/clob|text/i', $fieldType):
365: return 'text';
366: case preg_match('/blob|binary/i', $fieldType):
367: return 'binary';
368: case preg_match('/char|string/i', $fieldType):
369: return 'string';
370: case preg_match('/boolean/i', $fieldType):
371: return 'boolean';
372: }
373: }
374: }
375: