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
This template is still valid SQL: you can run it directly with the embedded literals, and bisql replaces those literals with bind parameters at runtime.
107
-
108
-
```clj
109
-
(nssql
110
-
(:require [bisql.core :as bisql]))
111
-
112
-
(bisql/defquery)
113
-
```
114
-
115
-
Then use the generated query function:
116
-
117
-
```clj
118
-
(nsapp.user-service
119
-
(:require [next.jdbc :as jdbc]
120
-
[sql.postgresql.public.users.core :as users]))
121
-
122
-
(defdatasource
123
-
(jdbc/get-datasource {:dbtype"postgresql"
124
-
:host"localhost"
125
-
:port5432
126
-
:dbname"bisql_dev"
127
-
:user"bisql"
128
-
:password"bisql"}))
129
-
130
-
(users/find-active datasource {:status"active"
131
-
:limit20})
132
-
```
133
-
134
-
`bisql.core/defquery` uses the current namespace only to find SQL files.
135
-
Each discovered SQL file defines executable query functions in the namespace derived from its file path, so the same SQL file always maps to the same namespace.
136
-
By default, query execution uses the `:next-jdbc` adapter.
137
-
138
-
### 3. Generate Typical Index-Friendly Queries
139
-
140
-
Continuing from the previous example, you can generate a config template (`bisql.edn`) and modify it if needed:
141
-
142
-
```sh
143
-
clojure -M -m bisql.cli gen-config
144
-
```
145
-
146
-
Then generate the CRUD SQL:
147
-
148
-
```sh
149
-
clojure -M -m bisql.cli gen-crud
150
-
```
151
-
152
-
Depending on the tables present in the target database, this writes files such as:
153
-
154
-
-`src/sql/postgresql/public/users/crud.sql`
155
-
-`src/sql/postgresql/public/orders/crud.sql`
156
-
157
-
Generated CRUD SQL includes templates such as `insert`, `insert-many`, `get-by-*`,
`BISQL_SCHEMA`, `BISQL_BASE_DIR`, `BISQL_DBTYPE`, and `BISQL_CONFIG`.
175
+
176
+
The precedence order is CLI options > environment variables > config file > defaults.
177
+
178
+
`gen-declarations` is an optional helper for projects that prefer explicit namespace
179
+
files. It generates navigation-oriented `declare` forms with docstrings derived
180
+
from the SQL templates, so IDEs and the REPL can jump to the intended namespace
181
+
and query source without letting a shallow `(defquery)` populate undeclared
182
+
namespaces. By default those docstrings include the project-relative SQL file path
183
+
and line number; pass `--include-sql-template` if you also want the SQL template
184
+
body included.
185
+
104
186
See also:
105
187
106
188
-[Rendering](rendering.md)
107
189
-[CRUD Generation](crud-generation.md)
190
+
-[Rendering Examples](rendering-examples.md)
191
+
192
+
## 5. Wrap up
108
193
109
-
## 5. Developer Workflow with Bisql
194
+
The developer workflow with Bisql is straightforward:
110
195
111
-
1.Run `clojure -M:bisql gen-crud` to generate the routine CRUD SQL you would otherwise write by hand.
196
+
1.Add `defquery` to your application code once, so Bisql can turn SQL files, including generated ones, into ordinary Clojure functions.
112
197
113
-
2.Add `defquery` to your application code once, so Bisql can turn SQL files, including generated ones, into ordinary Clojure functions.
198
+
2.Run `clojure -M:bisql gen-crud` to generate the routine CRUD SQL you would otherwise write by hand, and add hand-written SQL where you need more complex queries.
114
199
115
200
3. If needed, run `clojure -M:bisql gen-declarations` to generate matching `declare` forms for those functions.
0 commit comments