本文整理汇总了Python中django.db.models.sql.expressions.SQLEvaluator类的典型用法代码示例。如果您正苦于以下问题:Python SQLEvaluator类的具体用法?Python SQLEvaluator怎么用?Python SQLEvaluator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SQLEvaluator类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: as_sql
def as_sql(self):
"""
Creates the SQL for this query. Returns the SQL string and list of
parameters.
"""
from django.db.models.base import Model
self.pre_sql_setup()
if not self.query.values:
return '', ()
table = self.query.tables[0]
qn = self.quote_name_unless_alias
result = ['UPDATE %s' % qn(table)]
result.append('SET')
values, update_params = [], []
for field, model, val in self.query.values:
if hasattr(val, 'prepare_database_save'):
val = val.prepare_database_save(field)
else:
val = field.get_db_prep_save(val, connection=self.connection)
# Getting the placeholder for the field.
if hasattr(field, 'get_placeholder'):
placeholder = field.get_placeholder(val, self.connection)
else:
placeholder = '%s'
if hasattr(val, 'evaluate'):
val = SQLEvaluator(val, self.query, allow_joins=False)
name = field.column
if hasattr(val, 'as_sql'):
sql, params = val.as_sql(qn, self.connection)
values.append('%s = %s' % (qn(name), sql))
update_params.extend(params)
elif val is not None:
values.append('%s = %s' % (qn(name), placeholder))
update_params.append(val)
else:
values.append('%s = NULL' % qn(name))
if not values:
return '', ()
result.append(', '.join(values))
where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
if where:
result.append('WHERE %s' % where)
return ' '.join(result), tuple(update_params + params)
示例2: as_sql
def as_sql(self):
"""
Creates the SQL for this query. Returns the SQL string and list of
parameters.
"""
self.pre_sql_setup()
if not self.query.values:
return "", ()
table = self.query.tables[0]
qn = self.quote_name_unless_alias
result = ["UPDATE %s" % qn(table)]
result.append("SET")
values, update_params = [], []
for field, model, val in self.query.values:
if hasattr(val, "prepare_database_save"):
val = val.prepare_database_save(field)
else:
val = field.get_db_prep_save(val, connection=self.connection)
# Getting the placeholder for the field.
if hasattr(field, "get_placeholder"):
placeholder = field.get_placeholder(val, self.connection)
else:
placeholder = "%s"
if hasattr(val, "evaluate"):
val = SQLEvaluator(val, self.query, allow_joins=False)
name = field.column
if hasattr(val, "as_sql"):
sql, params = val.as_sql(qn, self.connection)
values.append("%s = %s" % (qn(name), sql))
update_params.extend(params)
elif val is not None:
values.append("%s = %s" % (qn(name), placeholder))
update_params.append(val)
else:
values.append("%s = NULL" % qn(name))
if not values:
return "", ()
result.append(", ".join(values))
where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
if where:
result.append("WHERE %s" % where)
return " ".join(result), tuple(update_params + params)
示例3: as_sql
def as_sql(self):
qn = self.connection.ops.quote_name
opts = self.query.model._meta
result = ['REPLACE INTO %s' % qn(opts.db_table)]
# This is a bit ugly, we have to scrape information from the where clause
# and put it into the field/values list. Sphinx will not accept an UPDATE
# statement that includes full text data, only INSERT/REPLACE INTO.
node = self.query.where.children[0]
if django.VERSION < DJANGO16:
lvalue, lookup_type, value_annot, params_or_value = node.children[0]
else:
lvalue, lookup_type, value_annot, params_or_value = node
(table_name, column_name, column_type), val = lvalue.process(lookup_type, params_or_value, self.connection)
fields, values, params = [column_name], ['%s'], [val[0]]
# Now build the rest of the fields into our query.
for field, model, val in self.query.values:
if hasattr(val, 'prepare_database_save'):
val = val.prepare_database_save(field)
else:
val = field.get_db_prep_save(val, connection=self.connection)
# Getting the placeholder for the field.
if hasattr(field, 'get_placeholder'):
placeholder = field.get_placeholder(val, self.connection)
else:
placeholder = '%s'
if hasattr(val, 'evaluate'):
val = SQLEvaluator(val, self.query, allow_joins=False)
name = field.column
if hasattr(val, 'as_sql'):
sql, params = val.as_sql(qn, self.connection)
values.append(sql)
params.extend(params)
elif val is not None:
values.append(placeholder)
params.append(val)
else:
values.append('NULL')
fields.append(name)
result.append('(%s)' % ', '.join(fields))
result.append('VALUES (%s)' % ', '.join(values))
return ' '.join(result), params