Edgewall Software
Modify

Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#7988 closed defect (worksforme)

Problems with sql_escape_percent function

Reported by: Zeratul Owned by:
Priority: normal Milestone:
Component: general Version: 0.11-stable
Severity: minor Keywords: sql sql_escape_percent cursor.execute
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description (last modified by Christian Boos)

Function sql_escape_percent(sql) from module trac/db/util.py is escaping quite too many characters. It's automatically used when executing sql statements with cursor.execute() and it replaces every '%' (percent sign) with '%%'. But '%' is sql special sign used in 'like' clauses - for example:

"SELECT oldvalue FROM ticket_change WHERE filed = 'comment' AND 
oldvalue LIKE '%.comment_number OR oldvalue = comment_number"

which may be useful for getting comment from database by comment_number. When above statement is executed via

cursor.execute("SELECT oldvalue FROM ticket_change WHERE filed =
 'comment' AND oldvalue LIKE '%.comment_number OR oldvalue = 
 comment_number")

the percent sign is doubled so the really executed query is:

"SELECT oldvalue FROM ticket_change WHERE filed = 'comment' AND 
oldvalue LIKE '%%.comment_number OR oldvalue = comment_number"

which theoretically gives the same results but is somehow strange…

Attachments (0)

Change History (2)

comment:1 by ebray, 16 years ago

Resolution: worksforme
Status: newclosed

That's what db.like() and db.like_escape() are for (besides that fact of different DBMS having subtle differences in LIKE). For example:

cursor.execute("SELECT name, author FROM wiki "
               "WHERE name " + db.like() +
               "GROUP BY name ORDER BY name, version",
               (db.like_escape(prefix) + '%',))

comment:2 by Christian Boos, 16 years ago

Description: modified (diff)

made description readable

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The ticket will remain with no owner.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from (none) to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.