623
623
class Insert(Expr):
625
def __init__(self, columns, values, table=Undef, default_table=Undef):
626
self.columns = columns
624
"""Expression representing an insert statement.
626
@ivar map: Dictionary mapping columns to values.
627
@ivar table: Table where the row should be inserted.
628
@ivar default_table: Table to use if no table is explicitly provided, and
629
no tables may be inferred from provided columns.
630
@ivar primary_columns: Tuple of columns forming the primary key of the
631
table where the row will be inserted. This is a hint used by backends
632
to process the insertion of rows.
633
@ivar primary_variables: Tuple of variables with values for the primary
634
key of the table where the row will be inserted. This is a hint used
635
by backends to process the insertion of rows.
638
def __init__(self, map, table=Undef, default_table=Undef,
639
primary_columns=Undef, primary_variables=Undef):
628
641
self.table = table
629
642
self.default_table = default_table
643
self.primary_columns = primary_columns
644
self.primary_variables = primary_variables
631
646
@compile.when(Insert)
632
647
def compile_insert(compile, insert, state):
633
648
state.push("context", COLUMN_NAME)
634
columns = compile(insert.columns, state)
649
columns = compile(tuple(insert.map), state)
635
650
state.context = TABLE
636
651
table = build_tables(compile, insert.table, insert.default_table, state)
637
652
state.context = EXPR
638
values = compile(insert.values, state)
653
values = compile(tuple(insert.map.itervalues()), state)
640
655
return "".join(["INSERT INTO ", table, " (", columns,
641
656
") VALUES (", values, ")"])
644
659
class Update(Expr):
646
def __init__(self, set, where=Undef, table=Undef, default_table=Undef):
661
def __init__(self, map, where=Undef, table=Undef, default_table=Undef):
648
663
self.where = where
649
664
self.table = table
650
665
self.default_table = default_table
652
667
@compile.when(Update)
653
668
def compile_update(compile, update, state):
655
670
state.push("context", COLUMN_NAME)
656
sets = ["%s=%s" % (compile(col, state), compile(set[col], state))
671
sets = ["%s=%s" % (compile(col, state), compile(map[col], state))
658
673
state.context = TABLE
659
674
tokens = ["UPDATE ", build_tables(compile, update.table,
660
675
update.default_table, state),
1214
1229
# --------------------------------------------------------------------
1232
class Sequence(Expr):
1233
"""Expression representing auto-incrementing support from the database.
1235
This should be translated into the *next* value of the named
1236
auto-incrementing sequence. There's no standard way to compile a
1237
sequence, since it's very database-dependent.
1239
This may be used as follows:
1241
class Class(object):
1243
id = Int(default=Sequence("my_sequence_name"))
1246
def __init__(self, name):
1250
# --------------------------------------------------------------------
1215
1251
# Utility functions.
1217
1253
def compare_columns(columns, values):