如何清理原始SQL文件中的注释
我在处理一个已经存在的SQL文件时遇到了问题,想要清理掉里面的注释和空行。这个文件有超过一万行,所以手动清理根本不现实。
我有一个小的Python脚本,但我不知道怎么处理多行插入语句中的注释。
代码:
f = file( 'file.sql', 'r' )
t = filter( lambda x: not x.startswith('--') \
and not x.isspace()
, f.readlines() )
f.close()
t #<- here the cleaned data should be
应该如何工作:
这部分应该被清理掉:
-- normal sql comment
这部分应该保持原样:
CREATE FUNCTION func1(a integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- comment
[...]
END;
$$;
INSERT INTO public.texts (multilinetext) VALUES ('
and more lines here \'
-- part of text
\'
[...]
');
5 个回答
1
这是对samplebias回答的一个扩展,适用于你的例子:
import sqlparse
sql_example = """--comment
SELECT * from test;
INSERT INTO test VALUES ('
-- test
a
');
"""
new_sql = []
for statement in sqlparse.parse(sql_example):
new_tockens = [stm for stm in statement.tokens
if not isinstance(stm, sqlparse.sql.Comment)]
new_statement = sqlparse.sql.TokenList(new_tockens)
new_sql.append(new_statement.to_unicode())
print sqlparse.format("\n".join(new_sql))
输出结果:
SELECT * from test;
INSERT INTO test VALUES ('
-- test
a
');
15
更新一下我的回答 :)
import sqlparse
sql_example = """--comment
SELECT * from test;
INSERT INTO test VALUES ('
-- test
a
');
"""
print sqlparse.format(sql_example, strip_comments=True).strip()
输出结果:
SELECT * from test; INSERT INTO test VALUES (' -- test a ');
这个方法能得到相同的结果,而且还考虑了其他一些特殊情况,写得也更简洁。
10
试试这个叫做 sqlparse 的模块。
更新的例子:在插入值里留下注释,以及在创建函数的代码块中添加注释。你可以进一步调整来改变它的行为:
import sqlparse
from sqlparse import tokens
queries = '''
CREATE FUNCTION func1(a integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- comment
END;
$$;
SELECT -- comment
* FROM -- comment
TABLE foo;
-- comment
INSERT INTO foo VALUES ('a -- foo bar');
INSERT INTO foo
VALUES ('
a
-- foo bar'
);
'''
IGNORE = set(['CREATE FUNCTION',]) # extend this
def _filter(stmt, allow=0):
ddl = [t for t in stmt.tokens if t.ttype in (tokens.DDL, tokens.Keyword)]
start = ' '.join(d.value for d in ddl[:2])
if ddl and start in IGNORE:
allow = 1
for tok in stmt.tokens:
if allow or not isinstance(tok, sqlparse.sql.Comment):
yield tok
for stmt in sqlparse.split(queries):
sql = sqlparse.parse(stmt)[0]
print sqlparse.sql.TokenList([t for t in _filter(sql)])
输出结果:
CREATE FUNCTION func1(a integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- comment
END;
$$;
SELECT * FROM TABLE foo;
INSERT INTO foo VALUES ('a -- foo bar');
INSERT INTO foo
VALUES ('
a
-- foo bar'
);