sripathikrishnan / jinjasql

Template Language for SQL with Automatic Bind Parameter Extraction
MIT License
820 stars 90 forks source link

Support passing JSON as dictionaries #14

Closed Rovanion closed 6 years ago

Rovanion commented 6 years ago

I'm trying to insert some JSON data into my database through JinjaSQL and Psycopg2. Though I'm getting stopped by JinjaSQL as it has the following check: https://github.com/hashedin/jinjasql/blob/93c9bbceedd45c2439a9b3698e9bddfc5df61958/jinjasql/core.py#L104

If we bypass this check and register an adaptor for dictionaries Psycopg2 will gladly eat the dictionary.

from psycopg2.extras import Json
register_adapter(dict, Json)

createAreaTemplate="""
  insert into  area_revisions (                                                                 
    area, modified_by, geo_json )                                                               
  values (                                                                                      
    {{ uuid }}, {{ modifiedBy }}, {{ geoJson }} )
"""

area = {
    "uuid":            uuid4(),
    "modifiedBy":      author['uuid'],
    "project":         parentProject['uuid'],
    "geoJson":         ""
}

query, params = prepareQuery(createAreaTemplate, area)
a = list(params)
a[2] = {}
cursor.execute(query, parameters)

Would it be possible to remove the is_dictionary check?

devashishsharma2302 commented 6 years ago

@Rovanion Thanks for reporting. It was sort of a pre-emptive strategy. Removed this check now (refer PR #15) Marking the issue as fixed. Hope this solves your issue. Feel free to reopen this if it doesn't.

Rovanion commented 6 years ago

Thank you, hopefully it helps someone in the future. In the meantime I sidestepped the issue by using psycopg2 directly where jinjasql stopped me and I will unfortunately not have time to test this change in my code due to project deadlines.