pglib 5.8.2__tar.gz → 5.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {pglib-5.8.2/pglib.egg-info → pglib-5.9.0}/PKG-INFO +1 -4
  2. {pglib-5.8.2 → pglib-5.9.0/pglib.egg-info}/PKG-INFO +1 -4
  3. {pglib-5.8.2 → pglib-5.9.0}/setup.py +1 -1
  4. {pglib-5.8.2 → pglib-5.9.0}/src/getdata.cpp +7 -2
  5. {pglib-5.8.2 → pglib-5.9.0}/src/pgarrays.cpp +172 -59
  6. {pglib-5.8.2 → pglib-5.9.0}/src/pgarrays.h +1 -0
  7. {pglib-5.8.2 → pglib-5.9.0}/src/pglib.h +2 -1
  8. {pglib-5.8.2 → pglib-5.9.0}/test/test_sync.py +10 -0
  9. {pglib-5.8.2 → pglib-5.9.0}/LICENSE +0 -0
  10. {pglib-5.8.2 → pglib-5.9.0}/MANIFEST.in +0 -0
  11. {pglib-5.8.2 → pglib-5.9.0}/README.rst +0 -0
  12. {pglib-5.8.2 → pglib-5.9.0}/pglib/__init__.py +0 -0
  13. {pglib-5.8.2 → pglib-5.9.0}/pglib/asyncpglib.py +0 -0
  14. {pglib-5.8.2 → pglib-5.9.0}/pglib.egg-info/SOURCES.txt +0 -0
  15. {pglib-5.8.2 → pglib-5.9.0}/pglib.egg-info/dependency_links.txt +0 -0
  16. {pglib-5.8.2 → pglib-5.9.0}/pglib.egg-info/top_level.txt +0 -0
  17. {pglib-5.8.2 → pglib-5.9.0}/setup.cfg +0 -0
  18. {pglib-5.8.2 → pglib-5.9.0}/src/byteswap.h +0 -0
  19. {pglib-5.8.2 → pglib-5.9.0}/src/connection.cpp +0 -0
  20. {pglib-5.8.2 → pglib-5.9.0}/src/connection.h +0 -0
  21. {pglib-5.8.2 → pglib-5.9.0}/src/conninfoopt.cpp +0 -0
  22. {pglib-5.8.2 → pglib-5.9.0}/src/conninfoopt.h +0 -0
  23. {pglib-5.8.2 → pglib-5.9.0}/src/datatypes.cpp +0 -0
  24. {pglib-5.8.2 → pglib-5.9.0}/src/datatypes.h +0 -0
  25. {pglib-5.8.2 → pglib-5.9.0}/src/debug.cpp +0 -0
  26. {pglib-5.8.2 → pglib-5.9.0}/src/debug.h +0 -0
  27. {pglib-5.8.2 → pglib-5.9.0}/src/enums.cpp +0 -0
  28. {pglib-5.8.2 → pglib-5.9.0}/src/enums.h +0 -0
  29. {pglib-5.8.2 → pglib-5.9.0}/src/errors.cpp +0 -0
  30. {pglib-5.8.2 → pglib-5.9.0}/src/errors.h +0 -0
  31. {pglib-5.8.2 → pglib-5.9.0}/src/getdata.h +0 -0
  32. {pglib-5.8.2 → pglib-5.9.0}/src/juliandate.cpp +0 -0
  33. {pglib-5.8.2 → pglib-5.9.0}/src/juliandate.h +0 -0
  34. {pglib-5.8.2 → pglib-5.9.0}/src/params.cpp +0 -0
  35. {pglib-5.8.2 → pglib-5.9.0}/src/params.h +0 -0
  36. {pglib-5.8.2 → pglib-5.9.0}/src/pglib.cpp +0 -0
  37. {pglib-5.8.2 → pglib-5.9.0}/src/pgtypes.h +0 -0
  38. {pglib-5.8.2 → pglib-5.9.0}/src/resultset.cpp +0 -0
  39. {pglib-5.8.2 → pglib-5.9.0}/src/resultset.h +0 -0
  40. {pglib-5.8.2 → pglib-5.9.0}/src/row.cpp +0 -0
  41. {pglib-5.8.2 → pglib-5.9.0}/src/row.h +0 -0
  42. {pglib-5.8.2 → pglib-5.9.0}/src/runtime.cpp +0 -0
  43. {pglib-5.8.2 → pglib-5.9.0}/src/runtime.h +0 -0
  44. {pglib-5.8.2 → pglib-5.9.0}/src/type_hstore.cpp +0 -0
  45. {pglib-5.8.2 → pglib-5.9.0}/src/type_hstore.h +0 -0
  46. {pglib-5.8.2 → pglib-5.9.0}/src/type_json.cpp +0 -0
  47. {pglib-5.8.2 → pglib-5.9.0}/src/type_json.h +0 -0
  48. {pglib-5.8.2 → pglib-5.9.0}/src/type_ltree.cpp +0 -0
  49. {pglib-5.8.2 → pglib-5.9.0}/src/type_ltree.h +0 -0
  50. {pglib-5.8.2 → pglib-5.9.0}/test/test_async.py +0 -0
  51. {pglib-5.8.2 → pglib-5.9.0}/test/testutils.py +0 -0
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pglib
3
- Version: 5.8.2
3
+ Version: 5.9.0
4
4
  Summary: A PostgreSQL interface
