protected function populateFromOptions($options) { foreach ($options as $name => $attrs) { $column = new Column($name); if (isset($attrs['field'])) { $column->setFieldName($attrs['field']); } if (isset($attrs['type'])) { $column->setType($attrs['type']); } if (isset($attrs['sortable'])) { $column->setSortable($attrs['sortable']); } if (isset($attrs['label'])) { $column->setLabel($attrs['label']); } if (isset($attrs['format'])) { $column->setFormat($attrs['format']); } if (isset($attrs['format_function'])) { $column->setFormatFunction($attrs['format_function']); } if (isset($attrs['template'])) { $column->setTemplate($attrs['template']); } if (isset($attrs['boolean_actions'])) { $column->setBooleanActions($attrs['boolean_actions']); } if (isset($attrs['get_methods'])) { $column->setMethods($attrs['get_methods']); } $this->add($column); } }
public function createEnumColumn($defaultValues, $defaultValue) { $column = new Column(); $column->setType(PropelTypes::ENUM); $column->setValueSet($defaultValues); $column->setDefaultValue($defaultValue); return $column; }
/** * Constructs an instance from a list of column headers as returned by the * Google Analytics API. * * @param array $headers * @param Google\Analytics\API $api */ public function __construct(array $headers, API $api) { $headerCount = count($headers); for ($i = 0; $i < $headerCount; $i++) { self::_validateHeader($headers[$i]); try { $column = $api->getColumn($headers[$i]['name']); } catch (InvalidArgumentException $e) { /* I don't expect this to happen but in case it does, we can create a representation of the column by building it on the fly, though it will be missing most of the available properties. */ $column = new Column(); $column->setID(API::addPrefix($headers[$i]['name'])); $column->setType($headers[$i]['columnType']); $column->setDataType($headers[$i]['dataType']); } $this->_columns[] = $column; $this->_columnIndicesByName[$column->getName()] = $i; } }
/** * Creates the column from a given array * * @param array $columns */ public function setColumns($columns) { if (false == is_array($columns)) { return; } foreach ($columns as $name => $options) { $column = new Column(); $type = $this->getOptionByKey("type", $options); $length = $this->getOptionByKey("length", $options); $notNull = $this->getOptionByKey("notNull", $options); $autoIncrement = $this->getOptionByKey("autoIncrement", $options); $index = $this->getOptionByKey("index", $options); $collate = $this->getOptionByKey("collate", $options); $column->setName($name); // Set all options if set if (false != $type) { $column->setType($type); } if (false != $length) { $column->setLength($length); } if (false != $notNull) { $column->setNotNull($notNull); } if (false != $autoIncrement) { $column->setAutoIncrement($autoIncrement); } if (false != $index) { $column->setIndex($index); } if (false != $collate) { $column->setCollate($collate); } // Add column $this->columns[] = $column; } }
/** * Populates the properties that store cached dimensions and metrics, * either from the database or from the API directly, depending on when * the data that we have was last fetched. */ private function _refreshColumnMetadata() { /* If we're not using a database, the best we can do is to cache the column metadata for the duration of this process' existence. */ if (self::$_dbConn) { try { $lastFetchData = self::_getLastFetchData('google_analytics_api_columns'); if ($lastFetchData) { $fetchDate = (int) $lastFetchData['fetch_date']; $etag = $lastFetchData['etag']; } else { $fetchDate = null; $etag = null; } $columns = null; $newETag = null; if ($this->_bypassColumnCache || !$fetchDate || $fetchDate <= time() - GOOGLE_ANALYTICS_API_METADATA_CACHE_DURATION) { $this->_bypassColumnCache = false; $request = new APIRequest('metadata/ga/columns'); if ($etag) { $request->setHeader('If-None-Match: ' . $etag); } $columns = $this->_makeRequest($request); if ($columns) { $stmt = self::$_DB_STATEMENTS['google_analytics_api_columns']['insert']; foreach ($columns as $column) { $stmt->bindValue(':name', $column->getName(), \PDO::PARAM_STR); $stmt->bindValue(':type', $column->getType(), \PDO::PARAM_STR); $stmt->bindValue(':data_type', $column->getDataType(), \PDO::PARAM_STR); $stmt->bindValue(':replaced_by', $column->getReplacementColumn(), \PDO::PARAM_STR); $stmt->bindValue(':group', $column->getGroup(), \PDO::PARAM_STR); $stmt->bindValue(':ui_name', $column->getUIName(), \PDO::PARAM_STR); $stmt->bindValue(':description', $column->getDescription(), \PDO::PARAM_STR); $stmt->bindValue(':calculation', $column->getCalculation(), \PDO::PARAM_STR); $stmt->bindValue(':min_template_index', $column->getMinTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':max_template_index', $column->getMaxTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':min_template_index_premium', $column->getPremiumMinTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':max_template_index_premium', $column->getPremiumMaxTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':allowed_in_segments', $column->isAllowedInSegments(), \PDO::PARAM_INT); $stmt->bindValue(':deprecated', $column->isDeprecated(), \PDO::PARAM_INT); $stmt->execute(); } $stmt = null; } if (array_key_exists('etag', $this->_responseParsed) && $this->_responseParsed['etag']) { $newETag = $this->_responseParsed['etag']; } else { $responseHeader = $this->getResponseHeaderAsAssociativeArray(); if (array_key_exists('ETag', $responseHeader) && $responseHeader['ETag']) { $newETag = $responseHeader['ETag']; } } if ($newETag) { self::_storeFetchData('google_analytics_api_columns', count($columns), $newETag); } } if (!$columns) { $columns = array(); $stmt = self::$_dbConn->query('SELECT * FROM google_analytics_api_columns'); $stmt->bindColumn('name', $name, \PDO::PARAM_STR); $stmt->bindColumn('type', $type, \PDO::PARAM_STR); $stmt->bindColumn('data_type', $dataType, \PDO::PARAM_STR); $stmt->bindColumn('replaced_by', $replacement, \PDO::PARAM_STR); $stmt->bindColumn('group', $group, \PDO::PARAM_STR); $stmt->bindColumn('ui_name', $uiName, \PDO::PARAM_STR); $stmt->bindColumn('description', $description, \PDO::PARAM_STR); $stmt->bindColumn('calculation', $calculation, \PDO::PARAM_STR); /* Null values get cast to zeros if I bind them as integers, so we'll rely on the object's setter to take care of the type casting. */ $stmt->bindColumn('min_template_index', $minTemplateIndex, \PDO::PARAM_STR); $stmt->bindColumn('max_template_index', $maxTemplateIndex, \PDO::PARAM_STR); $stmt->bindColumn('min_template_index_premium', $minTemplateIndexPremium, \PDO::PARAM_STR); $stmt->bindColumn('max_template_index_premium', $maxTemplateIndexPremium, \PDO::PARAM_STR); $stmt->bindColumn('allowed_in_segments', $allowedInSegments, \PDO::PARAM_BOOL); $stmt->bindColumn('deprecated', $deprecated, \PDO::PARAM_BOOL); while ($stmt->fetch(\PDO::FETCH_BOUND)) { $column = new Column(); $column->setID('ga:' . $name); $column->setReplacementColumn($replacement); $column->setType($type); $column->setDataType($dataType); $column->setGroup($group); $column->setUIName($uiName); $column->setDescription($description); $column->setCalculation($calculation); if ($minTemplateIndex !== null) { $column->setMinTemplateIndex($minTemplateIndex); } if ($maxTemplateIndex !== null) { $column->setMaxTemplateIndex($maxTemplateIndex); } if ($minTemplateIndexPremium !== null) { $column->setPremiumMinTemplateIndex($minTemplateIndexPremium); } if ($maxTemplateIndexPremium !== null) { $column->setPremiumMaxTemplateIndex($maxTemplateIndexPremium); } $column->isAllowedInSegments($allowedInSegments); $column->isDeprecated($deprecated); $columns[] = $column; } } } catch (\PDOException $e) { throw new RuntimeException('Caught database error while refreshing column metadata.', null, $e); } } else { $columns = $this->_makeRequest(new APIRequest('metadata/ga/columns')); } foreach ($columns as $column) { if ($column->getType() == 'DIMENSION') { $table =& self::$_dimensions; $list =& self::$_dimensionNames; } else { $table =& self::$_metrics; $list =& self::$_metricNames; } $name = $column->getName(); $table[$name] = $column; if (!$column->isDeprecated()) { $list[] = $name; } } usort(self::$_dimensionNames, 'strcasecmp'); usort(self::$_metricNames, 'strcasecmp'); }
public function testColumnIsFKAndPK() { $column = new Column(); $column->setName('id'); $column->setPrimaryKey(true); $column->setAutoIncrement(true); $column->setType('integer'); $table = new Table(); $table->setCommonName('table_one'); $table->addColumn($column); $db = new Database(); $db->setName('MultipleTables'); $db->addTable($table); $column = new Column(); $column->setName('id'); $column->setPrimaryKey(true); $column->setAutoIncrement(true); $column->setType('integer'); $c2 = new Column(); $c2->setPrimaryKey(true); $c2->setName('foreign_id'); $c2->setType('integer'); $table = new Table(); $table->setCommonName('table_two'); $table->addColumn($column); $table->addColumn($c2); $fk = new ForeignKey(); $fk->setName('FK_1'); $fk->addReference('foreign_id', 'id'); $fk->setForeignTableCommonName('table_one'); $table->addForeignKey($fk); $db->addTable($table); $expected = implode("\n", array('digraph G {', 'nodetable_one [label="{<table>table_one|<cols>id (integer) [PK]\\l}", shape=record];', 'nodetable_two [label="{<table>table_two|<cols>id (integer) [PK]\\lforeign_id (integer) [FK] [PK]\\l}", shape=record];', 'nodetable_two:cols -> nodetable_one:table [label="foreign_id=id"];', '}', '')); $this->assertEquals($expected, PropelDotGenerator::create($db)); }
public function testIsPhpArrayType() { $column = new Column(); $this->assertFalse($column->isPhpArrayType()); $column->setType(PropelTypes::PHP_ARRAY); $this->assertTrue($column->isPhpArrayType()); }
public function parseToDataObj() { $file_buffer = strtolower($this->_file_buffer); $file_buffer = str_replace("\n", ' ', $file_buffer); $file_buffer = str_replace("\t", ' ', $file_buffer); $file_buffer = preg_replace('/\\s+/si', " ", $file_buffer); $tables = explode('create table ', $file_buffer); // first ones throw away since all table definitions start with "create table" and this is exploding by it array_shift($tables); $db = array(); // empty table array foreach ($tables as $table) { $TableDef = new Table(); trim($table); preg_match('/`(.*?)`/', $table, $matches); $TableDef->name = trim($matches[1]); unset($matches); $column_pieces = explode('`' . $TableDef->name . '`', $table); $table_columns = $column_pieces[1]; $columns = explode(',', $table_columns); foreach ($columns as $column) { $Col = new Column(); $column = ' ' . trim($column) . ' '; // strip out things before a ` unless it's UNIQUE or PRIMARY, and then parse those. if (stripos($column, 'unique') == 1) { $keys = explode('`', $column); foreach ($keys as $key) { if (isset($TableDef->columns[$key])) { $TableDef->columns[$key]->unique = true; $TableDef->unique_keys[] = $key; } } } else { if (stripos($column, 'primary') == 1) { $keys = explode('`', $column); foreach ($keys as $key) { if (isset($TableDef->columns[$key])) { $TableDef->columns[$key]->primary = true; $TableDef->primary_keys[] = $key; } } } else { if (stripos($column, 'index') == 1) { $keys = explode('`', $column); foreach ($keys as $key) { if (isset($TableDef->columns[$key])) { $TableDef->columns[$key]->index = true; $TableDef->index_keys[] = $key; } } } else { $original_column = $column; // do appropriate stuff $column = preg_replace('/.*?`(.*?)/', '`$1', $column, 1); // thank you mr gregory preg_match_all('/`([a-zA-Z0-9_]+)`[\\s]*([\\w]+)[\\s]*\\(*[\\s]*([0-9]*)[\\s]*\\)*[\\s]+([^,]+)./', $column, $matches); if (!isset($matches[1][0])) { print_r($original_column); continue; } $Col->name = $matches[1][0]; $Col->setType($this->convertToStandardType($matches[2][0])); $size_array = $this->convertToStandardSizeArray($matches[3][0]); $Col->setSize($size_array[0], $size_array[1]); $column = ' ' . trim($matches[4][0]) . ' '; unset($matches); if (substr($Col->name, -3) == '_id') { $TableDef->possible_foreign_keys[] = $Col->name; } // match for binary, unsigned, or unsigned zerofill for attributes if (stripos($column, 'unsigned zerofill')) { $Col->setAttributes('unsigned zerofill'); } else { if (stripos($column, '')) { $Col->setAttributes('unsigned'); } else { if (stripos($column, 'binary')) { $Col->setAttributes('binary'); } } } // match "null" or "not null" and define the nullable attribute if (stripos($column, 'not null')) { $Col->nullable = false; } else { $Col->nullable = true; } // match primary, unique, index, or fulltext for index if (stripos($column, ' primary')) { $Col->primary = true; $TableDef->primary_keys[] = $Col->name; } else { if (stripos($column, ' unique')) { $Col->unique = true; $TableDef->unique_keys[] = $Col->name; } else { if (stripos($column, ' index')) { $Col->index = true; $TableDef->index_keys[] = $Col->name; } } } // match auto_increment for itself if (stripos($column, ' autoincrement')) { $Col->auto_increment = true; } // match COMMENT '*' for comments preg_match('/\'(.*?)\'/', $column, $matches); $Col->comments = isset($matches[1]) ? trim($matches[1]) : null; unset($matches); $Col->extra = $column; if (trim($column) == '') { echo "Failed: " . $original_column . "\n"; } $TableDef->columns[$Col->name] = $Col; } } } foreach ($TableDef->possible_foreign_keys as $fk_guess) { if (in_array($fk_guess, $TableDef->index_keys) || in_array($fk_guess, $TableDef->primary_keys) || in_array($fk_guess, $TableDef->unique_keys)) { $TableDef->foreign_keys[] = $fk_guess; } } } $db[] = $TableDef; } $this->_db = $db; }