Conververting datetime based on database dialects (#446)
This commit is contained in:
parent
77e9e6a5d7
commit
c4e3020369
1
TODO.md
1
TODO.md
|
|
@ -33,7 +33,6 @@ List of TODO items for Caravel
|
|||
* CREATE VIEW button from SQL editor
|
||||
* Test button for when editing SQL expression
|
||||
* Slider form element
|
||||
* datasource in explore mode could be a dropdown
|
||||
* [druid] Allow for post aggregations (ratios!)
|
||||
* in/notin filters autocomplete (druid)
|
||||
|
||||
|
|
|
|||
|
|
@ -410,6 +410,24 @@ class Database(Model, AuditMixinNullable):
|
|||
if self.sqlalchemy_uri.startswith(db_type):
|
||||
return grains
|
||||
|
||||
def dttm_converter(self, dttm):
|
||||
"""Returns a string that the database flavor understands as a date"""
|
||||
default = "'{}'".format(dttm.strftime('%Y-%m-%d %H:%M:%S.%f'))
|
||||
iso = dttm.isoformat()
|
||||
d = {
|
||||
'mssql': "CONVERT(DATETIME, '{}', 126)".format(iso), #untested
|
||||
'mysql': default,
|
||||
'oracle':
|
||||
"""TO_TIMESTAMP('{}', 'YYYY-MM-DD"T"HH24:MI:SS.ff6')""".format(
|
||||
dttm.isoformat()),
|
||||
'presto': default,
|
||||
'sqlite': default,
|
||||
}
|
||||
for k, v in d.items():
|
||||
if self.sqlalchemy_uri.startswith(k):
|
||||
return v
|
||||
return default
|
||||
|
||||
def grains_dict(self):
|
||||
return {grain.name: grain for grain in self.grains()}
|
||||
|
||||
|
|
@ -630,14 +648,16 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
|
|||
|
||||
tf = '%Y-%m-%d %H:%M:%S.%f'
|
||||
time_filter = [
|
||||
timestamp >= from_dttm.strftime(tf),
|
||||
timestamp <= to_dttm.strftime(tf),
|
||||
timestamp >= text(self.database.dttm_converter(from_dttm)),
|
||||
timestamp <= text(self.database.dttm_converter(to_dttm)),
|
||||
]
|
||||
inner_time_filter = copy(time_filter)
|
||||
if inner_from_dttm:
|
||||
inner_time_filter[0] = timestamp >= inner_from_dttm.strftime(tf)
|
||||
inner_time_filter[0] = timestamp >= text(
|
||||
self.database.dttm_converter(inner_from_dttm))
|
||||
if inner_to_dttm:
|
||||
inner_time_filter[1] = timestamp <= inner_to_dttm.strftime(tf)
|
||||
inner_time_filter[1] = timestamp <= text(
|
||||
self.database.dttm_converter(inner_to_dttm))
|
||||
else:
|
||||
inner_time_filter = []
|
||||
|
||||
|
|
|
|||
|
|
@ -112,7 +112,8 @@ class TableColumnInlineView(CompactCRUDMixin, CaravelModelView): # noqa
|
|||
can_delete = False
|
||||
edit_columns = [
|
||||
'column_name', 'verbose_name', 'description', 'groupby', 'filterable',
|
||||
'table', 'count_distinct', 'sum', 'min', 'max', 'expression', 'is_dttm']
|
||||
'table', 'count_distinct', 'sum', 'min', 'max', 'expression',
|
||||
'is_dttm', ]
|
||||
add_columns = edit_columns
|
||||
list_columns = [
|
||||
'column_name', 'type', 'groupby', 'filterable', 'count_distinct',
|
||||
|
|
|
|||
Loading…
Reference in New Issue