Spaces:
Sleeping
Sleeping
File size: 12,589 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | <?php
/*
* $Id: TableMap.php 536 2007-01-10 14:30:38Z heltem $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://propel.phpdb.org>.
*/
include_once 'propel/map/ColumnMap.php';
include_once 'propel/map/ValidatorMap.php';
/**
* TableMap is used to model a table in a database.
*
* GENERAL NOTE
* ------------
* The propel.map classes are abstract building-block classes for modeling
* the database at runtime. These classes are similar (a lite version) to the
* propel.engine.database.model classes, which are build-time modeling classes.
* These classes in themselves do not do any database metadata lookups, but instead
* are used by the MapBuilder classes that were generated for your datamodel. The
* MapBuilder that was created for your datamodel build a representation of your
* database by creating instances of the DatabaseMap, TableMap, ColumnMap, etc.
* classes. See propel/templates/om/php5/MapBuilder.tpl and the classes generated
* by that template for your datamodel to further understand how these are put
* together.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author John D. McNally <jmcnally@collab.net> (Torque)
* @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
* @version $Revision: 536 $
* @package propel.map
*/
class TableMap {
/** The columns in the table. */
private $columns;
/** The database this table belongs to. */
private $dbMap;
/** The name of the table. */
private $tableName;
/** The PHP name of the table. */
private $phpName;
/** The prefix on the table name. */
private $prefix;
/** Whether to use an id generator for pkey. */
private $useIdGenerator;
/**
* Object to store information that is needed if the
* for generating primary keys.
*/
private $pkInfo;
/**
* Construct a new TableMap.
*
* @param string $tableName The name of the table.
* @param DatabaseMap $containingDB A DatabaseMap that this table belongs to.
*/
public function __construct($tableName, DatabaseMap $containingDB)
{
$this->tableName = $tableName;
$this->dbMap = $containingDB;
$this->columns = array();
}
/**
* Normalizes the column name, removing table prefix and uppercasing.
* @param string $name
* @return string Normalized column name.
*/
private function normalizeColName($name) {
if (false !== ($pos = strpos($name, '.'))) {
$name = substr($name, $pos + 1);
}
$name = strtoupper($name);
return $name;
}
/**
* Does this table contain the specified column?
*
* @param string $name name of the column
* @return boolean True if the table contains the column.
*/
public function containsColumn($name)
{
if (!is_string($name)) {
$name = $name->getColumnName();
}
return isset($this->columns[$this->normalizeColName($name)]);
}
/**
* Get the DatabaseMap containing this TableMap.
*
* @return DatabaseMap A DatabaseMap.
*/
public function getDatabaseMap()
{
return $this->dbMap;
}
/**
* Get the name of the Table.
*
* @return string A String with the name of the table.
*/
public function getName()
{
return $this->tableName;
}
/**
* Get the PHP name of the Table.
*
* @return string A String with the name of the table.
*/
public function getPhpName()
{
return $this->phpName;
}
/**
* Set the PHP name of the Table.
*
* @param string $phpName The PHP Name for this table
*/
public function setPhpName($phpName)
{
$this->phpName = $phpName;
}
/**
* Get table prefix name.
*
* @return string A String with the prefix.
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Set table prefix name.
*
* @param string $prefix The prefix for the table name (ie: SCARAB for
* SCARAB_PROJECT).
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Whether to use Id generator for primary key.
* @return boolean
*/
public function isUseIdGenerator() {
return $this->useIdGenerator;
}
/**
* Get the information used to generate a primary key
*
* @return An Object.
*/
public function getPrimaryKeyMethodInfo()
{
return $this->pkInfo;
}
/**
* Get a ColumnMap[] of the columns in this table.
*
* @return array A ColumnMap[].
*/
public function getColumns()
{
return $this->columns;
}
/**
* Get a ColumnMap for the named table.
*
* @param string $name A String with the name of the table.
* @return ColumnMap A ColumnMap.
* @throws PropelException if the column is undefined
*/
public function getColumn($name)
{
$name = $this->normalizeColName($name);
if (!isset($this->columns[$name])) {
throw new PropelException("Cannot fetch ColumnMap for undefined column: " . $name);
}
return $this->columns[$name];
}
/**
* Add a primary key column to this Table.
*
* @param string $columnName A String with the column name.
* @param string $type A string specifying the PHP native type.
* @param int $creoleType The integer representing the Creole type.
* @param boolean $isNotNull Whether column does not allow NULL values.
* @param $size An int specifying the size.
* @return ColumnMap Newly added PrimaryKey column.
*/
public function addPrimaryKey($columnName, $phpName, $type, $creoleType, $isNotNull = false, $size = null)
{
return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, true, null, null);
}
/**
* Add a foreign key column to the table.
*
* @param string $columnName A String with the column name.
* @param string $type A string specifying the PHP native type.
* @param int $creoleType The integer representing the Creole type.
* @param string $fkTable A String with the foreign key table name.
* @param string $fkColumn A String with the foreign key column name.
* @param boolean $isNotNull Whether column does not allow NULL values.
* @param int $size An int specifying the size.
* @param string $defaultValue The default value for this column.
* @return ColumnMap Newly added ForeignKey column.
*/
public function addForeignKey($columnName, $phpName, $type, $creoleType, $fkTable, $fkColumn, $isNotNull = false, $size = 0)
{
return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, false, $fkTable, $fkColumn);
}
/**
* Add a foreign primary key column to the table.
*
* @param string $columnName A String with the column name.
* @param string $type A string specifying the PHP native type.
* @param int $creoleType The integer representing the Creole type.
* @param string $fkTable A String with the foreign key table name.
* @param string $fkColumn A String with the foreign key column name.
* @param boolean $isNotNull Whether column does not allow NULL values.
* @param int $size An int specifying the size.
* @param string $defaultValue The default value for this column.
* @return ColumnMap Newly created foreign pkey column.
*/
public function addForeignPrimaryKey($columnName, $phpName, $type, $creoleType, $fkTable, $fkColumn, $isNotNull = false, $size = 0)
{
return $this->addColumn($columnName, $phpName, $type, $creoleType, $isNotNull, $size, true, $fkTable, $fkColumn);
}
/**
* Add a pre-created column to this table. It will replace any
* existing column.
*
* @param ColumnMap $cmap A ColumnMap.
* @return ColumnMap The added column map.
*/
public function addConfiguredColumn($cmap)
{
$this->columns[ $cmap->getColumnName() ] = $cmap;
return $cmap;
}
/**
* Add a column to the table.
*
* @param string name A String with the column name.
* @param string $type A string specifying the PHP native type.
* @param int $creoleType The integer representing the Creole type.
* @param boolean $isNotNull Whether column does not allow NULL values.
* @param int $size An int specifying the size.
* @param boolean $pk True if column is a primary key.
* @param string $fkTable A String with the foreign key table name.
* @param $fkColumn A String with the foreign key column name.
* @param string $defaultValue The default value for this column.
* @return ColumnMap The newly created column.
*/
public function addColumn($name, $phpName, $type, $creoleType, $isNotNull = false, $size = null, $pk = null, $fkTable = null, $fkColumn = null)
{
$col = new ColumnMap($name, $this);
if ($fkTable && $fkColumn) {
if (substr_count($fkColumn, '.') > 0 && substr($fkColumn, strlen($fkTable)) !== false) {
$fkColumn = substr($fkColumn, strlen($fkTable) + 1);
}
$col->setForeignKey($fkTable, $fkColumn);
}
$col->setType($type);
$col->setCreoleType($creoleType);
$col->setPrimaryKey($pk);
$col->setSize($size);
$col->setPhpName($phpName);
$col->setNotNull($isNotNull);
$this->columns[$name] = $col;
return $this->columns[$name];
}
/**
* Add a validator to a table's column
*
* @param string $columnName The name of the validator's column
* @param string $name The rule name of this validator
* @param string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator)
* @param string $value
* @param string $message The error message which is returned on invalid values
* @return void
*/
public function addValidator($columnName, $name, $classname, $value, $message)
{
if (false !== ($pos = strpos($columnName, '.'))) {
$columnName = substr($columnName, $pos + 1);
}
$col = $this->getColumn($columnName);
if ($col !== null) {
$validator = new ValidatorMap($col);
$validator->setName($name);
$validator->setClass($classname);
$validator->setValue($value);
$validator->setMessage($message);
$col->addValidator($validator);
}
}
/**
* Set whether or not to use Id generator for primary key.
* @param boolean $bit
*/
public function setUseIdGenerator($bit) {
$this->useIdGenerator = $bit;
}
/**
* Sets the pk information needed to generate a key
*
* @param $pkInfo information needed to generate a key
*/
public function setPrimaryKeyMethodInfo($pkInfo)
{
$this->pkInfo = $pkInfo;
}
//---Utility methods for doing intelligent lookup of table names
/**
* Tell me if i have PREFIX in my string.
*
* @param data A String.
* @return boolean True if prefix is contained in data.
*/
private function hasPrefix($data)
{
return (substr($data, $this->getPrefix()) !== false);
}
/**
* Removes the PREFIX.
*
* @param string $data A String.
* @return string A String with data, but with prefix removed.
*/
private function removePrefix($data)
{
return substr($data, strlen($this->getPrefix()));
}
/**
* Removes the PREFIX, removes the underscores and makes
* first letter caps.
*
* SCARAB_FOO_BAR becomes FooBar.
*
* @param data A String.
* @return string A String with data processed.
*/
public final function removeUnderScores($data)
{
$tmp = null;
$out = "";
if ($this->hasPrefix($data)) {
$tmp = $this->removePrefix($data);
} else {
$tmp = $data;
}
$tok = strtok($tmp, "_");
while ($tok) {
$out .= ucfirst($tok);
$tok = strtok("_");
}
return $out;
}
/**
* Makes the first letter caps and the rest lowercase.
*
* @param string $data A String.
* @return string A String with data processed.
*/
private function firstLetterCaps($data)
{
return(ucfirst(strtolower($data)));
}
}
|