Django

Code

Ticket #815 (closed: fixed)

Opened 3 years ago

Last modified 3 years ago

[patch] select keyword to lookup_kwargs are being quoted incorrectly in query

Reported by: rezzrovv Assigned to: adrian
Milestone: Component: Core framework
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Here is the issue:

@@ -1466,7 +1519,7 @@

     # Add any additional SELECTs passed in via kwargs.
     if kwargs.get('select'):
-        select.extend(['(%s) AS %s' % (s[1], s[0]) for s in kwargs['select']])
+        select.extend(['(%s) AS %s' % (db.db.quote_name(s[1]), db.db.quote_name(s[0])) for s in kwargs['select']])

If you have: select={

'choice_count': 'SELECT COUNT(*) FROM choices WHERE poll_id = polls.id'

}

as described in the tutorial then you will get: select somecol, ("SELECT COUNT(*) FROM choices WHERE poll_id = polls.id") as "choice_count") from ... versus select somecol, (SELECT COUNT(*) FROM choices WHERE poll_id = polls.id) as "choice_count") from ... as it should be.

Simply reverting this one line back works but not sure why it was changed in the first place.

Attachments

Change History

11/16/05 09:45:12 changed by rezzrovv

  • owner changed from rjwittams to anonymous.
  • summary changed from (new-admin) select keyword to lookup_kwargs are being quoted incorrectly in query to select keyword to lookup_kwargs are being quoted incorrectly in query.

apparently this was merged from trunk and not new-admin as I thought.

11/16/05 09:49:19 changed by rjwittams

  • owner changed from anonymous to adrian.

The issue here is that some things passed to this keyword should be quoted and others shouldn't . So maybe this needs to be split into two kwargs, select and select_raw.

11/16/05 10:46:42 changed by hugo

  • summary changed from select keyword to lookup_kwargs are being quoted incorrectly in query to [patch] select keyword to lookup_kwargs are being quoted incorrectly in query.

Or we just check wether the to-be-quoted element contains any spaces - if it contains whitespace, it can't be a simple column name and doesn't need quoting. if it does not contain whitespace, it can be quoted without problems.

=== core/meta/__init__.py
==================================================================
--- core/meta/__init__.py  (revision 1831)
+++ core/meta/__init__.py  (local)
@@ -1518,8 +1518,14 @@
         _fill_table_cache(opts, select, tables, where, opts.db_table, [opts.db_table])
 
     # Add any additional SELECTs passed in via kwargs.
+    def quote_only_if_word(word):
+        if word.find(' ')>=0:
+            return word
+        else:
+            return db.db.quote_name(word)
+
     if kwargs.get('select'):
-        select.extend(['(%s) AS %s' % (db.db.quote_name(s[1]), db.db.quote_name(s[0])) for s in kwargs['select']])
+        select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), quote_only_if_word(s[0])) for s in kwargs['select']])
 
     # ORDER BY clause
     order_by = []

(this diff is against new_admin, but in trunk it should look mostly the same)

11/17/05 09:00:24 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [1274]) Fixed #815 -- 'select' keyword in DB API calls is now quoted correctly. Thanks, Hugo


Add/Change #815 ([patch] select keyword to lookup_kwargs are being quoted incorrectly in query)




Change Properties
Action