作者:elrossco2
项目:sql-parse
public function testEscape()
{
Context::setMode('ANSI_QUOTES');
$this->assertEquals('"test"', Context::escape('test'));
Context::setMode();
$this->assertEquals('`test`', Context::escape('test'));
$this->assertEquals(array('`a`', '`b`'), Context::escape(array('a', 'b')));
}
作者:Timande
项目:phpmyadmi
/**
* Parses unknown parts of the query.
*
* @return Token
*/
public function parseUnknown()
{
$token = $this->str[$this->last];
if (Context::isSeparator($token)) {
return null;
}
while (++$this->last < $this->len && !Context::isSeparator($this->str[$this->last])) {
$token .= $this->str[$this->last];
}
--$this->last;
return new Token($token);
}
作者:rushi96
项目:phpmyadmi
/**
* Index action
*
* @return void
*/
public function indexAction()
{
PMA_PageSettings::showGroup('TableStructure');
/**
* Function implementations for this script
*/
include_once 'libraries/check_user_privileges.lib.php';
include_once 'libraries/index.lib.php';
include_once 'libraries/sql.lib.php';
include_once 'libraries/bookmark.lib.php';
$this->response->getHeader()->getScripts()->addFiles(array('tbl_structure.js', 'indexes.js'));
/**
* Handle column moving
*/
if (isset($_REQUEST['move_columns']) && is_array($_REQUEST['move_columns']) && $this->response->isAjax()) {
$this->moveColumns();
return;
}
/**
* handle MySQL reserved words columns check
*/
if (isset($_REQUEST['reserved_word_check'])) {
if ($GLOBALS['cfg']['ReservedWordDisableWarning'] === false) {
$columns_names = $_REQUEST['field_name'];
$reserved_keywords_names = array();
foreach ($columns_names as $column) {
if (SqlParser\Context::isKeyword(trim($column), true)) {
$reserved_keywords_names[] = trim($column);
}
}
if (SqlParser\Context::isKeyword(trim($this->table), true)) {
$reserved_keywords_names[] = trim($this->table);
}
if (count($reserved_keywords_names) == 0) {
$this->response->isSuccess(false);
}
$this->response->addJSON('message', sprintf(_ngettext('The name \'%s\' is a MySQL reserved keyword.', 'The names \'%s\' are MySQL reserved keywords.', count($reserved_keywords_names)), implode(',', $reserved_keywords_names)));
} else {
$this->response->isSuccess(false);
}
return;
}
/**
* A click on Change has been made for one column
*/
if (isset($_REQUEST['change_column'])) {
$this->displayHtmlForColumnChange(null, 'tbl_structure.php');
return;
}
/**
* handle multiple field commands if required
*
* submit_mult_*_x comes from IE if <input type="img" ...> is used
*/
$submit_mult = $this->getMultipleFieldCommandType();
if (!empty($submit_mult)) {
if (isset($_REQUEST['selected_fld'])) {
if ($submit_mult == 'browse') {
// browsing the table displaying only selected columns
$this->displayTableBrowseForSelectedColumns($GLOBALS['goto'], $GLOBALS['pmaThemeImage']);
} else {
// handle multiple field commands
// handle confirmation of deleting multiple columns
$action = 'tbl_structure.php';
$GLOBALS['selected'] = $_REQUEST['selected_fld'];
list($what_ret, $query_type_ret, $is_unset_submit_mult, $mult_btn_ret, $centralColsError) = $this->getDataForSubmitMult($submit_mult, $_REQUEST['selected_fld'], $action);
//update the existing variables
// todo: refactor mult_submits.inc.php such as
// below globals are not needed anymore
if (isset($what_ret)) {
$GLOBALS['what'] = $what_ret;
global $what;
}
if (isset($query_type_ret)) {
$GLOBALS['query_type'] = $query_type_ret;
global $query_type;
}
if ($is_unset_submit_mult) {
unset($submit_mult);
}
if (isset($mult_btn_ret)) {
$GLOBALS['mult_btn'] = $mult_btn_ret;
global $mult_btn;
}
include 'libraries/mult_submits.inc.php';
/**
* if $submit_mult == 'change', execution will have stopped
* at this point
*/
if (empty($message)) {
$message = PMA_Message::success();
}
$this->response->addHTML(PMA_Util::getMessage($message, $sql_query));
}
} else {
//.........这里部分代码省略.........
作者:ryanfmurph
项目:phpmyadmi
/**
* Get all column names which are MySQL reserved words
*
* @return array
* @access public
*/
public function getReservedColumnNames()
{
$columns = $this->getColumns(false);
$return = array();
foreach ($columns as $column) {
$temp = explode('.', $column);
$column_name = $temp[2];
if (Context::isKeyword($column_name, true)) {
$return[] = $column_name;
}
}
return $return;
}
作者:TheBlackBloodyUnicor
项目:pico_wanderblo
/**
* @param Key $component The component to be built.
* @param array $options Parameters for building.
*
* @return string
*/
public static function build($component, array $options = array())
{
$ret = $component->type . ' ';
if (!empty($component->name)) {
$ret .= Context::escape($component->name) . ' ';
}
$columns = array();
foreach ($component->columns as $column) {
$tmp = Context::escape($column['name']);
if (isset($column['length'])) {
$tmp .= '(' . $column['length'] . ')';
}
$columns[] = $tmp;
}
$ret .= '(' . implode(',', $columns) . ') ' . $component->options;
return trim($ret);
}
作者:pous
项目:phpmyadmi
/**
* Index action
*
* @return void
*/
public function indexAction()
{
PageSettings::showGroup('TableStructure');
/**
* Function implementations for this script
*/
include_once 'libraries/check_user_privileges.lib.php';
include_once 'libraries/index.lib.php';
include_once 'libraries/sql.lib.php';
$this->response->getHeader()->getScripts()->addFiles(array('tbl_structure.js', 'indexes.js'));
/**
* Handle column moving
*/
if (isset($_REQUEST['move_columns']) && is_array($_REQUEST['move_columns']) && $this->response->isAjax()) {
$this->moveColumns();
return;
}
/**
* handle MySQL reserved words columns check
*/
if (isset($_REQUEST['reserved_word_check'])) {
if ($GLOBALS['cfg']['ReservedWordDisableWarning'] === false) {
$columns_names = $_REQUEST['field_name'];
$reserved_keywords_names = array();
foreach ($columns_names as $column) {
if (SqlParser\Context::isKeyword(trim($column), true)) {
$reserved_keywords_names[] = trim($column);
}
}
if (SqlParser\Context::isKeyword(trim($this->table), true)) {
$reserved_keywords_names[] = trim($this->table);
}
if (count($reserved_keywords_names) == 0) {
$this->response->setRequestStatus(false);
}
$this->response->addJSON('message', sprintf(_ngettext('The name \'%s\' is a MySQL reserved keyword.', 'The names \'%s\' are MySQL reserved keywords.', count($reserved_keywords_names)), implode(',', $reserved_keywords_names)));
} else {
$this->response->setRequestStatus(false);
}
return;
}
/**
* A click on Change has been made for one column
*/
if (isset($_REQUEST['change_column'])) {
$this->displayHtmlForColumnChange(null, 'tbl_structure.php');
return;
}
/**
* Adding or editing partitioning of the table
*/
if (isset($_REQUEST['edit_partitioning']) && !isset($_REQUEST['save_partitioning'])) {
$this->displayHtmlForPartitionChange();
return;
}
/**
* handle multiple field commands if required
*
* submit_mult_*_x comes from IE if <input type="img" ...> is used
*/
$submit_mult = $this->getMultipleFieldCommandType();
if (!empty($submit_mult)) {
if (isset($_REQUEST['selected_fld'])) {
if ($submit_mult == 'browse') {
// browsing the table displaying only selected columns
$this->displayTableBrowseForSelectedColumns($GLOBALS['goto'], $GLOBALS['pmaThemeImage']);
} else {
// handle multiple field commands
// handle confirmation of deleting multiple columns
$action = 'tbl_structure.php';
$GLOBALS['selected'] = $_REQUEST['selected_fld'];
list($what_ret, $query_type_ret, $is_unset_submit_mult, $mult_btn_ret, $centralColsError) = $this->getDataForSubmitMult($submit_mult, $_REQUEST['selected_fld'], $action);
//update the existing variables
// todo: refactor mult_submits.inc.php such as
// below globals are not needed anymore
if (isset($what_ret)) {
$GLOBALS['what'] = $what_ret;
global $what;
}
if (isset($query_type_ret)) {
$GLOBALS['query_type'] = $query_type_ret;
global $query_type;
}
if ($is_unset_submit_mult) {
unset($submit_mult);
}
if (isset($mult_btn_ret)) {
$GLOBALS['mult_btn'] = $mult_btn_ret;
global $mult_btn;
}
include 'libraries/mult_submits.inc.php';
/**
* if $submit_mult == 'change', execution will have stopped
* at this point
*/
//.........这里部分代码省略.........
作者:altesie
项目:FinalProjec
/**
* @param Reference $component The component to be built.
* @param array $options Parameters for building.
*
* @return string
*/
public static function build($component, array $options = array())
{
return trim(Context::escape($component->table) . ' (' . implode(', ', Context::escape($component->columns)) . ') ' . $component->options);
}
作者:altesie
项目:FinalProjec
/**
* @param CreateDefinition|CreateDefinition[] $component The component to be built.
* @param array $options Parameters for building.
*
* @return string
*/
public static function build($component, array $options = array())
{
if (is_array($component)) {
return "(\n " . implode(",\n ", $component) . "\n)";
} else {
$tmp = '';
if ($component->isConstraint) {
$tmp .= 'CONSTRAINT ';
}
if (isset($component->name) && $component->name !== '') {
$tmp .= Context::escape($component->name) . ' ';
}
if (!empty($component->type)) {
$tmp .= DataType::build($component->type, array('lowercase' => true)) . ' ';
}
if (!empty($component->key)) {
$tmp .= $component->key . ' ';
}
if (!empty($component->references)) {
$tmp .= 'REFERENCES ' . $component->references . ' ';
}
$tmp .= $component->options;
return trim($tmp);
}
}
作者:WSDC-NITWaranga
项目:phpmyadmi
/**
* @param Key $component The component to be built.
*
* @return string
*/
public static function build($component)
{
$ret = $component->type . ' ';
if (!empty($component->name)) {
$ret .= Context::escape($component->name) . ' ';
}
$ret .= '(' . implode(',', Context::escape($component->columns)) . ')';
$ret .= OptionsArray::build($component->options);
return trim($ret);
}
作者:ryanfmurph
项目:phpmyadmi
//.........这里部分代码省略.........
$old_table = $statement->name->table;
// Finding the aliased database name.
// The database might be empty so we have to add a few checks.
$new_database = null;
if (!empty($statement->name->database)) {
$new_database = $statement->name->database;
if (!empty($aliases[$old_database]['alias'])) {
$new_database = $aliases[$old_database]['alias'];
}
}
// Finding the aliases table name.
$new_table = $old_table;
if (!empty($aliases[$old_database]['tables'][$old_table]['alias'])) {
$new_table = $aliases[$old_database]['tables'][$old_table]['alias'];
}
// Replacing new values.
if ($statement->name->database !== $new_database || $statement->name->table !== $new_table) {
$statement->name->database = $new_database;
$statement->name->table = $new_table;
$statement->name->expr = null;
// Force rebuild.
$flag = true;
}
foreach ($statement->fields as $field) {
// Column name.
if (!empty($field->type)) {
if (!empty($aliases[$old_database]['tables'][$old_table]['columns'][$field->name])) {
$field->name = $aliases[$old_database]['tables'][$old_table]['columns'][$field->name];
$flag = true;
}
}
// Key's columns.
if (!empty($field->key)) {
foreach ($field->key->columns as $key => $column) {
if (!empty($aliases[$old_database]['tables'][$old_table]['columns'][$column['name']])) {
$field->key->columns[$key]['name'] = $aliases[$old_database]['tables'][$old_table]['columns'][$column['name']];
$flag = true;
}
}
}
// References.
if (!empty($field->references)) {
$ref_table = $field->references->table->table;
// Replacing table.
if (!empty($aliases[$old_database]['tables'][$ref_table]['alias'])) {
$field->references->table->table = $aliases[$old_database]['tables'][$ref_table]['alias'];
$field->references->table->expr = null;
$flag = true;
}
// Replacing column names.
foreach ($field->references->columns as $key => $column) {
if (!empty($aliases[$old_database]['tables'][$ref_table]['columns'][$column])) {
$field->references->columns[$key] = $aliases[$old_database]['tables'][$ref_table]['columns'][$column];
$flag = true;
}
}
}
}
} elseif ($statement->options->has('TRIGGER')) {
// Extracting the name of the old database and table from the
// statement to make sure the parameters are corect.
if (!empty($statement->table->database)) {
$old_database = $statement->table->database;
}
/**
* Old table name.
*
* @var string $old_table
*/
$old_table = $statement->table->table;
if (!empty($aliases[$old_database]['tables'][$old_table]['alias'])) {
$statement->table->table = $aliases[$old_database]['tables'][$old_table]['alias'];
$statement->table->expr = null;
// Force rebuild.
$flag = true;
}
}
if ($statement->options->has('TRIGGER') || $statement->options->has('PROCEDURE') || $statement->options->has('FUNCTION') || $statement->options->has('VIEW')) {
// Repalcing the body.
for ($i = 0, $count = count($statement->body); $i < $count; ++$i) {
/**
* Token parsed at this moment.
*
* @var Token $token
*/
$token = $statement->body[$i];
// Replacing only symbols (that are not variables) and unknown
// identifiers.
if ($token->type === Token::TYPE_SYMBOL && !($token->flags & Token::FLAG_SYMBOL_VARIABLE) || ($token->type === Token::TYPE_KEYWORD && !($token->flags & Token::FLAG_KEYWORD_RESERVED) || $token->type === Token::TYPE_NONE)) {
$alias = $this->getAlias($aliases, $token->value);
if (!empty($alias)) {
// Replacing the token.
$token->token = Context::escape($alias);
$flag = true;
}
}
}
}
return $statement->build();
}
作者:WSDC-NITWaranga
项目:phpmyadmi
/**
* @param ParameterDefinition[] $component The component to be built.
*
* @return string
*/
public static function build($component)
{
$ret = array();
foreach ($component as $c) {
$tmp = '';
if (!empty($c->inOut)) {
$tmp .= $c->inOut . ' ';
}
$ret[] = trim($tmp . Context::escape($c->name) . ' ' . DataType::build($c->type));
}
return '(' . implode(', ', $ret) . ')';
}
作者:WSDC-NITWaranga
项目:phpmyadmi
/**
* @param Reference $component The component to be built.
*
* @return string
*/
public static function build($component)
{
return trim(Context::escape($component->table) . ' (' . implode(', ', Context::escape($component->columns)) . ') ' . OptionsArray::build($component->options));
}
作者:dmitry-ph
项目:sql-parse
/**
* @expectedException Exception
* @expectedExceptionMessage Specified context ("\SqlParser\Contexts\ContextFoo") doesn't exist.
*/
public function testLoadError()
{
Context::load('Foo');
}
作者:blitze29
项目:phpmyadmi
//.........这里部分代码省略.........
if ($this->query[$i] === "\n") {
$this->status = 0;
}
continue;
} elseif ($this->status === static::STATUS_COMMENT_C) {
// C-like comments end in */.
if ($this->query[$i - 1] === '*' && $this->query[$i] === '/') {
$this->status = 0;
}
continue;
}
/*
* Checking if a string started.
*/
if ($this->query[$i] === '\'') {
$this->status = static::STATUS_STRING_SINGLE_QUOTES;
$this->current .= $this->query[$i];
continue;
} elseif ($this->query[$i] === '"') {
$this->status = static::STATUS_STRING_DOUBLE_QUOTES;
$this->current .= $this->query[$i];
continue;
} elseif ($this->query[$i] === '`') {
$this->status = static::STATUS_STRING_BACKTICK;
$this->current .= $this->query[$i];
continue;
}
/*
* Checking if a comment started.
*/
if ($this->query[$i] === '#') {
$this->status = static::STATUS_COMMENT_BASH;
continue;
} elseif ($i + 2 < $len && $this->query[$i] === '-' && $this->query[$i + 1] === '-' && Context::isWhitespace($this->query[$i + 2])) {
$this->status = static::STATUS_COMMENT_SQL;
continue;
} elseif ($i + 2 < $len && $this->query[$i] === '/' && $this->query[$i + 1] === '*' && $this->query[$i + 2] !== '!') {
$this->status = static::STATUS_COMMENT_C;
continue;
}
/*
* Handling `DELIMITER` statement.
*
* The code below basically checks for
* `strtoupper(substr($this->query, $i, 9)) === 'DELIMITER'`
*
* This optimization makes the code about 3 times faster.
*/
if ($i + 9 < $len && ($this->query[$i] === 'D' || $this->query[$i] === 'd') && ($this->query[$i + 1] === 'E' || $this->query[$i + 1] === 'e') && ($this->query[$i + 2] === 'L' || $this->query[$i + 2] === 'l') && ($this->query[$i + 3] === 'I' || $this->query[$i + 3] === 'i') && ($this->query[$i + 4] === 'M' || $this->query[$i + 4] === 'm') && ($this->query[$i + 5] === 'I' || $this->query[$i + 5] === 'i') && ($this->query[$i + 6] === 'T' || $this->query[$i + 6] === 't') && ($this->query[$i + 7] === 'E' || $this->query[$i + 7] === 'e') && ($this->query[$i + 8] === 'R' || $this->query[$i + 8] === 'r') && Context::isWhitespace($this->query[$i + 9])) {
// Saving the current index to be able to revert any parsing
// done in this block.
$iBak = $i;
$i += 9;
// Skipping `DELIMITER`.
// Skipping whitespaces.
while ($i < $len && Context::isWhitespace($this->query[$i])) {
++$i;
}
// Parsing the delimiter.
$delimiter = '';
while ($i < $len && !Context::isWhitespace($this->query[$i])) {
$delimiter .= $this->query[$i++];
}
// Checking if the delimiter definition ended.
if ($delimiter != '' && ($i < $len && Context::isWhitespace($this->query[$i]) || $i === $len && $end)) {
// Saving the delimiter.
作者:itgsod-philip-skalande
项目:phpmyadmi
/**
* @param ParameterDefinition[] $component The component to be built.
* @param array $options Parameters for building.
*
* @return string
*/
public static function build($component, array $options = array())
{
if (is_array($component)) {
return '(' . implode(', ', $component) . ')';
} else {
$tmp = '';
if (!empty($component->inOut)) {
$tmp .= $component->inOut . ' ';
}
return trim($tmp . Context::escape($component->name) . ' ' . $component->type);
}
}
作者:elrossco2
项目:sql-parse
public function testisSeparator()
{
$this->assertTrue(Context::isSeparator('+'));
$this->assertTrue(Context::isSeparator('.'));
$this->assertFalse(Context::isSeparator('1'));
$this->assertFalse(Context::isSeparator('E'));
$this->assertFalse(Context::isSeparator('_'));
}
作者:saisa
项目:phpmyadmi
/**
* @param FieldDefinition|FieldDefinition[] $component The component to be built.
*
* @return string
*/
public static function build($component)
{
if (is_array($component)) {
$ret = array();
foreach ($component as $c) {
$ret[] = static::build($c);
}
return "(\n" . implode(",\n", $ret) . "\n)";
} else {
$tmp = '';
if ($component->isConstraint) {
$tmp .= 'CONSTRAINT ';
}
if (!empty($component->name)) {
$tmp .= Context::escape($component->name) . ' ';
}
if (!empty($component->type)) {
$tmp .= DataType::build($component->type) . ' ';
}
if (!empty($component->key)) {
$tmp .= Key::build($component->key) . ' ';
}
if (!empty($component->references)) {
$tmp .= 'REFERENCES ' . Reference::build($component->references) . ' ';
}
$tmp .= OptionsArray::build($component->options);
return trim($tmp);
}
}
作者:nije
项目:phpmyadmi
/**
* Adds backquotes on both sides of a database, table or field name.
* in compatibility mode
*
* example:
* <code>
* echo backquoteCompat('owner`s db'); // `owner``s db`
*
* </code>
*
* @param mixed $a_name the database, table or field name to
* "backquote" or array of it
* @param string $compatibility string compatibility mode (used by dump
* functions)
* @param boolean $do_it a flag to bypass this function (used by dump
* functions)
*
* @return mixed the "backquoted" database, table or field name
*
* @access public
*/
public static function backquoteCompat(
$a_name,
$compatibility = 'MSSQL',
$do_it = true
) {
if (is_array($a_name)) {
foreach ($a_name as &$data) {
$data = self::backquoteCompat($data, $compatibility, $do_it);
}
return $a_name;
}
if (! $do_it) {
if (!Context::isKeyword($a_name)) {
return $a_name;
}
}
// @todo add more compatibility cases (ORACLE for example)
switch ($compatibility) {
case 'MSSQL':
$quote = '"';
break;
default:
$quote = "`";
break;
}
// '0' is also empty for php :-(
if (strlen($a_name) > 0 && $a_name !== '*') {
return $quote . $a_name . $quote;
} else {
return $a_name;
}
} // end of the 'backquoteCompat()' function
作者:WSDC-NITWaranga
项目:phpmyadmi
/**
* @param Expression $component The component to be built.
*
* @return string
*/
public static function build($component)
{
if (!empty($component->expr)) {
$ret = $component->expr;
} else {
$fields = array();
if (!empty($component->database)) {
$fields[] = $component->database;
}
if (!empty($component->table)) {
$fields[] = $component->table;
}
if (!empty($component->column)) {
$fields[] = $component->column;
}
$ret = implode('.', Context::escape($fields));
}
if (!empty($component->alias)) {
$ret .= ' AS ' . Context::escape($component->alias);
}
return $ret;
}
作者:wp-clou
项目:phpmyadmi
/**
* @param Expression|Expression[] $component The component to be built.
* @param array $options Parameters for building.
*
* @return string
*/
public static function build($component, array $options = array())
{
if (is_array($component)) {
return implode($component, ', ');
} else {
if ($component->expr !== '' && !is_null($component->expr)) {
$ret = $component->expr;
} else {
$fields = array();
if (isset($component->database) && $component->database !== '') {
$fields[] = $component->database;
}
if (isset($component->table) && $component->table !== '') {
$fields[] = $component->table;
}
if (isset($component->column) && $component->column !== '') {
$fields[] = $component->column;
}
$ret = implode('.', Context::escape($fields));
}
if (!empty($component->alias)) {
$ret .= ' AS ' . Context::escape($component->alias);
}
return $ret;
}
}