5
5
  Home-page: https://gitlab.com/mkleehammer/pglib
6
6
  Maintainer: Michael Kleehammer
7
7
  Maintainer-email: michael@kleehammer.com
8
8
  License: MIT
9
9
  Keywords: postgresql postgres
10
- Platform: UNKNOWN
11
10
  Classifier: Development Status :: 5 - Production/Stable
12
11
  Classifier: Intended Audience :: Developers
13
12
  Classifier: Topic :: Database
@@ -21,5 +20,3 @@ A PostgreSQL interface for Python.
21
20
  This provides an interface to the libpq library. It is not an DB API
22
21
  library, but is instead designed to match the interface PostgreSQL
23
22
  offers.
24
-
25
-
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pglib
3
- Version: 5.8.2
3
+ Version: 5.9.0
4
4
  Summary: A PostgreSQL interface
5
5
  Home-page: https://gitlab.com/mkleehammer/pglib
6
6
  Maintainer: Michael Kleehammer
7
7
  Maintainer-email: michael@kleehammer.com
8
8
  License: MIT
9
9
  Keywords: postgresql postgres
10
- Platform: UNKNOWN
11
10
  Classifier: Development Status :: 5 - Production/Stable
12
11
  Classifier: Intended Audience :: Developers
13
12
  Classifier: Topic :: Database
@@ -21,5 +20,3 @@ A PostgreSQL interface for Python.
21
20
  This provides an interface to the libpq library. It is not an DB API
22
21
  library, but is instead designed to match the interface PostgreSQL
23
22
  offers.
24
-
25
-
@@ -130,7 +130,7 @@ def _get_settings():
130
130
 
