예제 #1
0
    def ensure_sqlparse(
            sql: Union[sqlparse.sql.Statement, str]) -> sqlparse.sql.Statement:
        """Returns a :class:`sqlparse.sql.Statement` from ``sql``.

        Will return ``sql`` with no modification if it's already a
        :mod:`sqlparse.sql` object.

        Needed to accommodate dynamic addition of statements as strings to
        an existing :class:`~snowmobile.Script` object from
        from raw strings as opposed to a :class:`sqlparse.sql.Statement`
        objects as is done when reading a sql file.

        Args:
            sql (Union[sqlparse.sql.Statement, str]):
                Either a string of sql or an already parsed
                sqlparse.sql.Statement object.

        Returns (sqlparse.sql.Statement):
            A parsed sql statement.

        """
        if isinstance(sql, sqlparse.sql.Statement):
            return sql

        parsed = [
            s for s in sqlparse.parsestream(stream=sql.strip().strip(";"))
            if not s.value.isspace()
        ]
        assert (
            parsed
        ), f'sqlparse.parsestream("""\n{sql}"""\n) returned an empty statement.'
        return parsed[0]
예제 #2
0
    def test_split_stream(self):
        import types
        from cStringIO import StringIO

        stream = StringIO("SELECT 1; SELECT 2;")
        stmts = sqlparse.parsestream(stream)
        self.assertEqual(type(stmts), types.GeneratorType)
        self.assertEqual(len(list(stmts)), 2)
예제 #3
0
    def test_split_stream(self):
        import types
        from cStringIO import StringIO

        stream = StringIO("SELECT 1; SELECT 2;")
        stmts = sqlparse.parsestream(stream)
        self.assertEqual(type(stmts), types.GeneratorType)
        self.assertEqual(len(list(stmts)), 2)
def execute(engine, file_path):
    with open(file_path) as f:
        for parsed_statement in parsestream(f):
            statement = stringtype(parsed_statement).strip()
            if statement == '':
                continue
            if log is not None:
                log.info("Running: {statement}".format(statement=statement))
            try:
                engine.execute(statement)
            except ProgrammingError as e:
                if log is not None:
                    log.warning(e.message)
                else:
                    raise e
예제 #5
0
def _get_statements(path):
    '''
    Get statements from file
    '''
    with codecs.open(path, encoding='utf-8') as i:
        data = i.read()
    if u'/* pgmigrate-encoding: utf-8 */' not in data:
        try:
            data.encode('ascii')
        except UnicodeError as exc:
            raise MalformedStatement(
                'Non ascii symbols in file: {0}, {1}'.format(path, text(exc)))
    for statement in sqlparse.parsestream(data, encoding='utf-8'):
        st_str = text(statement).strip().encode('utf-8')
        if st_str:
            yield st_str
예제 #6
0
def _get_statements(path):
    """
    Get statements from file
    """
    with codecs.open(path, encoding='utf-8') as i:
        data = i.read()
    if MIGRATION_DEFAULT_ENCODING not in data:
        try:
            data.encode('ascii')
        except UnicodeError as exc:
            raise MalformedStatement(
                'Non ascii symbols in file: {0}, {1}'.format(path, text(exc)))
    data = sqlparse.format(data, strip_comments=True)
    for statement in sqlparse.parsestream(data, encoding='utf-8'):
        st_str = text(statement).strip().encode('utf-8')
        if st_str:
            yield st_str
예제 #7
0
def test_split_unicode_parsestream():
    stream = StringIO(u'SELECT ö')
    stmts = list(sqlparse.parsestream(stream))
    assert str(stmts[0]) == 'SELECT ö'
예제 #8
0
def test_split_encoding_parsestream():
    stream = StringIO("SELECT 1; SELECT 2;")
    stmts = list(sqlparse.parsestream(stream))
    assert isinstance(stmts[0].tokens[0].value, text_type)
예제 #9
0
def test_split_stream():
    stream = StringIO("SELECT 1; SELECT 2;")
    stmts = sqlparse.parsestream(stream)
    assert isinstance(stmts, types.GeneratorType)
    assert len(list(stmts)) == 2
예제 #10
0
def test_split_unicode_parsestream():
    stream = StringIO(u'SELECT ö')
    stmts = list(sqlparse.parsestream(stream))
    assert str(stmts[0]) == 'SELECT ö'
예제 #11
0
def test_split_encoding_parsestream():
    stream = StringIO("SELECT 1; SELECT 2;")
    stmts = list(sqlparse.parsestream(stream))
    assert isinstance(stmts[0].tokens[0].value, text_type)
예제 #12
0
 def test_encoding_parsestream(self):
     stream = StringIO("SELECT 1; SELECT 2;")
     stmts = list(sqlparse.parsestream(stream))
     self.assertEqual(type(stmts[0].tokens[0].value), text_type)
예제 #13
0
파일: stream.py 프로젝트: gschaden/dbmanagr
# -*- coding: utf-8 -*-
#
# Copyright © 2014 René Samselnig
#
# This file is part of Database Navigator.
#
# Database Navigator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Database Navigator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Database Navigator.  If not, see <http://www.gnu.org/licenses/>.
#

import sys
import os
import sqlparse

#newin = os.fdopen(sys.stdin.fileno(), 'r', 1)

s = sqlparse.parsestream(sys.stdin)

for i in s:
    print i
예제 #14
0
 def test_encoding_parsestream(self):
     stream = StringIO("SELECT 1; SELECT 2;")
     stmts = list(sqlparse.parsestream(stream))
     self.assertEqual(type(stmts[0].tokens[0].value), text_type)
예제 #15
0
파일: download.py 프로젝트: carpedm20/Bias
"""
Downloads the following:
- Korean Wikipedia texts
- Korean 

"""

from sqlparse import parsestream
from sqlparse.sql import Parenthesis

for statement in parsestream(open('data/test.sql')):
    texts = [str(token.tokens[1].tokens[-1]).decode('string_escape') for token in statement.tokens if isinstance(token, Parenthesis)]
    print texts
    texts = [text for text in texts if text[0] != '#']
    if texts:
        print "\n===\n".join(texts)
예제 #16
0
파일: download.py 프로젝트: carpedm20/Bias
"""
Downloads the following:
- Korean Wikipedia texts
- Korean 

"""

from sqlparse import parsestream
from sqlparse.sql import Parenthesis

for statement in parsestream(open('data/test.sql')):
    texts = [
        str(token.tokens[1].tokens[-1]).decode('string_escape')
        for token in statement.tokens if isinstance(token, Parenthesis)
    ]
    print texts
    texts = [text for text in texts if text[0] != '#']
    if texts:
        print "\n===\n".join(texts)
예제 #17
0
 def test_encoding_parsestream(self):
     from cStringIO import StringIO
     stream = StringIO("SELECT 1; SELECT 2;")
     stmts = list(sqlparse.parsestream(stream))
     self.assertEqual(type(stmts[0].tokens[0].value), unicode)
예제 #18
0
def test_split_stream():
    stream = StringIO("SELECT 1; SELECT 2;")
    stmts = sqlparse.parsestream(stream)
    assert isinstance(stmts, types.GeneratorType)
    assert len(list(stmts)) == 2
예제 #19
0
 def test_encoding_parsestream(self):
     from cStringIO import StringIO
     stream = StringIO("SELECT 1; SELECT 2;")
     stmts = list(sqlparse.parsestream(stream))
     self.assertEqual(type(stmts[0].tokens[0].value), unicode)
예제 #20
0
 def source_stream(self) -> sqlparse.sql.Statement:
     """Parses source sql into individual statements."""
     for s in sqlparse.parsestream(stream=self.source):
         if self.sn.cfg.script.is_valid_sql(s=s):
             yield s