PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/propel/runtime/lib/map/ColumnMap.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 522 lines | 221 code | 60 blank | 241 comment | 59 complexity | 140afaece85efc9692101604d079290f MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * ColumnMap is used to model a column of a table in a database.
  11. *
  12. * GENERAL NOTE
  13. * ------------
  14. * The propel.map classes are abstract building-block classes for modeling
  15. * the database at runtime. These classes are similar (a lite version) to the
  16. * propel.engine.database.model classes, which are build-time modeling classes.
  17. * These classes in themselves do not do any database metadata lookups.
  18. *
  19. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  20. * @author John D. McNally <jmcnally@collab.net> (Torque)
  21. * @version $Revision$
  22. * @package propel.runtime.map
  23. */
  24. class ColumnMap
  25. {
  26. // Propel type of the column
  27. protected $type;
  28. // Size of the column
  29. protected $size = 0;
  30. // Is it a primary key?
  31. protected $pk = false;
  32. // Is null value allowed?
  33. protected $notNull = false;
  34. // The default value for this column
  35. protected $defaultValue;
  36. // Name of the table that this column is related to
  37. protected $relatedTableName = "";
  38. // Name of the column that this column is related to
  39. protected $relatedColumnName = "";
  40. // The TableMap for this column
  41. protected $table;
  42. // The name of the column
  43. protected $columnName;
  44. // The php name of the column
  45. protected $phpName;
  46. // The validators for this column
  47. protected $validators = array();
  48. // The allowed values for an ENUM column
  49. protected $valueSet = array();
  50. // Is this a primaryString column?
  51. protected $isPkString = false;
  52. /**
  53. * Constructor.
  54. *
  55. * @param string $name The name of the column.
  56. * @param TableMap containingTable TableMap of the table this column is in.
  57. */
  58. public function __construct($name, TableMap $containingTable)
  59. {
  60. $this->columnName = $name;
  61. $this->table = $containingTable;
  62. }
  63. /**
  64. * Get the name of a column.
  65. *
  66. * @return string A String with the column name.
  67. */
  68. public function getName()
  69. {
  70. return $this->columnName;
  71. }
  72. /**
  73. * Get the table map this column belongs to.
  74. * @return TableMap
  75. */
  76. public function getTable()
  77. {
  78. return $this->table;
  79. }
  80. /**
  81. * Get the name of the table this column is in.
  82. *
  83. * @return string A String with the table name.
  84. */
  85. public function getTableName()
  86. {
  87. return $this->table->getName();
  88. }
  89. /**
  90. * Get the table name + column name.
  91. *
  92. * @return string A String with the full column name.
  93. */
  94. public function getFullyQualifiedName()
  95. {
  96. return $this->getTableName() . "." . $this->columnName;
  97. }
  98. /**
  99. * Set the php name of this column.
  100. *
  101. * @param string $phpName A string representing the PHP name.
  102. * @return void
  103. */
  104. public function setPhpName($phpName)
  105. {
  106. $this->phpName = $phpName;
  107. }
  108. /**
  109. * Get the name of a column.
  110. *
  111. * @return string A String with the column name.
  112. */
  113. public function getPhpName()
  114. {
  115. return $this->phpName;
  116. }
  117. /**
  118. * Set the Propel type of this column.
  119. *
  120. * @param string $type A string representing the Propel type (e.g. PropelColumnTypes::DATE).
  121. * @return void
  122. */
  123. public function setType($type)
  124. {
  125. $this->type = $type;
  126. }
  127. /**
  128. * Get the Propel type of this column.
  129. *
  130. * @return string A string representing the Propel type (e.g. PropelColumnTypes::DATE).
  131. */
  132. public function getType()
  133. {
  134. return $this->type;
  135. }
  136. /**
  137. * Get the PDO type of this column.
  138. *
  139. * @return int The PDO::PARMA_* value
  140. */
  141. public function getPdoType()
  142. {
  143. return PropelColumnTypes::getPdoType($this->type);
  144. }
  145. /**
  146. * Whether this is a BLOB, LONGVARBINARY, or VARBINARY.
  147. * @return boolean
  148. */
  149. public function isLob()
  150. {
  151. return ($this->type == PropelColumnTypes::BLOB || $this->type == PropelColumnTypes::VARBINARY || $this->type == PropelColumnTypes::LONGVARBINARY);
  152. }
  153. /**
  154. * Whether this is a DATE/TIME/TIMESTAMP column.
  155. *
  156. * @return boolean
  157. * @since 1.3
  158. */
  159. public function isTemporal()
  160. {
  161. return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME || $this->type == PropelColumnTypes::BU_DATE || $this->type == PropelColumnTypes::BU_TIMESTAMP);
  162. }
  163. /**
  164. * Whether this is a DATE/TIME/TIMESTAMP column that is post-epoch (1970).
  165. *
  166. * PHP cannot handle pre-epoch timestamps well -- hence the need to differentiate
  167. * between epoch and pre-epoch timestamps.
  168. *
  169. * @return boolean
  170. * @deprecated Propel supports non-epoch dates
  171. */
  172. public function isEpochTemporal()
  173. {
  174. return ($this->type == PropelColumnTypes::TIMESTAMP || $this->type == PropelColumnTypes::DATE || $this->type == PropelColumnTypes::TIME);
  175. }
  176. /**
  177. * Whether this column is numeric (int, decimal, bigint etc).
  178. * @return boolean
  179. */
  180. public function isNumeric()
  181. {
  182. return ($this->type == PropelColumnTypes::NUMERIC || $this->type == PropelColumnTypes::DECIMAL || $this->type == PropelColumnTypes::TINYINT || $this->type == PropelColumnTypes::SMALLINT || $this->type == PropelColumnTypes::INTEGER || $this->type == PropelColumnTypes::BIGINT || $this->type == PropelColumnTypes::REAL || $this->type == PropelColumnTypes::FLOAT || $this->type == PropelColumnTypes::DOUBLE);
  183. }
  184. /**
  185. * Whether this column is a text column (varchar, char, longvarchar).
  186. * @return boolean
  187. */
  188. public function isText()
  189. {
  190. return ($this->type == PropelColumnTypes::VARCHAR || $this->type == PropelColumnTypes::LONGVARCHAR || $this->type == PropelColumnTypes::CHAR);
  191. }
  192. /**
  193. * Set the size of this column.
  194. *
  195. * @param int $size An int specifying the size.
  196. * @return void
  197. */
  198. public function setSize($size)
  199. {
  200. $this->size = $size;
  201. }
  202. /**
  203. * Get the size of this column.
  204. *
  205. * @return int An int specifying the size.
  206. */
  207. public function getSize()
  208. {
  209. return $this->size;
  210. }
  211. /**
  212. * Set if this column is a primary key or not.
  213. *
  214. * @param boolean $pk True if column is a primary key.
  215. * @return void
  216. */
  217. public function setPrimaryKey($pk)
  218. {
  219. $this->pk = $pk;
  220. }
  221. /**
  222. * Is this column a primary key?
  223. *
  224. * @return boolean True if column is a primary key.
  225. */
  226. public function isPrimaryKey()
  227. {
  228. return $this->pk;
  229. }
  230. /**
  231. * Set if this column may be null.
  232. *
  233. * @param boolean nn True if column may be null.
  234. * @return void
  235. */
  236. public function setNotNull($nn)
  237. {
  238. $this->notNull = $nn;
  239. }
  240. /**
  241. * Is null value allowed ?
  242. *
  243. * @return boolean True if column may not be null.
  244. */
  245. public function isNotNull()
  246. {
  247. return ($this->notNull || $this->isPrimaryKey());
  248. }
  249. /**
  250. * Sets the default value for this column.
  251. * @param mixed $defaultValue the default value for the column
  252. * @return void
  253. */
  254. public function setDefaultValue($defaultValue)
  255. {
  256. $this->defaultValue = $defaultValue;
  257. }
  258. /**
  259. * Gets the default value for this column.
  260. * @return mixed String or NULL
  261. */
  262. public function getDefaultValue()
  263. {
  264. return $this->defaultValue;
  265. }
  266. /**
  267. * Set the foreign key for this column.
  268. *
  269. * @param string tableName The name of the table that is foreign.
  270. * @param string columnName The name of the column that is foreign.
  271. * @return void
  272. */
  273. public function setForeignKey($tableName, $columnName)
  274. {
  275. if ($tableName && $columnName) {
  276. $this->relatedTableName = $tableName;
  277. $this->relatedColumnName = $columnName;
  278. } else {
  279. $this->relatedTableName = "";
  280. $this->relatedColumnName = "";
  281. }
  282. }
  283. /**
  284. * Is this column a foreign key?
  285. *
  286. * @return boolean True if column is a foreign key.
  287. */
  288. public function isForeignKey()
  289. {
  290. if ($this->relatedTableName) {
  291. return true;
  292. } else {
  293. return false;
  294. }
  295. }
  296. /**
  297. * Get the RelationMap object for this foreign key
  298. *
  299. * @return RelationMap|null
  300. */
  301. public function getRelation()
  302. {
  303. if(!$this->relatedTableName) return null;
  304. foreach ($this->getTable()->getRelations() as $name => $relation) {
  305. if ($relation->getType() == RelationMap::MANY_TO_ONE) {
  306. if ($relation->getForeignTable()->getName() == $this->getRelatedTableName()
  307. && array_key_exists($this->getFullyQualifiedName(), $relation->getColumnMappings()))
  308. {
  309. return $relation;
  310. }
  311. }
  312. }
  313. }
  314. /**
  315. * Get the table.column that this column is related to.
  316. *
  317. * @return string A String with the full name for the related column.
  318. */
  319. public function getRelatedName()
  320. {
  321. return $this->relatedTableName . "." . $this->relatedColumnName;
  322. }
  323. /**
  324. * Get the table name that this column is related to.
  325. *
  326. * @return string A String with the name for the related table.
  327. */
  328. public function getRelatedTableName()
  329. {
  330. return $this->relatedTableName;
  331. }
  332. /**
  333. * Get the column name that this column is related to.
  334. *
  335. * @return string A String with the name for the related column.
  336. */
  337. public function getRelatedColumnName()
  338. {
  339. return $this->relatedColumnName;
  340. }
  341. /**
  342. * Get the TableMap object that this column is related to.
  343. *
  344. * @return TableMap The related TableMap object
  345. * @throws PropelException when called on a column with no foreign key
  346. */
  347. public function getRelatedTable()
  348. {
  349. if ($this->relatedTableName) {
  350. return $this->table->getDatabaseMap()->getTable($this->relatedTableName);
  351. } else {
  352. throw new PropelException("Cannot fetch RelatedTable for column with no foreign key: " . $this->columnName);
  353. }
  354. }
  355. /**
  356. * Get the TableMap object that this column is related to.
  357. *
  358. * @return ColumnMap The related ColumnMap object
  359. * @throws PropelException when called on a column with no foreign key
  360. */
  361. public function getRelatedColumn()
  362. {
  363. return $this->getRelatedTable()->getColumn($this->relatedColumnName);
  364. }
  365. public function addValidator($validator)
  366. {
  367. $this->validators[] = $validator;
  368. }
  369. public function hasValidators()
  370. {
  371. return count($this->validators) > 0;
  372. }
  373. public function getValidators()
  374. {
  375. return $this->validators;
  376. }
  377. /**
  378. * Set the valueSet of this column (only valid for ENUM columns).
  379. *
  380. * @param array $values A list of allowed values
  381. */
  382. public function setValueSet($values)
  383. {
  384. $this->valueSet = $values;
  385. }
  386. /**
  387. * Get the valueSet of this column (only valid for ENUM columns).
  388. *
  389. * @return array A list of allowed values
  390. */
  391. public function getValueSet()
  392. {
  393. return $this->valueSet;
  394. }
  395. public function isInValueSet($value)
  396. {
  397. return in_array($value, $this->valueSet);
  398. }
  399. public function getValueSetKey($value)
  400. {
  401. return array_search($value, $this->valueSet);
  402. }
  403. /**
  404. * Performs DB-specific ignore case, but only if the column type necessitates it.
  405. * @param string $str The expression we want to apply the ignore case formatting to (e.g. the column name).
  406. * @param DBAdapter $db
  407. * @return string
  408. */
  409. public function ignoreCase($str, DBAdapter $db)
  410. {
  411. if ($this->isText()) {
  412. return $db->ignoreCase($str);
  413. } else {
  414. return $str;
  415. }
  416. }
  417. /**
  418. * Normalizes the column name, removing table prefix and uppercasing.
  419. *
  420. * article.first_name becomes FIRST_NAME
  421. *
  422. * @param string $name
  423. * @return string Normalized column name.
  424. */
  425. public static function normalizeName($name)
  426. {
  427. if (false !== ($pos = strrpos($name, '.'))) {
  428. $name = substr($name, $pos + 1);
  429. }
  430. $name = strtoupper($name);
  431. return $name;
  432. }
  433. /**
  434. * Set this column to be a primaryString column.
  435. *
  436. * @param boolean $pkString
  437. */
  438. public function setPrimaryString($pkString)
  439. {
  440. $this->isPkString = (bool) $pkString;
  441. }
  442. /**
  443. * Is this column a primaryString column?
  444. *
  445. * @return boolean True, if this column is the primaryString column.
  446. */
  447. public function isPrimaryString()
  448. {
  449. return $this->isPkString;
  450. }
  451. // deprecated methods
  452. /**
  453. * Gets column name
  454. * @deprecated Use getName() instead
  455. * @return string
  456. * @deprecated Use getName() instead.
  457. */
  458. public function getColumnName()
  459. {
  460. return $this->getName();
  461. }
  462. }