Esempio n. 1
0
 def _create_column_name(self, token_queue):
     if len(token_queue) <= 0:
         raise SQLParseError(
             "Unable to get column name. token_queue is empty.")
     return sql.ColumnName(value=self._clean_identifiers_quote(
         token_queue.popleft().value),
                           ttype=T.Name)
Esempio n. 2
0
def validate_options(**options):
    """Validate options.
    """
    sql_dialect = options.get('sql_dialect')

    if sql_dialect and sql_dialect not in SQL_REGEX_WITH_DIALECT:
        raise SQLParseError('Invalid value for sql_dialect: '
                            '{0!r}'.format(sql_dialect))

    additional_keywords = options.get('additional_keywords')

    if additional_keywords:
        if not isinstance(additional_keywords, list):
            raise SQLParseError(
                'additional_keywords: '
                '{0!r} must be a list'.format(additional_keywords))
Esempio n. 3
0
 def _process_table_name(self, statement, cur_token_index=0):
     # 1st Name type is the table name
     table_name_token = statement.token_next_by_type(
         cur_token_index, T.Name)
     if not table_name_token:
         raise SQLParseError('Cannot find table name.')
     table_name_token_index = statement.token_index(table_name_token)
     table_name = self._clean_identifiers_quote(table_name_token.value)
     statement.tokens[table_name_token_index] = sql.TableName(
         value=table_name, ttype=T.Name)
     return table_name_token_index + 1
Esempio n. 4
0
 def _get_bit_default_value_token(self, token_queue, cur_token):
     # Refer to https://dev.mysql.com/doc/refman/5.5/en/bit-field-literals.html
     # for information of bit field literal
     if cur_token.normalized == u'b':
         # If default value of bit type is formatted as "b'001'", currently
         # it's split into two tokens: Name token (b) and Single token ('001').
         # So here it gets the next token for the actual default value.
         return token_queue.popleft()
     if cur_token.value == u'0':
         # If default value of bit type is formatted as "0b001", currently
         # it's split into two tokens: Integer token (0) and Name token ('b001').
         # So here it gets the next token to extract the actual default value.
         token = token_queue.popleft()
         token.value = token.value[1:]
         return token
     raise SQLParseError("Cannot get default value for BIT type.")
Esempio n. 5
0
    def _process_columns(self, statement):
        # Get the Parenthesis which contains column definitions
        parenthesis_token = statement.token_next_by_instance(
            0, sql.Parenthesis)
        if not parenthesis_token:
            raise SQLParseError('Cannot find column definitions')

        parenthesis_token_index = statement.token_index(parenthesis_token)
        columns_tokens = parenthesis_token.tokens
        columns_definition_tokens = []
        non_columns_definition_tokens = []
        for token_list in self._split_tokens_by_comma(columns_tokens[1:-1]):
            if self._is_column_definition(token_list):
                column_definition_token = self._create_column_definition(
                    token_list)
                columns_definition_tokens.append(column_definition_token)
            else:
                non_columns_definition_tokens.extend(token_list.tokens)
        columns_definition = sql.ColumnsDefinition(columns_definition_tokens)
        parenthesis_token_list = [columns_tokens[0], columns_definition]
        parenthesis_token_list.extend(non_columns_definition_tokens)
        parenthesis_token_list.append(columns_tokens[-1])
        statement.tokens[parenthesis_token_index:parenthesis_token_index +
                         1] = parenthesis_token_list
Esempio n. 6
0
def validate_options(options):
    """Validates options."""
    kwcase = options.get('keyword_case', None)
    if kwcase not in [None, 'upper', 'lower', 'capitalize']:
        raise SQLParseError('Invalid value for keyword_case: %r' % kwcase)

    idcase = options.get('identifier_case', None)
    if idcase not in [None, 'upper', 'lower', 'capitalize']:
        raise SQLParseError('Invalid value for identifier_case: %r' % idcase)

    ofrmt = options.get('output_format', None)
    if ofrmt not in [None, 'sql', 'python', 'php']:
        raise SQLParseError('Unknown output format: %r' % ofrmt)

    strip_comments = options.get('strip_comments', False)
    if strip_comments not in [True, False]:
        raise SQLParseError('Invalid value for strip_comments: %r' %
                            strip_comments)

    strip_ws = options.get('strip_whitespace', False)
    if strip_ws not in [True, False]:
        raise SQLParseError('Invalid value for strip_whitespace: %r' %
                            strip_ws)

    truncate_strings = options.get('truncate_strings', None)
    if truncate_strings is not None:
        try:
            truncate_strings = int(truncate_strings)
        except (ValueError, TypeError):
            raise SQLParseError('Invalid value for truncate_strings: %r' %
                                truncate_strings)
        if truncate_strings <= 1:
            raise SQLParseError('Invalid value for truncate_strings: %r' %
                                truncate_strings)
        options['truncate_strings'] = truncate_strings
        options['truncate_char'] = options.get('truncate_char', '[...]')

    reindent = options.get('reindent', False)
    if reindent not in [True, False]:
        raise SQLParseError('Invalid value for reindent: %r' % reindent)
    elif reindent:
        options['strip_whitespace'] = True
    indent_tabs = options.get('indent_tabs', False)
    if indent_tabs not in [True, False]:
        raise SQLParseError('Invalid value for indent_tabs: %r' % indent_tabs)
    elif indent_tabs:
        options['indent_char'] = '\t'
    else:
        options['indent_char'] = ' '
    indent_width = options.get('indent_width', 2)
    try:
        indent_width = int(indent_width)
    except (TypeError, ValueError):
        raise SQLParseError('indent_width requires an integer')
    if indent_width < 1:
        raise SQLParseError('indent_width requires an positive integer')
    options['indent_width'] = indent_width

    right_margin = options.get('right_margin', None)
    if right_margin is not None:
        try:
            right_margin = int(right_margin)
        except (TypeError, ValueError):
            raise SQLParseError('right_margin requires an integer')
        if right_margin < 10:
            raise SQLParseError('right_margin requires an integer > 10')
    options['right_margin'] = right_margin

    return options
Esempio n. 7
0
def validate_options(options):
    """Validates options."""
    kwcase = options.get('keyword_case')
    if kwcase not in [None, 'upper', 'lower', 'capitalize']:
        raise SQLParseError('Invalid value for keyword_case: '
                            '{0!r}'.format(kwcase))

    idcase = options.get('identifier_case')
    if idcase not in [None, 'upper', 'lower', 'capitalize']:
        raise SQLParseError('Invalid value for identifier_case: '
                            '{0!r}'.format(idcase))

    ofrmt = options.get('output_format')
    if ofrmt not in [None, 'sql', 'python', 'php']:
        raise SQLParseError('Unknown output format: ' '{0!r}'.format(ofrmt))

    strip_comments = options.get('strip_comments', False)
    if strip_comments not in [True, False]:
        raise SQLParseError('Invalid value for strip_comments: '
                            '{0!r}'.format(strip_comments))

    space_around_operators = options.get('use_space_around_operators', False)
    if space_around_operators not in [True, False]:
        raise SQLParseError('Invalid value for use_space_around_operators: '
                            '{0!r}'.format(space_around_operators))

    strip_ws = options.get('strip_whitespace', False)
    if strip_ws not in [True, False]:
        raise SQLParseError('Invalid value for strip_whitespace: '
                            '{0!r}'.format(strip_ws))

    truncate_strings = options.get('truncate_strings')
    if truncate_strings is not None:
        try:
            truncate_strings = int(truncate_strings)
        except (ValueError, TypeError):
            raise SQLParseError('Invalid value for truncate_strings: '
                                '{0!r}'.format(truncate_strings))
        if truncate_strings <= 1:
            raise SQLParseError('Invalid value for truncate_strings: '
                                '{0!r}'.format(truncate_strings))
        options['truncate_strings'] = truncate_strings
        options['truncate_char'] = options.get('truncate_char', '[...]')

    indent_columns = options.get('indent_columns', False)
    if indent_columns not in [True, False]:
        raise SQLParseError('Invalid value for indent_columns: '
                            '{0!r}'.format(indent_columns))
    elif indent_columns:
        options['reindent'] = True  # enforce reindent
    options['indent_columns'] = indent_columns

    reindent = options.get('reindent', False)
    if reindent not in [True, False]:
        raise SQLParseError('Invalid value for reindent: '
                            '{0!r}'.format(reindent))
    elif reindent:
        options['strip_whitespace'] = True

    reindent_aligned = options.get('reindent_aligned', False)
    if reindent_aligned not in [True, False]:
        raise SQLParseError('Invalid value for reindent_aligned: '
                            '{0!r}'.format(reindent))
    elif reindent_aligned:
        options['strip_whitespace'] = True

    indent_after_first = options.get('indent_after_first', False)
    if indent_after_first not in [True, False]:
        raise SQLParseError('Invalid value for indent_after_first: '
                            '{0!r}'.format(indent_after_first))
    options['indent_after_first'] = indent_after_first

    indent_tabs = options.get('indent_tabs', False)
    if indent_tabs not in [True, False]:
        raise SQLParseError('Invalid value for indent_tabs: '
                            '{0!r}'.format(indent_tabs))
    elif indent_tabs:
        options['indent_char'] = '\t'
    else:
        options['indent_char'] = ' '

    indent_width = options.get('indent_width', 2)
    try:
        indent_width = int(indent_width)
    except (TypeError, ValueError):
        raise SQLParseError('indent_width requires an integer')
    if indent_width < 1:
        raise SQLParseError('indent_width requires a positive integer')
    options['indent_width'] = indent_width

    wrap_after = options.get('wrap_after', 0)
    try:
        wrap_after = int(wrap_after)
    except (TypeError, ValueError):
        raise SQLParseError('wrap_after requires an integer')
    if wrap_after < 0:
        raise SQLParseError('wrap_after requires a positive integer')
    options['wrap_after'] = wrap_after

    comma_first = options.get('comma_first', False)
    if comma_first not in [True, False]:
        raise SQLParseError('comma_first requires a boolean value')
    options['comma_first'] = comma_first

    right_margin = options.get('right_margin')
    if right_margin is not None:
        try:
            right_margin = int(right_margin)
        except (TypeError, ValueError):
            raise SQLParseError('right_margin requires an integer')
        if right_margin < 10:
            raise SQLParseError('right_margin requires an integer > 10')
    options['right_margin'] = right_margin

    return options
Esempio n. 8
0
def validate_options(options):
    """Validates options."""
    kwcase = options.get("keyword_case")
    if kwcase not in [None, "upper", "lower", "capitalize"]:
        raise SQLParseError("Invalid value for keyword_case: "
                            "{!r}".format(kwcase))

    idcase = options.get("identifier_case")
    if idcase not in [None, "upper", "lower", "capitalize"]:
        raise SQLParseError("Invalid value for identifier_case: "
                            "{!r}".format(idcase))

    ofrmt = options.get("output_format")
    if ofrmt not in [None, "sql", "python", "php"]:
        raise SQLParseError("Unknown output format: " "{!r}".format(ofrmt))

    strip_comments = options.get("strip_comments", False)
    if strip_comments not in [True, False]:
        raise SQLParseError("Invalid value for strip_comments: "
                            "{!r}".format(strip_comments))

    space_around_operators = options.get("use_space_around_operators", False)
    if space_around_operators not in [True, False]:
        raise SQLParseError("Invalid value for use_space_around_operators: "
                            "{!r}".format(space_around_operators))

    strip_ws = options.get("strip_whitespace", False)
    if strip_ws not in [True, False]:
        raise SQLParseError("Invalid value for strip_whitespace: "
                            "{!r}".format(strip_ws))

    truncate_strings = options.get("truncate_strings")
    if truncate_strings is not None:
        try:
            truncate_strings = int(truncate_strings)
        except (ValueError, TypeError):
            raise SQLParseError("Invalid value for truncate_strings: "
                                "{!r}".format(truncate_strings))
        if truncate_strings <= 1:
            raise SQLParseError("Invalid value for truncate_strings: "
                                "{!r}".format(truncate_strings))
        options["truncate_strings"] = truncate_strings
        options["truncate_char"] = options.get("truncate_char", "[...]")

    indent_columns = options.get("indent_columns", False)
    if indent_columns not in [True, False]:
        raise SQLParseError("Invalid value for indent_columns: "
                            "{!r}".format(indent_columns))
    elif indent_columns:
        options["reindent"] = True  # enforce reindent
    options["indent_columns"] = indent_columns

    reindent = options.get("reindent", False)
    if reindent not in [True, False]:
        raise SQLParseError("Invalid value for reindent: "
                            "{!r}".format(reindent))
    elif reindent:
        options["strip_whitespace"] = True

    reindent_aligned = options.get("reindent_aligned", False)
    if reindent_aligned not in [True, False]:
        raise SQLParseError("Invalid value for reindent_aligned: "
                            "{!r}".format(reindent))
    elif reindent_aligned:
        options["strip_whitespace"] = True

    indent_after_first = options.get("indent_after_first", False)
    if indent_after_first not in [True, False]:
        raise SQLParseError("Invalid value for indent_after_first: "
                            "{!r}".format(indent_after_first))
    options["indent_after_first"] = indent_after_first

    indent_tabs = options.get("indent_tabs", False)
    if indent_tabs not in [True, False]:
        raise SQLParseError("Invalid value for indent_tabs: "
                            "{!r}".format(indent_tabs))
    elif indent_tabs:
        options["indent_char"] = "\t"
    else:
        options["indent_char"] = " "

    indent_width = options.get("indent_width", 2)
    try:
        indent_width = int(indent_width)
    except (TypeError, ValueError):
        raise SQLParseError("indent_width requires an integer")
    if indent_width < 1:
        raise SQLParseError("indent_width requires a positive integer")
    options["indent_width"] = indent_width

    wrap_after = options.get("wrap_after", 0)
    try:
        wrap_after = int(wrap_after)
    except (TypeError, ValueError):
        raise SQLParseError("wrap_after requires an integer")
    if wrap_after < 0:
        raise SQLParseError("wrap_after requires a positive integer")
    options["wrap_after"] = wrap_after

    comma_first = options.get("comma_first", False)
    if comma_first not in [True, False]:
        raise SQLParseError("comma_first requires a boolean value")
    options["comma_first"] = comma_first

    right_margin = options.get("right_margin")
    if right_margin is not None:
        try:
            right_margin = int(right_margin)
        except (TypeError, ValueError):
            raise SQLParseError("right_margin requires an integer")
        if right_margin < 10:
            raise SQLParseError("right_margin requires an integer > 10")
    options["right_margin"] = right_margin

    return options
Esempio n. 9
0
 def _create_column_type(self, token_queue):
     if len(token_queue) <= 0:
         raise SQLParseError(
             "Unable to get column type. token_queue is empty.")
     token = token_queue.popleft()
     return sql.ColumnType(value=token.value, ttype=T.Keyword)