Browse Source
* master: (156 commits) added property docs to AR test classes moved getPDOType() back to Command to avoid dependency on Schema updated @property annotations of web\Request moved redis out of yii\db namespace Fixes #884. Supports sending CSRF token via HTTP header. Use .prop() instead .attr(). use meta tags to pass CSRF token. Added memcached service in Travis YAML fixed typo cleanup db `use` statements doc fix. Better strtr arguments format Modified js registration position. renamed Request::csrfTokenName to csrfVar. added version, csrfVar and csrfToken to yii js module. Advanced application enhancements. Basic application enhancements. Better phpdoc for AccessControl Security::generateRandomKey enhancements: Fixes #875: Security::generateRandomKey() can now be safely used in URLs ... Conflicts: .travis.yml framework/yii/caching/RedisCache.php tests/unit/framework/caching/RedisCacheTest.phptags/2.0.0-beta
Carsten Brandt
11 years ago
245 changed files with 4587 additions and 720 deletions
@ -0,0 +1,29 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var string $name |
||||||
|
* @var string $message |
||||||
|
* @var Exception $exception |
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = $name; |
||||||
|
?> |
||||||
|
<div class="site-error"> |
||||||
|
|
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<div class="alert alert-danger"> |
||||||
|
<?php echo nl2br(Html::encode($message)); ?> |
||||||
|
</div> |
||||||
|
|
||||||
|
<p> |
||||||
|
The above error occurred while the Web server was processing your request. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
Please contact us if you think this is a server error. Thank you. |
||||||
|
</p> |
||||||
|
|
||||||
|
</div> |
@ -1,17 +1,11 @@ |
|||||||
<?php |
<?php |
||||||
return array( |
return array( |
||||||
|
'preload' => array( |
||||||
|
//'debug', |
||||||
|
), |
||||||
'modules' => array( |
'modules' => array( |
||||||
// 'debug' => array( |
// 'debug' => array( |
||||||
// 'class' => 'yii\debug\Module', |
// 'class' => 'yii\debug\Module', |
||||||
// ), |
// ), |
||||||
), |
), |
||||||
'components' => array( |
|
||||||
'log' => array( |
|
||||||
'targets' => array( |
|
||||||
// array( |
|
||||||
// 'class' => 'yii\log\DebugTarget', |
|
||||||
// ) |
|
||||||
), |
|
||||||
), |
|
||||||
), |
|
||||||
); |
); |
||||||
|
@ -1,17 +1,11 @@ |
|||||||
<?php |
<?php |
||||||
return array( |
return array( |
||||||
|
'preload' => array( |
||||||
|
//'debug', |
||||||
|
), |
||||||
'modules' => array( |
'modules' => array( |
||||||
// 'debug' => array( |
// 'debug' => array( |
||||||
// 'class' => 'yii\debug\Module', |
// 'class' => 'yii\debug\Module', |
||||||
// ), |
// ), |
||||||
), |
), |
||||||
'components' => array( |
|
||||||
'log' => array( |
|
||||||
'targets' => array( |
|
||||||
// array( |
|
||||||
// 'class' => 'yii\log\DebugTarget', |
|
||||||
// ) |
|
||||||
), |
|
||||||
), |
|
||||||
), |
|
||||||
); |
); |
||||||
|
@ -0,0 +1,29 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var string $name |
||||||
|
* @var string $message |
||||||
|
* @var Exception $exception |
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = $name; |
||||||
|
?> |
||||||
|
<div class="site-error"> |
||||||
|
|
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<div class="alert alert-danger"> |
||||||
|
<?php echo nl2br(Html::encode($message)); ?> |
||||||
|
</div> |
||||||
|
|
||||||
|
<p> |
||||||
|
The above error occurred while the Web server was processing your request. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
Please contact us if you think this is a server error. Thank you. |
||||||
|
</p> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,328 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\build\controllers; |
||||||
|
|
||||||
|
use yii\console\Controller; |
||||||
|
use yii\helpers\Console; |
||||||
|
use yii\helpers\FileHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* PhpDocController is there to help maintaining PHPDoc annotation in class files |
||||||
|
* |
||||||
|
* @author Carsten Brandt <mail@cebe.cc> |
||||||
|
* @author Alexander Makarov <sam@rmcreative.ru> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class PhpDocController extends Controller |
||||||
|
{ |
||||||
|
public $defaultAction = 'property'; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var bool whether to update class docs directly. Setting this to false will just output docs |
||||||
|
* for copy and paste. |
||||||
|
*/ |
||||||
|
public $updateFiles = true; |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates @property annotations in class files from getters and setters |
||||||
|
* |
||||||
|
* Property description will be taken from getter or setter or from an @property annotation |
||||||
|
* in the getters docblock if there is one defined. |
||||||
|
* |
||||||
|
* See https://github.com/yiisoft/yii2/wiki/Core-framework-code-style#documentation for details. |
||||||
|
* |
||||||
|
* @param null $root the directory to parse files from. Defaults to YII_PATH. |
||||||
|
*/ |
||||||
|
public function actionProperty($root=null) |
||||||
|
{ |
||||||
|
if ($root === null) { |
||||||
|
$root = YII_PATH; |
||||||
|
} |
||||||
|
$root = FileHelper::normalizePath($root); |
||||||
|
$options = array( |
||||||
|
'filter' => function ($path) { |
||||||
|
if (is_file($path)) { |
||||||
|
$file = basename($path); |
||||||
|
if ($file[0] < 'A' || $file[0] > 'Z') { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
}, |
||||||
|
'only' => array('.php'), |
||||||
|
'except' => array( |
||||||
|
'YiiBase.php', |
||||||
|
'Yii.php', |
||||||
|
'/debug/views/', |
||||||
|
'/requirements/', |
||||||
|
'/gii/views/', |
||||||
|
'/gii/generators/', |
||||||
|
), |
||||||
|
); |
||||||
|
$files = FileHelper::findFiles($root, $options); |
||||||
|
$nFilesTotal = 0; |
||||||
|
$nFilesUpdated = 0; |
||||||
|
foreach ($files as $file) { |
||||||
|
$result = $this->generateClassPropertyDocs($file); |
||||||
|
if ($result !== false) { |
||||||
|
list($className, $phpdoc) = $result; |
||||||
|
if ($this->updateFiles) { |
||||||
|
if ($this->updateClassPropertyDocs($file, $className, $phpdoc)) { |
||||||
|
$nFilesUpdated++; |
||||||
|
} |
||||||
|
} elseif (!empty($phpdoc)) { |
||||||
|
$this->stdout("\n[ " . $file . " ]\n\n", Console::BOLD); |
||||||
|
$this->stdout($phpdoc); |
||||||
|
} |
||||||
|
} |
||||||
|
$nFilesTotal++; |
||||||
|
} |
||||||
|
|
||||||
|
$this->stdout("\nParsed $nFilesTotal files.\n"); |
||||||
|
$this->stdout("Updated $nFilesUpdated files.\n"); |
||||||
|
} |
||||||
|
|
||||||
|
public function globalOptions() |
||||||
|
{ |
||||||
|
return array_merge(parent::globalOptions(), array('updateFiles')); |
||||||
|
} |
||||||
|
|
||||||
|
protected function updateClassPropertyDocs($file, $className, $propertyDoc) |
||||||
|
{ |
||||||
|
$ref = new \ReflectionClass($className); |
||||||
|
if ($ref->getFileName() != $file) { |
||||||
|
$this->stderr("[ERR] Unable to create ReflectionClass for class: $className loaded class is not from file: $file\n", Console::FG_RED); |
||||||
|
} |
||||||
|
|
||||||
|
if (!$ref->isSubclassOf('yii\base\Object') && $className != 'yii\base\Object') { |
||||||
|
$this->stderr("[INFO] Skipping class $className as it is not a subclass of yii\\base\\Object.\n", Console::FG_BLUE, Console::BOLD); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
$oldDoc = $ref->getDocComment(); |
||||||
|
$newDoc = $this->cleanDocComment($this->updateDocComment($oldDoc, $propertyDoc)); |
||||||
|
|
||||||
|
$seenSince = false; |
||||||
|
$seenAuthor = false; |
||||||
|
|
||||||
|
// TODO move these checks to different action |
||||||
|
$lines = explode("\n", $newDoc); |
||||||
|
if (trim($lines[1]) == '*' || substr(trim($lines[1]), 0, 3) == '* @') { |
||||||
|
$this->stderr("[WARN] Class $className has no short description.\n", Console::FG_YELLOW, Console::BOLD); |
||||||
|
} |
||||||
|
foreach($lines as $line) { |
||||||
|
if (substr(trim($line), 0, 9) == '* @since ') { |
||||||
|
$seenSince = true; |
||||||
|
} elseif (substr(trim($line), 0, 10) == '* @author ') { |
||||||
|
$seenAuthor = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (!$seenSince) { |
||||||
|
$this->stderr("[ERR] No @since found in class doc in file: $file\n", Console::FG_RED); |
||||||
|
} |
||||||
|
if (!$seenAuthor) { |
||||||
|
$this->stderr("[ERR] No @author found in class doc in file: $file\n", Console::FG_RED); |
||||||
|
} |
||||||
|
|
||||||
|
if (trim($oldDoc) != trim($newDoc)) { |
||||||
|
|
||||||
|
$fileContent = explode("\n", file_get_contents($file)); |
||||||
|
$start = $ref->getStartLine() - 2; |
||||||
|
$docStart = $start - count(explode("\n", $oldDoc)) + 1; |
||||||
|
|
||||||
|
$newFileContent = array(); |
||||||
|
$n = count($fileContent); |
||||||
|
for($i = 0; $i < $n; $i++) { |
||||||
|
if ($i > $start || $i < $docStart) { |
||||||
|
$newFileContent[] = $fileContent[$i]; |
||||||
|
} else { |
||||||
|
$newFileContent[] = trim($newDoc); |
||||||
|
$i = $start; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
file_put_contents($file, implode("\n", $newFileContent)); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* remove multi empty lines and trim trailing whitespace |
||||||
|
* |
||||||
|
* @param $doc |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
protected function cleanDocComment($doc) |
||||||
|
{ |
||||||
|
$lines = explode("\n", $doc); |
||||||
|
$n = count($lines); |
||||||
|
for($i = 0; $i < $n; $i++) { |
||||||
|
$lines[$i] = rtrim($lines[$i]); |
||||||
|
if (trim($lines[$i]) == '*' && trim($lines[$i + 1]) == '*') { |
||||||
|
unset($lines[$i]); |
||||||
|
} |
||||||
|
} |
||||||
|
return implode("\n", $lines); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* replace property annotations in doc comment |
||||||
|
* @param $doc |
||||||
|
* @param $properties |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
protected function updateDocComment($doc, $properties) |
||||||
|
{ |
||||||
|
$lines = explode("\n", $doc); |
||||||
|
$propertyPart = false; |
||||||
|
$propertyPosition = false; |
||||||
|
foreach($lines as $i => $line) { |
||||||
|
if (substr(trim($line), 0, 12) == '* @property ') { |
||||||
|
$propertyPart = true; |
||||||
|
} elseif ($propertyPart && trim($line) == '*') { |
||||||
|
$propertyPosition = $i; |
||||||
|
$propertyPart = false; |
||||||
|
} |
||||||
|
if (substr(trim($line), 0, 10) == '* @author ' && $propertyPosition === false) { |
||||||
|
$propertyPosition = $i - 1; |
||||||
|
$propertyPart = false; |
||||||
|
} |
||||||
|
if ($propertyPart) { |
||||||
|
unset($lines[$i]); |
||||||
|
} |
||||||
|
} |
||||||
|
$finalDoc = ''; |
||||||
|
foreach($lines as $i => $line) { |
||||||
|
$finalDoc .= $line . "\n"; |
||||||
|
if ($i == $propertyPosition) { |
||||||
|
$finalDoc .= $properties; |
||||||
|
} |
||||||
|
} |
||||||
|
return $finalDoc; |
||||||
|
} |
||||||
|
|
||||||
|
protected function generateClassPropertyDocs($fileName) |
||||||
|
{ |
||||||
|
$phpdoc = ""; |
||||||
|
$file = str_replace("\r", "", str_replace("\t", " ", file_get_contents($fileName, true))); |
||||||
|
$ns = $this->match('#\nnamespace (?<name>[\w\\\\]+);\n#', $file); |
||||||
|
$namespace = reset($ns); |
||||||
|
$namespace = $namespace['name']; |
||||||
|
$classes = $this->match('#\n(?:abstract )?class (?<name>\w+)( |\n)(extends )?.+\{(?<content>.*)\n\}(\n|$)#', $file); |
||||||
|
|
||||||
|
if (count($classes) > 1) { |
||||||
|
$this->stderr("[ERR] There should be only one class in a file: $fileName\n", Console::FG_RED); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (count($classes) < 1) { |
||||||
|
$interfaces = $this->match('#\ninterface (?<name>\w+)\n\{(?<content>.+)\n\}(\n|$)#', $file); |
||||||
|
if (count($interfaces) == 1) { |
||||||
|
return false; |
||||||
|
} elseif (count($interfaces) > 1) { |
||||||
|
$this->stderr("[ERR] There should be only one interface in a file: $fileName\n", Console::FG_RED); |
||||||
|
} else { |
||||||
|
$this->stderr("[ERR] No class in file: $fileName\n", Console::FG_RED); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
$className = null; |
||||||
|
foreach ($classes as &$class) { |
||||||
|
|
||||||
|
$className = $namespace . '\\' . $class['name']; |
||||||
|
|
||||||
|
$gets = $this->match( |
||||||
|
'#\* @return (?<type>[\w\\|\\\\\\[\\]]+)(?: (?<comment>(?:(?!\*/|\* @).)+?)(?:(?!\*/).)+|[\s\n]*)\*/' . |
||||||
|
'[\s\n]{2,}public function (?<kind>get)(?<name>\w+)\((?:,? ?\$\w+ ?= ?[^,]+)*\)#', |
||||||
|
$class['content']); |
||||||
|
$sets = $this->match( |
||||||
|
'#\* @param (?<type>[\w\\|\\\\\\[\\]]+) \$\w+(?: (?<comment>(?:(?!\*/|\* @).)+?)(?:(?!\*/).)+|[\s\n]*)\*/' . |
||||||
|
'[\s\n]{2,}public function (?<kind>set)(?<name>\w+)\(\$\w+(?:, ?\$\w+ ?= ?[^,]+)*\)#', |
||||||
|
$class['content']); |
||||||
|
// check for @property annotations in getter and setter |
||||||
|
$properties = $this->match( |
||||||
|
'#\* @(?<kind>property) (?<type>[\w\\|\\\\\\[\\]]+)(?: (?<comment>(?:(?!\*/|\* @).)+?)(?:(?!\*/).)+|[\s\n]*)\*/' . |
||||||
|
'[\s\n]{2,}public function [g|s]et(?<name>\w+)\(((?:,? ?\$\w+ ?= ?[^,]+)*|\$\w+(?:, ?\$\w+ ?= ?[^,]+)*)\)#', |
||||||
|
$class['content']); |
||||||
|
$acrs = array_merge($properties, $gets, $sets); |
||||||
|
//print_r($acrs); continue; |
||||||
|
|
||||||
|
$props = array(); |
||||||
|
foreach ($acrs as &$acr) { |
||||||
|
$acr['name'] = lcfirst($acr['name']); |
||||||
|
$acr['comment'] = trim(preg_replace('#(^|\n)\s+\*\s?#', '$1 * ', $acr['comment'])); |
||||||
|
$props[$acr['name']][$acr['kind']] = array( |
||||||
|
'type' => $acr['type'], |
||||||
|
'comment' => $this->fixSentence($acr['comment']), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
ksort($props); |
||||||
|
|
||||||
|
if (count($props) > 0) { |
||||||
|
$phpdoc .= " *\n"; |
||||||
|
foreach ($props as $propName => &$prop) { |
||||||
|
$docline = ' * @'; |
||||||
|
$docline .= 'property'; // Do not use property-read and property-write as few IDEs support complex syntax. |
||||||
|
$note = ''; |
||||||
|
if (isset($prop['get']) && isset($prop['set'])) { |
||||||
|
if ($prop['get']['type'] != $prop['set']['type']) { |
||||||
|
$note = ' Note that the type of this property differs in getter and setter.' |
||||||
|
. ' See [[get'.ucfirst($propName).'()]] and [[set'.ucfirst($propName).'()]] for details.'; |
||||||
|
} |
||||||
|
} elseif (isset($prop['get'])) { |
||||||
|
$note = ' This property is read-only.'; |
||||||
|
// $docline .= '-read'; |
||||||
|
} elseif (isset($prop['set'])) { |
||||||
|
$note = ' This property is write-only.'; |
||||||
|
// $docline .= '-write'; |
||||||
|
} else { |
||||||
|
continue; |
||||||
|
} |
||||||
|
$docline .= ' ' . $this->getPropParam($prop, 'type') . " $$propName "; |
||||||
|
$comment = explode("\n", $this->getPropParam($prop, 'comment') . $note); |
||||||
|
foreach ($comment as &$cline) { |
||||||
|
$cline = ltrim($cline, '* '); |
||||||
|
} |
||||||
|
$docline = wordwrap($docline . implode(' ', $comment), 110, "\n * ") . "\n"; |
||||||
|
|
||||||
|
$phpdoc .= $docline; |
||||||
|
} |
||||||
|
$phpdoc .= " *\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
return array($className, $phpdoc); |
||||||
|
} |
||||||
|
|
||||||
|
protected function match($pattern, $subject) |
||||||
|
{ |
||||||
|
$sets = array(); |
||||||
|
preg_match_all($pattern . 'suU', $subject, $sets, PREG_SET_ORDER); |
||||||
|
foreach ($sets as &$set) |
||||||
|
foreach ($set as $i => $match) |
||||||
|
if (is_numeric($i) /*&& $i != 0*/) |
||||||
|
unset($set[$i]); |
||||||
|
return $sets; |
||||||
|
} |
||||||
|
|
||||||
|
protected function fixSentence($str) |
||||||
|
{ |
||||||
|
// TODO fix word wrap |
||||||
|
if ($str == '') |
||||||
|
return ''; |
||||||
|
return strtoupper(substr($str, 0, 1)) . substr($str, 1) . ($str[strlen($str) - 1] != '.' ? '.' : ''); |
||||||
|
} |
||||||
|
|
||||||
|
protected function getPropParam($prop, $param) |
||||||
|
{ |
||||||
|
return isset($prop['property']) ? $prop['property'][$param] : (isset($prop['get']) ? $prop['get'][$param] : $prop['set'][$param]); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,117 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\cubrid; |
||||||
|
|
||||||
|
use yii\base\InvalidParamException; |
||||||
|
|
||||||
|
/** |
||||||
|
* QueryBuilder is the query builder for CUBRID databases (version 9.1.x and higher). |
||||||
|
* |
||||||
|
* @author Carsten Brandt <mail@cebe.cc> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class QueryBuilder extends \yii\db\QueryBuilder |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
||||||
|
*/ |
||||||
|
public $typeMap = array( |
||||||
|
Schema::TYPE_PK => 'int NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||||||
|
Schema::TYPE_BIGPK => 'bigint NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||||||
|
Schema::TYPE_STRING => 'varchar(255)', |
||||||
|
Schema::TYPE_TEXT => 'varchar', |
||||||
|
Schema::TYPE_SMALLINT => 'smallint', |
||||||
|
Schema::TYPE_INTEGER => 'int', |
||||||
|
Schema::TYPE_BIGINT => 'bigint', |
||||||
|
Schema::TYPE_FLOAT => 'float(7)', |
||||||
|
Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||||||
|
Schema::TYPE_DATETIME => 'datetime', |
||||||
|
Schema::TYPE_TIMESTAMP => 'timestamp', |
||||||
|
Schema::TYPE_TIME => 'time', |
||||||
|
Schema::TYPE_DATE => 'date', |
||||||
|
Schema::TYPE_BINARY => 'blob', |
||||||
|
Schema::TYPE_BOOLEAN => 'smallint', |
||||||
|
Schema::TYPE_MONEY => 'decimal(19,4)', |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a SQL statement for resetting the sequence value of a table's primary key. |
||||||
|
* The sequence will be reset such that the primary key of the next new row inserted |
||||||
|
* will have the specified value or 1. |
||||||
|
* @param string $tableName the name of the table whose primary key sequence will be reset |
||||||
|
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||||||
|
* the next new row's primary key will have a value 1. |
||||||
|
* @return string the SQL statement for resetting sequence |
||||||
|
* @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||||||
|
*/ |
||||||
|
public function resetSequence($tableName, $value = null) |
||||||
|
{ |
||||||
|
$table = $this->db->getTableSchema($tableName); |
||||||
|
if ($table !== null && $table->sequenceName !== null) { |
||||||
|
$tableName = $this->db->quoteTableName($tableName); |
||||||
|
if ($value === null) { |
||||||
|
$key = reset($table->primaryKey); |
||||||
|
$value = (int)$this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1; |
||||||
|
} else { |
||||||
|
$value = (int)$value; |
||||||
|
} |
||||||
|
return "ALTER TABLE " . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;"; |
||||||
|
} elseif ($table === null) { |
||||||
|
throw new InvalidParamException("Table not found: $tableName"); |
||||||
|
} else { |
||||||
|
throw new InvalidParamException("There is not sequence associated with table '$tableName'."); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates a batch INSERT SQL statement. |
||||||
|
* For example, |
||||||
|
* |
||||||
|
* ~~~ |
||||||
|
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array( |
||||||
|
* array('Tom', 30), |
||||||
|
* array('Jane', 20), |
||||||
|
* array('Linda', 25), |
||||||
|
* ))->execute(); |
||||||
|
* ~~~ |
||||||
|
* |
||||||
|
* Note that the values in each row must match the corresponding column names. |
||||||
|
* |
||||||
|
* @param string $table the table that new rows will be inserted into. |
||||||
|
* @param array $columns the column names |
||||||
|
* @param array $rows the rows to be batch inserted into the table |
||||||
|
* @return string the batch INSERT SQL statement |
||||||
|
*/ |
||||||
|
public function batchInsert($table, $columns, $rows) |
||||||
|
{ |
||||||
|
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { |
||||||
|
$columnSchemas = $tableSchema->columns; |
||||||
|
} else { |
||||||
|
$columnSchemas = array(); |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($columns as $i => $name) { |
||||||
|
$columns[$i] = $this->db->quoteColumnName($name); |
||||||
|
} |
||||||
|
|
||||||
|
$values = array(); |
||||||
|
foreach ($rows as $row) { |
||||||
|
$vs = array(); |
||||||
|
foreach ($row as $i => $value) { |
||||||
|
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) { |
||||||
|
$value = $columnSchemas[$columns[$i]]->typecast($value); |
||||||
|
} |
||||||
|
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value; |
||||||
|
} |
||||||
|
$values[] = '(' . implode(', ', $vs) . ')'; |
||||||
|
} |
||||||
|
|
||||||
|
return 'INSERT INTO ' . $this->db->quoteTableName($table) |
||||||
|
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,240 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\cubrid; |
||||||
|
|
||||||
|
use yii\db\Expression; |
||||||
|
use yii\db\TableSchema; |
||||||
|
use yii\db\ColumnSchema; |
||||||
|
|
||||||
|
/** |
||||||
|
* Schema is the class for retrieving metadata from a CUBRID database (version 9.1.x and higher). |
||||||
|
* |
||||||
|
* @author Carsten Brandt <mail@cebe.cc> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class Schema extends \yii\db\Schema |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
||||||
|
* Please refer to [CUBRID manual](http://www.cubrid.org/manual/91/en/sql/datatype.html) for |
||||||
|
* details on data types. |
||||||
|
*/ |
||||||
|
public $typeMap = array( |
||||||
|
// Numeric data types |
||||||
|
'short' => self::TYPE_SMALLINT, |
||||||
|
'smallint' => self::TYPE_SMALLINT, |
||||||
|
'int' => self::TYPE_INTEGER, |
||||||
|
'integer' => self::TYPE_INTEGER, |
||||||
|
'bigint' => self::TYPE_BIGINT, |
||||||
|
'numeric' => self::TYPE_DECIMAL, |
||||||
|
'decimal' => self::TYPE_DECIMAL, |
||||||
|
'float' => self::TYPE_FLOAT, |
||||||
|
'real' => self::TYPE_FLOAT, |
||||||
|
'double' => self::TYPE_FLOAT, |
||||||
|
'double precision' => self::TYPE_FLOAT, |
||||||
|
'monetary' => self::TYPE_MONEY, |
||||||
|
// Date/Time data types |
||||||
|
'date' => self::TYPE_DATE, |
||||||
|
'time' => self::TYPE_TIME, |
||||||
|
'timestamp' => self::TYPE_TIMESTAMP, |
||||||
|
'datetime' => self::TYPE_DATETIME, |
||||||
|
// String data types |
||||||
|
'char' => self::TYPE_STRING, |
||||||
|
'varchar' => self::TYPE_STRING, |
||||||
|
'char varying' => self::TYPE_STRING, |
||||||
|
'nchar' => self::TYPE_STRING, |
||||||
|
'nchar varying' => self::TYPE_STRING, |
||||||
|
'string' => self::TYPE_STRING, |
||||||
|
// BLOB/CLOB data types |
||||||
|
'blob' => self::TYPE_BINARY, |
||||||
|
'clob' => self::TYPE_BINARY, |
||||||
|
// Bit string data types |
||||||
|
'bit' => self::TYPE_STRING, |
||||||
|
'bit varying' => self::TYPE_STRING, |
||||||
|
// Collection data types (considered strings for now) |
||||||
|
'set' => self::TYPE_STRING, |
||||||
|
'multiset' => self::TYPE_STRING, |
||||||
|
'list' => self::TYPE_STRING, |
||||||
|
'sequence' => self::TYPE_STRING, |
||||||
|
'enum' => self::TYPE_STRING, |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a table name for use in a query. |
||||||
|
* A simple table name has no schema prefix. |
||||||
|
* @param string $name table name |
||||||
|
* @return string the properly quoted table name |
||||||
|
*/ |
||||||
|
public function quoteSimpleTableName($name) |
||||||
|
{ |
||||||
|
return strpos($name, '"') !== false ? $name : '"' . $name . '"'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a column name for use in a query. |
||||||
|
* A simple column name has no prefix. |
||||||
|
* @param string $name column name |
||||||
|
* @return string the properly quoted column name |
||||||
|
*/ |
||||||
|
public function quoteSimpleColumnName($name) |
||||||
|
{ |
||||||
|
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a string value for use in a query. |
||||||
|
* Note that if the parameter is not a string, it will be returned without change. |
||||||
|
* @param string $str string to be quoted |
||||||
|
* @return string the properly quoted string |
||||||
|
* @see http://www.php.net/manual/en/function.PDO-quote.php |
||||||
|
*/ |
||||||
|
public function quoteValue($str) |
||||||
|
{ |
||||||
|
if (!is_string($str)) { |
||||||
|
return $str; |
||||||
|
} |
||||||
|
|
||||||
|
$this->db->open(); |
||||||
|
// workaround for broken PDO::quote() implementation in CUBRID 9.1.0 http://jira.cubrid.org/browse/APIS-658 |
||||||
|
if (version_compare($this->db->pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION), '9.1.0', '<=')) { |
||||||
|
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'"; |
||||||
|
} else { |
||||||
|
return $this->db->pdo->quote($str); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a query builder for the CUBRID database. |
||||||
|
* @return QueryBuilder query builder instance |
||||||
|
*/ |
||||||
|
public function createQueryBuilder() |
||||||
|
{ |
||||||
|
return new QueryBuilder($this->db); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Loads the metadata for the specified table. |
||||||
|
* @param string $name table name |
||||||
|
* @return TableSchema driver dependent table metadata. Null if the table does not exist. |
||||||
|
*/ |
||||||
|
protected function loadTableSchema($name) |
||||||
|
{ |
||||||
|
$this->db->open(); |
||||||
|
$tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name); |
||||||
|
|
||||||
|
if (isset($tableInfo[0]['NAME'])) { |
||||||
|
$table = new TableSchema(); |
||||||
|
$table->name = $tableInfo[0]['NAME']; |
||||||
|
|
||||||
|
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name); |
||||||
|
$columns = $this->db->createCommand($sql)->queryAll(); |
||||||
|
|
||||||
|
foreach ($columns as $info) { |
||||||
|
$column = $this->loadColumnSchema($info); |
||||||
|
$table->columns[$column->name] = $column; |
||||||
|
if ($column->isPrimaryKey) { |
||||||
|
$table->primaryKey[] = $column->name; |
||||||
|
if ($column->autoIncrement) { |
||||||
|
$table->sequenceName = ''; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name); |
||||||
|
foreach($foreignKeys as $key) { |
||||||
|
if (isset($table->foreignKeys[$key['FK_NAME']])) { |
||||||
|
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME']; |
||||||
|
} else { |
||||||
|
$table->foreignKeys[$key['FK_NAME']] = array( |
||||||
|
$key['PKTABLE_NAME'], |
||||||
|
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME'] |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
$table->foreignKeys = array_values($table->foreignKeys); |
||||||
|
|
||||||
|
return $table; |
||||||
|
} else { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Loads the column information into a [[ColumnSchema]] object. |
||||||
|
* @param array $info column information |
||||||
|
* @return ColumnSchema the column schema object |
||||||
|
*/ |
||||||
|
protected function loadColumnSchema($info) |
||||||
|
{ |
||||||
|
$column = new ColumnSchema(); |
||||||
|
|
||||||
|
$column->name = $info['Field']; |
||||||
|
$column->allowNull = $info['Null'] === 'YES'; |
||||||
|
$column->isPrimaryKey = strpos($info['Key'], 'PRI') !== false; |
||||||
|
$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false; |
||||||
|
|
||||||
|
$column->dbType = strtolower($info['Type']); |
||||||
|
$column->unsigned = strpos($column->dbType, 'unsigned') !== false; |
||||||
|
|
||||||
|
$column->type = self::TYPE_STRING; |
||||||
|
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) { |
||||||
|
$type = $matches[1]; |
||||||
|
if (isset($this->typeMap[$type])) { |
||||||
|
$column->type = $this->typeMap[$type]; |
||||||
|
} |
||||||
|
if (!empty($matches[2])) { |
||||||
|
if ($type === 'enum') { |
||||||
|
$values = explode(',', $matches[2]); |
||||||
|
foreach ($values as $i => $value) { |
||||||
|
$values[$i] = trim($value, "'"); |
||||||
|
} |
||||||
|
$column->enumValues = $values; |
||||||
|
} else { |
||||||
|
$values = explode(',', $matches[2]); |
||||||
|
$column->size = $column->precision = (int)$values[0]; |
||||||
|
if (isset($values[1])) { |
||||||
|
$column->scale = (int)$values[1]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$column->phpType = $this->getColumnPhpType($column); |
||||||
|
|
||||||
|
if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP' || |
||||||
|
$column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' || |
||||||
|
$column->type === 'date' && $info['Default'] === 'SYS_DATE' || |
||||||
|
$column->type === 'time' && $info['Default'] === 'SYS_TIME' |
||||||
|
) { |
||||||
|
$column->defaultValue = new Expression($info['Default']); |
||||||
|
} else { |
||||||
|
$column->defaultValue = $column->typecast($info['Default']); |
||||||
|
} |
||||||
|
|
||||||
|
return $column; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns all table names in the database. |
||||||
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||||||
|
* @return array all table names in the database. The names have NO schema name prefix. |
||||||
|
*/ |
||||||
|
protected function findTableNames($schema = '') |
||||||
|
{ |
||||||
|
$this->db->open(); |
||||||
|
$tables = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE); |
||||||
|
$tableNames = array(); |
||||||
|
foreach($tables as $table) { |
||||||
|
// do not list system tables |
||||||
|
if ($table['TYPE'] != 0) { |
||||||
|
$tableNames[] = $table['NAME']; |
||||||
|
} |
||||||
|
} |
||||||
|
return $tableNames; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright © 2008-2011 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\mssql; |
||||||
|
|
||||||
|
/** |
||||||
|
* TableSchema represents the metadata of a database table. |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class TableSchema extends \yii\db\TableSchema |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var string name of the catalog (database) that this table belongs to. |
||||||
|
* Defaults to null, meaning no catalog (or the current database). |
||||||
|
*/ |
||||||
|
public $catalogName; |
||||||
|
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue