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
docs: update examples to use current SQLSpec API patterns (#37)
Bring examples up to date with latest SQLSpec architecture:
## π API Updates
- Changed `spec.register_config()` β `spec.add_config()`
- Updated SQL execution to use `SQL()` objects instead of raw strings
- Fixed Litestar examples to use proper SQLSpec plugin integration
- Updated service examples with correct session management
Copy file name to clipboardExpand all lines: README.md
+78-23Lines changed: 78 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,18 +4,26 @@
4
4
5
5
SQLSpec is an experimental Python library designed to streamline and modernize your SQL interactions across a variety of database systems. While still in its early stages, SQLSpec aims to provide a flexible, typed, and extensible interface for working with SQL in Python.
6
6
7
-
**Note**: SQLSpec is currently under active development and the API is subject to change. It is not yet ready for production use. Contributions are welcome!
7
+
**Note**: SQLSpec is currently under active development and the API is subject to change. It is not yet ready for production use. Contributions are welcome!
8
8
9
-
## Core Features (Planned but subject to change, removal or redesign)
9
+
## Core Features (Current and Planned)
10
+
11
+
### Currently Implemented
10
12
11
13
-**Consistent Database Session Interface**: Provides a consistent connectivity interface for interacting with one or more database systems, including SQLite, Postgres, DuckDB, MySQL, Oracle, SQL Server, Spanner, BigQuery, and more.
12
-
-**Emphasis on RAW SQL and Minimal Abstractions and Performance**: SQLSpec is a library for working with SQL in Python. It's goals are to offer minimal abstractions between the user and the database. It does not aim to be an ORM library.
14
+
-**Emphasis on RAW SQL and Minimal Abstractions**: SQLSpec is a library for working with SQL in Python. Its goals are to offer minimal abstractions between the user and the database. It does not aim to be an ORM library.
13
15
-**Type-Safe Queries**: Quickly map SQL queries to typed objects using libraries such as Pydantic, Msgspec, Attrs, etc.
14
-
-**Extensible Design**: Easily add support for new database dialects or extend existing functionality to meet your specific needs. Easily add support for async and sync database drivers.
15
-
-**Minimal Dependencies**: SQLSpec is designed to be lightweight and can run on it's own or with other libraries such as `litestar`, `fastapi`, `flask` and more. (Contributions welcome!)
16
-
-**Dynamic Query Manipulation**: Easily apply filters to pre-defined queries with a fluent, Pythonic API. Safely manipulate queries without the risk of SQL injection.
17
-
-**Dialect Validation and Conversion**: Use `sqlglot` to validate your SQL against specific dialects and seamlessly convert between them.
16
+
-**Extensible Design**: Easily add support for new database dialects or extend existing functionality to meet your specific needs. Easily add support for async and sync database drivers.
17
+
-**Minimal Dependencies**: SQLSpec is designed to be lightweight and can run on its own or with other libraries such as `litestar`, `fastapi`, `flask` and more. (Contributions welcome!)
18
18
-**Support for Async and Sync Database Drivers**: SQLSpec supports both async and sync database drivers, allowing you to choose the style that best fits your application.
19
+
20
+
### Experimental Features (API will change rapidly)
21
+
22
+
-**SQL Builder API**: Type-safe query builder with method chaining (experimental and subject to significant changes)
23
+
-**Dynamic Query Manipulation**: Apply filters to pre-defined queries with a fluent API. Safely manipulate queries without SQL injection risk.
24
+
-**Dialect Validation and Conversion**: Use `sqlglot` to validate your SQL against specific dialects and seamlessly convert between them.
25
+
-**Storage Operations**: Direct export to Parquet, CSV, JSON with Arrow integration
26
+
-**Instrumentation**: OpenTelemetry and Prometheus metrics support
19
27
-**Basic Migration Management**: A mechanism to generate empty migration files where you can add your own SQL and intelligently track which migrations have been applied.
20
28
21
29
## What SQLSpec Is Not (Yet)
@@ -26,16 +34,60 @@ SQLSpec is a work in progress. While it offers a solid foundation for modern SQL
26
34
27
35
We've talked about what SQLSpec is not, so let's look at what it can do.
28
36
29
-
These are just a few of the examples that demonstrate SQLSpec's flexibility and each of the bundled adapters offer the same config and driver interfaces.
37
+
These are just a few examples that demonstrate SQLSpec's flexibility. Each of the bundled adapters offers the same config and driver interfaces.
print(query.build().sql) # SELECT id, name, email FROM users WHERE active = ?
66
+
67
+
# More complex example with joins
68
+
query = (
69
+
sql.select("u.name", "COUNT(o.id) as order_count")
70
+
.from_("users u")
71
+
.left_join("orders o", "u.id = o.user_id")
72
+
.where("u.created_at > ?", "2024-01-01")
73
+
.group_by("u.name")
74
+
.having("COUNT(o.id) > ?", 5)
75
+
.order_by("order_count", desc=True)
76
+
)
77
+
78
+
# Execute the built query
79
+
with sql.provide_session(config) as session:
80
+
results = session.execute(query.build())
81
+
```
30
82
31
83
### DuckDB LLM
32
84
33
-
This is a quick implementation using some of the builtin Secret and Extension management features of SQLSpec's DuckDB integration.
85
+
This is a quick implementation using some of the built-in Secret and Extension management features of SQLSpec's DuckDB integration.
34
86
35
-
It allows you to communicate with any compatible OpenAPI conversations endpoint (such as Ollama). This examples:
87
+
It allows you to communicate with any compatible OpenAPI conversations endpoint (such as Ollama). This example:
36
88
37
89
- auto installs the `open_prompt` DuckDB extensions
38
-
- automatically creates the correct `open_prompt`comptaible secret required to use the extension
90
+
- automatically creates the correct `open_prompt`compatible secret required to use the extension
39
91
40
92
```py
41
93
# /// script
@@ -80,12 +132,12 @@ with sql.provide_session(etl_config) as session:
80
132
81
133
### DuckDB Gemini Embeddings
82
134
83
-
In this example, we are again using DuckDB. However, we are going to use the builtin to call the Google Gemini embeddings service directly from the database.
135
+
In this example, we are again using DuckDB. However, we are going to use the built-in to call the Google Gemini embeddings service directly from the database.
84
136
85
-
This example will
137
+
This example will:
86
138
87
139
- auto installs the `http_client` and `vss` (vector similarity search) DuckDB extensions
88
-
- when a connection is created, it ensures that the `generate_embeddings` macro exists in the DuckDB database.
140
+
- when a connection is created, it ensures that the `generate_embeddings` macro exists in the DuckDB database
89
141
- Execute a simple query to call the Google API
90
142
91
143
```py
@@ -131,8 +183,8 @@ etl_config = sql.add_config(
131
183
)
132
184
)
133
185
with sql.provide_session(etl_config) as session:
134
-
result = session.select_one("SELECT generate_embedding('example text')")
135
-
print(result) # result is a dictionary when `schema_type` is omitted.
186
+
result = session.execute("SELECT generate_embedding('example text')")
187
+
print(result.get_first()) # result is a dictionary when `schema_type` is omitted.
136
188
```
137
189
138
190
### Basic Litestar Integration
@@ -147,27 +199,29 @@ In this example we are going to demonstrate how to create a basic configuration
147
199
# ]
148
200
# ///
149
201
150
-
from aiosqlite import Connection
151
202
from litestar import Litestar, get
152
203
153
204
from sqlspec.adapters.aiosqlite import AiosqliteConfig, AiosqliteDriver
154
-
from sqlspec.extensions.litestar import SQLSpec
205
+
from sqlspec.extensions.litestar importDatabaseConfig, SQLSpec
SQLSpec originally drew inspiration from features found in the `aiosql` library. This is a great library for working withandexecutedSQL stored in files. It's unclear how much of an overlap there will be between the two libraries, but it's possible that some features will be contributed back to `aiosql` where appropriate.
224
+
SQLSpec originally drew inspiration from features found in the `aiosql` library. This is a great library for working with and executing SQL stored in files. It's unclear how much of an overlap there will be between the two libraries, but it's possible that some features will be contributed back to `aiosql` where appropriate.
171
225
172
226
## Current Focus: Universal Connectivity
173
227
@@ -207,9 +261,10 @@ This list is not final. If you have a driver you'd like to see added, please ope
207
261
-`litestar/`: Litestar framework integration β
208
262
-`fastapi/`: Future home of `fastapi` integration.
209
263
-`flask/`: Future home of `flask` integration.
210
-
-`*/`: Future home of your favorite framework integration π β¨
264
+
-`*/`: Future home of your favorite framework integration
211
265
-`base.py`: Contains base protocols for database configurations.
212
-
-`filters.py`: Contains the `Filter`class which is used to apply filters to pre-defined SQL queries.
266
+
-`statement/`: Contains the SQL statement system with builders, validation, and transformation.
267
+
-`storage/`: Contains unified storage operations for data import/export.
213
268
-`utils/`: Contains utility functions used throughout the project.
214
269
-`exceptions.py`: Contains custom exceptions for SQLSpec.
215
270
-`typing.py`: Contains type hints, type guards and several facades for optional libraries that are not required for the core functionality of SQLSpec.
Copy file name to clipboardExpand all lines: docs/PYPI_README.md
+78-23Lines changed: 78 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,18 +4,26 @@
4
4
5
5
SQLSpec is an experimental Python library designed to streamline and modernize your SQL interactions across a variety of database systems. While still in its early stages, SQLSpec aims to provide a flexible, typed, and extensible interface for working with SQL in Python.
6
6
7
-
**Note**: SQLSpec is currently under active development and the API is subject to change. It is not yet ready for production use. Contributions are welcome!
7
+
**Note**: SQLSpec is currently under active development and the API is subject to change. It is not yet ready for production use. Contributions are welcome!
8
8
9
-
## Core Features (Planned but subject to change, removal or redesign)
9
+
## Core Features (Current and Planned)
10
+
11
+
### Currently Implemented
10
12
11
13
-**Consistent Database Session Interface**: Provides a consistent connectivity interface for interacting with one or more database systems, including SQLite, Postgres, DuckDB, MySQL, Oracle, SQL Server, Spanner, BigQuery, and more.
12
-
-**Emphasis on RAW SQL and Minimal Abstractions and Performance**: SQLSpec is a library for working with SQL in Python. It's goals are to offer minimal abstractions between the user and the database. It does not aim to be an ORM library.
14
+
-**Emphasis on RAW SQL and Minimal Abstractions**: SQLSpec is a library for working with SQL in Python. Its goals are to offer minimal abstractions between the user and the database. It does not aim to be an ORM library.
13
15
-**Type-Safe Queries**: Quickly map SQL queries to typed objects using libraries such as Pydantic, Msgspec, Attrs, etc.
14
-
-**Extensible Design**: Easily add support for new database dialects or extend existing functionality to meet your specific needs. Easily add support for async and sync database drivers.
15
-
-**Minimal Dependencies**: SQLSpec is designed to be lightweight and can run on it's own or with other libraries such as `litestar`, `fastapi`, `flask` and more. (Contributions welcome!)
16
-
-**Dynamic Query Manipulation**: Easily apply filters to pre-defined queries with a fluent, Pythonic API. Safely manipulate queries without the risk of SQL injection.
17
-
-**Dialect Validation and Conversion**: Use `sqlglot` to validate your SQL against specific dialects and seamlessly convert between them.
16
+
-**Extensible Design**: Easily add support for new database dialects or extend existing functionality to meet your specific needs. Easily add support for async and sync database drivers.
17
+
-**Minimal Dependencies**: SQLSpec is designed to be lightweight and can run on its own or with other libraries such as `litestar`, `fastapi`, `flask` and more. (Contributions welcome!)
18
18
-**Support for Async and Sync Database Drivers**: SQLSpec supports both async and sync database drivers, allowing you to choose the style that best fits your application.
19
+
20
+
### Experimental Features (API will change rapidly)
21
+
22
+
-**SQL Builder API**: Type-safe query builder with method chaining (experimental and subject to significant changes)
23
+
-**Dynamic Query Manipulation**: Apply filters to pre-defined queries with a fluent API. Safely manipulate queries without SQL injection risk.
24
+
-**Dialect Validation and Conversion**: Use `sqlglot` to validate your SQL against specific dialects and seamlessly convert between them.
25
+
-**Storage Operations**: Direct export to Parquet, CSV, JSON with Arrow integration
26
+
-**Instrumentation**: OpenTelemetry and Prometheus metrics support
19
27
-**Basic Migration Management**: A mechanism to generate empty migration files where you can add your own SQL and intelligently track which migrations have been applied.
20
28
21
29
## What SQLSpec Is Not (Yet)
@@ -26,16 +34,60 @@ SQLSpec is a work in progress. While it offers a solid foundation for modern SQL
26
34
27
35
We've talked about what SQLSpec is not, so let's look at what it can do.
28
36
29
-
These are just a few of the examples that demonstrate SQLSpec's flexibility and each of the bundled adapters offer the same config and driver interfaces.
37
+
These are just a few examples that demonstrate SQLSpec's flexibility. Each of the bundled adapters offers the same config and driver interfaces.
print(query.build().sql) # SELECT id, name, email FROM users WHERE active = ?
66
+
67
+
# More complex example with joins
68
+
query = (
69
+
sql.select("u.name", "COUNT(o.id) as order_count")
70
+
.from_("users u")
71
+
.left_join("orders o", "u.id = o.user_id")
72
+
.where("u.created_at > ?", "2024-01-01")
73
+
.group_by("u.name")
74
+
.having("COUNT(o.id) > ?", 5)
75
+
.order_by("order_count", desc=True)
76
+
)
77
+
78
+
# Execute the built query
79
+
with sql.provide_session(config) as session:
80
+
results = session.execute(query.build())
81
+
```
30
82
31
83
### DuckDB LLM
32
84
33
-
This is a quick implementation using some of the builtin Secret and Extension management features of SQLSpec's DuckDB integration.
85
+
This is a quick implementation using some of the built-in Secret and Extension management features of SQLSpec's DuckDB integration.
34
86
35
-
It allows you to communicate with any compatible OpenAPI conversations endpoint (such as Ollama). This examples:
87
+
It allows you to communicate with any compatible OpenAPI conversations endpoint (such as Ollama). This example:
36
88
37
89
- auto installs the `open_prompt` DuckDB extensions
38
-
- automatically creates the correct `open_prompt`comptaible secret required to use the extension
90
+
- automatically creates the correct `open_prompt`compatible secret required to use the extension
39
91
40
92
```py
41
93
# /// script
@@ -80,12 +132,12 @@ with sql.provide_session(etl_config) as session:
80
132
81
133
### DuckDB Gemini Embeddings
82
134
83
-
In this example, we are again using DuckDB. However, we are going to use the builtin to call the Google Gemini embeddings service directly from the database.
135
+
In this example, we are again using DuckDB. However, we are going to use the built-in to call the Google Gemini embeddings service directly from the database.
84
136
85
-
This example will
137
+
This example will:
86
138
87
139
- auto installs the `http_client` and `vss` (vector similarity search) DuckDB extensions
88
-
- when a connection is created, it ensures that the `generate_embeddings` macro exists in the DuckDB database.
140
+
- when a connection is created, it ensures that the `generate_embeddings` macro exists in the DuckDB database
89
141
- Execute a simple query to call the Google API
90
142
91
143
```py
@@ -131,8 +183,8 @@ etl_config = sql.add_config(
131
183
)
132
184
)
133
185
with sql.provide_session(etl_config) as session:
134
-
result = session.select_one("SELECT generate_embedding('example text')")
135
-
print(result) # result is a dictionary when `schema_type` is omitted.
186
+
result = session.execute("SELECT generate_embedding('example text')")
187
+
print(result.get_first()) # result is a dictionary when `schema_type` is omitted.
136
188
```
137
189
138
190
### Basic Litestar Integration
@@ -147,27 +199,29 @@ In this example we are going to demonstrate how to create a basic configuration
147
199
# ]
148
200
# ///
149
201
150
-
from aiosqlite import Connection
151
202
from litestar import Litestar, get
152
203
153
204
from sqlspec.adapters.aiosqlite import AiosqliteConfig, AiosqliteDriver
154
-
from sqlspec.extensions.litestar import SQLSpec
205
+
from sqlspec.extensions.litestar importDatabaseConfig, SQLSpec
SQLSpec originally drew inspiration from features found in the `aiosql` library. This is a great library for working withandexecutedSQL stored in files. It's unclear how much of an overlap there will be between the two libraries, but it's possible that some features will be contributed back to `aiosql` where appropriate.
224
+
SQLSpec originally drew inspiration from features found in the `aiosql` library. This is a great library for working with and executing SQL stored in files. It's unclear how much of an overlap there will be between the two libraries, but it's possible that some features will be contributed back to `aiosql` where appropriate.
171
225
172
226
## Current Focus: Universal Connectivity
173
227
@@ -207,9 +261,10 @@ This list is not final. If you have a driver you'd like to see added, please ope
207
261
-`litestar/`: Litestar framework integration β
208
262
-`fastapi/`: Future home of `fastapi` integration.
209
263
-`flask/`: Future home of `flask` integration.
210
-
-`*/`: Future home of your favorite framework integration π β¨
264
+
-`*/`: Future home of your favorite framework integration
211
265
-`base.py`: Contains base protocols for database configurations.
212
-
-`filters.py`: Contains the `Filter`class which is used to apply filters to pre-defined SQL queries.
266
+
-`statement/`: Contains the SQL statement system with builders, validation, and transformation.
267
+
-`storage/`: Contains unified storage operations for data import/export.
213
268
-`utils/`: Contains utility functions used throughout the project.
214
269
-`exceptions.py`: Contains custom exceptions for SQLSpec.
215
270
-`typing.py`: Contains type hints, type guards and several facades for optional libraries that are not required for the core functionality of SQLSpec.
0 commit comments