Changeset 831

Show
Ignore:
Timestamp:
03/10/07 13:03:16 (2 years ago)
Author:
mfenniak
Message:

add initial stab at parameter conversion for qmark style

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pg8000/trunk/pg8000.py

    r827 r831  
    6666class NotSupportedError(DatabaseError): 
    6767    pass 
    68  
    6968 
    7069class DataIterator(object): 
     
    9695    threadsafety = 3 
    9796    paramstyle = 'none-of-the-above' 
     97 
     98    def convert_paramstyle(src_style, query, args): 
     99        # Break the query into a series of parts, unquoted, quoted, unquoted, and so on... 
     100        parts = [] 
     101        while 1: 
     102            idx = query.find("'") 
     103            if idx == -1: 
     104                parts.append(query) 
     105                break 
     106            else: 
     107                parts.append(query[:idx]) 
     108                query = query[idx+1:] 
     109            start = 0 
     110            while 1: 
     111                idx = query.find("'", start) 
     112                if idx == -1: 
     113                    raise ProgrammingError("unterminated quoted string") 
     114                if (idx+1) < len(query) and query[idx+1] == "'": 
     115                    # idx points to double-quote.  not terminating quote. 
     116                    start = idx + 2 
     117                    continue 
     118                else: 
     119                    parts.append(query[:idx]) 
     120                    query = query[idx+1:] 
     121                    break 
     122        # String broken up.  Now, in all unquoted sections, let's replace the 
     123        # param strings. 
     124        param_cnt = 1 
     125        params = [] 
     126        for i in range((len(parts) / 2) + 1): 
     127            pidx = i * 2 
     128            part = parts[pidx] 
     129            output = "" 
     130            while 1: 
     131                if src_style == "qmark": 
     132                    idx = part.find("?") 
     133                    if idx == -1: 
     134                        output += part 
     135                        break 
     136                    output += part[:idx] 
     137                    output += "$" + str(param_cnt) 
     138                    part = part[idx+1:] 
     139                    params.append(args[param_cnt-1]) 
     140                    param_cnt += 1 
     141            parts[pidx] = output 
     142        retval = "" 
     143        for i in range((len(parts) / 2) + 1): 
     144            pidx = i * 2 
     145            retval += parts[pidx] 
     146            pidx += 1 
     147            if pidx < len(parts): 
     148                retval += "'" + parts[pidx] + "'" 
     149        return retval, tuple(params) 
     150    convert_paramstyle = staticmethod(convert_paramstyle) 
    98151 
    99152    class CursorWrapper(object): 
  • pg8000/trunk/pg8000-test.py

    r827 r831  
    66 
    77import pg8000 
     8 
     9print "testing convert_paramstyle" 
     10new_query, new_args = pg8000.DBAPI.convert_paramstyle("qmark", "SELECT ?, ?, * FROM t WHERE a='say ''what?''' AND b=?", (1, 2, 3)) 
     11assert new_query == "SELECT $1, $2, * FROM t WHERE a='say ''what?''' AND b=$3" 
     12assert new_args == (1, 2, 3) 
    813 
    914#db = pg8000.Connection(host='joy', user='pg8000-test', database='pg8000-test', password='pg8000-test', socket_timeout=5)