Package yum :: Module sqlutils
[hide private]
[frames] | no frames]

Source Code for Module yum.sqlutils

  1  #!/usr/bin/python -tt 
  2  # This program is free software; you can redistribute it and/or modify 
  3  # it under the terms of version 2 of the GNU General Public License 
  4  # as published by the Free Software Foundation 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library General Public License for more details. 
 10  # 
 11  # You should have received a copy of the GNU General Public License 
 12  # along with this program; if not, write to the Free Software 
 13  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 14  # Copyright 2005 Duke University 
 15   
 16  """ 
 17  utility functions to handle differences in pysqlite versions 
 18  These are from Wichert Akkerman <wichert@deephackmode.org>'s python-dhm 
 19  http://www.wiggy.net/code/python-dhm 
 20  """ 
 21   
 22  try: 
 23      import sqlite3 as sqlite 
 24  except ImportError: 
 25      import sqlite 
 26   
27 -class TokenizeError(Exception):
28 """Tokenizer error class""" 29 pass
30
31 -def Tokenize(str, whitespace=" \t\r\n", quotes="\"", escapes="\\"):
32 """String tokenizer 33 34 This function tokenizes a string while taking quotation and 35 escaping into account. 36 37 >>> import dhm.strtools 38 >>> dhm.strtools.Tokenize("this is a test") 39 ['this', 'is', 'a', 'test'] 40 >>> dhm.strtools.Tokenize("this \"is a\" test") 41 ['this', 'is a', 'test'] 42 >>> dhm.strtools.Tokenize("this \\\"is\\\" a test") 43 ['this', '"is"', 'a', 'test'] 44 >>> dhm.strtools.Tokenize("this \"is a test") 45 Traceback (most recent call last): 46 File "<stdin>", line 1, in ? 47 File "/usr/local/lib/python2.2/site-packages/dhm/strtools.py", line 80, in Tokenize 48 raise TokenizeError, "Unexpected end of string in quoted text" 49 dhm.strtools.TokenizeError: Unexecpted end of string in quoted text 50 51 @param str: string to tokenize 52 @type str: string 53 @param whitespace: whitespace characters seperating tokens 54 @type whitespace: string 55 @param quotes: legal quoting characters 56 @type quotes: string 57 @param escapes: characters which can escape quoting characters 58 @type escapes: string 59 @return: list of tokens 60 @rtype: sequence of strings 61 """ 62 (buffer, tokens, curtoken, quote)=(str, [], None, None) 63 64 try: 65 while buffer: 66 if buffer[0]==quote: 67 quote=None 68 elif (quote==None) and (buffer[0] in quotes): 69 quote=buffer[0] 70 elif buffer[0] in whitespace: 71 if quote!=None: 72 curtoken+=buffer[0] 73 else: 74 tokens.append(curtoken) 75 curtoken=None 76 while buffer[1] in whitespace: 77 buffer=buffer[1:] 78 elif buffer[0] in escapes: 79 if curtoken==None: 80 curtoken=buffer[1] 81 else: 82 curtoken+=buffer[1] 83 buffer=buffer[1:] 84 else: 85 if curtoken==None: 86 curtoken=buffer[0] 87 else: 88 curtoken+=buffer[0] 89 90 buffer=buffer[1:] 91 except IndexError: 92 raise TokenizeError, "Unexpected end of string" 93 94 if quote: 95 raise TokenizeError, "Unexpected end of string in quoted text" 96 97 if curtoken!=None: 98 tokens.append(curtoken) 99 100 return tokens
101 102
103 -def QmarkToPyformat(query, params):
104 """Convert from qmark to pyformat parameter style. 105 106 The python DB-API 2.0 specifies four different possible parameter 107 styles that can be used by drivers. This function converts from the 108 qmark style to pyformat style. 109 110 @param query: SQL query to transform 111 @type query: string 112 @param params: arguments to query 113 @type params: sequence of strings 114 @return: converted query and parameters 115 @rtype: tuple with the new command and a dictionary of arguments 116 """ 117 tokens=Tokenize(query, quotes="'") 118 output=[] 119 count=1 120 for token in tokens: 121 if token.endswith("?"): 122 output.append(token[:-1] + "%%(param%d)s" % count) 123 count+=1 124 elif token.endswith("?,") or token.endswith("?)"): 125 ntoken = token[:-2] + "%%(param%d)s" % count 126 ntoken += token[-1] 127 output.append(ntoken) 128 count+=1 129 else: 130 output.append(token) 131 132 dict={} 133 count=1 134 for param in params: 135 dict["param%d" % count]=param 136 count+=1 137 138 return (" ".join(output), dict)
139 140
141 -def executeSQLPyFormat(cursor, query, params=None):
142 """ 143 Execute a python < 2.5 (external sqlite module) style query. 144 145 @param cursor: A sqlite cursor 146 @param query: The query to execute 147 @param params: An optional list of parameters to the query 148 """ 149 if params is None: 150 return cursor.execute(query) 151 152 # Leading whitespace confuses QmarkToPyformat() 153 query = query.strip() 154 (q, p) = QmarkToPyformat(query, params) 155 return cursor.execute(q, p)
156
157 -def executeSQLQmark(cursor, query, params=None):
158 """ 159 Execute a python 2.5 (sqlite3) style query. 160 161 @param cursor: A sqlite cursor 162 @param query: The query to execute 163 @param params: An optional list of parameters to the query 164 """ 165 if params is None: 166 return cursor.execute(query) 167 168 return cursor.execute(query, params)
169 170 if sqlite.version_info[0] > 1: 171 executeSQL = executeSQLQmark 172 else: 173 executeSQL = executeSQLPyFormat 174 175
176 -def sql_esc(pattern):
177 """ Apply SQLite escaping, if needed. Returns pattern and esc. """ 178 esc = '' 179 if "_" in pattern or "%" in pattern: 180 esc = ' ESCAPE "!"' 181 pattern = pattern.replace("!", "!!") 182 pattern = pattern.replace("%", "!%") 183 pattern = pattern.replace("_", "!_") 184 return (pattern, esc)
185
186 -def sql_esc_glob(patterns):
187 """ Converts patterns to SQL LIKE format, if required (or gives up if 188 not possible). """ 189 ret = [] 190 for pattern in patterns: 191 if '[' in pattern: # LIKE only has % and _, so [abc] can't be done. 192 return [] # So Load everything 193 194 # Convert to SQL LIKE format 195 (pattern, esc) = sql_esc(pattern) 196 pattern = pattern.replace("*", "%") 197 pattern = pattern.replace("?", "_") 198 ret.append((pattern, esc)) 199 return ret
200