lagun 0.1.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 (41) hide show
  1. lagun-0.1.0/LICENSE +21 -0
  2. lagun-0.1.0/PKG-INFO +83 -0
  3. lagun-0.1.0/README.md +46 -0
  4. lagun-0.1.0/lagun/__init__.py +2 -0
  5. lagun-0.1.0/lagun/api/__init__.py +0 -0
  6. lagun-0.1.0/lagun/api/config.py +140 -0
  7. lagun-0.1.0/lagun/api/export.py +238 -0
  8. lagun-0.1.0/lagun/api/import_data.py +218 -0
  9. lagun-0.1.0/lagun/api/query.py +165 -0
  10. lagun-0.1.0/lagun/api/schema.py +122 -0
  11. lagun-0.1.0/lagun/api/sessions.py +105 -0
  12. lagun-0.1.0/lagun/api/table_ops.py +204 -0
  13. lagun-0.1.0/lagun/cli.py +37 -0
  14. lagun-0.1.0/lagun/db/__init__.py +0 -0
  15. lagun-0.1.0/lagun/db/crypto.py +78 -0
  16. lagun-0.1.0/lagun/db/pool.py +67 -0
  17. lagun-0.1.0/lagun/db/session_store.py +167 -0
  18. lagun-0.1.0/lagun/db/utils.py +99 -0
  19. lagun-0.1.0/lagun/main.py +87 -0
  20. lagun-0.1.0/lagun/models/__init__.py +0 -0
  21. lagun-0.1.0/lagun/models/query.py +58 -0
  22. lagun-0.1.0/lagun/models/schema.py +76 -0
  23. lagun-0.1.0/lagun/models/session.py +59 -0
  24. lagun-0.1.0/lagun/static/assets/ExportDialog-CWNmn9L4.js +4 -0
  25. lagun-0.1.0/lagun/static/assets/TableSchemaView-CyBnv8tU.js +19 -0
  26. lagun-0.1.0/lagun/static/assets/index-BeLXjJhD.js +23 -0
  27. lagun-0.1.0/lagun/static/assets/index-BnFdfWG3.css +1 -0
  28. lagun-0.1.0/lagun/static/assets/vendor-ag-grid-NsbaVf01.js +178 -0
  29. lagun-0.1.0/lagun/static/assets/vendor-codemirror-D5amxuPd.js +21 -0
  30. lagun-0.1.0/lagun/static/assets/vendor-react-C_YsO-_2.js +1 -0
  31. lagun-0.1.0/lagun/static/assets/vendor-ui-BLV2MnSs.js +199 -0
  32. lagun-0.1.0/lagun/static/favicon.svg +22 -0
  33. lagun-0.1.0/lagun/static/index.html +22 -0
  34. lagun-0.1.0/lagun.egg-info/PKG-INFO +83 -0
  35. lagun-0.1.0/lagun.egg-info/SOURCES.txt +39 -0
  36. lagun-0.1.0/lagun.egg-info/dependency_links.txt +1 -0
  37. lagun-0.1.0/lagun.egg-info/entry_points.txt +2 -0
  38. lagun-0.1.0/lagun.egg-info/requires.txt +15 -0
  39. lagun-0.1.0/lagun.egg-info/top_level.txt +1 -0
  40. lagun-0.1.0/pyproject.toml +61 -0
  41. lagun-0.1.0/setup.cfg +4 -0
lagun-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Anudeep Dhavaleswarapu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
lagun-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.4
2
+ Name: lagun
3
+ Version: 0.1.0
4
+ Summary: A web-based MySQL/MariaDB GUI editor
5
+ Author-email: Anudeep Dhavaleswarapu <anudeepd2@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/anudeepd/lagun
8
+ Project-URL: Repository, https://github.com/anudeepd/lagun
9
+ Project-URL: Issues, https://github.com/anudeepd/lagun/issues
10
+ Keywords: mysql,mariadb,gui,web,database
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Web Environment
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Database :: Front-Ends
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: fastapi>=0.111.0
23
+ Requires-Dist: uvicorn[standard]>=0.29.0
24
+ Requires-Dist: aiomysql>=0.2.0
25
+ Requires-Dist: aiosqlite>=0.20.0
26
+ Requires-Dist: cryptography>=42.0.0
27
+ Requires-Dist: keyring>=25.0.0
28
+ Requires-Dist: click>=8.1.0
29
+ Requires-Dist: pydantic>=2.7.0
30
+ Requires-Dist: python-multipart>=0.0.9
31
+ Provides-Extra: dev
32
+ Requires-Dist: pytest>=8.0; extra == "dev"
33
+ Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
34
+ Requires-Dist: httpx>=0.27; extra == "dev"
35
+ Requires-Dist: testcontainers[mysql]>=4.0; extra == "dev"
36
+ Dynamic: license-file
37
+
38
+ <p align="center">
39
+ <img src="assets/logo.svg" alt="Lagun" width="120"/>
40
+ </p>
41
+
42
+ <h1 align="center">Lagun</h1>
43
+
44
+ <p align="center">A minimal, web-based MySQL/MariaDB GUI editor. Install it, run it, use it.</p>
45
+
46
+ ## Features
47
+
48
+ - **Web-based SQL editor** with syntax highlighting, autocompletion, and multi-tab support
49
+ - **Schema browser** — explore databases, tables, columns, and indexes
50
+ - **Schema management** — create, modify, and drop tables, columns, and indexes
51
+ - **In-line data editing** — edit cells, insert rows, delete rows directly in the grid
52
+ - **Import & export** — CSV and SQL formats with streaming for large datasets
53
+ - **Query history** — track past queries with execution time and row counts
54
+ - **Secure connections** — SSL/TLS, credentials stored in OS keyring, encrypted session backup
55
+
56
+ ## Install
57
+
58
+ ```bash
59
+ pip install git+https://github.com/anudeepd/lagun
60
+ ```
61
+
62
+ ## Usage
63
+
64
+ ```bash
65
+ lagun serve
66
+ ```
67
+
68
+ Opens the GUI in your browser. Connect to any MySQL or MariaDB database from there.
69
+
70
+ ## Development
71
+
72
+ Requires [uv](https://github.com/astral-sh/uv).
73
+
74
+ ```bash
75
+ git clone https://github.com/anudeepd/lagun
76
+ cd lagun
77
+ uv sync
78
+ uv run lagun serve
79
+ ```
80
+
81
+ ## License
82
+
83
+ MIT
lagun-0.1.0/README.md ADDED
@@ -0,0 +1,46 @@
1
+ <p align="center">
2
+ <img src="assets/logo.svg" alt="Lagun" width="120"/>
3
+ </p>
4
+
5
+ <h1 align="center">Lagun</h1>
6
+
7
+ <p align="center">A minimal, web-based MySQL/MariaDB GUI editor. Install it, run it, use it.</p>
8
+
9
+ ## Features
10
+
11
+ - **Web-based SQL editor** with syntax highlighting, autocompletion, and multi-tab support
12
+ - **Schema browser** — explore databases, tables, columns, and indexes
13
+ - **Schema management** — create, modify, and drop tables, columns, and indexes
14
+ - **In-line data editing** — edit cells, insert rows, delete rows directly in the grid
15
+ - **Import & export** — CSV and SQL formats with streaming for large datasets
16
+ - **Query history** — track past queries with execution time and row counts
17
+ - **Secure connections** — SSL/TLS, credentials stored in OS keyring, encrypted session backup
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ pip install git+https://github.com/anudeepd/lagun
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```bash
28
+ lagun serve
29
+ ```
30
+
31
+ Opens the GUI in your browser. Connect to any MySQL or MariaDB database from there.
32
+
33
+ ## Development
34
+
35
+ Requires [uv](https://github.com/astral-sh/uv).
36
+
37
+ ```bash
38
+ git clone https://github.com/anudeepd/lagun
39
+ cd lagun
40
+ uv sync
41
+ uv run lagun serve
42
+ ```
43
+
44
+ ## License
45
+
46
+ MIT
@@ -0,0 +1,2 @@
1
+ """Lagun — web-based MySQL/MariaDB editor."""
2
+ __version__ = "0.1.0"
File without changes
@@ -0,0 +1,140 @@
1
+ """Config export/import: backup and restore saved connection sessions."""
2
+ import base64
3
+ import json
4
+ import os
5
+ import secrets
6
+ import tempfile
7
+ from datetime import datetime, timezone
8
+ from typing import Optional
9
+
10
+ from cryptography.fernet import InvalidToken
11
+ from fastapi import APIRouter, Form, HTTPException, UploadFile
12
+ from fastapi.responses import FileResponse
13
+ from pydantic import BaseModel
14
+ from starlette.background import BackgroundTask
15
+
16
+ from lagun.db import session_store
17
+ from lagun.db.crypto import decrypt_password, encrypt_with_passphrase, decrypt_with_passphrase
18
+ from lagun.models.session import SessionCreate
19
+
20
+ router = APIRouter(tags=["config"])
21
+
22
+ _EXPORT_VERSION = 1
23
+ _KDF_ITERATIONS = 600_000
24
+
25
+
26
+ class ExportRequest(BaseModel):
27
+ passphrase: str
28
+
29
+
30
+ class ImportResult(BaseModel):
31
+ imported: int
32
+ skipped: int
33
+
34
+
35
+ @router.post("/config/export")
36
+ async def export_config(req: ExportRequest):
37
+ if not req.passphrase:
38
+ raise HTTPException(400, "A passphrase is required to protect the exported passwords")
39
+
40
+ raw_rows = await session_store.list_sessions_raw()
41
+ salt_bytes = secrets.token_bytes(16)
42
+ salt_b64 = base64.b64encode(salt_bytes).decode()
43
+
44
+ sessions_out = []
45
+ for s in raw_rows:
46
+ plaintext = decrypt_password(s["password_enc"])
47
+ sessions_out.append({
48
+ "name": s["name"],
49
+ "host": s["host"],
50
+ "port": s["port"],
51
+ "username": s["username"],
52
+ "password_enc": encrypt_with_passphrase(plaintext, req.passphrase, salt_bytes),
53
+ "default_db": s["default_db"],
54
+ "query_limit": s["query_limit"],
55
+ "ssl_enabled": bool(s["ssl_enabled"]),
56
+ "selected_databases": json.loads(s["selected_databases"] or "[]"),
57
+ })
58
+
59
+ payload = {
60
+ "version": _EXPORT_VERSION,
61
+ "exported_at": datetime.now(timezone.utc).isoformat(),
62
+ "kdf": "pbkdf2-hmac-sha256",
63
+ "kdf_salt": salt_b64,
64
+ "kdf_iterations": _KDF_ITERATIONS,
65
+ "sessions": sessions_out,
66
+ }
67
+
68
+ tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False, encoding="utf-8")
69
+ try:
70
+ json.dump(payload, tmp, indent=2, ensure_ascii=False)
71
+ tmp.flush()
72
+ tmp.close()
73
+ except Exception:
74
+ os.unlink(tmp.name)
75
+ raise
76
+
77
+ ts = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
78
+ filename = f"lagun_sessions_{ts}.json"
79
+ return FileResponse(
80
+ tmp.name,
81
+ media_type="application/json",
82
+ filename=filename,
83
+ headers={"Content-Disposition": f'attachment; filename="{filename}"'},
84
+ background=BackgroundTask(lambda: os.unlink(tmp.name) if os.path.exists(tmp.name) else None),
85
+ )
86
+
87
+
88
+ @router.post("/config/import", response_model=ImportResult)
89
+ async def import_config(
90
+ file: UploadFile,
91
+ passphrase: str = Form(...),
92
+ ):
93
+ if not passphrase:
94
+ raise HTTPException(400, "A passphrase is required to decrypt the imported passwords")
95
+
96
+ raw = await file.read()
97
+ if len(raw) > 5 * 1024 * 1024:
98
+ raise HTTPException(413, "File too large (max 5 MB)")
99
+
100
+ try:
101
+ payload = json.loads(raw.decode("utf-8"))
102
+ except (json.JSONDecodeError, UnicodeDecodeError) as exc:
103
+ raise HTTPException(400, f"Invalid JSON: {exc}")
104
+
105
+ if payload.get("version") != _EXPORT_VERSION:
106
+ raise HTTPException(400, f"Unsupported export version: {payload.get('version')}")
107
+
108
+ try:
109
+ salt_bytes = base64.b64decode(payload["kdf_salt"])
110
+ except Exception as exc:
111
+ raise HTTPException(400, f"Malformed kdf_salt: {exc}")
112
+
113
+ imported = 0
114
+ skipped = 0
115
+
116
+ for entry in payload.get("sessions", []):
117
+ try:
118
+ try:
119
+ plaintext_password = decrypt_with_passphrase(entry["password_enc"], passphrase, salt_bytes)
120
+ except InvalidToken:
121
+ raise HTTPException(400, "Wrong passphrase — could not decrypt passwords")
122
+
123
+ await session_store.create_session(SessionCreate(
124
+ name= entry["name"],
125
+ host= entry.get("host", "localhost"),
126
+ port= entry.get("port", 3306),
127
+ username= entry.get("username", ""),
128
+ password= plaintext_password,
129
+ default_db= entry.get("default_db"),
130
+ query_limit= entry.get("query_limit", 100),
131
+ ssl_enabled= bool(entry.get("ssl_enabled", False)),
132
+ selected_databases=entry.get("selected_databases", []),
133
+ ))
134
+ imported += 1
135
+ except HTTPException:
136
+ raise
137
+ except Exception:
138
+ skipped += 1
139
+
140
+ return ImportResult(imported=imported, skipped=skipped)
@@ -0,0 +1,238 @@
1
+ """Streaming export: INSERT SQL, DELETE SQL, CSV."""
2
+ import csv
3
+ import io
4
+ import re
5
+ from typing import Optional
6
+
7
+ from fastapi import APIRouter, HTTPException
8
+ from fastapi.responses import StreamingResponse
9
+ from pydantic import BaseModel
10
+
11
+ from lagun.db.pool import get_pool
12
+ from lagun.db.session_store import get_session
13
+ from lagun.db.utils import quote_ident, escape_value
14
+
15
+ router = APIRouter(tags=["export"])
16
+
17
+
18
+ class ExportRequest(BaseModel):
19
+ database: str
20
+ table: Optional[str] = None
21
+ sql: Optional[str] = None # custom SELECT; overrides table
22
+ format: str = "insert" # "insert" | "delete" | "delete+insert" | "csv"
23
+ batch_size: int = 500
24
+ pk_values: Optional[list] = None # list of dicts: [{pk_col: val, ...}, ...]
25
+ # CSV-specific options (ignored for other formats)
26
+ csv_delimiter: str = ","
27
+ csv_quotechar: str = '"'
28
+ csv_escapechar: str = '"'
29
+ csv_lineterminator: str = "\r\n"
30
+ csv_encoding: str = "utf-8" # "utf-8" | "utf-8-sig" | "ascii"
31
+
32
+
33
+ @router.post("/sessions/{session_id}/export")
34
+ async def export_data(session_id: str, req: ExportRequest):
35
+ s = await get_session(session_id)
36
+ if not s:
37
+ raise HTTPException(404, "Session not found")
38
+ pool = await get_pool(session_id)
39
+
40
+ if req.sql:
41
+ # Only allow SELECT statements for security
42
+ stripped = req.sql.strip().rstrip(";").strip()
43
+ if not re.match(r'^\s*SELECT\b', stripped, re.IGNORECASE):
44
+ raise HTTPException(400, "Only SELECT statements are allowed for export")
45
+ select_sql = req.sql
46
+ elif req.table:
47
+ select_sql = f"SELECT * FROM {quote_ident(req.database)}.{quote_ident(req.table)}"
48
+ if req.pk_values:
49
+ conditions = []
50
+ for pk_dict in req.pk_values:
51
+ parts = [f"{quote_ident(col)} = {escape_value(val)}" for col, val in pk_dict.items()]
52
+ conditions.append(f"({' AND '.join(parts)})")
53
+ select_sql += f" WHERE {' OR '.join(conditions)}"
54
+ else:
55
+ raise HTTPException(400, "Provide either 'table' or 'sql'")
56
+
57
+ async def _generate_insert():
58
+ async with pool.acquire() as conn:
59
+ async with conn.cursor() as cur:
60
+ await cur.execute(f"USE {quote_ident(req.database)}")
61
+ await cur.execute(select_sql)
62
+ cols = [d[0] for d in cur.description]
63
+ cols_sql = ", ".join(quote_ident(c) for c in cols)
64
+ tbl = req.table or "exported_data"
65
+ tbl_q = quote_ident(tbl)
66
+
67
+ yield f"-- Lagun export: {req.database}.{tbl}\n"
68
+ yield f"-- Format: INSERT\n\n"
69
+
70
+ batch = []
71
+ while True:
72
+ rows = await cur.fetchmany(req.batch_size)
73
+ if not rows:
74
+ break
75
+ for row in rows:
76
+ vals = ", ".join(escape_value(v) for v in row)
77
+ batch.append(f"({vals})")
78
+ yield (
79
+ f"INSERT INTO {tbl_q} ({cols_sql}) VALUES\n"
80
+ + ",\n".join(batch)
81
+ + ";\n"
82
+ )
83
+ batch = []
84
+
85
+ async def _generate_delete():
86
+ # Need PK columns from information_schema
87
+ async with pool.acquire() as conn:
88
+ async with conn.cursor() as cur:
89
+ await cur.execute(f"USE {quote_ident(req.database)}")
90
+ await cur.execute(
91
+ """SELECT COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE
92
+ WHERE TABLE_SCHEMA=%s AND TABLE_NAME=%s
93
+ AND CONSTRAINT_NAME='PRIMARY'
94
+ ORDER BY ORDINAL_POSITION""",
95
+ (req.database, req.table),
96
+ )
97
+ pk_rows = await cur.fetchall()
98
+ pk_cols = [r[0] for r in pk_rows]
99
+ if not pk_cols:
100
+ raise HTTPException(400, "Table has no primary key — cannot generate DELETE export")
101
+
102
+ tbl_q = f"{quote_ident(req.database)}.{quote_ident(req.table or 'tbl')}"
103
+ await cur.execute(select_sql)
104
+
105
+ yield f"-- Lagun export: {req.database}.{req.table}\n-- Format: DELETE\n\n"
106
+
107
+ while True:
108
+ rows = await cur.fetchmany(req.batch_size)
109
+ if not rows:
110
+ break
111
+ cols = [d[0] for d in cur.description]
112
+ for row in rows:
113
+ row_dict = dict(zip(cols, row))
114
+ where = " AND ".join(
115
+ f"{quote_ident(pk)} = {escape_value(row_dict[pk])}"
116
+ for pk in pk_cols
117
+ )
118
+ yield f"DELETE FROM {tbl_q} WHERE {where};\n"
119
+
120
+ async def _generate_delete_insert():
121
+ # Need PK columns from information_schema
122
+ async with pool.acquire() as conn:
123
+ async with conn.cursor() as cur:
124
+ await cur.execute(f"USE {quote_ident(req.database)}")
125
+ await cur.execute(
126
+ """SELECT COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE
127
+ WHERE TABLE_SCHEMA=%s AND TABLE_NAME=%s
128
+ AND CONSTRAINT_NAME='PRIMARY'
129
+ ORDER BY ORDINAL_POSITION""",
130
+ (req.database, req.table),
131
+ )
132
+ pk_rows = await cur.fetchall()
133
+ pk_cols = [r[0] for r in pk_rows]
134
+ if not pk_cols:
135
+ raise HTTPException(400, "Table has no primary key — cannot generate DELETE+INSERT export")
136
+
137
+ tbl_q = f"{quote_ident(req.database)}.{quote_ident(req.table or 'tbl')}"
138
+ await cur.execute(select_sql)
139
+
140
+ cols = None
141
+ cols_sql = None
142
+ yield f"-- Lagun export: {req.database}.{req.table}\n-- Format: DELETE+INSERT\n\n"
143
+
144
+ while True:
145
+ rows = await cur.fetchmany(req.batch_size)
146
+ if not rows:
147
+ break
148
+ if cols is None:
149
+ cols = [d[0] for d in cur.description]
150
+ cols_sql = ", ".join(quote_ident(c) for c in cols)
151
+ for row in rows:
152
+ row_dict = dict(zip(cols, row))
153
+ where = " AND ".join(
154
+ f"{quote_ident(pk)} = {escape_value(row_dict[pk])}"
155
+ for pk in pk_cols
156
+ )
157
+ vals = ", ".join(escape_value(v) for v in row)
158
+ yield f"DELETE FROM {tbl_q} WHERE {where};\n"
159
+ yield f"INSERT INTO {tbl_q} ({cols_sql}) VALUES ({vals});\n"
160
+
161
+ async def _generate_csv():
162
+ writer_kwargs: dict = dict(
163
+ delimiter=req.csv_delimiter,
164
+ lineterminator=req.csv_lineterminator,
165
+ )
166
+ escapechar = req.csv_escapechar or None # empty string → no escape char
167
+ if req.csv_quotechar:
168
+ writer_kwargs["quoting"] = csv.QUOTE_ALL
169
+ writer_kwargs["quotechar"] = req.csv_quotechar
170
+ if escapechar and escapechar != req.csv_quotechar:
171
+ writer_kwargs["escapechar"] = escapechar
172
+ else:
173
+ writer_kwargs["quoting"] = csv.QUOTE_NONE
174
+ writer_kwargs["escapechar"] = escapechar or "\\"
175
+
176
+ encoding = req.csv_encoding
177
+ # Determine byte encoding and error handling
178
+ if encoding == "ascii":
179
+ byte_enc, enc_errors = "ascii", "replace"
180
+ else:
181
+ byte_enc, enc_errors = "utf-8", "strict"
182
+
183
+ def to_bytes(text: str) -> bytes:
184
+ return text.encode(byte_enc, errors=enc_errors)
185
+
186
+ def clean_row(row: tuple) -> list:
187
+ # None → empty string so NULL doesn't export as the string "None"
188
+ return ["" if v is None else v for v in row]
189
+
190
+ async with pool.acquire() as conn:
191
+ async with conn.cursor() as cur:
192
+ await cur.execute(f"USE {quote_ident(req.database)}")
193
+ await cur.execute(select_sql)
194
+ cols = [d[0] for d in cur.description]
195
+
196
+ # Emit BOM bytes for utf-8-sig
197
+ if encoding == "utf-8-sig":
198
+ yield b"\xef\xbb\xbf"
199
+
200
+ buf = io.StringIO()
201
+ writer = csv.writer(buf, **writer_kwargs)
202
+ writer.writerow(cols)
203
+ yield to_bytes(buf.getvalue())
204
+
205
+ while True:
206
+ rows = await cur.fetchmany(req.batch_size)
207
+ if not rows:
208
+ break
209
+ buf = io.StringIO()
210
+ writer = csv.writer(buf, **writer_kwargs)
211
+ writer.writerows(clean_row(row) for row in rows)
212
+ yield to_bytes(buf.getvalue())
213
+
214
+ if req.format == "insert":
215
+ gen = _generate_insert()
216
+ media = "text/plain"
217
+ filename = f"{req.database}_{req.table}_insert.sql"
218
+ elif req.format == "delete":
219
+ gen = _generate_delete()
220
+ media = "text/plain"
221
+ filename = f"{req.database}_{req.table}_delete.sql"
222
+ elif req.format == "delete+insert":
223
+ gen = _generate_delete_insert()
224
+ media = "text/plain"
225
+ filename = f"{req.database}_{req.table}_delete_insert.sql"
226
+ elif req.format == "csv":
227
+ gen = _generate_csv()
228
+ enc = req.csv_encoding.replace("-sig", "")
229
+ media = f"text/csv; charset={enc}"
230
+ filename = f"{req.database}_{req.table or 'query'}.csv"
231
+ else:
232
+ raise HTTPException(400, f"Unknown format: {req.format!r}")
233
+
234
+ return StreamingResponse(
235
+ gen,
236
+ media_type=media,
237
+ headers={"Content-Disposition": f'attachment; filename="{filename}"'},
238
+ )