Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / DB / Reflection / DBColumn.php
blob343e3a134fba30a2ade111acf45e9b6d876e6977
1 <?php
2 /** @package verysimple::DB::Reflection */
3 require_once('verysimple/Phreeze/FieldMap.php');
5 /**
6 * DBcolumn is an object representation of column
8 * @package verysimple::DB::Reflection
9 * @author Jason Hinkle
10 * @copyright 1997-2007 VerySimple, Inc.
11 * @license http://www.gnu.org/licenses/lgpl.html LGPL
12 * @version 1.0
14 class DBColumn
16 public $Table;
17 public $Name;
18 public $Type;
19 public $Unsigned;
20 public $Size;
21 public $Key;
22 public $Null;
23 public $Default;
24 public $Extra;
25 public $Comment;
26 public $NameWithoutPrefix; // populated by DBTable if there is a prefix
27 public $MaxSize;
28 public $Keys = array ();
29 public $Constraints = array ();
31 /**
32 * Instantiate new DBColumn
34 * @access public
35 * @param DBTable $table
36 * @param Array $row
37 * result from "describe table" statement
39 function __construct($table, $row)
41 // typical type is something like varchar(40)
42 $typesize = explode("(", $row ["Type"]);
44 $tmp = isset($typesize [1]) ? str_replace(")", "", $typesize [1]) : "";
45 $sizesign = explode(" ", $tmp);
47 $this->Table = & $table;
48 $this->Name = $row ["Field"];
49 $this->NameWithoutPrefix = $row ["Field"];
50 $this->Type = $typesize [0];
51 $this->Unsigned = isset($sizesign [1]);
52 $this->Null = $row ["Null"];
53 $this->Key = $row ["Key"];
54 $this->Default = $row ["Default"];
55 $this->Extra = $row ["Extra"];
57 // enums are a little different because they contain a list of legal values instead of a size limit
58 if ($this->IsEnum()) {
59 // enum size is in the format 'val1','val2',...
60 $this->Size = explode("','", substr($sizesign [0], 1, - 1));
61 $this->MaxSize = 0;
62 } else {
63 $this->Size = $sizesign [0];
64 // size may be saved for decimals as "n,n" so we need to convert that to an int
65 $tmp = explode(",", $this->Size);
66 $this->MaxSize = count($tmp) > 1 ? ($tmp [0] + $tmp [1]) : $this->Size;
69 // if ($this->Key == "MUL") print " ########################## " . print_r($row,1) . " ########################## ";
72 /**
73 * Return true if this column is an enum type
75 * @return boolean
77 function IsEnum()
79 return $this->Type == 'enum';
82 /**
83 * Return the enum values if this column type is an enum
85 * @return array
87 function GetEnumValues()
89 return $this->IsEnum() ? $this->Size : array ();
92 /**
93 * Return the Phreeze column constant that most closely matches this column type
95 * @return string
97 function GetPhreezeType()
99 return FieldMap::GetConstantFromType($this->Type);
103 * Return the PHP variable type that most closely matches this column type
105 * @return string
107 function GetPhpType()
109 $rt = $this->Type;
110 switch ($this->Type) {
111 case "smallint":
112 case "bigint":
113 case "tinyint":
114 case "mediumint":
115 $rt = "int";
116 break;
117 case "varchar":
118 case "text":
119 case "tinytext":
120 $rt = "string";
121 break;
122 case "date":
123 case "datetime":
124 $rt = "date";
125 break;
126 case "decimal":
127 case "float":
128 $rt = "float";
129 break;
130 default:
131 break;
134 return $rt;
138 * Get the SQLite type that most closely matches this column type
140 * @return string
142 function GetSqliteType()
144 $rt = $this->Type;
145 switch ($this->Type) {
146 case "int":
147 $rt = "integer";
148 break;
149 case "smallint":
150 $rt = "integer";
151 break;
152 case "tinyint":
153 $rt = "integer";
154 break;
155 case "varchar":
156 $rt = "text";
157 break;
158 case "text":
159 $rt = "text";
160 break;
161 case "tinytext":
162 $rt = "text";
163 break;
164 case "date":
165 $rt = "datetime";
166 break;
167 case "datetime":
168 $rt = "datetime";
169 break;
170 case "mediumint":
171 $rt = "integer";
172 break;
173 case "bigint":
174 $rt = "integer";
175 break;
176 case "decimal":
177 $rt = "real";
178 break;
179 case "float":
180 $rt = "real";
181 break;
182 default:
183 break;
186 return $rt;
190 * Return the AS3/Flex type that most closely matches this column type
192 * @return string
194 function GetFlexType()
196 $rt = $this->Type;
197 switch ($this->Type) {
198 case "int":
199 $rt = "int";
200 break;
201 case "smallint":
202 $rt = "int";
203 break;
204 case "tinyint":
205 $rt = $this->MaxSize > 1 ? "int" : "Boolean";
206 break;
207 case "varchar":
208 $rt = "String";
209 break;
210 case "text":
211 $rt = "String";
212 break;
213 case "tinytext":
214 $rt = "String";
215 break;
216 case "datetime":
217 $rt = "Date";
218 break;
219 case "mediumint":
220 $rt = "int";
221 break;
222 case "bigint":
223 $rt = "int";
224 break;
225 case "decimal":
226 $rt = "Number";
227 break;
228 case "float":
229 $rt = "Number";
230 break;
231 default:
232 break;
235 return $rt;