131
131
  setup(
132
132
  name='pglib',
133
- version='5.8.2',
133
+ version='5.9.0',
134
134
  description='A PostgreSQL interface',
135
135
  long_description=long_description,
136
136
  maintainer='Michael Kleehammer',
@@ -286,8 +286,10 @@ static PyObject* GetInterval(const char* p) // , bool integer_datetimes)
286
286
  uint32_t month = swaps4(pinterval->month);
287
287
  uint32_t year = month / 12;
288
288
 
289
- if (month || year)
290
- return PyErr_Format(Error, "Years and months are not supported in intervals");
289
+ if (month || year) {
290
+ PyErr_SetString(Error, "Years and months are not supported in intervals");
291
+ return 0;
292
+ }
291
293
 
292
294
  int seconds = (second) + (60 * minute) + (3600 * hour);
293
295
  int days = day;
@@ -399,6 +401,9 @@ PyObject* ConvertValue(PGresult* result, int iRow, int iCol, bool integer_dateti
399
401
  case TEXTARRAYOID:
400
402
  return GetTextArray(p);
401
403
 
404
+ case JSONBARRAYOID:
405
+ return GetJSONBArray(p);
406
+
402
407
  case DATEARRAYOID:
403
408
  return GetDateArray(p);
404
409
 
@@ -1,4 +1,3 @@
1
-
2
1
  // Sending and receiving arrays. Right now we only support strings and integers, but there is
3
2
  // no reason we can't add more types.
4
3
  //
@@ -28,6 +27,7 @@
28
27
  #include "params.h"
29
28
  #include "juliandate.h"
30
29
  #include "errors.h"
30
+ #include "runtime.h"
31
31
  #include "debug.h"
32
32
 
33
33
 
@@ -39,6 +39,12 @@ void Arrays_Init()
39
39
 
40
40
  struct ArrayHeader
41
41
  {
42
+ // The fields in a PostgreSQL array. We use this when writing an array. All int fields
43
+ // will be in host / big endian, so use ntohl and htonl.
44
+ //
45
+ // Technically an array header only includes the first 3 fields. Each dimension then has a
46
+ // count and lbound. Since we only support one dimension right now, I've put them together.
47
+
42
48
  uint32_t ndim; // number of dimensions - we support 1
43
49
  uint32_t flags; // binary format? 0 or 1, but unused by the server?
44
50
  Oid oid; // type of elements
@@ -47,6 +53,46 @@ struct ArrayHeader
47
53
  char buffer[0];
48
54
  };
49
55
 
56
+
57
+ struct ArrayInfo {
58
+ // The important array fields, copied and converted to native endianess (so don't use ntohl
59
+ // or htonl).
60
+ Oid oid; // type of elements
61
+ uint32_t len; // number of elements
62
+ const char* data; // pointer to first element
63
+ };
64
+
65
+
66
+ inline uint8_t ReadU8(const char ** pp) {
67
+ // Reads an unsigned int from `pp` *and* increments the pointer *pp.
68
+ uint8_t n = **(uint8_t**)pp;
69
+ *pp += sizeof(char);
70
+ return n;
71
+ }
72
+
73
+ inline int64_t Read64(const char ** pp) {
74
+ // Reads an int64 from `pp` *and* increments the pointer *pp.
75
+ uint64_t n = **(uint64_t**)pp;
76
+ *pp += sizeof(uint64_t);
77
+ return swaps8(n);
78
+ }
79
+
80
+ inline int32_t Read32(const char ** pp) {
81
+ // Reads an int from `pp` *and* increments the pointer *pp.
82
+ uint32_t n = **(uint32_t**)pp;
83
+ *pp += sizeof(uint32_t);
84
+ return (int32_t)htonl(n);
85
+ }
86
+
87
+ inline uint32_t ReadU32(const char ** pp) {
88
+ // Reads an unsigned int from `pp` *and* increments the pointer *pp.
89
+ uint32_t n = **(uint32_t**)pp;
90
+ *pp += sizeof(uint32_t);
91
+ return htonl(n);
92
+ }
93
+
94
+
95
+
50
96
  static PyObject* FindFirstParam(Object& seq)
51
97
  {
52
98
  // Returns a *borrowed* reference to the first non-None parameter in the sequence. Returns
@@ -63,6 +109,40 @@ static PyObject* FindFirstParam(Object& seq)
63
109
  }
64
110
 
65
111
 
112
+ static bool ReadArrayInfo(const char* p, ArrayInfo &info, Oid expected_type) {
113
+ // Read the array header from `p` into `info` and return true if valid and something pglib
114
+ // can handle. If there is an error, an exception will be set and false will be returned.
115
+
116
+ uint32_t ndim = ReadU32(&p);
117
+ if (ndim > 1) {
118
+ SetStringError(Error, "pglib can only read single dimensional arrays (ndim=%d)", (int)ndim);
119
+ return false;
120
+ }
121
+
122
+ ReadU32(&p); // format: 0 or 1. Not used right now.
123
+
124
+ info.oid = (Oid)ReadU32(&p);
125
+ if (info.oid != expected_type) {
126
+ SetStringError(Error, "Unable to read array. Expected element type %u not %u",
127
+ (uint)expected_type, (uint)info.oid);
128
+ return false;
129
+ }
130
+
131
+ // The length is within the dimension header, which we don't have if there are no dimensions.
132
+
133
+ if (ndim != 0) {
134
+ info.len = ReadU32(&p);
135
+ ReadU32(&p); // lower bound
136
+ } else {
137
+ info.len = 0;
138
+ }
139
+
140
+ info.data = p;
141
+
142
+ return true;
143
+ }
144
+
145
+
66
146
  static bool BindDateArray(Params& params, Object& seq, Py_ssize_t cItems)
67
147
  {
68
148
  // Loop through the array and make sure each entry is a date.
@@ -328,26 +408,22 @@ bool BindArray(Params& params, PyObject* param)
328
408
  PyObject* GetDateArray(const char* p)
329
409
  {
330
410
  // Reads a DATEARRAYOID array result and returns a list of datetime.date objects.
411
+ ArrayInfo info;
412
+ if (!ReadArrayInfo(p, info, DATEOID)) {
413
+ return 0;
414
+ }
331
415
 
332
- const ArrayHeader* phdr = (const ArrayHeader*)p;
333
-
334
- if (ntohl(phdr->ndim) > 1)
335
- return SetStringError(Error, "pglib can only read single dimensional arrays (ndim=%d)",
336
- ntohl(phdr->ndim));
337
-
338
- Py_ssize_t dim = ntohl(phdr->ndim) == 0 ? 0 : ntohl(phdr->dim);
339
-
340
- Object list(PyList_New(dim));
416
+ Object list(PyList_New(info.len));
341
417
  if (!list)
342
418
  return 0;
343
419
 
344
- if (dim > 0)
345
- {
346
- int32_t* pT = (int32_t*)&phdr->buffer[0];
420
+ if (info.len) {
421
+ const char* data = info.data;
347
422
 
348
- for (int i = 0; i < dim; i++)
423
+ for (uint32_t i = 0; i < info.len; i++)
349
424
  {
350
- int32_t len = ntohl(*pT++);
425
+ int32_t len = Read32(&data);
426
+
351
427
  if (len == -1)
352
428
  {
353
429
  Py_INCREF(Py_None);
@@ -355,7 +431,7 @@ PyObject* GetDateArray(const char* p)
355
431
  }
356
432
  else
357
433
  {
358
- uint32_t value = swapu4(*pT++) + JULIAN_START;
434
+ uint32_t value = Read32(&data) + JULIAN_START;
359
435
  int year, month, date;
360
436
  julianToDate(value, year, month, date);
361
437
  PyObject* d = PyDate_FromDate(year, month, date);
@@ -374,25 +450,22 @@ PyObject* GetInt4Array(const char* p)
374
450
  {
375
451
  // Reads an INT4ARRAYOID array result and returns a list of integers.
376
452
 
377
- const ArrayHeader* phdr = (const ArrayHeader*)p;
378
-
379
- if (ntohl(phdr->ndim) > 1)
380
- return SetStringError(Error, "pglib can only read single dimensional arrays (ndim=%d)",
381
- ntohl(phdr->ndim));
382
-
383
- Py_ssize_t dim = ntohl(phdr->ndim) == 0 ? 0 : ntohl(phdr->dim);
453
+ ArrayInfo info;
454
+ if (!ReadArrayInfo(p, info, INT4OID)) {
455
+ return 0;
456
+ }
384
457
 
385
- Object list(PyList_New(dim));
458
+ Object list(PyList_New(info.len));
386
459
  if (!list)
387
460
  return 0;
388
461
 
389
- if (dim > 0)
390
- {
391
- int32_t* pT = (int32_t*)&phdr->buffer[0];
462
+ if (info.len) {
463
+ const char* data = info.data;
392
464
 
393
- for (int i = 0; i < dim; i++)
465
+ for (uint32_t i = 0; i < info.len; i++)
394
466
  {
395
- int32_t len = ntohl(*pT++);
467
+ int32_t len = Read32(&data);
468
+
396
469
  if (len == -1)
397
470
  {
398
471
  Py_INCREF(Py_None);
@@ -400,10 +473,11 @@ PyObject* GetInt4Array(const char* p)
400
473
  }
401
474
  else
402
475
  {
403
- int32_t val = ntohl(*pT++);
476
+ int32_t val = Read32(&data);
404
477
  PyObject* l = PyLong_FromLong(val);
405
478
  if (!l)
406
479
  return 0;
480
+
407
481
  PyList_SET_ITEM(list.Get(), i, l);
408
482
  }
409
483
  }
@@ -416,26 +490,21 @@ PyObject* GetInt8Array(const char* p)
416
490
  {
417
491
  // Reads an INT4ARRAYOID array result and returns a list of integers.
418
492
 
419
- const ArrayHeader* phdr = (const ArrayHeader*)p;
420
-
421
- if (ntohl(phdr->ndim) > 1)
422
- return SetStringError(Error, "pglib can only read single dimensional arrays (ndim=%d)",
423
- ntohl(phdr->ndim));
424
-
425
- Py_ssize_t dim = ntohl(phdr->ndim) == 0 ? 0 : ntohl(phdr->dim);
493
+ ArrayInfo info;
494
+ if (!ReadArrayInfo(p, info, INT8OID)) {
495
+ return 0;
496
+ }
426
497
 
427
- Object list(PyList_New(dim));
498
+ Object list(PyList_New(info.len));
428
499
  if (!list)
429
500
  return 0;
430
501
 
431
- if (dim > 0)
432
- {
433
- const char* pT = &phdr->buffer[0];
502
+ if (info.len) {
503
+ const char* data = info.data;
434
504
 
435
- for (int i = 0; i < dim; i++)
505
+ for (uint32_t i = 0; i < info.len; i++)
436
506
  {
437
- int32_t len = ntohl(*(int32_t*)pT);
438
- pT += 4;
507
+ int32_t len = Read32(&data);
439
508
 
440
509
  if (len == -1)
441
510
  {
@@ -444,11 +513,11 @@ PyObject* GetInt8Array(const char* p)
444
513
  }
445
514
  else
446
515
  {
447
- int64_t val = swapu8(*(uint64_t*)pT);
448
- pT += 8;
516
+ int64_t val = Read64(&data);
449
517
  PyObject* l = PyLong_FromLongLong(val);
450
518
  if (!l)
451
519
  return 0;
520
+
452
521
  PyList_SET_ITEM(list.Get(), i, l);
453
522
  }
454
523
  }
@@ -458,28 +527,25 @@ PyObject* GetInt8Array(const char* p)
458
527
  }
459
528
 
460
529
 
530
+
461
531
  PyObject* GetTextArray(const char* p)
462
532
  {
463
- const ArrayHeader* phdr = (const ArrayHeader*)p;
464
-
465
- if (ntohl(phdr->ndim) > 1)
466
- return SetStringError(Error, "pglib can only read single dimensional arrays (ndim=%d)",
467
- ntohl(phdr->ndim));
468
-
469
- Py_ssize_t dim = ntohl(phdr->ndim) == 0 ? 0 : ntohl(phdr->dim);
533
+ ArrayInfo info;
534
+ if (!ReadArrayInfo(p, info, TEXTOID)) {
535
+ return 0;
536
+ }
470
537
 
471
- Object list(PyList_New(dim));
538
+ Object list(PyList_New(info.len));
472
539
  if (!list)
473
540
  return 0;
474
541
 
475
- if (dim > 0)
542
+ if (info.len > 0)
476
543
  {
477
- const char* pT = &phdr->buffer[0];
544
+ const char* pT = info.data;
478
545
 
479
- for (int i = 0; i < dim; i++)
546
+ for (uint32_t i = 0; i < info.len; i++)
480
547
  {
481
- int32_t len = ntohl(*(uint32_t*)pT);
482
- pT += 4;
548
+ int32_t len = Read32(&pT);
483
549
 
484
550
  if (len == -1)
485
551
  {
@@ -499,3 +565,50 @@ PyObject* GetTextArray(const char* p)
499
565
 
500
566
  return list.Detach();
501
567
  }
568
+
569
+
570
+ PyObject* GetJSONBArray(const char* p) {
571
+ ArrayInfo info;
572
+ if (!ReadArrayInfo(p, info, JSONBOID)) {
573
+ return 0;
574
+ }
575
+
576
+ Object list(PyList_New(info.len));
577
+ if (!list)
578
+ return 0;
579
+
580
+ if (info.len > 0) {
581
+ PyObject* func = GetFunction("json.loads");
582
+ if (!func)
583
+ return 0;
584
+
585
+ const char* data = info.data;
586
+
587
+ for (uint32_t i = 0; i < info.len; i++) {
588
+ // Each element is (len, version (1), cstr)
589
+
590
+ int32_t len = Read32(&data);
591
+
592
+ PyObject* value = 0;
593
+ if (len == -1) {
594
+ value = Py_None;
595
+ Py_INCREF(Py_None);
596
+ } else {
597
+ uint8_t version = ReadU8(&data);
598
+
599
+ if (version != 1) {
600
+ return SetStringError(Error, "Unexpected JSONB version %u in array", (uint32_t)version);
601
+ }
602
+
603
+ value = PyObject_CallFunction(func, "s", data);
604
+ if (!value)
605
+ return 0;
606
+ data += len - 1; // -1 because we already incremented past version
607
+ }
608
+
609
+ PyList_SET_ITEM(list.Get(), i, value);
610
+ }
611
+ }
612
+
613
+ return list.Detach();
614
+ }
@@ -14,5 +14,6 @@ PyObject* GetInt4Array(const char* p);
14
14
  PyObject* GetInt8Array(const char* p);
15
15
  PyObject* GetTextArray(const char* p);
16
16
  PyObject* GetDateArray(const char* p);
17
+ PyObject* GetJSONBArray(const char* p);
17
18
 
18
19
  #endif
@@ -57,7 +57,7 @@ inline void UNUSED(...) { }
57
57
 
58
58
  extern PyObject* Error;
59
59
 
60
- // From pg_type.h
60
+ // From src/include/catalog/pg_type.dat
61
61
 
62
62
  #define ANYARRAYOID 2277
63
63
  #define ANYOID 2276
@@ -89,6 +89,7 @@ extern PyObject* Error;
89
89
  #define VARCHAROID 1043
90
90
  #define JSONOID 114
91
91
  #define JSONBOID 3802
92
+ #define JSONBARRAYOID 3807
92
93
 
93
94
  // #define TIMESTAMPARRAYOID 1115
94
95
  // #define TIMEARRAYOID 1183
@@ -1230,6 +1230,16 @@ def test_empty_jsonb(cnxn):
1230
1230
  assert value == result
1231
1231
 
1232
1232
 
1233
+ def test_jsonb_array(cnxn):
1234
+ # REVIEW: We can only read these for now.
1235
+ value = [{"a": 1}, {"a": 22}]
1236
+ cnxn.execute("create table t1(j jsonb)")
1237
+ cnxn.execute("""insert into t1 values('{"a": 1}')""")
1238
+ cnxn.execute("""insert into t1 values('{"a": 22}')""")
1239
+
1240
+ result = cnxn.fetchval("select array_agg(j) from t1")
1241
+ assert result == value
1242
+
1233
1243
  def test_info(cnxn):
1234
1244
  info = cnxn.info()
1235
1245
  assert info['dbname'] == 'test'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes