public function testGetColumnDDLComment()
 {
     $column = new Column('foo');
     $column->getDomain()->copy($this->getPlatform()->getDomainForType('INTEGER'));
     $column->setDescription('This is column Foo');
     $expected = '`foo` INTEGER COMMENT \'This is column Foo\'';
     $this->assertEquals($expected, $this->getPlatform()->getColumnDDL($column));
 }
示例#2
0
 /**
  * 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');
 }
示例#3
0
 /**
  * Factory method creating a Column object
  * based on a row from the 'show columns from ' MySQL query result.
  *
  * @param array $row An associative array with the following keys:
  *                       Field, Type, Null, Key, Default, Extra.
  *
  * @return Column
  */
 public function getColumnFromRow($row, Table $table)
 {
     $name = $row['Field'];
     $is_nullable = $row['Null'] == 'YES';
     $autoincrement = strpos($row['Extra'], 'auto_increment') !== false;
     $size = null;
     $precision = null;
     $scale = null;
     $sqlType = false;
     $desc = $row['Comment'];
     $regexp = '/^
         (\\w+)        # column type [1]
         [\\(]         # (
             ?([\\d,]*)  # size or size, precision [2]
         [\\)]         # )
         ?\\s*         # whitespace
         (\\w*)        # extra description (UNSIGNED, CHARACTER SET, ...) [3]
     $/x';
     if (preg_match($regexp, $row['Type'], $matches)) {
         $nativeType = $matches[1];
         if ($matches[2]) {
             if (($cpos = strpos($matches[2], ',')) !== false) {
                 $size = (int) substr($matches[2], 0, $cpos);
                 $precision = $size;
                 $scale = (int) substr($matches[2], $cpos + 1);
             } else {
                 $size = (int) $matches[2];
             }
         }
         if ($matches[3]) {
             $sqlType = $row['Type'];
         }
         foreach (self::$defaultTypeSizes as $type => $defaultSize) {
             if ($nativeType == $type && $size == $defaultSize && $scale === null) {
                 $size = null;
                 continue;
             }
         }
     } elseif (preg_match('/^(\\w+)\\(/', $row['Type'], $matches)) {
         $nativeType = $matches[1];
         if ($nativeType == 'enum') {
             $sqlType = $row['Type'];
         }
     } else {
         $nativeType = $row['Type'];
     }
     //BLOBs can't have any default values in MySQL
     $default = preg_match('~blob|text~', $nativeType) ? null : $row['Default'];
     $propelType = $this->getMappedPropelType($nativeType);
     if (!$propelType) {
         $propelType = Column::DEFAULT_TYPE;
         $sqlType = $row['Type'];
         $this->warn("Column [" . $table->getName() . "." . $name . "] has a column type (" . $nativeType . ") that Propel does not support.");
     }
     // Special case for TINYINT(1) which is a BOOLEAN
     if (PropelTypes::TINYINT === $propelType && 1 === $size) {
         $propelType = PropelTypes::BOOLEAN;
     }
     $column = new Column($name);
     $column->setTable($table);
     $column->setDomainForType($propelType);
     if ($sqlType) {
         $column->getDomain()->replaceSqlType($sqlType);
     }
     $column->getDomain()->replaceSize($size);
     $column->getDomain()->replaceScale($scale);
     if ($default !== null) {
         if ($propelType == PropelTypes::BOOLEAN) {
             if ($default == '1') {
                 $default = 'true';
             }
             if ($default == '0') {
                 $default = 'false';
             }
         }
         if (in_array($default, array('CURRENT_TIMESTAMP'))) {
             $type = ColumnDefaultValue::TYPE_EXPR;
         } else {
             $type = ColumnDefaultValue::TYPE_VALUE;
         }
         $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $type));
     }
     $column->setAutoIncrement($autoincrement);
     $column->setNotNull(!$is_nullable);
     if ($this->addVendorInfo) {
         $vi = $this->getNewVendorInfoObject($row);
         $column->addVendorInfo($vi);
     }
     if ($desc) {
         if (!$this->isUtf8($desc)) {
             $desc = utf8_encode($desc);
         }
         $column->setDescription($desc);
     }
     return $column;
 }