/** * Construct model. * @param string $name Name of model. */ public function __construct($name = 'Extension') { parent::__construct($name); $this->addField('canonicalName', tr('Canonical name'), DataType::string()); $this->addField('name', tr('Name'), DataType::string()); $this->addField('version', tr('Version'), DataType::string()); $this->addField('description', tr('Description'), DataType::text()); $this->addField('enabled', tr('Enabled'), DataType::boolean()); }
/** * Convert output of SHOW COLUMN to DataType. * @param array $row Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { $null = $row['is_nullable'] != 'NO'; $default = null; if (isset($row['column_default'])) { $default = $row['column_default']; } $type = $row['data_type']; if (strpos($type, 'int') !== false) { $intFlags = 0; if (preg_match('/^nextval\\(/', $default) === 1) { $intFlags = DataType::AUTO_INCREMENT; $default = null; } else { if (isset($default)) { $default = intval($default); } } if (strpos($type, 'bigint') !== false) { return DataType::integer($intFlags | DataType::BIG, $null, $default); } if (strpos($type, 'smallint') !== false) { return DataType::integer($intFlags | DataType::SMALL, $null, $default); } return DataType::integer($intFlags, $null, $default); } if (strpos($type, 'double') !== false) { return DataType::float($null, isset($default) ? floatval($default) : null); } if (strpos($type, 'bool') !== false) { return DataType::boolean($null, isset($default) ? boolval($default) : null); } if (preg_match("/^'(.*)'::[a-z ]+\$/", $default, $matches) === 1) { $default = $matches[1]; } else { $default = null; } if (strpos($type, 'character') !== false) { $length = $row['character_maximum_length']; return DataType::string($length, $null, $default); } if (strpos($type, 'date') !== false) { return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'timestamp') !== false) { return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'text') !== false) { return DataType::text($null, $default); } throw new TypeException(tr('Unsupported PostgreSQL type "%1" for column: %2', $row['data_type'], $row['column_name'])); }
/** * Substitute and encode variables in an expression. * * Placeholders (see also {@see DataType::fromPlaceHolder()}: * <code> * true // Boolean true * false // Boolean false * {AnyModelName} // A model name * [anyFieldName] // A column/field name * "any string" // A string * ? // Any scalar value. * %m %model // A table/model object or name * %c %column %field // A column/field name * %_ // A placeholder placeholder, can also be a type, e.g. where(..., 'id = %_', $type, $value) * %i %int %integer // An integer value * %f %float // A floating point value * %s %str %string // A string * %t $text // Text * %b %bool %boolean // A boolean value * %date // A date value * %d %datetime // A date/time value * %n %bin %binary // A binary object * %AnyEnumClassName // An enum value of that class * %anyPlaceholder() // A tuple of values * </code> * * @param string|Condition $format Expression format, use placeholders instead of values. * @param mixed[] $vars List of values to replace placeholders with. * @param Quoter $quoter Quoter object for quoting identifieres and literals. * @return string The interpolated expression. */ public static function interpolate($format, $vars, Quoter $quoter) { if ($format instanceof self) { return $format->toString($quoter); } assume(is_string($format)); $boolean = DataType::boolean(); $true = $quoter->quoteLiteral($boolean, true); $false = $quoter->quoteLiteral($boolean, false); $format = preg_replace('/\\btrue\\b/i', $true, $format); $format = preg_replace('/\\bfalse\\b/i', $false, $format); $string = DataType::text(); $format = preg_replace_callback('/"((?:[^"\\\\]|\\\\.)*)"|\\{(.+?)\\}|\\[(.+?)\\]/', function ($matches) use($quoter, $string) { if (isset($matches[3])) { return $quoter->quoteField($matches[3]); } else { if (isset($matches[2])) { return $quoter->quoteModel($matches[2]); } else { return $quoter->quoteLiteral($string, stripslashes($matches[1])); } } }, $format); $i = 0; return preg_replace_callback('/((\\?)|%([a-z_\\\\]+))(\\(\\))?/i', function ($matches) use($vars, &$i, $quoter) { $value = $vars[$i]; $i++; $type = null; if (isset($matches[3]) and $matches[3] == '_') { if (!is_string($value)) { assume($value instanceof DataType); $value = $value->placeholder; } $matches[3] = ltrim($value, '%'); $value = $vars[$i]; $i++; } if (isset($matches[3]) and ($matches[3] == 'm' or $matches[3] == 'model')) { if (!is_string($value)) { assume($value instanceof BasicModel); $value = $value->getName(); } return $quoter->quoteModel($value); } if (isset($matches[3]) and ($matches[3] == 'c' or $matches[3] == 'column' or $matches[3] == 'field')) { assume(is_string($value)); return $quoter->quoteField($value); } if (isset($matches[3]) and $matches[3] != '()') { $type = DataType::fromPlaceholder($matches[3]); } if (!isset($type)) { $type = DataType::detectType($value); } if (isset($matches[4]) or isset($matches[3]) and $matches[3] == '()') { assume(is_array($value)); foreach ($value as $key => $v) { $value[$key] = $quoter->quoteLiteral($type, $v); } return '(' . implode(', ', $value) . ')'; } return $quoter->quoteLiteral($type, $value); }, $format); }