You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -175,55 +175,78 @@ The results object is a JSON object that contains the metadata and data from the
175
175
-`is_done` field indicates whether the query has finished executing
176
176
-`success` field indicates whether the query was successful.
177
177
178
-
### Query and Run
178
+
### Configure Connection Details with `.ini` file
179
179
180
-
To create and run a query in a single step, use the `query_and_run` method:
180
+
The connection details can be stored in a `.ini` file and passed directly to `SQLJob` or `PoolJob` objects:
181
+
182
+
```ini title=mapepire.ini
183
+
[myserver]
184
+
SERVER="SERVER"
185
+
PORT="PORT"
186
+
USER="USER"
187
+
PASSWORD="PASSWORD"
188
+
```
189
+
190
+
Then pass the path to the `.ini` file and the section name to the `SQLJob` object:
181
191
182
192
```python
183
-
import configparser
184
193
from mapepire_python.client.sql_job import SQLJob
185
-
from mapepire_python.data_types import DaemonServer
186
194
187
-
config = configparser.ConfigParser()
188
-
config.read('mapepire.ini')
195
+
with SQLJob("./mapepire.ini", section="myserver") as sql_job:
196
+
with sql_job.query("select * from sample.employee") as query:
197
+
result = query.run(rows_to_fetch=1)
198
+
print(result)
199
+
```
189
200
190
-
creds = DaemonServer(
191
-
host=config['mapepire']['SERVER'],
192
-
port=config['mapepire']['PORT'],
193
-
user=config['mapepire']['USER'],
194
-
password=config['mapepire']['PASSWORD'],
195
-
ignoreUnauthorized=True
196
-
)
201
+
If `section` is not provided, the first section in the `.ini` file will be used.
197
202
198
-
with SQLJob(creds) as sql_job:
203
+
204
+
## Running Queries
205
+
206
+
The following examples all assume that the connection details are stored in a `.ini` file called `mapepire.ini` in the root of the project.
207
+
208
+
There are four main ways to run queries using `mapepire-python`:
209
+
1. Using the `SQLJob` object to run queries synchronously
210
+
2. Using the `PoolJob` object to run queries asynchronously
211
+
3. Using the `Pool` object to run queries "concurrently"
212
+
4. Using PEP 249 Implementation
213
+
214
+
### 1. Using the `SQLJob` object to run queries synchronously
215
+
216
+
Using python context managers, the `SQLJob` object can be used to create and run queries synchronously. `sql_job` and `query` objects are automatically closed after running the query.
217
+
218
+
```python
219
+
from mapepire_python.client.sql_job import SQLJob
220
+
221
+
with SQLJob("./mapepire.ini") as sql_job:
222
+
with sql_job.query("select * from sample.employee") as query:
223
+
result = query.run(rows_to_fetch=1)
224
+
print(result)
225
+
```
226
+
227
+
#### Query and run
228
+
229
+
To create and run a query in a single step, use the `query_and_run` method:
230
+
231
+
```python
232
+
from mapepire_python.client.sql_job import SQLJob
233
+
234
+
with SQLJob("./mapepire.ini") as sql_job:
199
235
# query automatically closed after running
200
236
results = sql_job.query_and_run("select * from sample.employee", rows_to_fetch=1)
201
237
print(result)
202
238
```
203
239
204
-
### Asynchronous Query Execution
240
+
### 2. Using the `PoolJob` object to run queries asynchronously
205
241
206
242
The `PoolJob` object can be used to create and run queries asynchronously:
207
243
208
244
```python
209
245
import asyncio
210
-
import configparser
211
246
from mapepire_python.pool.pool_job import PoolJob
212
-
from mapepire_python.data_types import DaemonServer
213
-
214
-
config = configparser.ConfigParser()
215
-
config.read('mapepire.ini')
216
-
217
-
creds = DaemonServer(
218
-
host=config['mapepire']['SERVER'],
219
-
port=config['mapepire']['PORT'],
220
-
user=config['mapepire']['USER'],
221
-
password=config['mapepire']['PASSWORD'],
222
-
ignoreUnauthorized=True
223
-
)
224
247
225
248
asyncdefmain():
226
-
asyncwith PoolJob(creds=creds) as pool_job:
249
+
asyncwith PoolJob("./mapepire.ini") as pool_job:
227
250
asyncwith pool_job.query('select * from sample.employee') as query:
228
251
res =await query.run(rows_to_fetch=1)
229
252
@@ -236,24 +259,11 @@ To run a create and run a query asynchronously in a single step, use the `query_
236
259
237
260
```python
238
261
import asyncio
239
-
import configparser
240
262
from mapepire_python.pool.pool_job import PoolJob
241
-
from mapepire_python.data_types import DaemonServer
242
-
243
-
config = configparser.ConfigParser()
244
-
config.read('mapepire.ini')
245
-
246
-
creds = DaemonServer(
247
-
host=config['mapepire']['SERVER'],
248
-
port=config['mapepire']['PORT'],
249
-
user=config['mapepire']['USER'],
250
-
password=config['mapepire']['PASSWORD'],
251
-
ignoreUnauthorized=True
252
-
)
253
263
254
264
asyncdefmain():
255
-
asyncwith PoolJob(creds=creds) as pool_job:
256
-
res =await pool_job.query_and_run(rows_to_fetch=1)
265
+
asyncwith PoolJob("./mapepire.ini") as pool_job:
266
+
res =await pool_job.query_and_run("select * from sample.employee", rows_to_fetch=1)
257
267
print(res)
258
268
259
269
if__name__=='__main__':
@@ -262,32 +272,18 @@ if __name__ == '__main__':
262
272
```
263
273
264
274
265
-
##Pooling (beta)
275
+
### 3. Using the `Pool` object to run queries "concurrently"
266
276
267
277
The `Pool` object can be used to create a pool of `PoolJob` objects to run queries concurrently.
268
278
269
279
```python
270
280
import asyncio
271
-
import configparser
272
281
from mapepire_python.pool.pool_client import Pool, PoolOptions
273
-
from mapepire_python.data_types import DaemonServer
274
-
275
-
config = configparser.ConfigParser()
276
-
config.read('mapepire.ini')
277
-
278
-
creds = DaemonServer(
279
-
host=config['mapepire']['SERVER'],
280
-
port=config['mapepire']['PORT'],
281
-
user=config['mapepire']['USER'],
282
-
password=config['mapepire']['PASSWORD'],
283
-
ignoreUnauthorized=True
284
-
)
285
-
286
282
287
283
asyncdefmain():
288
284
asyncwith Pool(
289
285
options=PoolOptions(
290
-
creds=creds,
286
+
creds="./mapepire.ini",
291
287
opts=None,
292
288
max_size=5,
293
289
starting_size=3
@@ -306,14 +302,65 @@ async def main():
306
302
307
303
if__name__=='__main__':
308
304
asyncio.run(main())
309
-
310
305
```
311
306
This script will create a pool of 3 `PoolJob` objects and run the query `values (job_name)` concurrently. The results will be printed to the console.
PEP 249 is the Python Database API Specification v2.0. The `mapepire-python` client provides a PEP 249 implementation that allows you to use the `Connection` and `Cursor` objects to interact with the Mapepire server. Like the examples above, we can pass the `mapepire.ini` file to the `connect` function to create a connection to the server:
315
+
316
+
```python
317
+
from mapepire_python import connect
318
+
319
+
with connect("./mapepire.ini") as conn:
320
+
with conn.execute("select * from sample.employee") as cursor:
321
+
result = cursor.fetchone()
322
+
print(result)
323
+
```
324
+
325
+
#### `fetchmany()` and `fetchall()` methods
326
+
327
+
The `Cursor` object provides the `fetchmany()` and `fetchall()` methods to fetch multiple rows from the result set:
328
+
329
+
```python
330
+
with connect("./mapepire.ini") as conn:
331
+
with conn.execute("select * from sample.employee") as cursor:
332
+
results = cursor.fetchmany(size=2)
333
+
print(results)
334
+
```
335
+
---
336
+
337
+
```python
338
+
with connect("./mapepire.ini") as conn:
339
+
with conn.execute("select * from sample.employee") as cursor:
340
+
results = cursor.fetchall()
341
+
print(results)
342
+
```
343
+
344
+
### PEP 249 Asynchronous Implementation
345
+
346
+
The PEP 249 implementation also provides an asynchronous interface for running queries. The `connect` function returns an asynchronous context manager that can be used with the `async with` statement:
347
+
348
+
```python
349
+
import asyncio
350
+
from mapepire_python.asycnio import connect
351
+
352
+
asyncdefmain():
353
+
asyncwith connect("./mapepire.ini") as conn:
354
+
asyncwithawait conn.execute("select * from sample.employee") as cursor:
355
+
result =await cursor.fetchone()
356
+
print(result)
357
+
358
+
if__name__=='__main__':
359
+
asyncio.run(main())
360
+
```
361
+
362
+
363
+
317
364
## Allow all certificates
318
365
319
366
On the `DaemonServer` interface, the `ignoreUnauthorized` set to `true` will allow either self-signed certificates or certificates from a CA.
0 commit comments