Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Python by asdf ( 7 years ago )
# int = exactly this number of arguments
# tuple = between n1 and n2 args, accept as numbered params
# empty list = accept any number of arguments, accept as 1 tuple/list
def select_expr(eval, cols, frm, js, where, group, order):
q = f"SELECT {eval(cols)} FROM {eval(frm)} {eval(js)} "
if where:
q = f"{q} {eval(where)} "
if group:
q = f"{q} {eval(group)} "
if order:
q = f"{q} {eval(order)} "
return q
elements = {
'TABLE': (1, lambda r, out: out),
'ALIAS': (2, lambda r, out1, out2: f"({r(out1)}) AS {out2} "),
'SELECT': (6, select_expr),
'JOINS': ("*", lambda r, js: " ".join([f"{r(j)} " for j in js]))
}
def element_count(element):
return elements[element][0]
def substitution_rule(element):
return elements[element][1]
def cls_mth_maker(key, n_elements):
if isinstance(n_elements, int):
@classmethod
def cm(cls, *args):
assert len(args) == n_elements
return key.upper(), args[0] if len(args) == 1 else args
elif isinstance(n_elements, list):
@classmethod
def cm(cls, *args):
assert len(n_elements) == 0 \
or ((n_elements[0] == "*" or len(args) >= n_elements[0])
and n_elements[-1] == "*" or len(args) <= n_elements[-1])
return key.upper(), args
elif isinstance(n_elements, tuple):
@classmethod
def cm(cls, *args):
assert len(n_elements) == 0 \
or ((n_elements[0] == "*" or len(args) >= n_elements[0])
and n_elements[-1] == "*" or len(args) <= n_elements[-1])
return (key.upper(), *args)
else:
raise ValueError()
return cm
class SQL:
pass
for x in elements:
setattr(SQL, x, cls_mth_maker(x, elements[x]))
Revise this Paste