-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
239 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ def export(defn): | |
|
||
from . import dbcommands | ||
from . import iocommands | ||
|
||
delimiter = iocommands.delimiter_command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
import re | ||
import sqlparse | ||
|
||
|
||
class DelimiterCommand(object): | ||
def __init__(self): | ||
self._delimiter = ';' | ||
|
||
def _split(self, sql): | ||
"""Temporary workaround until sqlparse.split() learns about custom | ||
delimiters.""" | ||
|
||
placeholder = "\ufffc" # unicode object replacement character | ||
|
||
if self._delimiter == ';': | ||
return sqlparse.split(sql) | ||
|
||
# We must find a string that original sql does not contain. | ||
# Most likely, our placeholder is enough, but if not, keep looking | ||
while placeholder in sql: | ||
placeholder += placeholder[0] | ||
sql = sql.replace(';', placeholder) | ||
sql = sql.replace(self._delimiter, ';') | ||
|
||
split = sqlparse.split(sql) | ||
|
||
return [ | ||
stmt.replace(';', self._delimiter).replace(placeholder, ';') | ||
for stmt in split | ||
] | ||
|
||
def queries_iter(self, input): | ||
"""Iterate over queries in the input string.""" | ||
|
||
queries = self._split(input) | ||
while queries: | ||
for sql in queries: | ||
delimiter = self._delimiter | ||
sql = queries.pop(0) | ||
if sql.endswith(delimiter): | ||
trailing_delimiter = True | ||
sql = sql.strip(delimiter) | ||
else: | ||
trailing_delimiter = False | ||
|
||
yield sql | ||
|
||
# if the delimiter was changed by the last command, | ||
# re-split everything, and if we previously stripped | ||
# the delimiter, append it to the end | ||
if self._delimiter != delimiter: | ||
combined_statement = ' '.join([sql] + queries) | ||
if trailing_delimiter: | ||
combined_statement += delimiter | ||
queries = self._split(combined_statement)[1:] | ||
|
||
def set(self, arg, **_): | ||
"""Change delimiter. | ||
Since `arg` is everything that follows the DELIMITER token | ||
after sqlparse (it may include other statements separated by | ||
the new delimiter), we want to set the delimiter to the first | ||
word of it. | ||
""" | ||
match = arg and re.search(r'[^\s]+', arg) | ||
if not match: | ||
message = 'Missing required argument, delimiter' | ||
return [(None, None, None, message)] | ||
|
||
delimiter = match.group() | ||
if delimiter.lower() == 'delimiter': | ||
return [(None, None, None, 'Invalid delimiter "delimiter"')] | ||
|
||
self._delimiter = delimiter | ||
return [(None, None, None, "Changed delimiter to {}".format(delimiter))] | ||
|
||
@property | ||
def current(self): | ||
return self._delimiter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.