~sidnei/storm/mssql-support

« back to all changes in this revision

Viewing changes to storm/expr.py

  • Committer: Gustavo Niemeyer
  • Date: 2007-08-07 23:15:09 UTC
  • mfrom: (156.1.8 preset-primary-key)
  • Revision ID: gustavo@niemeyer.net-20070807231509-iw58uiygzuef5wth
Merged preset-primary-key branch [r=radix,jkakar] [f=126080]

This branch introduces a new hook which allows backends to modify
variables which are part of the primary key of an object before
they are inserted in the database.  Also, lazy values may now be
used in the primary key, which means that it's possible to set these
primary variables to SQL expressions if needed.

Other minor changes include an optimization in Result: it will
preset the raw cursor's arraysize to a better value if it's found
to be 1.  Result.__iter__() was also fixed to parse rows through
the from_database hook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
621
621
 
622
622
 
623
623
class Insert(Expr):
624
 
 
625
 
    def __init__(self, columns, values, table=Undef, default_table=Undef):
626
 
        self.columns = columns
627
 
        self.values = values
 
624
    """Expression representing an insert statement.
 
625
 
 
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.
 
636
    """
 
637
 
 
638
    def __init__(self, map, table=Undef, default_table=Undef,
 
639
                 primary_columns=Undef, primary_variables=Undef):
 
640
        self.map = map
628
641
        self.table = table
629
642
        self.default_table = default_table
 
643
        self.primary_columns = primary_columns
 
644
        self.primary_variables = primary_variables
630
645
 
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)
639
654
    state.pop()
640
655
    return "".join(["INSERT INTO ", table, " (", columns,
641
656
                    ") VALUES (", values, ")"])
643
658
 
644
659
class Update(Expr):
645
660
 
646
 
    def __init__(self, set, where=Undef, table=Undef, default_table=Undef):
647
 
        self.set = set
 
661
    def __init__(self, map, where=Undef, table=Undef, default_table=Undef):
 
662
        self.map = map
648
663
        self.where = where
649
664
        self.table = table
650
665
        self.default_table = default_table
651
666
 
652
667
@compile.when(Update)
653
668
def compile_update(compile, update, state):
654
 
    set = update.set
 
669
    map = update.map
655
670
    state.push("context", COLUMN_NAME)
656
 
    sets = ["%s=%s" % (compile(col, state), compile(set[col], state))
657
 
            for col in set]
 
671
    sets = ["%s=%s" % (compile(col, state), compile(map[col], state))
 
672
            for col in map]
658
673
    state.context = TABLE
659
674
    tokens = ["UPDATE ", build_tables(compile, update.table,
660
675
                                      update.default_table, state),
1212
1227
 
1213
1228
 
1214
1229
# --------------------------------------------------------------------
 
1230
# Sequences.
 
1231
 
 
1232
class Sequence(Expr):
 
1233
    """Expression representing auto-incrementing support from the database.
 
1234
 
 
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.
 
1238
 
 
1239
    This may be used as follows:
 
1240
 
 
1241
      class Class(object):
 
1242
          (...)
 
1243
          id = Int(default=Sequence("my_sequence_name"))
 
1244
    """
 
1245
 
 
1246
    def __init__(self, name):
 
1247
        self.name = name
 
1248
 
 
1249
 
 
1250
# --------------------------------------------------------------------
1215
1251
# Utility functions.
1216
1252
 
1217
1253
def compare_columns(columns, values):