harlequin-postgres 0.2.2__tar.gz → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: harlequin-postgres
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: A Harlequin adapter for Postgres.
5
5
  License: MIT
6
6
  Author: Ted Conbeer
@@ -11,7 +11,7 @@ Classifier: Programming Language :: Python :: 3
11
11
  Classifier: Programming Language :: Python :: 3.9
12
12
  Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
- Requires-Dist: harlequin (>=1.7,<2.0)
14
+ Requires-Dist: harlequin (>=1.20,<2.0)
15
15
  Requires-Dist: psycopg2-binary (>=2.9.9,<3.0.0)
16
16
  Description-Content-Type: text/markdown
17
17
 
@@ -85,4 +85,10 @@ Many more options are available; to see the full list, run:
85
85
  harlequin --help
86
86
  ```
87
87
 
88
+ ## Manual Transactions
89
+
90
+ To use Manual transaction mode, click on the label in the Run Query Bar to toggle the transaction mode from Auto to Manual.
91
+
92
+ ## Further Documentation
93
+
88
94
  For more information, see the [Harlequin Docs](https://harlequin.sh/docs/postgres/index).
@@ -68,4 +68,10 @@ Many more options are available; to see the full list, run:
68
68
  harlequin --help
69
69
  ```
70
70
 
71
+ ## Manual Transactions
72
+
73
+ To use Manual transaction mode, click on the label in the Run Query Bar to toggle the transaction mode from Auto to Manual.
74
+
75
+ ## Further Documentation
76
+
71
77
  For more information, see the [Harlequin Docs](https://harlequin.sh/docs/postgres/index).
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "harlequin-postgres"
3
- version = "0.2.2"
3
+ version = "0.3.0"
4
4
  description = "A Harlequin adapter for Postgres."
5
5
  authors = ["Ted Conbeer <tconbeer@users.noreply.github.com>"]
6
6
  license = "MIT"
@@ -14,13 +14,13 @@ postgres = "harlequin_postgres:HarlequinPostgresAdapter"
14
14
 
15
15
  [tool.poetry.dependencies]
16
16
  python = ">=3.8.1,<4.0"
17
- harlequin = "^1.7"
17
+ harlequin = "^1.20"
18
18
  psycopg2-binary = "^2.9.9"
19
19
 
20
20
  [tool.poetry.group.dev.dependencies]
21
- ruff = "^0.1.6"
21
+ ruff = "^0.5"
22
22
  pytest = "^7.4.3"
23
- mypy = "^1.7.0"
23
+ mypy = "^1.10.0"
24
24
  pre-commit = "^3.5.0"
25
25
  importlib_metadata = { version = ">=4.6.0", python = "<3.10.0" }
26
26
  types-psycopg2 = "^2.9.21.16"
@@ -32,9 +32,11 @@ build-backend = "poetry.core.masonry.api"
32
32
 
33
33
 
34
34
  [tool.ruff]
35
- select = ["A", "B", "E", "F", "I"]
36
35
  target-version = "py38"
37
36
 
37
+ [tool.ruff.lint]
38
+ select = ["A", "B", "E", "F", "I"]
39
+
38
40
  [tool.mypy]
39
41
  python_version = "3.8"
40
42
  files = [
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ from itertools import cycle
3
4
  from typing import Any, Sequence
4
5
 
5
6
  from harlequin import (
@@ -7,10 +8,11 @@ from harlequin import (
7
8
  HarlequinCompletion,
8
9
  HarlequinConnection,
9
10
  HarlequinCursor,
11
+ HarlequinTransactionMode,
10
12
  )
11
13
  from harlequin.catalog import Catalog, CatalogItem
12
14
  from harlequin.exception import HarlequinConnectionError, HarlequinQueryError
13
- from psycopg2.extensions import connection, cursor
15
+ from psycopg2.extensions import TRANSACTION_STATUS_IDLE, connection, cursor
14
16
  from psycopg2.pool import ThreadedConnectionPool
15
17
  from textual_fastdatatable.backend import AutoBackendType
16
18
 
@@ -62,23 +64,43 @@ class HarlequinPostgresConnection(HarlequinConnection):
62
64
  try:
63
65
  if conn_str and conn_str[0]:
64
66
  self.pool: ThreadedConnectionPool = ThreadedConnectionPool(
65
- 1, 5, dsn=conn_str[0], **options
67
+ 2, 5, dsn=conn_str[0], **options
66
68
  )
67
69
  else:
68
- self.pool = ThreadedConnectionPool(1, 5, **options)
70
+ self.pool = ThreadedConnectionPool(2, 5, **options)
69
71
  except Exception as e:
70
72
  raise HarlequinConnectionError(
71
73
  msg=str(e), title="Harlequin could not connect to Postgres."
72
74
  ) from e
73
75
 
76
+ self._main_conn: connection = self.pool.getconn(key="main")
77
+ self._transaction_modes = cycle(
78
+ [
79
+ HarlequinTransactionMode(label="Auto"),
80
+ HarlequinTransactionMode(
81
+ label="Manual",
82
+ commit=self.commit,
83
+ rollback=self.rollback,
84
+ ),
85
+ ]
86
+ )
87
+ self.toggle_transaction_mode()
88
+
74
89
  def execute(self, query: str) -> HarlequinCursor | None:
90
+ if (
91
+ self.transaction_mode.label != "Auto"
92
+ and self._main_conn.info.transaction_status == TRANSACTION_STATUS_IDLE
93
+ ):
94
+ cur = self._main_conn.cursor()
95
+ cur.execute(query="begin;")
96
+ cur.close()
97
+
75
98
  try:
76
- conn: connection = self.pool.getconn(key="main")
77
- with conn: # autocommit transaction
78
- cur = conn.cursor()
79
- cur.execute(query=query)
99
+ cur = self._main_conn.cursor()
100
+ cur.execute(query=query)
80
101
  except Exception as e:
81
102
  cur.close()
103
+ self.rollback()
82
104
  raise HarlequinQueryError(
83
105
  msg=str(e),
84
106
  title="Harlequin encountered an error while executing your query.",
@@ -89,8 +111,12 @@ class HarlequinPostgresConnection(HarlequinConnection):
89
111
  else:
90
112
  cur.close()
91
113
  return None
92
- finally:
93
- self.pool.putconn(conn, key="main")
114
+
115
+ def commit(self) -> None:
116
+ self._main_conn.commit()
117
+
118
+ def rollback(self) -> None:
119
+ self._main_conn.rollback()
94
120
 
95
121
  def get_catalog(self) -> Catalog:
96
122
  databases = self._get_databases()
@@ -146,7 +172,30 @@ class HarlequinPostgresConnection(HarlequinConnection):
146
172
  completions = _get_completions(conn)
147
173
  self.pool.putconn(conn)
148
174
  return completions
149
- ...
175
+
176
+ def close(self) -> None:
177
+ self.pool.putconn(self._main_conn, key="main")
178
+ self.pool.closeall()
179
+
180
+ @property
181
+ def transaction_mode(self) -> HarlequinTransactionMode:
182
+ return self._transaction_mode
183
+
184
+ def toggle_transaction_mode(self) -> HarlequinTransactionMode:
185
+ self._transaction_mode = next(self._transaction_modes)
186
+ self._sync_transaction_mode()
187
+ return self._transaction_mode
188
+
189
+ def _sync_transaction_mode(self) -> None:
190
+ """
191
+ Sync this class's transaction mode with the main connection
192
+ """
193
+ conn = self._main_conn
194
+ if self.transaction_mode.label == "Auto":
195
+ conn.autocommit = True
196
+ conn.commit()
197
+ else:
198
+ conn.autocommit = False
150
199
 
151
200
  def _get_databases(self) -> list[tuple[str]]:
152
201
  conn: connection = self.pool.getconn()
@@ -33,10 +33,7 @@ port = TextOption(
33
33
 
34
34
  dbname = TextOption(
35
35
  name="dbname",
36
- description=(
37
- "Port number to connect to at the server host, or socket file name extension "
38
- "for Unix-domain connections."
39
- ),
36
+ description=("The database name to use when connecting with the Postgres server."),
40
37
  short_decls=["-d"],
41
38
  default="postgres",
42
39
  )