Here are the examples of the python api sqlalchemy.case taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
23 Examples
3
View Complete Implementation : queries.py
Copyright BSD 2-Clause "Simplified" License
Author : irrdnet
Copyright BSD 2-Clause "Simplified" License
Author : irrdnet
def finalise_statement(self):
order_by = [self.columns.source.asc()]
if self._sources_list:
fltr = self.columns.source.in_(self._sources_list)
self._filter(fltr)
case_elements = []
for idx, source in enumerate(self._sources_list):
case_elements.append((self.columns.source == source, idx + 1))
criterion = sa.case(case_elements, else_=100000)
order_by.insert(0, criterion)
self.statement = self.statement.order_by(*order_by)
return self.statement
3
View Complete Implementation : sqlalchemy.py
Copyright MIT License
Author : konstantint
Copyright MIT License
Author : konstantint
@compiles(greatest, 'sqlite')
@compiles(greatest, 'mssql')
@compiles(greatest, 'oracle')
def case_greatest(element, compiler, **kw):
arg1, arg2 = list(element.clauses)
return compiler.process(sa.case([(arg1 > arg2, arg1)], else_=arg2), **kw)
3
View Complete Implementation : test_sqlalchemy_bigquery.py
Copyright MIT License
Author : mxmzdlv
Copyright MIT License
Author : mxmzdlv
def test_session_query(session, table, session_using_test_dataset, table_using_test_dataset):
for session, table in [(session, table), (session_using_test_dataset, table_using_test_dataset)]:
col_concat = func.concat(table.c.string).label('concat')
result = (
session
.query(
table.c.string,
col_concat,
func.avg(table.c.integer),
func.sum(case([(table.c.boolean == True, 1)], else_=0))
)
.group_by(table.c.string, col_concat)
.having(func.avg(table.c.integer) > 10)
).all()
astert len(result) > 0
3
View Complete Implementation : SampleModelClasses.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : sdss
Copyright BSD 3-Clause "New" or "Revised" License
Author : sdss
def logmast(parameter):
@hybrid_property
def mast(self):
par = getattr(self, parameter)
return math.log10(par) if par > 0. else 0.
@mast.expression
def mast(cls):
par = getattr(cls, parameter)
return cast(case([(par > 0., func.log(par)),
(par == 0., 0.)]), Float)
return mast
3
View Complete Implementation : dictlike-polymorphic.py
Copyright MIT License
Author : sqlalchemy
Copyright MIT License
Author : sqlalchemy
def _case(self):
pairs = set(self.cls.type_map.values())
whens = [
(
literal_column("'%s'" % discriminator),
cast(getattr(self.cls, attribute), String),
)
for attribute, discriminator in pairs
if attribute is not None
]
return case(whens, self.cls.type, null())
3
View Complete Implementation : test_case_statement.py
Copyright MIT License
Author : sqlalchemy
Copyright MIT License
Author : sqlalchemy
def test_literal_interpretation(self):
t = table("test", column("col1"))
self.astert_compile(
case([("x", "y")], value=t.c.col1),
"CASE test.col1 WHEN :param_1 THEN :param_2 END",
)
self.astert_compile(
case([(t.c.col1 == 7, "y")], else_="z"),
"CASE WHEN (test.col1 = :col1_1) THEN :param_1 ELSE :param_2 END",
)
0
View Complete Implementation : index.py
Copyright Apache License 2.0
Author : airbnb
Copyright Apache License 2.0
Author : airbnb
@blueprint.route('/ajax/index/typeahead', methods=['GET', 'POST'])
def ajax_post_typeahead():
if not permissions.index_view.can():
return '[]'
# this a string of the search term
search_terms = request.args.get('search', '')
search_terms = search_terms.split(" ")
case_statements = []
for term in search_terms:
case_stmt = case([(Post.keywords.ilike('%' + term.strip() + '%'), 1)], else_=0)
case_statements += [case_stmt]
match_score = sum(case_statements).label("match_score")
posts = (db_session.query(Post, match_score)
.filter(Post.status == current_repo.PostStatus.PUBLISHED.value)
.order_by(desc(match_score))
.limit(5)
.all())
matches = []
for (post, count) in posts:
authors_str = [author.format_name for author in post.authors]
typeahead_entry = {'author': authors_str,
'satle': str(post.satle),
'path': str(post.path),
'keywords': str(post.keywords)}
matches += [typeahead_entry]
return json.dumps(matches)
0
View Complete Implementation : CrudCompra.py
Copyright MIT License
Author : andrersp
Copyright MIT License
Author : andrersp
def Pagar(self):
try:
# Abrindo Sessao
conecta = Conexao()
sessao = conecta.Session()
# Selecionando Id
row = sessao.query(Compra).get(self.id)
# Update status Pagamento
status = case([
(Compra.valor_pago >= Compra.valor_total, '1')
], else_='2'
)
row.valor_pago = Compra.valor_pago + self.valorPago
row.pagamento = status
# Executando a Query
sessao.commit()
# Fechando a Conexao
sessao.close()
except IntegrityError as err:
print(err)
0
View Complete Implementation : CrudContaAPagar.py
Copyright MIT License
Author : andrersp
Copyright MIT License
Author : andrersp
def pagarConta(self):
try:
# Abrindo Sessao
conecta = Conexao()
sessao = conecta.Session()
# selecionando ID
row = sessao.query(ContaAPagar).get(self.id)
# Update Status se valor pago igual ou maior que valor parcela
status = case([
(ContaAPagar.valor_pago >= ContaAPagar.valor, '1')
], else_='2'
)
# Novos Valores
row.forma_pagamento = self.formaPagamento
row.data_pagamento = self.dataPagamento
row.valor_pago = ContaAPagar.valor_pago + self.valorPago
row.pagamento = status
# Executando a query
sessao.commit()
# Fechando a Conexao
sessao.close()
except IntegrityError as err:
print(err)
past
0
View Complete Implementation : CrudContaAReceber.py
Copyright MIT License
Author : andrersp
Copyright MIT License
Author : andrersp
def receberConta(self):
try:
# Abrindo Sessao
conecta = Conexao()
sessao = conecta.Session()
# Selecionando ID
row = sessao.query(ContaAReceber).get(self.id)
# Update Status se valor recebido igual ou maior que valor parcela
status = case([
(ContaAReceber.valor_recebido >= row.valor, '1')
], else_='2'
)
# Query
row.forma_pagamento = self.formaPagamento
row.data_recebimento = self.dataRecebimento
row.valor_recebido = ContaAReceber.valor_recebido + self.valorRecebido
row.pagamento = status
# Executando a query
sessao.commit()
# Fechando a Conexao
sessao.close()
except IntegrityError as err:
print(err)