Skip to content

Commit

Permalink
Add full range of options for parameter markers
Browse files Browse the repository at this point in the history
Fix bug with parsing results in Python 3
  • Loading branch information
Israel Brewster committed Feb 10, 2016
1 parent 161befc commit 65789d9
Show file tree
Hide file tree
Showing 503 changed files with 36 additions and 115,473 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ package.sh
testMain.c
dist
*.egg-info
virtualenv
9 changes: 9 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
p4d is a Python Database API 2.0 compliant driver for the 4D (4th Dimension) database server. As such, usage should be familiar to anyone who has used any python database modules before. This module is based off of a C library provided by 4D, and integrated with Python using CFFI. As such, installation of this module does require CFFI.
v0.9 2016-02-10:
- Fix bug that called .decode() on a str object in python 3
- Add support for "pyformat" style parameter markers
- Add support for "format" style parameter markers
- Add support for "named" style parameter markers

v0.8 2015-11-24:
- Changes for python 3 compatibility

v0.7 2015-09-30:
- Fix bug with running multiple queries in a row on the same cursor

Expand Down
28 changes: 24 additions & 4 deletions p4d/p4d.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import absolute_import,unicode_literals

import os, sys, binascii
from cffi import FFI
from cffi.verifier import Verifier
from dateutil import parser
from datetime import datetime, timedelta, time, date
from collections import defaultdict
import time as timemod
import threading, glob
import threading, glob, re


########################################################################
Expand Down Expand Up @@ -244,6 +242,28 @@ def execute(self, query, params=[], describe=True):
if self.connection.connected == False:
raise InternalError("Database not connected")

# See if we are using named parameters. If so, break them out (we always need qmark style in the end)
if isinstance(params, dict):
new_params = []
# Parse query string for references to dict entries
regex = re.compile('%\(([^\)]+)\)s')
for key in re.findall(regex, query):
new_params.append(params[key]) # Will raise key error if the query string argument is not in params.

if not new_params:
# We didn't match anything in the query string for the %()s format markers. Try named (:name) instead
regex = re.compile(':([A-Za-z0-9]+)')
for key in re.findall(regex, query):
new_params.append(params[key])

query = re.sub(regex, '?', query)
params = new_params

# If using "format" parameter markers, just convert all %<whatever> markers to ?'s
query = re.sub('%[A-Za-z]', '?', query)
query.replace('%%', '%') # Replace double-quotes with single quote


# if any parameter is a tuple, we need to modify the query string and
# make multiple passes through the parameters, breaking out one tuple/list
# each time.
Expand Down Expand Up @@ -443,7 +463,7 @@ def fetchone(self):

self.lib4d_sql.fourd_field_to_string(self.result, col, inbuff, strlen)
strdata = inbuff[0]
output = str(ffi.buffer(strdata, strlen[0])[:])
output = ffi.buffer(strdata, strlen[0])[:]
if strdata != ffi.NULL:
self.lib4d_sql.free(strdata) #must call free explicitly, otherwise we leak.
strdata = ffi.NULL
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def finalize_options(self):
setup(
zip_safe=False,
name="p4d",
version="0.7",
version="0.9",
install_requires=["cffi", ],
setup_requires=['cffi', ],
setup_requires=['cffi', 'python-dateutil' ],
packages=find_packages(),
# need to include these files to be able to build our shared library
package_data={'p4d': ['py_fourd.h'],},
Expand All @@ -51,5 +51,4 @@ def finalize_options(self):
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 2'],
keywords='datababase drivers DBI 4d'

)
1 change: 0 additions & 1 deletion virtualenv/.Python

This file was deleted.

80 changes: 0 additions & 80 deletions virtualenv/bin/activate

This file was deleted.

42 changes: 0 additions & 42 deletions virtualenv/bin/activate.csh

This file was deleted.

74 changes: 0 additions & 74 deletions virtualenv/bin/activate.fish

This file was deleted.

34 changes: 0 additions & 34 deletions virtualenv/bin/activate_this.py

This file was deleted.

11 changes: 0 additions & 11 deletions virtualenv/bin/easy_install

This file was deleted.

11 changes: 0 additions & 11 deletions virtualenv/bin/easy_install-3.5

This file was deleted.

11 changes: 0 additions & 11 deletions virtualenv/bin/pip

This file was deleted.

11 changes: 0 additions & 11 deletions virtualenv/bin/pip3

This file was deleted.

11 changes: 0 additions & 11 deletions virtualenv/bin/pip3.5

This file was deleted.

1 change: 0 additions & 1 deletion virtualenv/bin/python

This file was deleted.

1 change: 0 additions & 1 deletion virtualenv/bin/python3

This file was deleted.

Binary file removed virtualenv/bin/python3.5
Binary file not shown.
Loading

0 comments on commit 65789d9

Please sign in to comment.