Changeset 832

Show
Ignore:
Timestamp:
03/12/07 09:59:37 (1 year ago)
Author:
mfenniak
Message:

Updated paramstyle func + tests

Files:

Legend:

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

    r831 r832  
    123123        # param strings. 
    124124        param_cnt = 1 
    125         params = [] 
     125        if src_style == "qmark" or src_style == "format": 
     126            params = [] 
     127        elif src_style == "numeric": 
     128            params = args 
    126129        for i in range((len(parts) / 2) + 1): 
    127130            pidx = i * 2 
     
    139142                    params.append(args[param_cnt-1]) 
    140143                    param_cnt += 1 
     144                elif src_style == "format": 
     145                    idx = part.find("%") 
     146                    if idx == -1: 
     147                        output += part 
     148                        break 
     149                    elif (idx+1) == len(part): 
     150                        raise ProgrammingError("parameter quote : found at end of unquoted str") 
     151                    output += part[:idx] 
     152                    if part[idx+1] == "%": 
     153                        # %% escapes a percent sign. 
     154                        output += "%" 
     155                    else: 
     156                        output += "$" + str(param_cnt) 
     157                        params.append(args[param_cnt - 1]) 
     158                        param_cnt += 1 
     159                    part = part[idx+2:] 
     160                elif src_style == "numeric": 
     161                    idx = part.find(":") 
     162                    if idx == -1: 
     163                        output += part 
     164                        break 
     165                    elif (idx+1) == len(part): 
     166                        raise ProgrammingError("parameter quote : found at end of unquoted str") 
     167                    output += part[:idx] 
     168                    output += "$" + part[idx+1] 
     169                    part = part[idx+2:] 
     170                else: 
     171                    raise NotSupportedError("paramstyle %r not supported" % src_style) 
    141172            parts[pidx] = output 
    142173        retval = "" 
  • pg8000/trunk/pg8000-test.py

    r831 r832  
    88 
    99print "testing convert_paramstyle" 
     10 
    1011new_query, new_args = pg8000.DBAPI.convert_paramstyle("qmark", "SELECT ?, ?, * FROM t WHERE a='say ''what?''' AND b=?", (1, 2, 3)) 
    1112assert new_query == "SELECT $1, $2, * FROM t WHERE a='say ''what?''' AND b=$3" 
    1213assert new_args == (1, 2, 3) 
     14 
     15new_query, new_args = pg8000.DBAPI.convert_paramstyle("qmark", "SELECT ?, ?, * FROM t WHERE a=? AND b='are you ''sure?'", (1, 2, 3)) 
     16assert new_query == "SELECT $1, $2, * FROM t WHERE a=$3 AND b='are you ''sure?'" 
     17assert new_args == (1, 2, 3) 
     18 
     19new_query, new_args = pg8000.DBAPI.convert_paramstyle("numeric", "SELECT :2, :1, * FROM t WHERE a=:3", (1, 2, 3)) 
     20assert new_query == "SELECT $2, $1, * FROM t WHERE a=$3" 
     21assert new_args == (1, 2, 3) 
     22 
     23new_query, new_args = pg8000.DBAPI.convert_paramstyle("format", "SELECT %s, %s, * FROM t WHERE a=%s", (1, 2, 3)) 
     24print repr(new_query), repr(new_args) 
     25assert new_query == "SELECT $1, $2, * FROM t WHERE a=$3" 
     26assert new_args == (1, 2, 3) 
     27 
    1328 
    1429#db = pg8000.Connection(host='joy', user='pg8000-test', database='pg8000-test', password='pg8000-test', socket_timeout=5)