django-sql-dashboard icon indicating copy to clipboard operation
django-sql-dashboard copied to clipboard

Support non-string parameters and default values

Open ipamo opened this issue 3 years ago • 1 comments
trafficstars

Passing non-string parameters and default values can be achieved today using something like:

select *
where grade >= cast(%(grade)s as integer)
and is_allowed = cast(coalesce(nullif(%(is_allowed)s,''), 'true') as boolean)

But this is not very readable. Would be nice to support some other patterns, for example:

select *
where grade >= %(grade)d
and is_allowed = %(is_allowed:true)b

I know that psycopg2 supports only string parameters, but it does not seem too difficult to manually handle simple cases (like numbers and booleans) while still caring about SQL injections.

For now, I've proposed pull request #148 to refactor the named-parameter feature within the library, so that developpers can easily extend it according to their needs. That would be usefull in any case.

I think it would also be usefull that simple cases (like those mentionned above) be part of the library. What do you think?

ipamo avatar Mar 25 '22 22:03 ipamo

Implementation example: ExtendedParameter.

Activation in settings.py using DASHBOARD_PARAMETER_CLASS = 'django_sql_dashboard.extensions.ExtendedParameter'.

ipamo avatar Mar 28 '22 09:03 ipamo