1: <?php
2:
3: /**
4: * A helper class used by the QCubed Code Generator to describe a table's column
5: *
6: * @package Codegen
7: * @property QSqlTable|QTypeTable $OwnerTable Table in which this column exists
8: * @property boolean $PrimaryKey Is the column a (part of) primary key
9: * @property string $Name Column name
10: * @property string $PropertyName Corresponding property name for the table
11: * @property string $VariableName Corresponding variable name (in ORM class and elsewhere)
12: * @property string $VariableType Type of data this column is supposed to store (constant from QType class)
13: * @property string $VariableTypeAsConstant Variable type expressed as QType casted string (integer column would have this value as: "QType::Integer")
14: * @property string $DbType Type in the database
15: * @property int $Length If applicable, the length of data to be stored (useful for varchar data types)
16: * @property mixed $Default Default value of the column
17: * @property boolean $NotNull Is this column a "NOT NULL" column?
18: * @property boolean $Identity Is this column an Identity column?
19: * @property boolean $Indexed Is there a single column index on this column?
20: * @property boolean $Unique Does this column have a 'Unique' key defined on it?
21: * @property boolean $Timestamp Can this column contain a timestamp value?
22: * @property QReference $Reference Reference to another column (if this one is a foreign key)
23: * @property array $Options Options for codegen
24: * @property string $Comment Comment on the column
25: */
26: class QSqlColumn extends QBaseClass {
27:
28: /////////////////////////////
29: // Protected Member Variables
30: /////////////////////////////
31:
32: /**
33: * @var QSqlTable The table in which this column exists.
34: */
35: protected $objOwnerTable;
36:
37: /**
38: * Specifies whether or not the column is a Primary Key
39: * @var bool PrimaryKey
40: */
41: protected $blnPrimaryKey;
42:
43: /**
44: * Name of the column as defined in the database
45: * So for example, "first_name"
46: * @var string Name
47: */
48: protected $strName;
49:
50: /**
51: * Name of the column as an object Property
52: * So for "first_name", it would be FirstName
53: * @var string PropertyName
54: */
55: protected $strPropertyName;
56:
57: /**
58: * Name of the column as an object protected Member Variable
59: * So for "first_name VARCHAR(50)", it would be strFirstName
60: * @var string VariableName
61: */
62: protected $strVariableName;
63:
64: /**
65: * The type of the protected member variable (uses one of the string constants from the QType class)
66: * @var string VariableType
67: */
68: protected $strVariableType;
69:
70: /**
71: * The type of the protected member variable (uses the actual constant from the Type class)
72: * @var string VariableType
73: */
74: protected $strVariableTypeAsConstant;
75:
76: /**
77: * The actual type of the column in the database (uses one of the string constants from the DatabaseType class)
78: * @var string DbType
79: */
80: protected $strDbType;
81:
82: /**
83: * Length of the column as defined in the database
84: * @var int Length
85: */
86: protected $intLength;
87:
88: /**
89: * The default value for the column as defined in the database
90: * @var mixed Default
91: */
92: protected $mixDefault;
93:
94: /**
95: * Specifies whether or not the column is specified as "NOT NULL"
96: * @var bool NotNull
97: */
98: protected $blnNotNull;
99:
100: /**
101: * Specifies whether or not the column is an identiy column (like auto_increment)
102: * @var bool Identity
103: */
104: protected $blnIdentity;
105:
106: /**
107: * Specifies whether or not the column is a single-column Index
108: * @var bool Indexed
109: */
110: protected $blnIndexed;
111:
112: /**
113: * Specifies whether or not the column is a unique
114: * @var bool Unique
115: */
116: protected $blnUnique;
117:
118: /**
119: * Specifies whether or not the column is a system-updated "timestamp" column
120: * @var bool Timestamp
121: */
122: protected $blnTimestamp;
123:
124: /**
125: * If the table column is foreign keyed off another column, then this
126: * Column instance would be a reference to another object
127: * @var QReference Reference
128: */
129: protected $objReference;
130:
131: /**
132: * The string value of the comment field in the database.
133: * @var string Comment
134: */
135: protected $strComment;
136:
137: /**
138: * Various overrides and options embedded in the comment for the column as a json object.
139: * @var array Overrides
140: */
141: protected $options = array();
142:
143: /**
144: * For Timestamp columns, will add to the sql code to set this field to NOW whenever there is a save
145: * @var boolean
146: */
147: protected $blnAutoUpdate;
148:
149:
150: ////////////////////
151: // Public Overriders
152: ////////////////////
153:
154: /**
155: * Override method to perform a property "Get"
156: * This will get the value of $strName
157: *
158: * @param string $strName Name of the property to get
159: * @throws Exception
160: * @throws QCallerException
161: * @return mixed
162: */
163: public function __get($strName) {
164: switch ($strName) {
165: case 'OwnerTable':
166: return $this->objOwnerTable;
167: case 'PrimaryKey':
168: return $this->blnPrimaryKey;
169: case 'Name':
170: return $this->strName;
171: case 'PropertyName':
172: return $this->strPropertyName;
173: case 'VariableName':
174: return $this->strVariableName;
175: case 'VariableType':
176: return $this->strVariableType;
177: case 'VariableTypeAsConstant':
178: return $this->strVariableTypeAsConstant;
179: case 'DbType':
180: return $this->strDbType;
181: case 'Length':
182: return $this->intLength;
183: case 'Default':
184: return $this->mixDefault;
185: case 'NotNull':
186: return $this->blnNotNull;
187: case 'Identity':
188: return $this->blnIdentity;
189: case 'Indexed':
190: return $this->blnIndexed;
191: case 'Unique':
192: return $this->blnUnique;
193: case 'Timestamp':
194: return $this->blnTimestamp;
195: case 'Reference':
196: return $this->objReference;
197: case 'Comment':
198: return $this->strComment;
199: case 'Options':
200: return $this->options;
201: case 'AutoUpdate':
202: return $this->blnAutoUpdate;
203: default:
204: try {
205: return parent::__get($strName);
206: } catch (QCallerException $objExc) {
207: $objExc->IncrementOffset();
208: throw $objExc;
209: }
210: }
211: }
212:
213: /**
214: * Override method to perform a property "Set"
215: * This will set the property $strName to be $mixValue
216: *
217: * @param string $strName Name of the property to set
218: * @param string $mixValue New value of the property
219: * @throws Exception
220: * @throws QCallerException
221: * @return mixed
222: */
223: public function __set($strName, $mixValue) {
224: try {
225: switch ($strName) {
226: case 'OwnerTable':
227: //return $this->objOwnerTable = QType::cast($mixValue, 'QSqlTable');
228: // $mixValue might be a QSqlTable or a QTypeTable
229: return $this->objOwnerTable = $mixValue;
230: case 'PrimaryKey':
231: return $this->blnPrimaryKey = QType::Cast($mixValue, QType::Boolean);
232: case 'Name':
233: return $this->strName = QType::Cast($mixValue, QType::String);
234: case 'PropertyName':
235: return $this->strPropertyName = QType::Cast($mixValue, QType::String);
236: case 'VariableName':
237: return $this->strVariableName = QType::Cast($mixValue, QType::String);
238: case 'VariableType':
239: return $this->strVariableType = QType::Cast($mixValue, QType::String);
240: case 'VariableTypeAsConstant':
241: return $this->strVariableTypeAsConstant = QType::Cast($mixValue, QType::String);
242: case 'DbType':
243: return $this->strDbType = QType::Cast($mixValue, QType::String);
244: case 'Length':
245: return $this->intLength = QType::Cast($mixValue, QType::Integer);
246: case 'Default':
247: if ($mixValue === null || (($mixValue == '' || $mixValue == '0000-00-00 00:00:00' || $mixValue == '0000-00-00') && !$this->blnNotNull))
248: return $this->mixDefault = null;
249: else if (is_int($mixValue))
250: return $this->mixDefault = QType::Cast($mixValue, QType::Integer);
251: else if (is_numeric($mixValue))
252: return $this->mixDefault = QType::Cast($mixValue, QType::Float);
253: else
254: return $this->mixDefault = QType::Cast($mixValue, QType::String);
255: case 'NotNull':
256: return $this->blnNotNull = QType::Cast($mixValue, QType::Boolean);
257: case 'Identity':
258: return $this->blnIdentity = QType::Cast($mixValue, QType::Boolean);
259: case 'Indexed':
260: return $this->blnIndexed = QType::Cast($mixValue, QType::Boolean);
261: case 'Unique':
262: return $this->blnUnique = QType::Cast($mixValue, QType::Boolean);
263: case 'Timestamp':
264: return $this->blnTimestamp = QType::Cast($mixValue, QType::Boolean);
265: case 'Reference':
266: return $this->objReference = QType::Cast($mixValue, 'QReference');
267: case 'Comment':
268: return $this->strComment = QType::Cast($mixValue, QType::String);
269: case 'Options':
270: return $this->options = QType::Cast($mixValue, QType::ArrayType);
271: case 'AutoUpdate':
272: return $this->blnAutoUpdate = QType::Cast($mixValue, QType::Boolean);
273: default:
274: return parent::__set($strName, $mixValue);
275: }
276: } catch (QCallerException $objExc) {
277: $objExc->IncrementOffset();
278: throw $objExc;
279: }
280: }
281: }
282: