本文整理汇总了Python中LibCIA.Database类的典型用法代码示例。如果您正苦于以下问题:Python Database类的具体用法?Python Database怎么用?Python Database使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _reinforce
def _reinforce(self, cursor):
"""Database interaction implementing reinforce()"""
# First touch the edge to make sure it exists. We have to do this
# inside two autoCreateTargetFor levels, to create both stats targets
# if they don't yet exist.
self.a._autoCreateTargetFor(
cursor,
self.b._autoCreateTargetFor,
cursor,
cursor.execute,
"INSERT IGNORE INTO stats_relations "
"(target_a_path, target_b_path) VALUES(%s, %s)"
% (Database.quote(self.a.path, "varchar"), Database.quote(self.b.path, "varchar")),
)
cursor.execute(
"UPDATE stats_relations "
"SET strength = strength + 1, freshness = %s "
"WHERE target_a_path = %s AND target_b_path = %s"
% (
Database.quote(int(time.time()), "bigint"),
Database.quote(self.a.path, "varchar"),
Database.quote(self.b.path, "varchar"),
)
)
示例2: notify
def notify(self, scope):
"""Notify all subscribers to this stats target of a change in 'scope'"""
# Get a list of applicable triggers from the database
Database.pool.runQuery("SELECT id, `trigger` FROM stats_subscriptions "
"WHERE target_path = %s "
"AND (scope is NULL or scope = %s)" %
(Database.quote(self.target.path, 'varchar'),
Database.quote(scope, 'varchar'))).addCallback(self.runTriggers)
示例3: initQuery
def initQuery(self):
self.query = self.query % dict(
path = Database.quote(self.targetPath, 'varchar'),
limit = self.numItems,
counter = Database.quote(self.counter, 'varchar'),
counter_attrib = self.counterAttrib,
sort = self.sort,
hint = self.hint,
)
示例4: _createCounter
def _createCounter(self, cursor, name):
"""Internal function to create one blank counter if it doesn't exist."""
try:
cursor.execute("INSERT INTO stats_counters (target_path, name) VALUES(%s, %s)" %
(Database.quote(self.target.path, 'varchar'),
Database.quote(name, 'varchar')))
except:
# Ignore duplicate key errors
if str(sys.exc_info()[1]).find("duplicate key") < 0:
raise
示例5: _triggerFailure
def _triggerFailure(self, cursor, failure, id, maxFailures=3):
# Increment the consecutive failure count
log.msg("Failed to notify subscriber %d for %r: %r" % (id, self.target, failure))
cursor.execute("UPDATE stats_subscriptions SET failures = failures + 1 WHERE id = %s" %
Database.quote(id, 'bigint'))
# Cancel the subscription if we've had too many failures
cursor.execute("DELETE FROM stats_subscriptions WHERE id = %s AND failures > %s" %
(Database.quote(id, 'bigint'),
Database.quote(maxFailures, 'int')))
if cursor.rowcount:
log.msg("Unsubscribing subscriber %d for %r, more than %d consecutive failures" %
(id, self.target, maxFailures))
示例6: _subscribe
def _subscribe(self, cursor, target, client, trigger, scope=None, ttl=25*60*60):
"""A database interaction for adding subscriptions.
'target' must be the StatsTarget object this subscription is for.
'client' is the IP address of the client requesting this subscription.
'trigger' is a trigger pickle, as returned by makeTrigger
'scope' refers to a part of the stats target this refers to. By default, all of it.
'ttl' is the time to live for this subscription, 25 hours by default.
"""
cursor.execute("INSERT INTO stats_subscriptions "
"(target_path, expiration, scope, client, `trigger`) "
"VALUES (%s, %s, %s, %s, '%s')" %
(Database.quote(target.path, 'varchar'),
Database.quote(int(time.time() + ttl), 'bigint'),
Database.quote(scope, 'varchar'),
Database.quote(client, 'varchar'),
Database.quoteBlob(trigger)))
示例7: _runQuery
def _runQuery(self, cursor, filter):
# Set up and run two SQL queries, one for each side of the graph link that this
# target may be on. We can't do this in one step and still have the server use
# its indexes effectively.
filterSql = RelatedFilter(filter).sql
sections = {}
for thisSide, otherSide in ( ('a','b'), ('b', 'a') ):
cursor.execute(self.query % dict(
path = Database.quote(self.target.path, 'varchar'),
filter = filterSql,
thisSide = thisSide,
otherSide = otherSide,
))
while 1:
row = cursor.fetchone()
if not row:
break
# Drop rows into sections according to their parent path
try:
sections[row[0]].append(row[1:])
except KeyError:
sections[row[0]] = [row[1:]]
# Sort sections descending by freshness
for items in sections.itervalues():
items.sort()
items.reverse()
return sections
示例8: clear
def clear(self):
"""Delete everything associated with this stats target. Returns a Deferred
indicating the completion of this operation.
"""
# Delete the item in stats_target- the other tables will be
# deleted due to cascading foreign keys
return Database.pool.runOperation("DELETE FROM stats_catalog WHERE target_path = %s" %
Database.quote(self.path, 'varchar'))
示例9: render_rows
def render_rows(self, context):
photo_query = """
SELECT IM.path, IM.width, IM.height
FROM stats_statstarget ST
LEFT OUTER JOIN images_imageinstance IM
ON (IM.source_id = ST.photo_id AND IM.thumbnail_size = 256)
WHERE ST.path = %s
""" % Database.quote(
self.target.path, "varchar"
)
# XXX: This is hacky. Search for exclusive owners of this target.
owner_query = """
SELECT UA.id, UA.access, UA.user_id
FROM stats_statstarget ST
LEFT OUTER JOIN accounts_project PROJ ON (PROJ.target_id = ST.id)
LEFT OUTER JOIN accounts_author AUTH ON (AUTH.target_id = ST.id)
LEFT OUTER JOIN django_content_type CT_AUTH
ON (CT_AUTH.app_label = 'accounts' AND CT_AUTH.model = 'author')
LEFT OUTER JOIN django_content_type CT_PROJ
ON (CT_PROJ.app_label = 'accounts' AND CT_PROJ.model = 'project')
LEFT OUTER JOIN accounts_userasset UA
ON ( (UA.content_type_id = CT_AUTH.id AND UA.object_id = AUTH.id)
OR (UA.content_type_id = CT_PROJ.id AND UA.object_id = PROJ.id))
WHERE ST.path = %s AND UA.access > 1
""" % Database.quote(
self.target.path, "varchar"
)
# Grab the metadata keys we'll need and wait for them to become available
result = defer.Deferred()
defer.gatherResults(
[
self.metadata.getValue("url"),
self.metadata.getValue("description"),
Database.pool.runQuery(photo_query),
Database.pool.runQuery(owner_query),
]
).addCallback(self._render_rows, context, result).addErrback(result.errback)
return result
示例10: render_rows
def render_rows(self, context):
# First we run a big SQL query to gather all the data for this catalog.
# Control is passed to _render_rows once we have the query results.
result = defer.Deferred()
Database.pool.runQuery(self.query % {
'path': Database.quote(self.target.path, 'varchar'),
'limit': self.limit,
}).addCallback(
self._render_rows, context, result
).addErrback(result.errback)
return result
示例11: _catalog
def _catalog(self, cursor):
"""Database interaction representing the internals of catalog()"""
cursor.execute("SELECT target_path FROM stats_catalog WHERE parent_path = %s" %
Database.quote(self.path, 'varchar'))
results = []
while True:
row = cursor.fetchone()
if row is None:
break
results.append(StatsTarget(row[0]))
return results
示例12: checkOneRollover
def checkOneRollover(self, cursor, previous, current):
"""Check for rollovers in one pair of consecutive time intervals,
like yesterday/today or lastMonth/thisMonth. This is meant to
be run inside a database interaction.
"""
# Delete counters that are too old to bother keeping at all
cursor.execute("DELETE FROM stats_counters "
"WHERE (name = %s OR name = %s) "
"AND first_time < %s" %
(Database.quote(previous, 'varchar'),
Database.quote(current, 'varchar'),
Database.quote(long(TimeUtil.Interval(previous).getFirstTimestamp()), 'bigint')))
# Roll over remaining counters that are too old for current
# but still within the range of previous. Note that there is a
# race condition in which this update will fail because a timer
# has been incremented between it and the above DELETE. It could
# be prevented by locking the table, but it's probably not worth
# it. If the rollover fails this time, it will get another chance.
cursor.execute("UPDATE stats_counters SET name = %s "
"WHERE name = %s "
"AND first_time < %s" %
(Database.quote(previous, 'varchar'),
Database.quote(current, 'varchar'),
Database.quote(long(TimeUtil.Interval(current).getFirstTimestamp()), 'bigint')))
示例13: render_photo
def render_photo(self, context):
# First figure out if we have a photo. Actually render it in the Deferred if we do.
photo_query = """
SELECT IM.path
FROM stats_statstarget ST
LEFT OUTER JOIN images_imageinstance IM
ON (IM.source_id = ST.photo_id AND IM.thumbnail_size = 128)
WHERE ST.path = %s
""" % Database.quote(self.target.path, 'varchar')
result = defer.Deferred()
Database.pool.runQuery(photo_query).addCallback(
self._render_photo, context, result).addErrback(result.errback)
return result
示例14: _create
def _create(self, cursor):
"""Internal function to create a new stats target, meant to be run from
inside a database interaction. This is actually a recursive operation
that tries to create parent stats targets if necessary.
NOTE: this -must- ignore duplicate keys to avoid a race condition in which
one thread, in _autoCreateTargetFor, decides to create a new target
but before that target is fully created another thread also decides
it needs a new target.
"""
parent = self.parent()
if parent:
# If we have a parent, we have to worry about creating it
# if it doesn't exist and generating the proper parent path.
parent._autoCreateTargetFor(cursor, cursor.execute,
"INSERT IGNORE INTO stats_catalog (parent_path, target_path) VALUES(%s, %s)" %
(Database.quote(parent.path, 'varchar'),
Database.quote(self.path, 'varchar')))
else:
# This is the root node. We still need to insert a parent to keep the
# table consistent, but our parent in this case is NULL.
cursor.execute("INSERT IGNORE INTO stats_catalog (target_path) VALUES(%s)" %
Database.quote(self.path, 'varchar'))
示例15: _updateCache
def _updateCache(self, cursor):
"""Database interaction to update our counter cache"""
cursor.execute("SELECT name, first_time, last_time, event_count FROM stats_counters WHERE"
" target_path = %s" %
Database.quote(self.target.path, 'varchar'))
results = {}
while True:
row = cursor.fetchone()
if row is None:
break
results[row[0]] = {
'firstEventTime': row[1],
'lastEventTime': row[2],
'eventCount': row[3],
}
self.cache = results