Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DbTypes.php
Go to the documentation of this file.
1<?php
2
4
14class DbTypes {
15 const TYPES=["tinyint"=>0,"int"=>0,"decimal"=>0,"float"=>0,"double"=>0,"smallint"=>0,"mediumint"=>0,"bigint"=>0,
16 "date"=>"NULL","time"=>"NULL","datetime"=>"CURRENT_TIMESTAMP","timestamp"=>"CURRENT_TIMESTAMP","year"=>"'0000'",
17 "tinytext"=>"NULL","text"=>"NULL","mediumtext"=>"NULL","longtext"=>"NULL",
18 "tinyblob"=>"NULL","blob"=>"NULL","mediumblob"=>"NULL","longblob"=>"NULL",
19 "char"=>"NULL","varchar"=>"NULL","binary"=>"NULL","varbinary"=>"NULL",
20 "enum"=>"''","set"=>"''"
21 ];
22 const DEFAULT_TYPE="varchar(30)";
23
24 protected static $typeMatch='@([\s\S]*?)((?:\‍((?:\d)+\‍))*?)$@';
25 protected static $sizeMatch='@(?:[\s\S]*?)(?:\‍((\d+)\‍))*?$@';
26 protected static $intMatch='@^.*?int.*?((?:\‍((?:\d)+\‍))*?)$@';
27 protected static $stringMatch='@^.*?char|text|binary.*?((?:\‍((?:\d)+\‍))*?)$@';
28 protected static $floatMatch='@^.*?float|decimal|numeric|double.*?((?:\‍((?:\d)+\‍))*?)$@';
29
30
31 protected static function get_($value,$regex,$pos=1){
32 $matches=[];
33 if (\preg_match($regex, $value,$matches)) {
34 if(isset($matches[$pos])){
35 return $matches[$pos];
36 }
37 }
38 return null;
39 }
40
41 public static function isInt($fieldType){
42 return \preg_match(self::$intMatch, $fieldType);
43 }
44
45 public static function isString($fieldType){
46 return \preg_match(self::$stringMatch, $fieldType);
47 }
48
49 public static function isFloat($fieldType){
50 return \preg_match(self::$floatMatch, $fieldType);
51 }
52
53 public static function isBoolean($fieldType){
54 return $fieldType==='tinyint(1)' || $fieldType==='boolean' || $fieldType==='bool';
55 }
56
57 public static function getSize($dbType){
58 return self::get_($dbType, self::$sizeMatch);
59 }
60
61 public static function getType($dbType){
62 return self::get_($dbType, self::$typeMatch);
63 }
64
65 public static function asPhpType($dbType){
66 if(self::isBoolean($dbType)){
67 return 'bool';
68 }
69 if(self::isInt($dbType)){
70 return 'int';
71 }
72 if(self::isFloat($dbType)){
73 return 'float';
74 }
75 if(self::isString($dbType)){
76 return 'string';
77 }
78 return '';
79 }
80
81}
82
Manage Databases types.
Definition DbTypes.php:14
static getSize($dbType)
Definition DbTypes.php:57
static isString($fieldType)
Definition DbTypes.php:45
static asPhpType($dbType)
Definition DbTypes.php:65
static get_($value, $regex, $pos=1)
Definition DbTypes.php:31
static isInt($fieldType)
Definition DbTypes.php:41
static isBoolean($fieldType)
Definition DbTypes.php:53
static isFloat($fieldType)
Definition DbTypes.php:49
static getType($dbType)
Definition DbTypes.php:61