Skip to content

Commit 935a1d9

Browse files
authored
Merge pull request #153 from MonetDBSolutions/issue151_monetdbe_bind
Issue151 monetdbe bind
2 parents d69edb8 + 931460d commit 935a1d9

File tree

16 files changed

+349
-300
lines changed

16 files changed

+349
-300
lines changed

.github/workflows/linux.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ on:
66
tags: [ "*" ]
77
pull_request:
88
branches: [ master ]
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
911

1012
jobs:
1113
linux-test:
1214
runs-on: ubuntu-20.04
1315
strategy:
1416
matrix:
15-
branch: [Jul2021]
17+
branch: [default, Jul2021]
1618
container: monetdb/dev-builds:${{ matrix.branch }}
1719
steps:
1820
-
@@ -29,11 +31,12 @@ jobs:
2931
run: py.test -vv
3032

3133
linux-wheel:
34+
needs: linux-test
3235
runs-on: ubuntu-20.04
3336
strategy:
3437
matrix:
3538
python-version: [3.6, 3.7, 3.8, 3.9]
36-
branch: [Jul2021]
39+
branch: [default, Jul2021]
3740
container: monetdb/dev-builds:${{ matrix.branch}}_manylinux
3841

3942
steps:

.github/workflows/osx.yml

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,56 @@ on:
66
tags: [ "*" ]
77
pull_request:
88
branches: [ master ]
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
911

1012
jobs:
11-
osx-monetdb-Jul2021:
13+
osx-monetdb:
1214
runs-on: macos-10.15
15+
strategy:
16+
matrix:
17+
branch: [default, Jul2021]
1318
env:
14-
MONETDB_BRANCH: Jul2021
19+
MONETDB_BRANCH: ${{ matrix.branch }}
1520
steps:
1621
-
1722
name: brew packages
18-
run: brew install monetdb
19-
-
23+
run: brew install bison
24+
-
25+
name: download MonetDB
26+
run: curl https://dev.monetdb.org/hg/MonetDB/archive/${{ env.MONETDB_BRANCH }}.tar.bz2 -O
27+
-
28+
name: extract MonetDB
29+
run: tar jxvf ${{ env.MONETDB_BRANCH }}.tar.bz2
30+
-
31+
name: build monetdb
32+
run: |
33+
mkdir MonetDB-${{ env.MONETDB_BRANCH }}/build
34+
cd MonetDB-${{ env.MONETDB_BRANCH }}/build
35+
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/monetdb/11.40.1 \
36+
-DWITH_CRYPTO=OFF \
37+
-DPY3INTEGRATION=OFF \
38+
-DCMAKE_BUILD_TYPE=Release \
39+
-DASSERT=OFF \
40+
-DRINTEGRATION=OFF \
41+
-DBISON_EXECUTABLE=/usr/local/opt/bison/bin/bison
42+
make install -j3
43+
-
2044
uses: actions/upload-artifact@v2
2145
with:
2246
name: monetdb-osx-${{ env.MONETDB_BRANCH }}
2347
path: /usr/local/Cellar/monetdb
2448

25-
# osx-monetdb-default:
26-
# runs-on: macos-10.15
27-
# env:
28-
# MONETDB_BRANCH: default
29-
# steps:
30-
# -
31-
# name: brew packages
32-
# run: brew install bison
33-
# -
34-
# name: download MonetDB
35-
# run: curl https://dev.monetdb.org/hg/MonetDB/archive/${{ env.MONETDB_BRANCH }}.tar.bz2 -O
36-
# -
37-
# name: extract MonetDB
38-
# run: tar jxvf ${{ env.MONETDB_BRANCH }}.tar.bz2
39-
# -
40-
# name: build monetdb
41-
# run: |
42-
# mkdir MonetDB-${{ env.MONETDB_BRANCH }}/build
43-
# cd MonetDB-${{ env.MONETDB_BRANCH }}/build
44-
# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/monetdb/11.40.1 \
45-
# -DWITH_CRYPTO=OFF \
46-
# -DPY3INTEGRATION=OFF \
47-
# -DCMAKE_BUILD_TYPE=Release \
48-
# -DASSERT=OFF \
49-
# -DRINTEGRATION=OFF \
50-
# -DBISON_EXECUTABLE=/usr/local/opt/bison/bin/bison
51-
# make install -j3
52-
# -
53-
# uses: actions/upload-artifact@v2
54-
# with:
55-
# name: monetdb-osx-${{ env.MONETDB_BRANCH }}
56-
# path: /usr/local/Cellar/monetdb
57-
5849
osx-wheel:
5950
# continue-on-error: true
6051
runs-on: macos-10.15
6152
strategy:
6253
matrix:
6354
python-version: [ 3.6, 3.7, 3.8, 3.9 ]
64-
branch: [Jul2021]
55+
branch: [default, Jul2021]
6556
env:
6657
MONETDB_BRANCH: ${{ matrix.branch }}
67-
needs: [osx-monetdb-Jul2021]
58+
needs: [osx-monetdb]
6859
steps:
6960
-
7061
name: Print MONETDB_BRANCH to make sure it is set

