-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
375 lines (281 loc) · 10.2 KB
/
main.py
File metadata and controls
375 lines (281 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# -*- coding: utf-8 -*-
"""
Zeko SQL Builder — Interactive CLI reference and helper tool.
Explore the Kotlin DSL, generate query examples, and get dependency snippets.
"""
from __future__ import annotations
import os
import subprocess
import textwrap
from typing import Callable, Dict
from rich import box
from rich.align import Align
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from rich.table import Table
from utils import ensure_env
console = Console()
LOGO = r"""
______ _ _____ ____ _ ____ _ _ _
|___ / | | / ____|/ __ \| | | _ \ (_) | | |
/ / ___| | _____ | (___ | | | | | | |_) |_ _ _| | __| | ___ _ __
/ / / _ \ |/ / _ \ \___ \| | | | | | _ <| | | | | |/ _` |/ _ \ '__|
/ /_| __/ < (_) | ____) | |__| | |____ | |_) | |_| | | | (_| | __/ |
/_____\___|_|\_\___/ |_____/ \___\_\______| |____/ \__,_|_|_|\__,_|\___|_|
"""
APP_TITLE = "Zeko SQL Builder CLI"
def _clear() -> None:
os.system("cls" if os.name == "nt" else "clear")
def _render_header() -> None:
logo_panel = Panel.fit(
LOGO,
title=APP_TITLE,
title_align="center",
style="bold magenta",
border_style="bright_blue",
)
console.print(Align.center(logo_panel))
def _wait_for_key() -> None:
console.print()
console.print("[dim]Press Enter to return to menu...[/dim]")
try:
input()
except KeyboardInterrupt:
pass
def _screen_quick_start() -> None:
_clear()
_render_header()
console.print()
text = """
[bold]Quick Start Guide[/bold]
Zeko SQL Builder is a lightweight Kotlin library for building SQL queries
using a clean, type-safe DSL. No annotations, no XML, no configuration files.
[bold cyan]Step 1:[/bold cyan] Add the dependency to your project (see Dependency Snippets).
[bold cyan]Step 2:[/bold cyan] Import and start building queries:
[white]import io.zeko.db.sql.Query[/white]
[bold cyan]Step 3:[/bold cyan] Build your first query:
[white]val sql = Query().fields("id", "name", "email")
.from("users")
.where("status" eq 1)
.toSql()[/white]
[bold cyan]Result:[/bold cyan]
[green]SELECT id, name, email FROM users WHERE status = 1[/green]
"""
console.print(Panel.fit(textwrap.dedent(text).strip(), border_style="green"))
_wait_for_key()
def _screen_query_examples() -> None:
from reference import get_query_examples
_clear()
_render_header()
console.print()
examples = get_query_examples()
for title, code, result in examples:
content = f"[bold]{title}[/bold]\n\n[white]{code}[/white]"
if result:
content += f"\n\n[green]-- Result:\n{result}[/green]"
console.print(Panel(content, border_style="bright_magenta"))
console.print()
_wait_for_key()
def _screen_dsl_reference() -> None:
from reference import get_dsl_reference
_clear()
_render_header()
console.print()
sections = get_dsl_reference()
for section_title, entries in sections:
table = Table(
title=section_title,
box=box.ROUNDED,
show_lines=True,
)
table.add_column("Method", style="bold cyan")
table.add_column("Description", style="white")
table.add_column("Example", style="bright_white")
for method, desc, example in entries:
table.add_row(method, desc, example)
console.print(table)
console.print()
_wait_for_key()
def _screen_where_builder() -> None:
from query_builder import interactive_where_builder
_clear()
_render_header()
console.print()
interactive_where_builder(console)
_wait_for_key()
def _screen_joins_reference() -> None:
from reference import get_join_reference
_clear()
_render_header()
console.print()
join_types, example = get_join_reference()
table = Table(
title="JOIN Types in Zeko SQL Builder",
box=box.ROUNDED,
show_lines=True,
)
table.add_column("Method", style="bold cyan")
table.add_column("SQL", style="white")
table.add_column("Usage", style="bright_white")
for method, sql, usage in join_types:
table.add_row(method, sql, usage)
console.print(table)
console.print()
console.print(Panel(example, title="[bold]Full JOIN Example[/bold]", border_style="bright_magenta"))
_wait_for_key()
def _screen_aggregations() -> None:
from reference import get_aggregation_reference
_clear()
_render_header()
console.print()
agg_funcs, custom_example = get_aggregation_reference()
table = Table(
title="Aggregation Functions",
box=box.MINIMAL_DOUBLE_HEAD,
show_lines=True,
)
table.add_column("Function", style="bold magenta")
table.add_column("SQL Equivalent", style="white")
for name, sql_eq in agg_funcs:
table.add_row(name, sql_eq)
console.print(table)
console.print()
console.print(
Panel(
custom_example,
title="[bold]Custom Aggregation Example[/bold]",
border_style="bright_blue",
)
)
_wait_for_key()
def _screen_dependency_snippets() -> None:
from config import load_preferences
_clear()
_render_header()
console.print()
prefs = load_preferences()
preferred = prefs.get("build_tool", "maven")
maven_xml = textwrap.dedent("""
<dependency>
<groupId>io.zeko</groupId>
<artifactId>zeko-sql-builder</artifactId>
<version>1.4.0</version>
</dependency>
""").strip()
gradle_groovy = textwrap.dedent("""
implementation 'io.zeko:zeko-sql-builder:1.4.0'
""").strip()
gradle_kts = textwrap.dedent("""
implementation("io.zeko:zeko-sql-builder:1.4.0")
""").strip()
console.print(Panel("[bold]Maven (pom.xml)[/bold]", border_style="green" if preferred == "maven" else "dim"))
console.print(f"[white]{maven_xml}[/white]\n")
console.print(Panel("[bold]Gradle (build.gradle)[/bold]", border_style="green" if preferred == "gradle" else "dim"))
console.print(f"[white]{gradle_groovy}[/white]\n")
console.print(Panel("[bold]Gradle Kotlin DSL (build.gradle.kts)[/bold]", border_style="green" if preferred == "gradle-kts" else "dim"))
console.print(f"[white]{gradle_kts}[/white]\n")
optional_deps = textwrap.dedent("""
[bold]Optional Dependencies:[/bold]
Jasync MySQL driver:
com.github.jasync-sql:jasync-mysql:1.2.3
HikariCP connection pool:
com.zaxxer:HikariCP:5.0.1
Vert.x JDBC client:
io.vertx:vertx-jdbc-client:4.1.1
Kotlin Coroutines:
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3
""").strip()
console.print(Panel(optional_deps, border_style="cyan"))
_wait_for_key()
def _screen_about() -> None:
from pathlib import Path
_clear()
_render_header()
console.print()
about_path = Path(__file__).resolve().parent / "about.txt"
about_line = ""
if about_path.exists():
about_line = about_path.read_text(encoding="utf-8").strip()
text = textwrap.dedent("""
[bold]About Zeko SQL Builder CLI[/bold]
This is an interactive reference and helper tool for the Zeko SQL Builder
Kotlin library. It helps developers explore the DSL syntax, generate query
examples, build WHERE clauses interactively, and get dependency snippets
for Maven and Gradle.
[bold]Zeko SQL Builder[/bold] is a lightweight, high-performance SQL library
for Kotlin that provides:
- Clean DSL for building SQL queries
- No configuration files, annotations, or XML
- Minimal overhead (mostly string operations)
- Extensible with custom operators and SQL functions
- Database abstraction via HikariCP, Jasync, and Vert.x
[bold]Links:[/bold]
- Repository: https://github.com/nicoschl/zeko-sql-builder
- Maven Central: io.zeko:zeko-sql-builder
[bold]Version:[/bold] CLI v1.0.0 | Library v1.4.0
""").strip()
console.print(Panel(text, border_style="blue"))
if about_line:
console.print(f"\n[dim]{about_line}[/dim]")
_wait_for_key()
def _exit_app() -> None:
_clear()
console.print(
Align.center(
Panel.fit(
"[bold green]Thank you for using Zeko SQL Builder CLI.[/bold green]",
border_style="green",
)
)
)
raise SystemExit(0)
@ensure_env
def main() -> None:
actions: Dict[str, Callable[[], None]] = {
"1": _screen_quick_start,
"2": _screen_query_examples,
"3": _screen_dsl_reference,
"4": _screen_where_builder,
"5": _screen_joins_reference,
"6": _screen_aggregations,
"7": _screen_dependency_snippets,
"8": _screen_about,
"0": _exit_app,
}
while True:
_clear()
_render_header()
console.print()
table = Table(
title="Main Menu",
title_style="bold cyan",
box=box.ROUNDED,
show_lines=True,
)
table.add_column("Key", justify="center", style="bold yellow", no_wrap=True)
table.add_column("Action", style="bold white")
table.add_row("[bold green]1[/bold green]", "Quick Start")
table.add_row("[bold green]2[/bold green]", "Query Examples")
table.add_row("[bold green]3[/bold green]", "DSL Reference")
table.add_row("[bold green]4[/bold green]", "WHERE Clause Builder")
table.add_row("[bold green]5[/bold green]", "JOINs Reference")
table.add_row("[bold green]6[/bold green]", "Aggregations")
table.add_row("[bold green]7[/bold green]", "Dependency Snippets")
table.add_row("[bold green]8[/bold green]", "About")
table.add_row("[bold red]0[/bold red]", "Exit")
console.print(table)
console.print()
choice = Prompt.ask(
"[bold cyan]Select an option[/bold cyan]",
choices=list(actions.keys()),
default="8",
)
action = actions.get(choice)
if action:
action()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
_exit_app()