Skip to content

Commit

Permalink
Use __class__ look up instead of name
Browse files Browse the repository at this point in the history
.. because you know it's Python.
  • Loading branch information
paetzke committed Sep 7, 2015
1 parent cfe47fe commit 39018e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
19 changes: 1 addition & 18 deletions format_sql/parser.py
Expand Up @@ -137,28 +137,24 @@ def __repr__(self):


class Semicolon(Value):
name = 'SEMICOLON'

def __repr__(self):
return 'Semicolon(%s)' % self.value


class Is(SingleValue):
name = 'IS'

def __repr__(self):
return 'Is(%s)' % self.value


class Null(SingleValue):
name = 'NULL'

def __repr__(self):
return 'Null(%s)' % self.value


class GroupBy:
name = 'GROUP BY'

def __init__(self, values, with_rollup=None):
self.values = values
Expand All @@ -172,14 +168,12 @@ def __repr__(self):


class From(SingleAndListValue):
name = 'FROM'

def __repr__(self):
return 'From(%s, values=%s)' % (self.value, self.values)


class Func:
name = 'FUNC'

def __init__(self, name, args, as_=None, alias=None):
self.name = name
Expand All @@ -192,7 +186,6 @@ def __eq__(self, other):


class Having:
name = 'HAVING'

def __init__(self, value, values):
self.value = value
Expand All @@ -212,7 +205,6 @@ def __repr__(self):


class Insert:
name = 'INSERT'

def __init__(self, insert, table, values=None, cols=None, select=None):
self.insert = insert
Expand All @@ -229,7 +221,6 @@ def __repr__(self):


class Values:
name = 'VALUES'

def __init__(self, value, values):
self.value = value
Expand All @@ -243,7 +234,6 @@ def __repr__(self):


class Limit:
name = 'LIMIT'

def __init__(self, row_count, offset=None, offset_keyword=None):
self.row_count = row_count
Expand All @@ -258,7 +248,6 @@ def __repr__(self):


class Link(SingleValue):
name = 'LINK'

def __repr__(self):
return 'Link(%s)' % self.value
Expand All @@ -268,18 +257,16 @@ def __str__(self):


class Not(SingleValue):
name = 'NOT'
pass


class On(SingleAndListValue):
name = 'ON'

def __repr__(self):
return 'On(%s, %s)' % (self.value, self.values)


class Operator(SingleValue):
name = 'OPERATOR'

def __repr__(self):
return 'Operator(%s)' % self.value
Expand All @@ -289,7 +276,6 @@ def __str__(self):


class OrderBy:
name = 'ORDER BY'

def __init__(self, values):
self.values = values
Expand All @@ -302,7 +288,6 @@ def __repr__(self):


class Condition:
name = 'condition'

def __init__(self, values):
self.values = values
Expand All @@ -315,7 +300,6 @@ def __repr__(self):


class Select(SingleAndListValue):
name = 'SELECT'

def __repr__(self):
return 'Select(%s, %s)' % (self.value, self.values)
Expand All @@ -331,7 +315,6 @@ def __eq__(self, other):


class Where:
name = 'WHERE'

def __init__(self, value, conditions):
self.value = value
Expand Down
20 changes: 10 additions & 10 deletions format_sql/styler.py
Expand Up @@ -313,19 +313,19 @@ def style(statements, indent=0, keyword_upper=True, liner=None):
liner = Liner()

structures = {
From.name: _style_from,
GroupBy.name: _style_group_by,
Having.name: _style_having,
Insert.name: _style_insert,
Limit.name: _style_limit,
OrderBy.name: _style_order_by,
Select.name: _style_select,
Semicolon.name: _style_semicolon,
Where.name: _style_where,
From: _style_from,
GroupBy: _style_group_by,
Having: _style_having,
Insert: _style_insert,
Limit: _style_limit,
OrderBy: _style_order_by,
Select: _style_select,
Semicolon: _style_semicolon,
Where: _style_where,
}

for i, statement in enumerate(statements):
func = structures[statement.name]
func = structures[statement.__class__]
try:
func(statement, liner=liner, indent=indent)
except IndexError:
Expand Down

0 comments on commit 39018e0

Please sign in to comment.