.github/workflows/windows.yml

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
tags: [ "*" ]
77
pull_request:
88
branches: [ master ]
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
911

1012
# the prefix and version numbers for the branches need to be manually copied (for now) from:
1113
# http://monetdb.cwi.nl/testweb/web/status.php
@@ -14,14 +16,13 @@ jobs:
1416
runs-on: windows-2019
1517
strategy:
1618
matrix:
17-
MONETDB_BRANCH: [ Jul2021 ]
1819
include:
19-
# - MONETDB_BRANCH: default
20-
# MONETDB_WIN_PREFIX: 82411
21-
# MONETDB_WIN_VERSION: c51ff55bd174
20+
- MONETDB_BRANCH: default
21+
MONETDB_WIN_PREFIX: 82777
22+
MONETDB_WIN_VERSION: f619cf62c92d
2223
- MONETDB_BRANCH: Jul2021
23-
MONETDB_WIN_PREFIX: 82733
24-
MONETDB_WIN_VERSION: 3a8a37d1da2b
24+
MONETDB_WIN_PREFIX: 82786
25+
MONETDB_WIN_VERSION: e39e91cde6b5
2526

2627
env:
2728
MSI: MonetDB5-SQL-Installer-x86_64-${{ matrix.MONETDB_WIN_VERSION }}.msi
@@ -53,7 +54,7 @@ jobs:
5354
strategy:
5455
matrix:
5556
python-version: [ 3.6, 3.7, 3.8, 3.9 ]
56-
branch: [ Jul2021 ]
57+
branch: [ default, Jul2021 ]
5758
env:
5859
MONETDB_BRANCH: ${{ matrix.branch }}
5960
steps:
@@ -77,21 +78,7 @@ jobs:
7778
name: Collect DLLs
7879
run: |
7980
ls 'C:\Program Files\MonetDB\MonetDB5\bin'
80-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\bat.dll' monetdbe\.
81-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\mapi.dll' monetdbe\.
82-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\monetdb5.dll' monetdbe\.
83-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\monetdbe.dll' monetdbe\.
84-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\monetdbsql.dll' monetdbe\.
85-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\stream.dll' monetdbe\.
86-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\libcrypto-1_1-x64.dll' monetdbe\.
87-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\iconv-2.dll' monetdbe\.
88-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\libxml2.dll' monetdbe\.
89-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\pcre.dll' monetdbe\.
90-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\zlib1.dll' monetdbe\.
91-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\lzma.dll' monetdbe\.
92-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\charset-1.dll' monetdbe\.
93-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\bz2.dll' monetdbe\.
94-
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\lz4.dll' monetdbe\.
81+
Copy-Item 'C:\Program Files\MonetDB\MonetDB5\bin\*.dll' monetdbe\.
9582
ls monetdbe
9683
-
9784
name: Upgrade pip and wheel for all pythons

monetdbe/_cffi/builder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
print("**MONETDB**: If this is incorrect, set the MONETDB_BRANCH environment variable during monetdbe-python build\n")
1515

1616
branch_file = str(Path(__file__).parent / 'branch.py')
17+
newer_then_jul2021 = monetdb_branch.lower() not in ("oct2020", "jul2021")
1718

1819
with open(branch_file, 'w') as f:
1920
f.write("# this file is created by the cffi interface builder and contains the monetdb branch env variable.\n\n")
2021
f.write(f"monetdb_branch = '{monetdb_branch}'\n")
22+
f.write(f"newer_then_jul2021 = {newer_then_jul2021}\n")
2123

2224

2325
default = monetdb_branch.lower() in ("default", "jul2021")
@@ -38,7 +40,7 @@
3840
with open(embed_path, 'r') as f:
3941
content = f.read()
4042
template = Template(content)
41-
cdef = template.render(win32=win32, default=default)
43+
cdef = template.render(win32=win32, default=default, newer_then_jul2021=newer_then_jul2021)
4244
ffibuilder.cdef(cdef)
4345

4446

monetdbe/_cffi/convert/bind.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def monetdbe_int(data: int) -> ffi.CData:
1010
return ffi.new("int *", data)
1111

1212

13-
def bind_str(data: str) -> bytes:
14-
return str(data).encode()
13+
def bind_str(data: str) -> ffi.CData:
14+
return ffi.new("char[]", str(data).encode())
1515

1616

1717
def bind_float(data: float) -> ffi.CData:

