Thanks! This works like a charm.
For the next person...
`year_week(column)` turns a timestamp into "{Year}.{week of year}". I
should probably 0pad the week , but not now.
----------
from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles
import sqlalchemy.types
class year_week(expression.FunctionElement):
type = sqlalchemy.types.String()
name = 'year_week'
@compiles(year_week)
def year_week__default(element, compiler, **kw):
# return compiler.visit_function(element)
"""
## select extract(week from timestamp_event) from table_a;
week_num = sqlalchemy.sql.expression.extract('WEEK',
LetsencryptServerCertificate.timestamp_signed)
"""
args = list(element.clauses)
return "concat(extract(year from %s), '.', extract(week from %s)) " % (
compiler.process(args[0]),
compiler.process(args[0]),
)
@compiles(year_week, 'postgresql')
def year_week__postgresql(element, compiler, **kw):
"""
# select to_char(timestamp_event, 'YYYY.WW') from table_a;
week_num =
sqlalchemy.func.to_char(LetsencryptServerCertificate.timestamp_signed,
'YYYY.WW')
"""
args = list(element.clauses)
return "to_char(%s, 'YYYY.WW')" % (
compiler.process(args[0]),
)
@compiles(year_week, 'sqlite')
def year_week__sqlite(element, compiler, **kw):
"""
# strftime('%Y.%W', cast(LetsencryptServerCertificate.timestamp_signed)
as text)
week_num = sqlalchemy.func.strftime('%Y.%W',
sqlalchemy.cast(TABLE.COLUMN,
sqlalchemy.Unicode
)
)
"""
args = list(element.clauses)
return "strftime('%%Y.%%W', %s)" % (
compiler.process(args[0]),
)
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.