~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/database/sqlbase.py

[r=intellectronica][ui=none] extend BugtaskSet.search() to look up
        hardware related bug tasks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import pytz
15
15
import storm
16
16
from storm.databases.postgres import compile as postgres_compile
 
17
from storm.expr import State
 
18
from storm.expr import compile as storm_compile
17
19
from storm.locals import Storm, Store
18
20
from storm.zope.interfaces import IZStorm
19
21
 
38
40
    'commit',
39
41
    'ConflictingTransactionManagerError',
40
42
    'connect',
 
43
    'convert_storm_clause_to_string',
41
44
    'cursor',
42
45
    'expire_from_cache',
43
46
    'flush_database_caches',
592
595
quoteIdentifier = quote_identifier # Backwards compatibility for now.
593
596
 
594
597
 
 
598
def convert_storm_clause_to_string(storm_clause):
 
599
    """Convert a Storm expression into a plain string.
 
600
 
 
601
    :param storm_clause: A Storm expression
 
602
 
 
603
    A helper function allowing to use a Storm expressions in old-style
 
604
    code which builds for example WHERE expressions as plain strings.
 
605
 
 
606
    >>> from lp.bugs.model.bug import Bug
 
607
    >>> from lp.bugs.model.bugtask import BugTask
 
608
    >>> from lp.bugs.interfaces.bugtask import BugTaskImportance
 
609
    >>> from storm.expr import And, Or
 
610
 
 
611
    >>> print convert_storm_clause_to_string(BugTask)
 
612
    BugTask
 
613
 
 
614
    >>> print convert_storm_clause_to_string(BugTask.id == 16)
 
615
    BugTask.id = 16
 
616
 
 
617
    >>> print convert_storm_clause_to_string(
 
618
    ...     BugTask.importance == BugTaskImportance.UNKNOWN)
 
619
    BugTask.importance = 999
 
620
 
 
621
    >>> print convert_storm_clause_to_string(Bug.title == "foo'bar'")
 
622
    Bug.title = 'foo''bar'''
 
623
 
 
624
    >>> print convert_storm_clause_to_string(
 
625
    ...     Or(BugTask.importance == BugTaskImportance.UNKNOWN,
 
626
    ...        BugTask.importance == BugTaskImportance.HIGH))
 
627
    BugTask.importance = 999 OR BugTask.importance = 40
 
628
 
 
629
    >>> print convert_storm_clause_to_string(
 
630
    ...    And(Bug.title == 'foo', BugTask.bug == Bug.id,
 
631
    ...        Or(BugTask.importance == BugTaskImportance.UNKNOWN,
 
632
    ...           BugTask.importance == BugTaskImportance.HIGH)))
 
633
    Bug.title = 'foo' AND BugTask.bug = Bug.id AND
 
634
    (BugTask.importance = 999 OR BugTask.importance = 40)
 
635
    """
 
636
    state = State()
 
637
    clause = storm_compile(storm_clause, state)
 
638
    if len(state.parameters):
 
639
        parameters = [param.get(to_db=True) for param in state.parameters]
 
640
        clause = clause.replace('?', '%s') % sqlvalues(*parameters)
 
641
    return clause
 
642
 
595
643
def flush_database_updates():
596
644
    """Flushes all pending database updates.
597
645