monetdbe/_cffi/embed.h.j2

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ typedef struct {
2525

2626
typedef enum {
2727
monetdbe_bool, monetdbe_int8_t, monetdbe_int16_t, monetdbe_int32_t, monetdbe_int64_t,
28-
{% if not win32 %}
29-
{# windows does not support 128-bit #}
28+
{% if HAVE_HGE %}
3029
monetdbe_int128_t,
3130
{% endif %}
3231
monetdbe_size_t, monetdbe_float, monetdbe_double,
@@ -115,7 +114,11 @@ extern char* monetdbe_query(monetdbe_database dbhdl, char* query, monetdbe_resul
115114
extern char* monetdbe_result_fetch(monetdbe_result *mres, monetdbe_column** res, size_t column_index);
116115
extern char* monetdbe_cleanup_result(monetdbe_database dbhdl, monetdbe_result* result);
117116

117+
{% if newer_then_jul2021 %}
118+
extern char* monetdbe_prepare(monetdbe_database dbhdl, char *query, monetdbe_statement **stmt, monetdbe_result** result);
119+
{% else %}
118120
extern char* monetdbe_prepare(monetdbe_database dbhdl, char *query, monetdbe_statement **stmt);
121+
{% endif %}
119122
extern char* monetdbe_bind(monetdbe_statement *stmt, void *data, size_t parameter_nr);
120123
extern char* monetdbe_execute(monetdbe_statement *stmt, monetdbe_result **result, monetdbe_cnt* affected_rows);
121124
extern char* monetdbe_cleanup_statement(monetdbe_database dbhdl, monetdbe_statement *stmt);

monetdbe/_cffi/internal.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,14 @@ def __init__(
113113
self.nr_threads = nr_threads
114114
self.have_hge = have_hge
115115
self._switch()
116+
self._monetdbe_database = self.open()
116117

117118
@classmethod
118119
def set_active_context(cls, active_context: Optional['Internal']):
119120
cls._active_context = active_context
120121

121-
@classmethod
122-
def set_active_connection(cls, active_connection: Optional['Connection']):
123-
cls._active_connection = active_connection
124-
125-
@classmethod
126-
def set_monetdbe_database(cls, connection: Optional[monetdbe_database]):
127-
cls._monetdbe_database = connection
122+
def set_monetdbe_database(self, connection: Optional[monetdbe_database]):
123+
self._monetdbe_database = connection
128124

129125
def __del__(self):
130126
if self._active_context == self:
@@ -135,17 +131,10 @@ def _switch(self):
135131
if self._active_context == self:
136132
return
137133

138-
# this is a bit scary but just to make sure the previous connection
139-
# can't touch us anymore
140-
if self._active_connection:
141-
self._active_connection._internal = None
142-
143-
self.close()
144-
self.set_monetdbe_database(self.open())
145134
self.set_active_context(self)
146-
self.set_active_connection(self._connection)
147135

148136
def cleanup_result(self, result: monetdbe_result):
137+
self._switch()
149138
_logger.info("cleanup_result called")
150139
if result and self._monetdbe_database:
151140
check_error(lib.monetdbe_cleanup_result(self._monetdbe_database, result))
@@ -180,11 +169,13 @@ def open(self) -> monetdbe_database:
180169
lib.monetdbe_close(connection)
181170
else:
182171
error = errors.get(result_code, "unknown error")
183-
raise exceptions.OperationalError(f"Failed to open database: {error} (code {result_code})")
172+
msg = f"Failed to open database: {error} (code {result_code})"
173+
raise exceptions.OperationalError(msg)
184174

185175
return connection
186176

187177
def close(self) -> None:
178+
self._switch()
188179
if self._monetdbe_database:
189180
if lib.monetdbe_close(self._monetdbe_database):
190181
raise exceptions.OperationalError("Failed to close database")
@@ -193,8 +184,6 @@ def close(self) -> None:
193184
if self._active_context:
194185
self.set_active_context(None)
195186

196-
self.set_active_connection(None)
197-
198187
def query(self, query: str, make_result: bool = False) -> Tuple[Optional[Any], int]:
199188
"""
200189
Execute a query.
@@ -268,7 +257,7 @@ def append(self, table: str, data: Mapping[str, np.ndarray], schema: str = 'sys'
268257
def prepare(self, query: str) -> monetdbe_statement:
269258
self._switch()
270259
stmt = ffi.new("monetdbe_statement **")
271-
check_error(lib.monetdbe_prepare(self._monetdbe_database, str(query).encode(), stmt))
260+
check_error(lib.monetdbe_prepare(self._monetdbe_database, str(query).encode(), stmt, ffi.NULL))
272261
return stmt[0]
273262

274263
def cleanup_statement(self, statement: monetdbe_statement) -> None:
@@ -296,3 +285,14 @@ def get_columns(self, table: str, schema: str = 'sys') -> Iterator[Tuple[str, in
296285
name = ffi.string(names_p[0][i]).decode()
297286
type_ = types_p[0][i]
298287
yield name, type_
288+
289+
290+
from monetdbe._cffi.branch import newer_then_jul2021
291+
if not newer_then_jul2021:
292+
def prepare(self, query: str) -> monetdbe_statement:
293+
self._switch()
294+
stmt = ffi.new("monetdbe_statement **")
295+
check_error(lib.monetdbe_prepare(self._monetdbe_database, str(query).encode(), stmt))
296+
return stmt[0]
297+
298+
setattr(Internal, 'prepare', prepare)

monetdbe/cursors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def __init__(self, con: 'Connection'):
3737

3838
self._fetch_generator: Optional[Iterator['Row']] = None
3939

40+
def __enter__(self):
41+
return self
42+
43+
def __exit__(self, exc_type, exc_value, traceback):
44+
self.close()
45+
4046
def __del__(self):
4147
self.close()
4248

0 commit comments

Comments
 (0)