Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
48178920
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
48178920
编写于
6月 18, 2012
作者:
A
Alexey Milovidov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dbms: development [#CONV-2944].
上级
9a18d75b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
101 addition
and
0 deletion
+101
-0
dbms/src/Interpreters/InterpreterRenameQuery.cpp
dbms/src/Interpreters/InterpreterRenameQuery.cpp
+101
-0
未找到文件。
dbms/src/Interpreters/InterpreterRenameQuery.cpp
0 → 100644
浏览文件 @
48178920
#include <Poco/File.h>
#include <Poco/FileStream.h>
#include <DB/Common/escapeForFileName.h>
#include <DB/IO/ReadBufferFromFile.h>
#include <DB/IO/WriteBufferFromString.h>
#include <DB/IO/copyData.h>
#include <DB/Parsers/ASTRenameQuery.h>
#include <DB/Parsers/ParserCreateQuery.h>
#include <DB/Parsers/formatAST.h>
#include <DB/Interpreters/InterpreterRenameQuery.h>
namespace
DB
{
InterpreterRenameQuery
::
InterpreterRenameQuery
(
ASTPtr
query_ptr_
,
Context
&
context_
)
:
query_ptr
(
query_ptr_
),
context
(
context_
)
{
}
void
InterpreterRenameQuery
::
execute
()
{
/** Все таблицы переименовываются под глобальной блокировкой. */
Poco
::
ScopedLock
<
Poco
::
Mutex
>
lock
(
*
context
.
mutex
);
ASTRenameQuery
&
rename
=
dynamic_cast
<
ASTRenameQuery
&>
(
*
query_ptr
);
/** Если в процессе переименования произошла ошибка, то может быть переименована только часть таблиц,
* или состояние может стать неконсистентным. (Это имеет смысл исправить.)
*/
for
(
ASTRenameQuery
::
Elements
::
const_iterator
it
=
rename
.
elements
.
begin
();
it
!=
rename
.
elements
.
end
();
++
it
)
{
String
from_database_name
=
it
->
from
.
database
.
empty
()
?
context
.
current_database
:
it
->
from
.
database
;
String
from_database_name_escaped
=
escapeForFileName
(
from_database_name
);
String
from_table_name
=
it
->
from
.
table
;
String
from_table_name_escaped
=
escapeForFileName
(
from_table_name
);
String
from_metadata_path
=
context
.
path
+
"metadata/"
+
from_database_name_escaped
+
"/"
+
(
!
from_table_name
.
empty
()
?
from_table_name_escaped
+
".sql"
:
""
);
String
to_database_name
=
it
->
to
.
database
.
empty
()
?
context
.
current_database
:
it
->
to
.
database
;
String
to_database_name_escaped
=
escapeForFileName
(
to_database_name
);
String
to_table_name
=
it
->
to
.
table
;
String
to_table_name_escaped
=
escapeForFileName
(
to_table_name
);
String
to_metadata_path
=
context
.
path
+
"metadata/"
+
to_database_name_escaped
+
"/"
+
(
!
to_table_name
.
empty
()
?
to_table_name_escaped
+
".sql"
:
""
);
context
.
assertExists
(
from_database_name
,
from_table_name
);
context
.
assertDoesntExist
(
to_database_name
,
to_table_name
);
/// Уведомляем таблицу о том, что она переименовается.
(
*
context
.
databases
)[
from_database_name
][
from_table_name
]
->
rename
(
context
.
path
+
"data/"
+
to_database_name_escaped
+
"/"
,
to_table_name
);
/// Пишем новый файл с метаданными.
{
String
create_query
;
{
ReadBufferFromFile
in
(
from_metadata_path
,
1024
);
WriteBufferFromString
out
(
create_query
);
copyData
(
in
,
out
);
}
ParserCreateQuery
parser
;
const
char
*
pos
=
create_query
.
data
();
const
char
*
end
=
pos
+
create_query
.
size
();
ASTPtr
ast
;
String
expected
;
bool
parse_res
=
parser
.
parse
(
pos
,
end
,
ast
,
expected
);
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
if
(
!
parse_res
||
(
pos
!=
end
&&
*
pos
!=
';'
))
throw
Exception
(
"Syntax error in file "
+
from_metadata_path
+
": failed at position "
+
Poco
::
NumberFormatter
::
format
(
pos
-
create_query
.
data
())
+
": "
+
std
::
string
(
pos
,
std
::
min
(
SHOW_CHARS_ON_SYNTAX_ERROR
,
end
-
pos
))
+
", expected "
+
(
parse_res
?
"end of query"
:
expected
)
+
"."
,
ErrorCodes
::
SYNTAX_ERROR
);
dynamic_cast
<
ASTCreateQuery
&>
(
*
ast
).
table
=
to_table_name
;
Poco
::
FileOutputStream
ostr
(
to_metadata_path
);
formatAST
(
*
ast
,
ostr
,
0
,
false
);
ostr
<<
'\n'
;
}
/// Переименовываем таблицу в контексте.
StoragePtr
table
=
(
*
context
.
databases
)[
from_database_name
][
from_table_name
];
(
*
context
.
databases
)[
from_database_name
].
erase
((
*
context
.
databases
)[
from_database_name
].
find
(
from_table_name
));
(
*
context
.
databases
)[
to_database_name
][
to_table_name
]
=
table
;
/// Удаляем старый файл с метаданными.
Poco
::
File
(
from_metadata_path
).
remove
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录