Skip to content

Commit e85f262

Browse files
Minor code beautifications for DuckDB (#94)
1 parent b2e4cba commit e85f262

File tree

14 files changed

+200
-118
lines changed

14 files changed

+200
-118
lines changed

.github/workflows/linux-cxx20-vcpkg.yaml

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ jobs:
6060
- compiler: gcc
6161
compiler-version: 14
6262
db: mysql
63+
- compiler: llvm
64+
compiler-version: 16
65+
db: duckdb
66+
- compiler: llvm
67+
compiler-version: 18
68+
db: duckdb
69+
- compiler: gcc
70+
compiler-version: 11
71+
additional-dep: "g++-11"
72+
db: duckdb
73+
- compiler: gcc
74+
compiler-version: 12
75+
db: duckdb
76+
- compiler: gcc
77+
compiler-version: 14
78+
db: duckdb
6379
name: "${{ github.job }} (${{ matrix.compiler }}-${{ matrix.compiler-version }}-${{ matrix.db }})"
6480
concurrency:
6581
group: ci-${{ github.ref }}-${{ github.job }}-${{ matrix.compiler }}-${{ matrix.compiler-version }}-${{ matrix.db }}
@@ -101,7 +117,22 @@ jobs:
101117
sudo ln -s $(which ccache) /usr/local/bin/$CC
102118
sudo ln -s $(which ccache) /usr/local/bin/$CXX
103119
$CXX --version
104-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF
120+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF
121+
cmake --build build
122+
- name: Compile
123+
if: matrix.db == 'duckdb'
124+
run: |
125+
if [[ "${{ matrix.compiler }}" == "llvm" ]]; then
126+
export CC=clang-${{ matrix.compiler-version }}
127+
export CXX=clang++-${{ matrix.compiler-version }}
128+
elif [[ "${{ matrix.compiler }}" == "gcc" ]]; then
129+
export CC=gcc-${{ matrix.compiler-version }}
130+
export CXX=g++-${{ matrix.compiler-version }}
131+
fi
132+
sudo ln -s $(which ccache) /usr/local/bin/$CC
133+
sudo ln -s $(which ccache) /usr/local/bin/$CXX
134+
$CXX --version
135+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF
105136
cmake --build build
106137
- name: Compile
107138
if: matrix.db == 'sqlite'
@@ -116,7 +147,7 @@ jobs:
116147
sudo ln -s $(which ccache) /usr/local/bin/$CC
117148
sudo ln -s $(which ccache) /usr/local/bin/$CXX
118149
$CXX --version
119-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_CHECK_HEADERS=ON
150+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_CHECK_HEADERS=ON
120151
cmake --build build
121152
- name: Compile
122153
if: matrix.db == 'mysql'
@@ -131,7 +162,7 @@ jobs:
131162
sudo ln -s $(which ccache) /usr/local/bin/$CC
132163
sudo ln -s $(which ccache) /usr/local/bin/$CXX
133164
$CXX --version
134-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-linux-dynamic
165+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-linux-dynamic
135166
cmake --build build
136167
- name: Set up postgres
137168
if: matrix.db == 'postgres'

.github/workflows/macos-cxx20-vcpkg.yaml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ jobs:
1818
db: sqlite
1919
- os: "macos-latest"
2020
db: mysql
21-
- os: "macos-13"
22-
db: postgres
23-
- os: "macos-13"
24-
db: sqlite
25-
- os: "macos-13"
26-
db: mysql
21+
- os: "macos-latest"
22+
db: duckdb
2723
name: "${{ github.job }} (${{ matrix.os }}-${{ matrix.db }})"
2824
concurrency:
2925
group: ci-${{ github.ref }}-${{ github.job }}-${{ matrix.os }}-${{ matrix.db }}
@@ -50,8 +46,21 @@ jobs:
5046
- name: Run vcpkg
5147
uses: lukka/run-vcpkg@v11
5248
- name: Install ninja
53-
run: brew install ninja autoconf bison flex
49+
run: brew install ninja autoconf bison flex pkg-config icu4c
5450
if: matrix.os == 'macos-latest'
51+
- name: Compile
52+
if: matrix.db == 'duckdb'
53+
env:
54+
CC: clang
55+
CXX: clang++
56+
run: |
57+
if [[ "${{ matrix.os == 'macos-latest' }}" == "true" ]]; then
58+
export VCPKG_FORCE_SYSTEM_BINARIES=arm
59+
export CMAKE_GENERATOR=Ninja
60+
fi
61+
$CXX --version
62+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF
63+
cmake --build build -j 4
5564
- name: Compile
5665
if: matrix.db == 'postgres'
5766
env:
@@ -64,7 +73,7 @@ jobs:
6473
export MACOSX_DEPLOYMENT_TARGET="$(sw_vers -productVersion)"
6574
fi
6675
$CXX --version
67-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
76+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
6877
cmake --build build -j 4
6978
- name: Compile
7079
if: matrix.db == 'sqlite'
@@ -77,7 +86,7 @@ jobs:
7786
export CMAKE_GENERATOR=Ninja
7887
fi
7988
$CXX --version
80-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF
89+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF
8190
cmake --build build -j 4
8291
- name: Compile
8392
if: matrix.db == 'mysql'
@@ -91,9 +100,9 @@ jobs:
91100
fi
92101
$CXX --version
93102
if [[ "${{ matrix.os == 'macos-latest' }}" == "true" ]]; then
94-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=arm64-osx-dynamic -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
103+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=arm64-osx-dynamic -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
95104
else
96-
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-osx-dynamic -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
105+
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-osx-dynamic -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON
97106
fi
98107
cmake --build build -j 4
99108
- name: Run tests

.github/workflows/windows-cxx20-vcpkg.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
fail-fast: false
1212
matrix:
1313
include:
14+
- db: duckdb
1415
- db: postgres
1516
- db: sqlite
1617
- db: mysql
@@ -33,6 +34,11 @@ jobs:
3334
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
3435
- uses: ilammy/msvc-dev-cmd@v1
3536
- uses: lukka/run-vcpkg@v11
37+
- name: Compile
38+
if: matrix.db == 'duckdb'
39+
run: |
40+
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF
41+
cmake --build build --config Release -j4
3642
- name: Compile
3743
if: matrix.db == 'postgres'
3844
run: |

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![Generic badge](https://img.shields.io/badge/gcc-11+-blue.svg)](https://shields.io/)
77
[![Generic badge](https://img.shields.io/badge/clang-14+-blue.svg)](https://shields.io/)
88
[![Generic badge](https://img.shields.io/badge/MSVC-17+-blue.svg)](https://shields.io/)
9+
[![Conan Center](https://img.shields.io/conan/v/sqlgen)](https://conan.io/center/recipes/sqlgen)
910

1011
**📖 Documentation**: [Click here](docs/README.md)
1112

@@ -31,14 +32,21 @@ The following table lists the databases currently supported by sqlgen and the un
3132

3233
| Database | Library | Version | License | Remarks |
3334
|---------------|--------------------------------------------------------------------------|--------------|---------------| -----------------------------------------------------|
34-
| DuckDB | [duckdb](https://github.com/duckdb/duckdb) | >= 1.4.1 | MIT | |
35+
| DuckDB | [duckdb](https://github.com/duckdb/duckdb) | >= 1.4.2 | MIT | |
3536
| MySQL/MariaDB | [libmariadb](https://github.com/mariadb-corporation/mariadb-connector-c) | >= 3.4.5 | LGPL | |
3637
| PostgreSQL | [libpq](https://github.com/postgres/postgres) | >= 16.4 | PostgreSQL | Will work for all libpq-compatible databases |
3738
| sqlite | [sqlite](https://sqlite.org/index.html) | >= 3.49.1 | Public Domain | |
3839

3940
## Quick Start
4041

41-
### Installation using vcpkg
42+
### Install using vcpkg or Conan
43+
44+
You can install the latest release of sqlgen
45+
using either [vcpkg](https://vcpkg.io/en/package/sqlgen) or [Conan](https://conan.io/center/recipes/sqlgen).
46+
47+
### Build using vcpkg
48+
49+
Alternatively, you can build sqlgen from source using vcpkg:
4250

4351
1. Make sure you have the required dependencies installed (skip this step on Windows):
4452
```bash
@@ -66,15 +74,17 @@ Run `./vcpkg/vcpkg help triplets` to view all supported triplets.
6674
Common triplets for shared libraries are `x64-linux-dynamic`,
6775
`arm64-osx-dynamic` or `x64-osx-dynamic`.
6876

69-
Add `-DSQLGEN_MYSQL=ON` to support MySQL/MariaDB.
77+
Add `-DSQLGEN_MYSQL=ON` to support MySQL/MariaDB. Add `-DSQLGEN_DUCKDB=ON` to support DuckDB.
7078

7179
4. Include in your CMake project:
7280
```cmake
7381
find_package(sqlgen REQUIRED)
7482
target_link_libraries(your_target PRIVATE sqlgen::sqlgen)
7583
```
7684

77-
### Installation using Conan
85+
### Build using Conan
86+
87+
You can also build sqlgen from source using Conan:
7888

7989
1. Install Conan (assuming you have Python and pipx installed):
8090

conanfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SQLGenConan(ConanFile):
1919
license = "MIT"
2020
url = "https://github.com/conan-io/conan-center-index"
2121
homepage = "https://github.com/getml/sqlgen"
22-
topics = ("postgres", "sqlite", "orm")
22+
topics = ("duckdb", "mypy", "postgres", "sqlite", "orm")
2323
package_type = "library"
2424
settings = "os", "arch", "compiler", "build_type"
2525

@@ -29,13 +29,15 @@ class SQLGenConan(ConanFile):
2929
"with_mysql": [True, False],
3030
"with_postgres": [True, False],
3131
"with_sqlite3": [True, False],
32+
"with_duckdb": [True, False],
3233
}
3334
default_options = {
3435
"shared": False,
3536
"fPIC": True,
3637
"with_mysql": False,
3738
"with_postgres": True,
3839
"with_sqlite3": True,
40+
"with_duckdb": False,
3941
}
4042

4143
def config_options(self):
@@ -54,6 +56,8 @@ def requirements(self):
5456
self.requires("libpq/17.5", transitive_headers=True)
5557
if self.options.with_sqlite3:
5658
self.requires("sqlite3/3.49.1", transitive_headers=True)
59+
if self.options.with_duckdb:
60+
self.requires("duckdb/1.1.3", transitive_headers=True)
5761

5862
def build_requirements(self):
5963
self.tool_requires("cmake/[>=3.23 <4]")
@@ -78,6 +82,7 @@ def generate(self):
7882
tc.cache_variables["SQLGEN_MYSQL"] = self.options.with_mysql
7983
tc.cache_variables["SQLGEN_POSTGRES"] = self.options.with_postgres
8084
tc.cache_variables["SQLGEN_SQLITE3"] = self.options.with_sqlite3
85+
tc.cache_variables["SQLGEN_DUCKDB"] = self.options.with_duckdb
8186
tc.cache_variables["SQLGEN_USE_VCPKG"] = False
8287
tc.generate()
8388

include/sqlgen/duckdb/DuckDBAppender.hpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,13 @@ class SQLGEN_API DuckDBAppender {
1717
static Result<Ref<DuckDBAppender>> make(
1818
const std::string& _sql, const ConnPtr& _conn,
1919
const std::vector<const char*>& _columns,
20-
const std::vector<duckdb_logical_type>& _types) {
21-
try {
22-
return Ref<DuckDBAppender>::make(_sql, _conn, _columns, _types);
23-
} catch (const std::exception& e) {
24-
return error(e.what());
25-
}
26-
}
20+
const std::vector<duckdb_logical_type>& _types);
2721

2822
DuckDBAppender(const std::string& _sql, const ConnPtr& _conn,
2923
std::vector<const char*> _columns,
30-
std::vector<duckdb_logical_type> _types)
31-
: destroy_(false) {
32-
if (duckdb_appender_create_query(
33-
_conn->conn(), _sql.c_str(), static_cast<idx_t>(_columns.size()),
34-
_types.data(), "sqlgen_appended_data", _columns.data(),
35-
&appender_) == DuckDBError) {
36-
throw std::runtime_error("Could not create appender.");
37-
}
38-
destroy_ = true;
39-
}
24+
std::vector<duckdb_logical_type> _types);
4025

41-
~DuckDBAppender() {
42-
if (destroy_) {
43-
duckdb_appender_destroy(&appender_);
44-
}
45-
}
26+
~DuckDBAppender();
4627

4728
DuckDBAppender(const DuckDBAppender& _other) = delete;
4829

@@ -53,25 +34,11 @@ class SQLGEN_API DuckDBAppender {
5334

5435
DuckDBAppender& operator=(const DuckDBAppender& _other) = delete;
5536

56-
DuckDBAppender& operator=(DuckDBAppender&& _other) {
57-
if (this == &_other) {
58-
return *this;
59-
}
60-
destroy_ = _other.destroy_;
61-
appender_ = _other.appender_;
62-
_other.destroy_ = false;
63-
return *this;
64-
}
37+
DuckDBAppender& operator=(DuckDBAppender&& _other);
6538

6639
duckdb_appender& appender() { return appender_; }
6740

68-
Result<Nothing> close() {
69-
const auto state = duckdb_appender_close(appender_);
70-
if (state == DuckDBError) {
71-
return error(duckdb_appender_error(appender_));
72-
}
73-
return Nothing{};
74-
}
41+
Result<Nothing> close();
7542

7643
private:
7744
bool destroy_;

include/sqlgen/duckdb/DuckDBResult.hpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,11 @@ class SQLGEN_API DuckDBResult {
1515

1616
public:
1717
static Result<Ref<DuckDBResult>> make(const std::string& _query,
18-
const ConnPtr& _conn) {
19-
try {
20-
return Ref<DuckDBResult>::make(_query, _conn);
21-
} catch (const std::exception& e) {
22-
return error(e.what());
23-
}
24-
}
18+
const ConnPtr& _conn);
2519

26-
DuckDBResult(const std::string& _query, const ConnPtr& _conn)
27-
: destroy_(false) {
28-
if (duckdb_query(_conn->conn(), _query.c_str(), &res_) == DuckDBError) {
29-
throw std::runtime_error(duckdb_result_error(&res_));
30-
}
31-
destroy_ = true;
32-
}
20+
DuckDBResult(const std::string& _query, const ConnPtr& _conn);
3321

34-
~DuckDBResult() {
35-
if (destroy_) {
36-
duckdb_destroy_result(&res_);
37-
}
38-
}
22+
~DuckDBResult();
3923

4024
DuckDBResult(const DuckDBResult& _other) = delete;
4125

@@ -46,15 +30,7 @@ class SQLGEN_API DuckDBResult {
4630

4731
DuckDBResult& operator=(const DuckDBResult& _other) = delete;
4832

49-
DuckDBResult& operator=(DuckDBResult&& _other) {
50-
if (this == &_other) {
51-
return *this;
52-
}
53-
destroy_ = _other.destroy_;
54-
res_ = _other.res_;
55-
_other.destroy_ = false;
56-
return *this;
57-
}
33+
DuckDBResult& operator=(DuckDBResult&& _other);
5834

5935
duckdb_result& res() { return res_; }
6036

sqlgen-config.cmake.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@PACKAGE_INIT@
22

3+
set(SQLGEN_DUCKDB @SQLGEN_DUCKDB@)
4+
set(SQLGEN_MYSQL @SQLGEN_MYSQL@)
35
set(SQLGEN_POSTGRES @SQLGEN_POSTGRES@)
46
set(SQLGEN_SQLITE3 @SQLGEN_SQLITE3@)
57

@@ -9,6 +11,14 @@ include(${CMAKE_CURRENT_LIST_DIR}/sqlgen-exports.cmake)
911

1012
find_dependency(reflectcpp)
1113

14+
if(SQLGEN_DUCKDB)
15+
find_dependency(DuckDB)
16+
endif()
17+
18+
if(SQLGEN_MYSQL)
19+
find_dependency(unofficial-libmariadb)
20+
endif()
21+
1222
if(SQLGEN_POSTGRES)
1323
find_dependency(PostgreSQL)
1424
endif()

0 commit comments

Comments
 (0)