├── .github └── workflows │ ├── release.yaml │ ├── rename-wheels.py │ ├── test.yaml │ └── upload-deno-assets.js ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── Makefile ├── README.md ├── VERSION ├── bench.sh ├── bench.sql ├── bindings ├── go │ ├── go.mod │ ├── lib.go │ └── ulid │ │ ├── sqlite-ulid.h │ │ └── ulid.go ├── ruby │ ├── .gitignore │ ├── Gemfile │ ├── Rakefile │ ├── lib │ │ ├── sqlite_ulid.rb │ │ ├── version.rb │ │ └── version.rb.tmpl │ └── sqlite_ulid.gemspec └── sqlite-utils │ ├── .gitignore │ ├── README.md │ ├── pyproject.toml │ ├── pyproject.toml.tmpl │ └── sqlite_utils_sqlite_ulid │ ├── __init__.py │ ├── version.py │ └── version.py.tmpl ├── build.rs ├── deno ├── README.md ├── README.md.tmpl ├── deno.json ├── deno.json.tmpl ├── deno.lock ├── mod.ts └── test.ts ├── docs.md ├── examples ├── c │ ├── .gitignore │ ├── Makefile │ └── demo.c └── go │ ├── .gitignore │ ├── Makefile │ ├── demo.go │ ├── go.mod │ └── go.sum ├── npm ├── .gitignore ├── README.md ├── platform-package.README.md.tmpl ├── platform-package.package.json.tmpl ├── sqlite-ulid-darwin-arm64 │ ├── README.md │ ├── lib │ │ └── .gitkeep │ └── package.json ├── sqlite-ulid-darwin-x64 │ ├── README.md │ ├── lib │ │ └── .gitkeep │ └── package.json ├── sqlite-ulid-linux-x64 │ ├── README.md │ ├── lib │ │ └── .gitkeep │ └── package.json ├── sqlite-ulid-windows-x64 │ ├── README.md │ ├── lib │ │ └── .gitkeep │ └── package.json └── sqlite-ulid │ ├── README.md │ ├── package.json │ ├── package.json.tmpl │ ├── src │ └── index.js │ └── test.js ├── python ├── .gitignore ├── README.md ├── datasette_sqlite_ulid │ ├── README.md │ ├── datasette_sqlite_ulid │ │ ├── __init__.py │ │ └── version.py │ ├── setup.py │ └── tests │ │ └── test_sqlite_ulid.py └── sqlite_ulid │ ├── README.md │ ├── noop.c │ ├── setup.py │ └── sqlite_ulid │ ├── __init__.py │ └── version.py ├── scripts ├── deno_generate_package.sh ├── npm_generate_platform_packages.sh └── publish_release.sh ├── sqlite-ulid.h ├── src └── lib.rs ├── test.sql └── tests ├── test-loadable.py └── test-python.py /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: "Release" 2 | on: 3 | release: 4 | types: [published] 5 | workflow_dispatch: 6 | permissions: 7 | contents: read 8 | jobs: 9 | build-ubuntu-extension: 10 | name: Build ubuntu 11 | runs-on: ubuntu-20.04 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: actions/cache@v3 15 | with: 16 | path: | 17 | ~/.cargo/bin/ 18 | ~/.cargo/registry/index/ 19 | ~/.cargo/registry/cache/ 20 | ~/.cargo/git/db/ 21 | target/ 22 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 23 | - uses: actions-rs/toolchain@v1 24 | with: 25 | toolchain: stable 26 | - run: make loadable-release static-release 27 | - name: Upload artifacts 28 | uses: actions/upload-artifact@v2 29 | with: 30 | name: sqlite-ulid-ubuntu 31 | path: dist/release/ulid0.so 32 | build-ubuntu-python: 33 | runs-on: ubuntu-20.04 34 | needs: [build-ubuntu-extension] 35 | steps: 36 | - uses: actions/checkout@v3 37 | - name: Download workflow artifacts 38 | uses: actions/download-artifact@v3 39 | with: 40 | name: sqlite-ulid-ubuntu 41 | path: dist/release/ 42 | - uses: actions/setup-python@v3 43 | - run: pip install wheel 44 | - run: make python-release 45 | - run: make datasette-release 46 | - uses: actions/upload-artifact@v3 47 | with: 48 | name: sqlite-ulid-ubuntu-wheels 49 | path: dist/release/wheels/*.whl 50 | build-macos-extension: 51 | name: Build macos-latest 52 | runs-on: macos-latest 53 | steps: 54 | - uses: actions/checkout@v2 55 | - uses: actions/cache@v3 56 | with: 57 | path: | 58 | ~/.cargo/bin/ 59 | ~/.cargo/registry/index/ 60 | ~/.cargo/registry/cache/ 61 | ~/.cargo/git/db/ 62 | target/ 63 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 64 | - uses: actions-rs/toolchain@v1 65 | with: 66 | toolchain: stable 67 | - run: make loadable-release static-release 68 | - name: Upload artifacts 69 | uses: actions/upload-artifact@v2 70 | with: 71 | name: sqlite-ulid-macos 72 | path: dist/release/ulid0.dylib 73 | build-macos-python: 74 | runs-on: macos-latest 75 | needs: [build-macos-extension] 76 | steps: 77 | - uses: actions/checkout@v3 78 | - name: Download workflow artifacts 79 | uses: actions/download-artifact@v3 80 | with: 81 | name: sqlite-ulid-macos 82 | path: dist/release/ 83 | - uses: actions/setup-python@v3 84 | - run: pip install wheel 85 | - run: make python-release 86 | - run: make datasette-release 87 | - uses: actions/upload-artifact@v3 88 | with: 89 | name: sqlite-ulid-macos-wheels 90 | path: dist/release/wheels/*.whl 91 | build-macos-arm-extension: 92 | name: Build macos-latest with arm 93 | runs-on: macos-latest 94 | steps: 95 | - uses: actions/checkout@v3 96 | - uses: actions/cache@v3 97 | with: 98 | path: | 99 | ~/.cargo/bin/ 100 | ~/.cargo/registry/index/ 101 | ~/.cargo/registry/cache/ 102 | ~/.cargo/git/db/ 103 | target/ 104 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 105 | - uses: actions-rs/toolchain@v1 106 | with: 107 | toolchain: stable 108 | - run: rustup target add aarch64-apple-darwin 109 | - run: make loadable-release static-release target=aarch64-apple-darwin 110 | - name: Upload artifacts 111 | uses: actions/upload-artifact@v3 112 | with: 113 | name: sqlite-ulid-macos-arm 114 | path: dist/release/ulid0.dylib 115 | build-macos-arm-python: 116 | runs-on: macos-latest 117 | needs: [build-macos-arm-extension] 118 | steps: 119 | - uses: actions/checkout@v3 120 | - name: Download workflow artifacts 121 | uses: actions/download-artifact@v3 122 | with: 123 | name: sqlite-ulid-macos-arm 124 | path: dist/release/ 125 | - uses: actions/setup-python@v3 126 | - run: pip install wheel 127 | - run: make python-release IS_MACOS_ARM=1 128 | - run: make datasette-release 129 | - uses: actions/upload-artifact@v3 130 | with: 131 | name: sqlite-ulid-macos-arm-wheels 132 | path: dist/release/wheels/*.whl 133 | build-windows-extension: 134 | name: Build windows-latest 135 | runs-on: windows-latest 136 | steps: 137 | - uses: actions/checkout@v2 138 | - uses: actions/cache@v3 139 | with: 140 | path: | 141 | ~/.cargo/bin/ 142 | ~/.cargo/registry/index/ 143 | ~/.cargo/registry/cache/ 144 | ~/.cargo/git/db/ 145 | target/ 146 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 147 | - uses: actions-rs/toolchain@v1 148 | with: 149 | toolchain: stable 150 | - run: make loadable-release static-release target=x86_64-pc-windows-gnu 151 | - name: Upload artifacts 152 | uses: actions/upload-artifact@v2 153 | with: 154 | name: sqlite-ulid-windows 155 | path: dist/release/ulid0.dll 156 | build-windows-python: 157 | runs-on: windows-latest 158 | needs: [build-windows-extension] 159 | steps: 160 | - uses: actions/checkout@v3 161 | - name: Download workflow artifacts 162 | uses: actions/download-artifact@v3 163 | with: 164 | name: sqlite-ulid-windows 165 | path: dist/release/ 166 | - uses: actions/setup-python@v3 167 | - run: pip install wheel 168 | - run: make python-release 169 | - run: make datasette-release 170 | - uses: actions/upload-artifact@v3 171 | with: 172 | name: sqlite-ulid-windows-wheels 173 | path: dist/release/wheels/*.whl 174 | build-datasette-sqlite-utils: 175 | runs-on: ubuntu-20.04 176 | steps: 177 | - uses: actions/checkout@v3 178 | - uses: actions/setup-python@v3 179 | - run: pip install wheel build 180 | - run: make datasette-release sqlite-utils-release 181 | - uses: actions/upload-artifact@v3 182 | with: 183 | name: sqlite-ulid-datasette-sqlite-utils-wheels 184 | path: dist/release/wheels/*.whl 185 | upload-extensions: 186 | name: Upload release assets 187 | needs: 188 | [ 189 | build-macos-extension, 190 | build-macos-arm-extension, 191 | build-ubuntu-extension, 192 | build-windows-extension, 193 | ] 194 | permissions: 195 | contents: write 196 | runs-on: ubuntu-latest 197 | steps: 198 | - uses: actions/checkout@v2 199 | - uses: actions/download-artifact@v2 200 | - uses: asg017/upload-spm@main 201 | id: upload-spm 202 | with: 203 | name: sqlite-ulid 204 | github-token: ${{ secrets.GITHUB_TOKEN }} 205 | platforms: | 206 | linux-x86_64: sqlite-ulid-ubuntu/* 207 | macos-x86_64: sqlite-ulid-macos/* 208 | macos-aarch64: sqlite-ulid-macos-arm/* 209 | windows-x86_64: sqlite-ulid-windows/* 210 | upload-deno: 211 | name: Upload Deno release assets 212 | needs: 213 | [ 214 | build-macos-extension, 215 | build-macos-arm-extension, 216 | build-ubuntu-extension, 217 | build-windows-extension, 218 | ] 219 | permissions: 220 | contents: write 221 | runs-on: ubuntu-latest 222 | steps: 223 | - uses: actions/checkout@v3 224 | - name: Download workflow artifacts 225 | uses: actions/download-artifact@v2 226 | - uses: actions/github-script@v6 227 | with: 228 | github-token: ${{ secrets.GITHUB_TOKEN }} 229 | script: | 230 | const script = require('.github/workflows/upload-deno-assets.js') 231 | await script({github, context}) 232 | upload-npm: 233 | needs: 234 | [ 235 | build-macos-extension, 236 | build-macos-arm-extension, 237 | build-ubuntu-extension, 238 | build-windows-extension, 239 | ] 240 | runs-on: ubuntu-latest 241 | steps: 242 | - uses: actions/checkout@v3 243 | - name: Download workflow artifacts 244 | uses: actions/download-artifact@v2 245 | - run: | 246 | cp sqlite-ulid-ubuntu/ulid0.so npm/sqlite-ulid-linux-x64/lib/ulid0.so 247 | cp sqlite-ulid-macos/ulid0.dylib npm/sqlite-ulid-darwin-x64/lib/ulid0.dylib 248 | cp sqlite-ulid-macos-arm/ulid0.dylib npm/sqlite-ulid-darwin-arm64/lib/ulid0.dylib 249 | cp sqlite-ulid-windows/ulid0.dll npm/sqlite-ulid-windows-x64/lib/ulid0.dll 250 | - name: Install node 251 | uses: actions/setup-node@v3 252 | with: 253 | node-version: "16" 254 | registry-url: "https://registry.npmjs.org" 255 | - name: Publish NPM sqlite-ulid-linux-x64 256 | working-directory: npm/sqlite-ulid-linux-x64 257 | run: npm publish --access public 258 | env: 259 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 260 | - name: Publish NPM sqlite-ulid-darwin-x64 261 | working-directory: npm/sqlite-ulid-darwin-x64 262 | run: npm publish --access public 263 | env: 264 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 265 | - name: Publish NPM sqlite-ulid-darwin-arm64 266 | working-directory: npm/sqlite-ulid-darwin-arm64 267 | run: npm publish --access public 268 | env: 269 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 270 | - name: Publish NPM sqlite-ulid-windows-x64 271 | working-directory: npm/sqlite-ulid-windows-x64 272 | run: npm publish --access public 273 | env: 274 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 275 | - name: Publish NPM sqlite-ulid 276 | working-directory: npm/sqlite-ulid 277 | run: npm publish --access public 278 | env: 279 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 280 | upload-gem: 281 | needs: 282 | [ 283 | build-macos-extension, 284 | build-macos-arm-extension, 285 | build-ubuntu-extension, 286 | build-windows-extension, 287 | ] 288 | permissions: 289 | contents: write 290 | runs-on: ubuntu-latest 291 | steps: 292 | - uses: actions/checkout@v2 293 | - uses: actions/download-artifact@v2 294 | - uses: ruby/setup-ruby@v1 295 | with: 296 | ruby-version: 3.2 297 | - run: | 298 | rm bindings/ruby/lib/*.{dylib,so,dll} || true 299 | cp sqlite-ulid-macos/*.dylib bindings/ruby/lib 300 | gem -C bindings/ruby build -o x86_64-darwin.gem sqlite_ulid.gemspec 301 | env: 302 | PLATFORM: x86_64-darwin 303 | - run: | 304 | rm bindings/ruby/lib/*.{dylib,so,dll} || true 305 | cp sqlite-ulid-macos-arm/*.dylib bindings/ruby/lib 306 | gem -C bindings/ruby build -o arm64-darwin.gem sqlite_ulid.gemspec 307 | env: 308 | PLATFORM: arm64-darwin 309 | - run: | 310 | rm bindings/ruby/lib/*.{dylib,so,dll} || true 311 | cp sqlite-ulid-ubuntu/*.so bindings/ruby/lib 312 | gem -C bindings/ruby build -o x86_64-linux.gem sqlite_ulid.gemspec 313 | env: 314 | PLATFORM: x86_64-linux 315 | - run: | 316 | rm bindings/ruby/lib/*.{dylib,so,dll} || true 317 | cp sqlite-ulid-windows/*.dll bindings/ruby/lib 318 | gem -C bindings/ruby build -o ${{ env.PLATFORM }}.gem sqlite_ulid.gemspec 319 | env: 320 | PLATFORM: x64-mingw32 321 | - run: | 322 | gem push bindings/ruby/x86_64-linux.gem 323 | gem push bindings/ruby/x86_64-darwin.gem 324 | gem push bindings/ruby/arm64-darwin.gem 325 | gem push bindings/ruby/x64-mingw32.gem 326 | env: 327 | GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }} 328 | upload-crate: 329 | runs-on: ubuntu-latest 330 | needs: [upload-extensions] 331 | steps: 332 | - uses: actions/checkout@v2 333 | - uses: actions-rs/toolchain@v1 334 | with: 335 | toolchain: stable 336 | - run: cargo publish 337 | env: 338 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} 339 | upload-pypi: 340 | needs: 341 | [ 342 | build-ubuntu-python, 343 | build-macos-python, 344 | build-macos-arm-python, 345 | build-windows-python, 346 | build-datasette-sqlite-utils, 347 | ] 348 | runs-on: ubuntu-latest 349 | steps: 350 | - uses: actions/download-artifact@v3 351 | with: 352 | name: sqlite-ulid-windows-wheels 353 | path: dist 354 | - uses: actions/download-artifact@v3 355 | with: 356 | name: sqlite-ulid-ubuntu-wheels 357 | path: dist 358 | - uses: actions/download-artifact@v3 359 | with: 360 | name: sqlite-ulid-macos-wheels 361 | path: dist 362 | - uses: actions/download-artifact@v3 363 | with: 364 | name: sqlite-ulid-macos-arm-wheels 365 | path: dist 366 | - uses: actions/download-artifact@v3 367 | with: 368 | name: sqlite-ulid-datasette-sqlite-utils-wheels 369 | path: dist 370 | - uses: pypa/gh-action-pypi-publish@release/v1 371 | with: 372 | password: ${{ secrets.PYPI_API_TOKEN }} 373 | skip_existing: true 374 | -------------------------------------------------------------------------------- /.github/workflows/rename-wheels.py: -------------------------------------------------------------------------------- 1 | # This file is a small utility that rename all .whl files in a given directory 2 | # and "generalizes" them. The wheels made by python/sqlite_ulid contain the 3 | # pre-compiled sqlite extension, but those aren't bound by a specfic Python 4 | # runtime or version, that other wheels might be. So, this file will rename 5 | # those wheels to be "generalized", like replacing "c37-cp37" to "py3-none". 6 | import sys 7 | import os 8 | from pathlib import Path 9 | 10 | wheel_dir = sys.argv[1] 11 | 12 | is_macos_arm_build = '--is-macos-arm' in sys.argv 13 | 14 | for filename in os.listdir(wheel_dir): 15 | filename = Path(wheel_dir, filename) 16 | if not filename.suffix == '.whl': 17 | continue 18 | new_filename = (filename.name 19 | .replace('cp37-cp37', 'py3-none') 20 | .replace('cp38-cp38', 'py3-none') 21 | .replace('cp39-cp39', 'py3-none') 22 | .replace('cp310-cp310', 'py3-none') 23 | .replace('cp311-cp311', 'py3-none') 24 | .replace('linux_x86_64', 'manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux1_x86_64') 25 | 26 | 27 | ) 28 | if is_macos_arm_build: 29 | new_filename = new_filename.replace('macosx_12_0_universal2', 'macosx_11_0_arm64') 30 | else: 31 | new_filename = new_filename.replace('macosx_12_0_universal2', 'macosx_10_6_x86_64') 32 | print("renaming", filename, "to", new_filename) 33 | os.rename(filename, Path(wheel_dir, new_filename)) -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: "build" 2 | on: 3 | push: 4 | branches: 5 | - main 6 | permissions: 7 | contents: read 8 | jobs: 9 | build-ubuntu-extension: 10 | name: Building ubuntu 11 | runs-on: ubuntu-20.04 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: actions/cache@v3 15 | with: 16 | path: | 17 | ~/.cargo/bin/ 18 | ~/.cargo/registry/index/ 19 | ~/.cargo/registry/cache/ 20 | ~/.cargo/git/db/ 21 | target/ 22 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 23 | - uses: actions-rs/toolchain@v1 24 | with: 25 | toolchain: stable 26 | - run: make loadable static 27 | - name: Upload artifacts 28 | uses: actions/upload-artifact@v3 29 | with: 30 | name: sqlite-ulid-ubuntu 31 | path: dist/debug/* 32 | build-ubuntu-python: 33 | runs-on: ubuntu-20.04 34 | needs: [build-ubuntu-extension] 35 | steps: 36 | - uses: actions/checkout@v3 37 | - name: Download workflow artifacts 38 | uses: actions/download-artifact@v3 39 | with: 40 | name: sqlite-ulid-ubuntu 41 | path: dist/debug/ 42 | - uses: actions/setup-python@v3 43 | - run: pip install wheel 44 | - run: make python 45 | - run: make datasette 46 | - uses: actions/upload-artifact@v3 47 | with: 48 | name: sqlite-ulid-ubuntu-wheels 49 | path: dist/debug/wheels/*.whl 50 | test-ubuntu: 51 | runs-on: ubuntu-20.04 52 | needs: [build-ubuntu-extension, build-ubuntu-python] 53 | env: 54 | DENO_DIR: deno_cache 55 | steps: 56 | - uses: actions/checkout@v3 57 | - uses: actions/download-artifact@v3 58 | with: 59 | name: sqlite-ulid-ubuntu 60 | path: dist/debug/ 61 | - uses: actions/download-artifact@v3 62 | with: 63 | name: sqlite-ulid-ubuntu 64 | path: npm/sqlite-ulid-linux-x64/lib 65 | - uses: actions/download-artifact@v3 66 | with: 67 | name: sqlite-ulid-ubuntu-wheels 68 | path: dist/debug/ 69 | - run: pip3 install --find-links dist/debug/ sqlite_ulid 70 | - run: make test-loadable 71 | - run: make test-python 72 | # for test-npm 73 | - uses: actions/setup-node@v3 74 | with: 75 | cache: "npm" 76 | cache-dependency-path: npm/sqlite-ulid/package.json 77 | - run: npm install 78 | working-directory: npm/sqlite-ulid 79 | - run: make test-npm 80 | # for test-deno 81 | - uses: denoland/setup-deno@v1 82 | with: 83 | deno-version: v1.30 84 | - name: Cache Deno dependencies 85 | uses: actions/cache@v3 86 | with: 87 | path: ${{ env.DENO_DIR }} 88 | key: ${{ runner.os }}-${{ hashFiles('deno/deno.lock') }} 89 | - run: make test-deno 90 | env: 91 | DENO_SQLITE_ULID_PATH: ${{ github.workspace }}/dist/debug/ulid0 92 | - run: go run demo.go 93 | working-directory: examples/go 94 | env: 95 | CGO_LDFLAGS: -L${{ github.workspace }}/dist/debug 96 | build-macos-extension: 97 | name: Building macos-latest 98 | runs-on: macos-latest 99 | steps: 100 | - uses: actions/checkout@v3 101 | - uses: actions/cache@v3 102 | with: 103 | path: | 104 | ~/.cargo/bin/ 105 | ~/.cargo/registry/index/ 106 | ~/.cargo/registry/cache/ 107 | ~/.cargo/git/db/ 108 | target/ 109 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 110 | - uses: actions-rs/toolchain@v1 111 | with: 112 | toolchain: stable 113 | - run: make loadable static 114 | - name: Upload artifacts 115 | uses: actions/upload-artifact@v3 116 | with: 117 | name: sqlite-ulid-macos 118 | path: dist/debug/* 119 | build-macos-python: 120 | runs-on: macos-latest 121 | needs: [build-macos-extension] 122 | steps: 123 | - uses: actions/checkout@v3 124 | - name: Download workflow artifacts 125 | uses: actions/download-artifact@v3 126 | with: 127 | name: sqlite-ulid-macos 128 | path: dist/debug/ 129 | - uses: actions/setup-python@v3 130 | - run: pip install wheel 131 | - run: make python 132 | - run: make datasette 133 | - uses: actions/upload-artifact@v3 134 | with: 135 | name: sqlite-ulid-macos-wheels 136 | path: dist/debug/wheels/*.whl 137 | test-macos: 138 | runs-on: macos-latest 139 | needs: [build-macos-extension, build-macos-python] 140 | env: 141 | DENO_DIR: deno_cache 142 | steps: 143 | - uses: actions/checkout@v3 144 | - uses: actions/download-artifact@v3 145 | with: 146 | name: sqlite-ulid-macos 147 | path: dist/debug/ 148 | - uses: actions/download-artifact@v3 149 | with: 150 | name: sqlite-ulid-macos 151 | path: npm/sqlite-ulid-darwin-x64/lib 152 | - uses: actions/download-artifact@v3 153 | with: 154 | name: sqlite-ulid-macos-wheels 155 | path: dist/debug/ 156 | - run: brew install python 157 | - run: /usr/local/opt/python@3/libexec/bin/pip install --find-links dist/debug/ sqlite_ulid 158 | - run: make test-loadable python=/usr/local/opt/python@3/libexec/bin/python 159 | - run: make test-python python=/usr/local/opt/python@3/libexec/bin/python 160 | # for test-npm 161 | - uses: actions/setup-node@v3 162 | with: 163 | cache: "npm" 164 | cache-dependency-path: npm/sqlite-ulid/package.json 165 | - run: npm install 166 | working-directory: npm/sqlite-ulid 167 | - run: make test-npm 168 | # for test-deno 169 | - uses: denoland/setup-deno@v1 170 | with: 171 | deno-version: v1.30 172 | - name: Cache Deno dependencies 173 | uses: actions/cache@v3 174 | with: 175 | path: ${{ env.DENO_DIR }} 176 | key: ${{ runner.os }}-${{ hashFiles('deno/deno.lock') }} 177 | - run: make test-deno 178 | env: 179 | DENO_SQLITE_ULID_PATH: ${{ github.workspace }}/dist/debug/ulid0 180 | - run: go run demo.go 181 | working-directory: examples/go 182 | env: 183 | CGO_LDFLAGS: -L${{ github.workspace }}/dist/debug 184 | build-macos-arm-extension: 185 | name: Building macos arm extension 186 | runs-on: macos-latest 187 | steps: 188 | - uses: actions/checkout@v3 189 | - uses: actions/cache@v3 190 | with: 191 | path: | 192 | ~/.cargo/bin/ 193 | ~/.cargo/registry/index/ 194 | ~/.cargo/registry/cache/ 195 | ~/.cargo/git/db/ 196 | target/ 197 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 198 | - uses: actions-rs/toolchain@v1 199 | with: 200 | toolchain: stable 201 | - run: rustup target add aarch64-apple-darwin 202 | - run: make loadable static target=aarch64-apple-darwin 203 | - name: Upload artifacts 204 | uses: actions/upload-artifact@v3 205 | with: 206 | name: sqlite-ulid-macos-arm 207 | path: dist/debug/* 208 | build-macos-arm-python: 209 | runs-on: macos-latest 210 | needs: [build-macos-arm-extension] 211 | steps: 212 | - uses: actions/checkout@v3 213 | - name: Download workflow artifacts 214 | uses: actions/download-artifact@v3 215 | with: 216 | name: sqlite-ulid-macos-arm 217 | path: dist/debug/ 218 | - uses: actions/setup-python@v3 219 | - run: pip install wheel 220 | - run: make python IS_MACOS_ARM=1 221 | - run: make datasette 222 | - uses: actions/upload-artifact@v3 223 | with: 224 | name: sqlite-ulid-macos-arm-wheels 225 | path: dist/debug/wheels/*.whl 226 | build-windows-extension: 227 | name: Building windows extension 228 | runs-on: windows-latest 229 | steps: 230 | - uses: actions/checkout@v3 231 | - uses: actions/cache@v3 232 | with: 233 | path: | 234 | ~/.cargo/bin/ 235 | ~/.cargo/registry/index/ 236 | ~/.cargo/registry/cache/ 237 | ~/.cargo/git/db/ 238 | target/ 239 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 240 | - uses: actions-rs/toolchain@v1 241 | with: 242 | toolchain: 1.67.0 243 | override: true 244 | default: true 245 | - run: rustup target add x86_64-pc-windows-gnu 246 | - run: rustup set default-host x86_64-pc-windows-gnu 247 | - run: cargo --version 248 | - run: make loadable static target=x86_64-pc-windows-gnu 249 | env: 250 | RUSTFLAGS: "--print=native-static-libs --print=link-args" 251 | - run: ls dist/debug 252 | - run: ls target/debug 253 | - name: Upload artifacts 254 | uses: actions/upload-artifact@v3 255 | with: 256 | name: sqlite-ulid-windows 257 | path: dist/debug/* 258 | build-windows-python: 259 | runs-on: windows-latest 260 | needs: [build-windows-extension] 261 | steps: 262 | - uses: actions/checkout@v3 263 | - name: Download workflow artifacts 264 | uses: actions/download-artifact@v3 265 | with: 266 | name: sqlite-ulid-windows 267 | path: dist/debug/ 268 | - uses: actions/setup-python@v3 269 | - run: pip install wheel 270 | - run: make python 271 | - run: make datasette 272 | - uses: actions/upload-artifact@v3 273 | with: 274 | name: sqlite-ulid-windows-wheels 275 | path: dist/debug/wheels/*.whl 276 | test-windows: 277 | runs-on: windows-latest 278 | needs: [build-windows-extension, build-windows-python] 279 | env: 280 | DENO_DIR: deno_cache 281 | steps: 282 | - uses: actions/checkout@v3 283 | - uses: actions/download-artifact@v3 284 | with: 285 | name: sqlite-ulid-windows 286 | path: dist/debug/ 287 | - uses: actions/download-artifact@v3 288 | with: 289 | name: sqlite-ulid-windows 290 | path: npm/sqlite-ulid-windows-x64/lib 291 | - uses: actions/download-artifact@v3 292 | with: 293 | name: sqlite-ulid-windows-wheels 294 | path: dist/debug/ 295 | - run: pip install --find-links dist/debug/ sqlite_ulid 296 | - run: make test-loadable 297 | - run: make test-python 298 | # for test-npm 299 | - uses: actions/setup-node@v3 300 | with: 301 | cache: "npm" 302 | cache-dependency-path: npm/sqlite-ulid/package.json 303 | - run: npm install 304 | working-directory: npm/sqlite-ulid 305 | - run: make test-npm 306 | # for test-deno 307 | - uses: denoland/setup-deno@v1 308 | with: 309 | deno-version: v1.30 310 | - name: Cache Deno dependencies 311 | uses: actions/cache@v3 312 | with: 313 | path: ${{ env.DENO_DIR }} 314 | key: ${{ runner.os }}-${{ hashFiles('deno/deno.lock') }} 315 | - run: make test-deno 316 | env: 317 | DENO_SQLITE_ULID_PATH: ${{ github.workspace }}/dist/debug/ulid0 318 | test-macos-c: 319 | runs-on: macos-latest 320 | needs: [build-macos-extension] 321 | steps: 322 | - uses: actions/checkout@v3 323 | - uses: actions/download-artifact@v3 324 | with: 325 | name: sqlite-ulid-macos 326 | path: dist/debug/ 327 | - run: brew install sqlite 328 | - run: make demo 329 | working-directory: examples/c 330 | - run: examples/c/demo 331 | test-ubuntu-c: 332 | runs-on: ubuntu-20.04 333 | needs: [build-ubuntu-extension] 334 | steps: 335 | - uses: actions/checkout@v3 336 | - uses: actions/download-artifact@v3 337 | with: 338 | name: sqlite-ulid-ubuntu 339 | path: dist/debug/ 340 | - run: make demo CFLAGS="-pthread -ldl" 341 | working-directory: examples/c 342 | - run: examples/c/demo 343 | test-windows-c: 344 | runs-on: windows-latest 345 | needs: [build-windows-extension] 346 | steps: 347 | - uses: actions/checkout@v3 348 | - uses: actions/download-artifact@v3 349 | with: 350 | name: sqlite-ulid-windows 351 | path: dist/debug/ 352 | - run: | 353 | mkdir -p ${{ github.workspace }}/sqlite 354 | cd ${{ github.workspace }}/sqlite 355 | curl -LO https://www.sqlite.org/2023/sqlite-amalgamation-3420000.zip 356 | unzip sqlite-amalgamation-3420000.zip 357 | mv sqlite-amalgamation-3420000/* . 358 | curl -LO https://www.sqlite.org/2023/sqlite-tools-linux-x86-3420000.zip 359 | unzip sqlite-tools-linux-x86-3420000.zip 360 | - run: make demo CFLAGS="-I${{ github.workspace }}\sqlite -L${{ github.workspace }}\sqlite" 361 | working-directory: examples/c 362 | - run: examples/c/demo 363 | test-windows-go: 364 | runs-on: windows-latest 365 | needs: [build-windows-extension] 366 | steps: 367 | - uses: actions/checkout@v3 368 | - uses: actions/download-artifact@v3 369 | with: 370 | name: sqlite-ulid-windows 371 | path: dist/debug/ 372 | # sqlite3ext.h aint in windows github action runner, to download it ourself 373 | - run: | 374 | mkdir -p ${{ github.workspace }}/sqlite 375 | cd ${{ github.workspace }}/sqlite 376 | curl -LO https://www.sqlite.org/2023/sqlite-amalgamation-3420000.zip 377 | unzip sqlite-amalgamation-3420000.zip 378 | mv sqlite-amalgamation-3420000/* . 379 | - run: echo "${{ github.workspace }}\dist\debug" >> $GITHUB_PATH 380 | - run: ls ${{ github.workspace }}\dist\debug 381 | - run: ls ${{ github.workspace }}\sqlite 382 | - name: Setup tmate session 383 | uses: mxschmitt/action-tmate@v3 384 | with: 385 | limit-access-to-actor: true 386 | - run: go run demo.go 387 | working-directory: examples/go 388 | env: 389 | CGO_LDFLAGS: -L${{ github.workspace }}\dist\debug 390 | CGO_CFLAGS: -I${{ github.workspace }}\sqlite 391 | CGO_ENABLED: 1 392 | CC: x86_64-w64-mingw32-gcc 393 | -------------------------------------------------------------------------------- /.github/workflows/upload-deno-assets.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs").promises; 2 | 3 | const compiled_extensions = [ 4 | { 5 | path: "sqlite-ulid-macos-arm/ulid0.dylib", 6 | name: "deno-darwin-aarch64.ulid0.dylib", 7 | }, 8 | { 9 | path: "sqlite-ulid-macos/ulid0.dylib", 10 | name: "deno-darwin-x86_64.ulid0.dylib", 11 | }, 12 | { 13 | path: "sqlite-ulid-ubuntu/ulid0.so", 14 | name: "deno-linux-x86_64.ulid0.so", 15 | }, 16 | { 17 | path: "sqlite-ulid-windows/ulid0.dll", 18 | name: "deno-windows-x86_64.ulid0.dll", 19 | }, 20 | ]; 21 | 22 | module.exports = async ({ github, context }) => { 23 | const { owner, repo } = context.repo; 24 | const release = await github.rest.repos.getReleaseByTag({ 25 | owner, 26 | repo, 27 | tag: process.env.GITHUB_REF.replace("refs/tags/", ""), 28 | }); 29 | const release_id = release.data.id; 30 | 31 | await Promise.all( 32 | compiled_extensions.map(async ({ name, path }) => { 33 | return github.rest.repos.uploadReleaseAsset({ 34 | owner, 35 | repo, 36 | release_id, 37 | name, 38 | data: await fs.readFile(path), 39 | }); 40 | }) 41 | ); 42 | }; 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | uuid.c 4 | *.dylib 5 | dist/ 6 | .vscode -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sqlite-ulid" 3 | version = "0.2.2-alpha.1" 4 | edition = "2021" 5 | authors = ["Alex Garcia "] 6 | description = "A SQLite extension for working with ULIDs" 7 | homepage = "https://github.com/asg017/sqlite-ulid" 8 | repository = "https://github.com/asg017/sqlite-ulid" 9 | keywords = ["sqlite", "sqlite-extension"] 10 | license = "MIT/Apache-2.0" 11 | 12 | [dependencies] 13 | sqlite-loadable = "0.0.6-alpha.6" 14 | ulid = "1.0.0" 15 | chrono = "0.4.23" 16 | 17 | [lib] 18 | crate-type=["lib", "staticlib", "cdylib"] 19 | 20 | # temp to try fix windows static builds 21 | [profile.release] 22 | panic = "abort" 23 | 24 | [profile.dev] 25 | panic = "abort" 26 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alex Garcia 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. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | 3 | VERSION=$(shell cat VERSION) 4 | 5 | ifeq ($(shell uname -s),Darwin) 6 | CONFIG_DARWIN=y 7 | else ifeq ($(OS),Windows_NT) 8 | CONFIG_WINDOWS=y 9 | else 10 | CONFIG_LINUX=y 11 | endif 12 | 13 | LIBRARY_PREFIX=lib 14 | ifdef CONFIG_DARWIN 15 | LOADABLE_EXTENSION=dylib 16 | STATIC_EXTENSION=a 17 | endif 18 | 19 | ifdef CONFIG_LINUX 20 | LOADABLE_EXTENSION=so 21 | STATIC_EXTENSION=a 22 | endif 23 | 24 | 25 | ifdef CONFIG_WINDOWS 26 | LOADABLE_EXTENSION=dll 27 | LIBRARY_PREFIX= 28 | STATIC_EXTENSION=a 29 | endif 30 | 31 | prefix=dist 32 | TARGET_LOADABLE=$(prefix)/debug/ulid0.$(LOADABLE_EXTENSION) 33 | TARGET_LOADABLE_RELEASE=$(prefix)/release/ulid0.$(LOADABLE_EXTENSION) 34 | 35 | TARGET_STATIC=$(prefix)/debug/libsqlite_ulid0.$(STATIC_EXTENSION) 36 | TARGET_STATIC_RELEASE=$(prefix)/release/libsqlite_ulid0.$(STATIC_EXTENSION) 37 | 38 | TARGET_H=$(prefix)/debug/sqlite-ulid.h 39 | TARGET_H_RELEASE=$(prefix)/release/sqlite-ulid.h 40 | 41 | TARGET_WHEELS=$(prefix)/debug/wheels 42 | TARGET_WHEELS_RELEASE=$(prefix)/release/wheels 43 | 44 | INTERMEDIATE_PYPACKAGE_EXTENSION=python/sqlite_ulid/sqlite_ulid/ulid0.$(LOADABLE_EXTENSION) 45 | 46 | ifdef target 47 | CARGO_TARGET=--target=$(target) 48 | BUILT_LOCATION=target/$(target)/debug/$(LIBRARY_PREFIX)sqlite_ulid.$(LOADABLE_EXTENSION) 49 | BUILT_LOCATION_RELEASE=target/$(target)/release/$(LIBRARY_PREFIX)sqlite_ulid.$(LOADABLE_EXTENSION) 50 | BUILT_LOCATION_STATIC=target/$(target)/debug/libsqlite_ulid.$(STATIC_EXTENSION) 51 | BUILT_LOCATION_STATIC_RELEASE=target/$(target)/release/libsqlite_ulid.$(STATIC_EXTENSION) 52 | else 53 | CARGO_TARGET= 54 | BUILT_LOCATION=target/debug/$(LIBRARY_PREFIX)sqlite_ulid.$(LOADABLE_EXTENSION) 55 | BUILT_LOCATION_RELEASE=target/release/$(LIBRARY_PREFIX)sqlite_ulid.$(LOADABLE_EXTENSION) 56 | BUILT_LOCATION_STATIC=target/debug/libsqlite_ulid.$(STATIC_EXTENSION) 57 | BUILT_LOCATION_STATIC_RELEASE=target/release/libsqlite_ulid.$(STATIC_EXTENSION) 58 | endif 59 | 60 | ifdef python 61 | PYTHON=$(python) 62 | else 63 | PYTHON=python3 64 | endif 65 | 66 | ifdef IS_MACOS_ARM 67 | RENAME_WHEELS_ARGS=--is-macos-arm 68 | else 69 | RENAME_WHEELS_ARGS= 70 | endif 71 | 72 | $(prefix): 73 | mkdir -p $(prefix)/debug 74 | mkdir -p $(prefix)/release 75 | 76 | $(TARGET_WHEELS): $(prefix) 77 | mkdir -p $(TARGET_WHEELS) 78 | 79 | $(TARGET_WHEELS_RELEASE): $(prefix) 80 | mkdir -p $(TARGET_WHEELS_RELEASE) 81 | 82 | $(TARGET_LOADABLE): $(prefix) $(shell find . -type f -name '*.rs') 83 | cargo build --verbose $(CARGO_TARGET) 84 | cp $(BUILT_LOCATION) $@ 85 | 86 | $(TARGET_LOADABLE_RELEASE): $(prefix) $(shell find . -type f -name '*.rs') 87 | cargo build --verbose --release $(CARGO_TARGET) 88 | cp $(BUILT_LOCATION_RELEASE) $@ 89 | 90 | $(TARGET_STATIC): $(prefix) $(shell find . -type f -name '*.rs') 91 | cargo build --verbose $(CARGO_TARGET) --features=sqlite-loadable/static 92 | ls target 93 | ls target/$(target)/debug 94 | cp $(BUILT_LOCATION_STATIC) $@ 95 | 96 | $(TARGET_STATIC_RELEASE): $(prefix) $(shell find . -type f -name '*.rs') 97 | cargo build --verbose --release $(CARGO_TARGET) --features=sqlite-loadable/static 98 | cp $(BUILT_LOCATION_STATIC_RELEASE) $@ 99 | 100 | $(TARGET_H): sqlite-ulid.h 101 | cp $< $@ 102 | 103 | $(TARGET_H_RELEASE): sqlite-ulid.h 104 | cp $< $@ 105 | 106 | python: $(TARGET_WHEELS) $(TARGET_LOADABLE) python/sqlite_ulid/setup.py python/sqlite_ulid/sqlite_ulid/__init__.py .github/workflows/rename-wheels.py 107 | cp $(TARGET_LOADABLE) $(INTERMEDIATE_PYPACKAGE_EXTENSION) 108 | rm $(TARGET_WHEELS)/sqlite_ulid* || true 109 | pip3 wheel python/sqlite_ulid/ -w $(TARGET_WHEELS) 110 | python3 .github/workflows/rename-wheels.py $(TARGET_WHEELS) $(RENAME_WHEELS_ARGS) 111 | 112 | python-release: $(TARGET_LOADABLE_RELEASE) $(TARGET_WHEELS_RELEASE) python/sqlite_ulid/setup.py python/sqlite_ulid/sqlite_ulid/__init__.py .github/workflows/rename-wheels.py 113 | cp $(TARGET_LOADABLE_RELEASE) $(INTERMEDIATE_PYPACKAGE_EXTENSION) 114 | rm $(TARGET_WHEELS_RELEASE)/sqlite_ulid* || true 115 | pip3 wheel python/sqlite_ulid/ -w $(TARGET_WHEELS_RELEASE) 116 | python3 .github/workflows/rename-wheels.py $(TARGET_WHEELS_RELEASE) $(RENAME_WHEELS_ARGS) 117 | 118 | datasette: $(TARGET_WHEELS) python/datasette_sqlite_ulid/setup.py python/datasette_sqlite_ulid/datasette_sqlite_ulid/__init__.py 119 | rm $(TARGET_WHEELS)/datasette* || true 120 | pip3 wheel python/datasette_sqlite_ulid/ --no-deps -w $(TARGET_WHEELS) 121 | 122 | datasette-release: $(TARGET_WHEELS_RELEASE) python/datasette_sqlite_ulid/setup.py python/datasette_sqlite_ulid/datasette_sqlite_ulid/__init__.py 123 | rm $(TARGET_WHEELS_RELEASE)/datasette* || true 124 | pip3 wheel python/datasette_sqlite_ulid/ --no-deps -w $(TARGET_WHEELS_RELEASE) 125 | 126 | bindings/sqlite-utils/pyproject.toml: bindings/sqlite-utils/pyproject.toml.tmpl VERSION 127 | VERSION=$(VERSION) envsubst < $< > $@ 128 | echo "✅ generated $@" 129 | 130 | bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py: bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py.tmpl VERSION 131 | VERSION=$(VERSION) envsubst < $< > $@ 132 | echo "✅ generated $@" 133 | 134 | sqlite-utils: $(TARGET_WHEELS) bindings/sqlite-utils/pyproject.toml bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py 135 | python3 -m build bindings/sqlite-utils -w -o $(TARGET_WHEELS) 136 | 137 | sqlite-utils-release: $(TARGET_WHEELS) bindings/sqlite-utils/pyproject.toml bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py 138 | python3 -m build bindings/sqlite-utils -w -o $(TARGET_WHEELS_RELEASE) 139 | 140 | npm: VERSION npm/platform-package.README.md.tmpl npm/platform-package.package.json.tmpl npm/sqlite-ulid/package.json.tmpl scripts/npm_generate_platform_packages.sh 141 | scripts/npm_generate_platform_packages.sh 142 | 143 | deno: VERSION deno/deno.json.tmpl 144 | scripts/deno_generate_package.sh 145 | 146 | Cargo.toml: VERSION 147 | cargo set-version `cat VERSION` 148 | 149 | python/sqlite_ulid/sqlite_ulid/version.py: VERSION 150 | printf '__version__ = "%s"\n__version_info__ = tuple(__version__.split("."))\n' `cat VERSION` > $@ 151 | 152 | python/datasette_sqlite_ulid/datasette_sqlite_ulid/version.py: VERSION 153 | printf '__version__ = "%s"\n__version_info__ = tuple(__version__.split("."))\n' `cat VERSION` > $@ 154 | 155 | bindings/ruby/lib/version.rb: bindings/ruby/lib/version.rb.tmpl VERSION 156 | VERSION=$(VERSION) envsubst < $< > $@ 157 | 158 | ruby: bindings/ruby/lib/version.rb 159 | 160 | bindings/go/ulid/sqlite-ulid.h: sqlite-ulid.h 161 | cp $< $@ 162 | 163 | go: bindings/go/ulid/sqlite-ulid.h 164 | 165 | version: 166 | make Cargo.toml 167 | make python/sqlite_ulid/sqlite_ulid/version.py 168 | make python/datasette_sqlite_ulid/datasette_sqlite_ulid/version.py 169 | make bindings/sqlite-utils/pyproject.toml bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py 170 | make npm 171 | make deno 172 | make ruby 173 | 174 | format: 175 | cargo fmt 176 | 177 | release: $(TARGET_LOADABLE_RELEASE) $(TARGET_STATIC_RELEASE) 178 | 179 | loadable: $(TARGET_LOADABLE) 180 | loadable-release: $(TARGET_LOADABLE_RELEASE) 181 | 182 | static: $(TARGET_STATIC) $(TARGET_H) 183 | static-release: $(TARGET_STATIC_RELEASE) $(TARGET_H_RELEASE) 184 | 185 | debug: loadable static python datasette 186 | release: loadable-release static-release python-release datasette-release 187 | 188 | clean: 189 | rm dist/* 190 | cargo clean 191 | 192 | test-loadable: 193 | $(PYTHON) tests/test-loadable.py 194 | 195 | test-python: 196 | $(PYTHON) tests/test-python.py 197 | 198 | test-npm: 199 | node npm/sqlite-ulid/test.js 200 | 201 | test-deno: 202 | deno task --config deno/deno.json test 203 | 204 | test: 205 | make test-loadable 206 | make test-python 207 | make test-npm 208 | make test-deno 209 | 210 | publish-release: 211 | ./scripts/publish_release.sh 212 | 213 | .PHONY: clean \ 214 | test test-loadable test-python test-npm test-deno \ 215 | loadable loadable-release \ 216 | python python-release \ 217 | datasette datasette-release \ 218 | sqlite-utils sqlite-utils-release \ 219 | static static-release \ 220 | debug release \ 221 | format version publish-release \ 222 | npm deno ruby go 223 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sqlite-ulid 2 | 3 | A SQLite extension for generating and working with [ULIDs](https://github.com/ulid/spec). Built on top of [sqlite-loadable-rs](https://github.com/asg017/sqlite-loadable-rs) and [ulid-rs](https://github.com/dylanhart/ulid-rs). 4 | 5 | If your company or organization finds this library useful, consider [supporting my work](#supporting)! 6 | 7 | ## Usage 8 | 9 | ```sql 10 | .load ./ulid0 11 | 12 | select ulid(); -- '01gqr4j69cc7w1xdbarkcbpq17' 13 | select ulid_bytes(); -- X'0185310899dd7662b8f1e5adf9a5e7c0' 14 | select ulid_with_prefix('invoice'); -- 'invoice_01gqr4jmhxhc92x1kqkpxb8j16' 15 | select ulid_with_datetime('2023-01-26 22:53:20.556); -- '01gqr4j69cc7w1xdbarkcbpq17' 16 | select ulid_datetime('01gqr4j69cc7w1xdbarkcbpq17') -- '2023-01-26 22:53:20.556' 17 | ``` 18 | 19 | Use as a `PRIMARY KEY` for a table. 20 | 21 | ```sql 22 | create table log_events( 23 | id ulid primary key, 24 | data any 25 | ); 26 | 27 | 28 | insert into log_events(id, data) values (ulid(), 1); 29 | insert into log_events(id, data) values (ulid(), 2); 30 | insert into log_events(id, data) values (ulid(), 3); 31 | 32 | select * from log_events; 33 | /* 34 | ┌────────────────────────────┬──────┐ 35 | │ id │ data │ 36 | ├────────────────────────────┼──────┤ 37 | │ 01gqr4vr487bytsf10ktfmheg4 │ 1 │ 38 | │ 01gqr4vr4dfcfk80m2yp6j866z │ 2 │ 39 | │ 01gqr4vrjxg0yex9jr0f100v1c │ 3 │ 40 | └────────────────────────────┴──────┘ 41 | */ 42 | ``` 43 | 44 | Consider using [`ulid_bytes()`](./docs.md#ulid_bytes) for speed and smaller IDs. They generate about 1.6x faster than `ulid()`, and take up 16 bytes instead of 26 bytes. You can use `ulid()` to create a text representation of a BLOB ULID. 45 | 46 | ```sql 47 | 48 | create table log_events( 49 | id ulid primary key, 50 | data any 51 | ); 52 | 53 | 54 | insert into log_events(id, data) values (ulid_bytes(), 1); 55 | insert into log_events(id, data) values (ulid_bytes(), 2); 56 | insert into log_events(id, data) values (ulid_bytes(), 3); 57 | 58 | select hex(id), ulid(id), data from log_events; 59 | /* 60 | ┌──────────────────────────────────┬────────────────────────────┬──────┐ 61 | │ hex(id) │ ulid(id) │ data │ 62 | ├──────────────────────────────────┼────────────────────────────┼──────┤ 63 | │ 0185F0539EBF286DA9F56BA4D9981783 │ 01gqr577nz51ptkxbbmkcsg5w3 │ 1 │ 64 | │ 0185F0539EC54F85745C1ECB64DF3A97 │ 01gqr577p59y2q8q0ysdjdyemq │ 2 │ 65 | │ 0185F0539ED48113F6F67BF3F6A4BFF7 │ 01gqr577pmg49zdxkvyfva9fzq │ 3 │ 66 | └──────────────────────────────────┴────────────────────────────┴──────┘ 67 | */ 68 | ``` 69 | 70 | Extract the timestamp component of a ULID with [`ulid_datetime()`](./docs.md#ulid_datetime). 71 | 72 | ```sql 73 | select ulid_datetime(ulid()); -- '2023-01-26 23:07:36.508' 74 | select unixepoch(ulid_datetime(ulid())); -- 1674774499 75 | select strftime('%Y-%m-%d', ulid_datetime(ulid())); -- '2023-01-26'' 76 | ``` 77 | 78 | Consider using [`ulid_with_prefix()`](./docs.md#ulid_with_prefix) to generate a text ULID with a given prefix, to differentiate between different ID types. 79 | 80 | ```sql 81 | select ulid_with_prefix('customer'); -- 'customer_01gqr5j1ebk31wv30wgp8ebehj' 82 | select ulid_with_prefix('product'); -- 'product_01gqr5prjgsa77dhrxf2dt1dgv' 83 | select ulid_with_prefix('order'); -- 'order_01gqr5q35n68jk0sycy1ntr083' 84 | 85 | 86 | ``` 87 | 88 | ## Quick benchmarks 89 | 90 | Not definitive, hastily ran on a Macbook, not representative of real-life usecases. The `uuid()` SQL function comes from the [official `uuid.c` extension](https://sqlite.org/src/file/ext/misc/uuid.c). 91 | 92 | | Test case | Time | 93 | | ---------------------------------------------- | ----------------------------------------------- | 94 | | `generate_series()` to generate 1 million rows | `28.5 ms ± 0.8 ms` (`1x`) | 95 | | Calling `ulid_bytes()` 1 million times | `88.4 ms ± 2.8 ms`,, `3.10 ± 0.13` slower | 96 | | Calling `uuid()` 1 million times | `141.6 ms ± 1.5 ms`, or `4.97 ± 0.15` slower | 97 | | Calling `ulid()` 1 million times | `344.3 ms ± 11.9 ms`, or `12.07 ± 0.53` slower | 98 | 99 | So `ulid_bytes()` is pretty fast, but returns an unreadable blob instead of a nicely formatted text ID. The `ulid()` function does that, but is more than twice as slow than `uuid()`. 100 | 101 | However, generating 1 million `ulid()` IDs in ~350ms is most likely "good enough" for most SQLite usecases. 102 | 103 | ## Using with... 104 | 105 | | Language | Install | | 106 | | -------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 107 | | Python | `pip install sqlite-ulid` | [![PyPI](https://img.shields.io/pypi/v/sqlite-ulid.svg?color=blue&logo=python&logoColor=white)](https://pypi.org/project/sqlite-ulid/) | 108 | | Datasette | `datasette install datasette-sqlite-ulid` | [![Datasette](https://img.shields.io/pypi/v/datasette-sqlite-ulid.svg?color=B6B6D9&label=Datasette+plugin&logoColor=white&logo=python)](https://datasette.io/plugins/datasette-sqlite-ulid) | 109 | | Node.js | `npm install sqlite-ulid` | [![npm](https://img.shields.io/npm/v/sqlite-ulid.svg?color=green&logo=nodedotjs&logoColor=white)](https://www.npmjs.com/package/sqlite-ulid) | 110 | | Deno | [`deno.land/x/sqlite_ulid`](https://deno.land/x/sqlite_ulid) | [![deno.land/x release](https://img.shields.io/github/v/release/asg017/sqlite-ulid?color=fef8d2&include_prereleases&label=deno.land%2Fx&logo=deno)](https://deno.land/x/sqlite_ulid) | 111 | | Ruby | `gem install sqlite-ulid` | ![Gem](https://img.shields.io/gem/v/sqlite-ulid?color=red&logo=rubygems&logoColor=white) | 112 | | Rust | `cargo add sqlite-ulid` | [![Crates.io](https://img.shields.io/crates/v/sqlite-ulid?logo=rust)](https://crates.io/crates/sqlite-ulid) | 113 | | Github Release | | ![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/asg017/sqlite-ulid?color=lightgrey&include_prereleases&label=Github+release&logo=github) | 114 | 115 | 119 | 120 | The [Releases page](https://github.com/asg017/sqlite-ulid/releases) contains pre-built binaries for Linux x86_64, MacOS, and Windows. 121 | 122 | ### Python 123 | 124 | For Python developers, install the [`sqlite-ulid` package](https://pypi.org/package/sqlite-ulid/) with: 125 | 126 | ``` 127 | pip install sqlite-ulid 128 | ``` 129 | 130 | ```python 131 | import sqlite3 132 | import sqlite_ulid 133 | db = sqlite3.connect(':memory:') 134 | db.enable_load_extension(True) 135 | sqlite_ulid.load(db) 136 | db.execute('select ulid()').fetchone() 137 | # ('01gr7gwc5aq22ycea6j8kxq4s9',) 138 | ``` 139 | 140 | See [`python/sqlite_ulid`](./python/sqlite_ulid/README.md) for more details. 141 | 142 | ### Node.js 143 | 144 | For Node.js developers, install the [`sqlite-ulid` npm package](https://www.npmjs.com/package/sqlite-ulid) with: 145 | 146 | ``` 147 | npm install sqlite-ulid 148 | ``` 149 | 150 | ```js 151 | import Database from "better-sqlite3"; 152 | import * as sqlite_ulid from "sqlite-ulid"; 153 | 154 | const db = new Database(":memory:"); 155 | db.loadExtension(sqlite_ulid.getLoadablePath()); 156 | ``` 157 | 158 | See [`npm/sqlite-ulid/README.md`](./npm/sqlite-ulid/README.md) for more details. 159 | 160 | ### Deno 161 | 162 | For [Deno](https://deno.land/) developers, use the [deno.land/x/sqlite_ulid](https://deno.land/x/sqlite_ulid) module: 163 | 164 | ```ts 165 | import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; 166 | import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid@v${VERSION}/mod.ts"; 167 | 168 | const db = new Database(":memory:"); 169 | 170 | db.enableLoadExtension = true; 171 | sqlite_ulid.load(db); 172 | 173 | const [version] = db.prepare("select ulid_version()").value<[string]>()!; 174 | 175 | console.log(version); 176 | ``` 177 | 178 | ### Datasette 179 | 180 | And for [Datasette](https://datasette.io/), install the [`datasette-sqlite-ulid` plugin](https://datasette.io/plugins/datasette-sqlite-ulid) with: 181 | 182 | ``` 183 | datasette install datasette-sqlite-ulid 184 | ``` 185 | 186 | See [`python/datasette_sqlite_ulid`](./python/datasette_sqlite_ulid/README.md) for more details. 187 | 188 | ### As a loadable extension 189 | 190 | If you want to use `sqlite-ulid` as a [Runtime-loadable extension](https://www.sqlite.org/loadext.html), Download the `ulid0.dylib` (for MacOS), `ulid0.so` (Linux), or `ulid0.dll` (Windows) file [from a release](https://github.com/asg017/sqlite-ulid/releases) and load it into your SQLite environment. 191 | 192 | > **Note:** 193 | > The `0` in the filename (`ulid0.dylib`/ `ulid0.so`/`ulid0.dll`) denotes the major version of `sqlite-ulid`. Currently `sqlite-ulid` is pre v1, so expect breaking changes in future versions. 194 | 195 | For example, if you are using the [SQLite CLI](https://www.sqlite.org/cli.html), you can load the library like so: 196 | 197 | ```sql 198 | .load ./ulid0 199 | select ulid_version(); 200 | -- v0.1.0 201 | ``` 202 | 203 | In Python, you should prefer the [`sqlite-ulid` Python package](./python/sqlite_ulid/README.md). However, you can manually load a pre-compiled extension with the builtin [sqlite3 module](https://docs.python.org/3/library/sqlite3.html): 204 | 205 | ```python 206 | import sqlite3 207 | con = sqlite3.connect(":memory:") 208 | con.enable_load_extension(True) 209 | con.load_extension("./ulid0") 210 | print(con.execute("select ulid_version()").fetchone()) 211 | # ('v0.1.0',) 212 | ``` 213 | 214 | Or in Node.js using [better-sqlite3](https://github.com/WiseLibs/better-sqlite3): 215 | 216 | ```javascript 217 | const Database = require("better-sqlite3"); 218 | const db = new Database(":memory:"); 219 | db.loadExtension("./ulid0"); 220 | console.log(db.prepare("select ulid_version()").get()); 221 | // { 'ulid_version()': 'v0.1.0' } 222 | ``` 223 | 224 | With [Datasette](https://datasette.io/), you should prefer the [`datasette-sqlite-ulid` Datasette plugin](./python/datasette_sqlite_ulid/README.md). However, you can manually load a pre-compiled extension into a Datasette instance like so: 225 | 226 | ``` 227 | datasette data.db --load-extension ./ulid0 228 | ``` 229 | 230 | ## Supporting 231 | 232 | I (Alex 👋🏼) spent a lot of time and energy on this project and [many other open source projects](https://github.com/asg017?tab=repositories&q=&type=&language=&sort=stargazers). If your company or organization uses this library (or you're feeling generous), then please [consider supporting my work](https://alexgarcia.ulid/work.html), or share this project with a friend! 233 | 234 | ## See also 235 | 236 | - [sqlite-xsv](https://github.com/asg017/sqlite-xsv), A SQLite extension for working with CSVs 237 | - [sqlite-loadable](https://github.com/asg017/sqlite-loadable-rs), A framework for writing SQLite extensions in Rust 238 | - [sqlite-http](https://github.com/asg017/sqlite-http), A SQLite extension for making HTTP requests 239 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.2.2-alpha.1 -------------------------------------------------------------------------------- /bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | hyperfine --warmup=10 \ 3 | 'sqlite3x :memory: "select count(value) from generate_series(1, 1e6);"' \ 4 | 'sqlite3x :memory: ".load target/release/libulid0" "select count(ulid_bytes()) from generate_series(1, 1e6);"' \ 5 | 'sqlite3x :memory: ".load ./uuid" "select count(uuid()) from generate_series(1, 1e6);"' \ 6 | 'sqlite3x :memory: ".load target/release/libulid0" "select count(ulid()) from generate_series(1, 1e6);"' 7 | -------------------------------------------------------------------------------- /bench.sql: -------------------------------------------------------------------------------- 1 | .load target/release/libulid0 2 | .load ./uuid 3 | 4 | select count(value) from generate_series(1, 1e6); 5 | select count(ulid()) from generate_series(1, 1e6); 6 | select count(ulid_bytes()) from generate_series(1, 1e6); 7 | select count(uuid()) from generate_series(1, 1e6); 8 | 9 | 10 | select uuid(), ulid(), ulid_bytes(); -------------------------------------------------------------------------------- /bindings/go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/asg017/sqlite-ulid/bindings/go 2 | 3 | go 1.20 4 | -------------------------------------------------------------------------------- /bindings/go/lib.go: -------------------------------------------------------------------------------- 1 | // asdflakdjf 2 | package ulid 3 | 4 | import ( 5 | ulid "github.com/asg017/sqlite-ulid/bindings/go/ulid" 6 | ) 7 | 8 | // please?? 9 | func init() { 10 | ulid.Auto() 11 | } 12 | -------------------------------------------------------------------------------- /bindings/go/ulid/sqlite-ulid.h: -------------------------------------------------------------------------------- 1 | #ifndef _SQLITE_ulid_H 2 | #define _SQLITE_ulid_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int sqlite3_ulid_init(sqlite3*, char**, const sqlite3_api_routines*); 9 | 10 | #ifdef __cplusplus 11 | } /* end of the 'extern "C"' block */ 12 | #endif 13 | 14 | #endif /* ifndef _SQLITE_ulid_H */ 15 | -------------------------------------------------------------------------------- /bindings/go/ulid/ulid.go: -------------------------------------------------------------------------------- 1 | // yoyo 2 | package ulid 3 | 4 | // #cgo LDFLAGS: -lsqlite_ulid0 5 | // #cgo CFLAGS: -DSQLITE_CORE 6 | // #include 7 | // #include "sqlite-ulid.h" 8 | // 9 | import "C" 10 | 11 | // Once called, every future new SQLite3 connection created in this process 12 | // will have the ulid extension loaded. It will persist until [Cancel] is 13 | // called. 14 | // 15 | // Calls [sqlite3_auto_extension()] under the hood. 16 | // 17 | // [sqlite3_auto_extension()]: https://www.sqlite.org/c3ref/auto_extension.html 18 | func Auto() { 19 | C.sqlite3_auto_extension( (*[0]byte) ((C.sqlite3_ulid_init)) ); 20 | } 21 | 22 | // "Cancels" any previous calls to [Auto]. Any new SQLite3 connections created 23 | // will not have the ulid extension loaded. 24 | // 25 | // Calls sqlite3_cancel_auto_extension() under the hood. 26 | func Cancel() { 27 | C.sqlite3_cancel_auto_extension( (*[0]byte) (C.sqlite3_ulid_init) ); 28 | } 29 | -------------------------------------------------------------------------------- /bindings/ruby/.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | -------------------------------------------------------------------------------- /bindings/ruby/Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 4 | 5 | gemspec 6 | -------------------------------------------------------------------------------- /bindings/ruby/Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | task :default => :spec 3 | -------------------------------------------------------------------------------- /bindings/ruby/lib/sqlite_ulid.rb: -------------------------------------------------------------------------------- 1 | require "version" 2 | 3 | module SqliteUlid 4 | class Error < StandardError; end 5 | def self.ulid_loadable_path 6 | File.expand_path('../ulid0', __FILE__) 7 | end 8 | def self.load(db) 9 | db.load_extension(self.ulid_loadable_path) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /bindings/ruby/lib/version.rb: -------------------------------------------------------------------------------- 1 | # automatically generated, do not edit by hand. 2 | module SqliteUlid 3 | VERSION = "0.2.2-alpha.1" 4 | end 5 | -------------------------------------------------------------------------------- /bindings/ruby/lib/version.rb.tmpl: -------------------------------------------------------------------------------- 1 | # automatically generated, do not edit by hand. 2 | module SqliteUlid 3 | VERSION = "${VERSION}" 4 | end 5 | -------------------------------------------------------------------------------- /bindings/ruby/sqlite_ulid.gemspec: -------------------------------------------------------------------------------- 1 | 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "version" 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "sqlite-ulid" 8 | spec.version = SqliteUlid::VERSION 9 | spec.authors = ["Alex Garcia"] 10 | spec.email = ["alexsebastian.garcia@gmail.com"] 11 | 12 | spec.summary = "a" 13 | spec.description = "b" 14 | spec.homepage = "https://github.com/asg017/sqlite-ulid" 15 | spec.license = "MIT" 16 | 17 | # The --platform flag would work in most cases, but on a GH action 18 | # linux runner, it would set platform to "ruby" and not "x86-linux". 19 | # Setting this to Gem::Platform::CURRENT 20 | spec.platform = ENV['PLATFORM'] 21 | 22 | if spec.respond_to?(:metadata) 23 | 24 | spec.metadata["homepage_uri"] = spec.homepage 25 | spec.metadata["source_code_uri"] = spec.homepage 26 | spec.metadata["changelog_uri"] = spec.homepage 27 | else 28 | raise "RubyGems 2.0 or newer is required to protect against " \ 29 | "public gem pushes." 30 | end 31 | 32 | spec.files = Dir["lib/*.rb"] + Dir.glob('lib/*.{so,dylib,dll}') 33 | 34 | spec.require_paths = ["lib"] 35 | 36 | spec.add_development_dependency "bundler", "~> 1.17" 37 | spec.add_development_dependency "rake", "~> 10.0" 38 | end 39 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/README.md: -------------------------------------------------------------------------------- 1 | # `sqlite-utils-sqlite-ulid` 2 | 3 | A `sqlite-utils` plugin that registers the `sqlite-ulid` extension. 4 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/pyproject.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "sqlite-utils-sqlite-ulid" 3 | version = "0.2.2-alpha.1" 4 | description = "TODO" 5 | readme = "README.md" 6 | authors = [{name = "Alex Garcia"}] 7 | license = {text = "Apache-2.0"} 8 | classifiers = [] 9 | 10 | dependencies = [ 11 | "sqlite-utils", 12 | "sqlite-ulid" 13 | ] 14 | 15 | [project.urls] 16 | Homepage = "https://github.com/asg017/sqlite-ulid" 17 | Changelog = "https://github.com/asg017/sqlite-ulid/releases" 18 | Issues = "https://github.com/asg017/sqlite-ulid/issues" 19 | 20 | [project.entry-points.sqlite_utils] 21 | sqlite_ulid = "sqlite_utils_sqlite_ulid" 22 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/pyproject.toml.tmpl: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "sqlite-utils-sqlite-ulid" 3 | version = "${VERSION}" 4 | description = "TODO" 5 | readme = "README.md" 6 | authors = [{name = "Alex Garcia"}] 7 | license = {text = "Apache-2.0"} 8 | classifiers = [] 9 | 10 | dependencies = [ 11 | "sqlite-utils", 12 | "sqlite-ulid" 13 | ] 14 | 15 | [project.urls] 16 | Homepage = "https://github.com/asg017/sqlite-ulid" 17 | Changelog = "https://github.com/asg017/sqlite-ulid/releases" 18 | Issues = "https://github.com/asg017/sqlite-ulid/issues" 19 | 20 | [project.entry-points.sqlite_utils] 21 | sqlite_ulid = "sqlite_utils_sqlite_ulid" 22 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/sqlite_utils_sqlite_ulid/__init__.py: -------------------------------------------------------------------------------- 1 | from sqlite_utils import hookimpl 2 | import sqlite_ulid 3 | 4 | from sqlite_utils_sqlite_ulid.version import __version_info__, __version__ 5 | 6 | 7 | @hookimpl 8 | def prepare_connection(conn): 9 | conn.enable_load_extension(True) 10 | sqlite_ulid.load(conn) 11 | conn.enable_load_extension(False) 12 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.2.2-alpha.1" 2 | __version_info__ = tuple(__version__.split(".")) 3 | -------------------------------------------------------------------------------- /bindings/sqlite-utils/sqlite_utils_sqlite_ulid/version.py.tmpl: -------------------------------------------------------------------------------- 1 | __version__ = "${VERSION}" 2 | __version_info__ = tuple(__version__.split(".")) 3 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | use std::process::Command; 2 | fn main() { 3 | let output = Command::new("git") 4 | .args(["rev-parse", "HEAD"]) 5 | .output() 6 | .unwrap(); 7 | let git_hash = String::from_utf8(output.stdout).unwrap(); 8 | println!("cargo:rustc-env=GIT_HASH={}", git_hash); 9 | } 10 | -------------------------------------------------------------------------------- /deno/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # `x/sqlite_ulid` Deno Module 4 | 5 | [![Tags](https://img.shields.io/github/release/asg017/sqlite-ulid)](https://github.com/asg017/sqlite-ulid/releases) 6 | [![Doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/sqlite-ulid@0.2.2-alpha.1/mod.ts) 7 | 8 | The [`sqlite-ulid`](https://github.com/asg017/sqlite-ulid) SQLite extension is available to Deno developers with the [`x/sqlite_ulid`](https://deno.land/x/sqlite_ulid) Deno module. It works with [`x/sqlite3`](https://deno.land/x/sqlite3), the fastest and native Deno SQLite3 module. 9 | 10 | ```js 11 | import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; 12 | import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid@v0.2.2-alpha.1/mod.ts"; 13 | 14 | const db = new Database(":memory:"); 15 | 16 | db.enableLoadExtension = true; 17 | sqlite_ulid.load(db); 18 | 19 | const [version] = db 20 | .prepare("select ulid_version()") 21 | .value<[string]>()!; 22 | 23 | console.log(version); 24 | 25 | ``` 26 | 27 | Like `x/sqlite3`, `x/sqlite_ulid` requires network and filesystem permissions to download and cache the pre-compiled SQLite extension for your machine. Though `x/sqlite3` already requires `--allow-ffi` and `--unstable`, so you might as well use `--allow-all`/`-A`. 28 | 29 | ```bash 30 | deno run -A --unstable 31 | ``` 32 | 33 | `x/sqlite_ulid` does not work with [`x/sqlite`](https://deno.land/x/sqlite@v3.7.0), which is a WASM-based Deno SQLite module that does not support loading extensions. 34 | -------------------------------------------------------------------------------- /deno/README.md.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | # `x/sqlite_ulid` Deno Module 4 | 5 | [![Tags](https://img.shields.io/github/release/asg017/sqlite-ulid)](https://github.com/asg017/sqlite-ulid/releases) 6 | [![Doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/sqlite-ulid@${VERSION}/mod.ts) 7 | 8 | The [`sqlite-ulid`](https://github.com/asg017/sqlite-ulid) SQLite extension is available to Deno developers with the [`x/sqlite_ulid`](https://deno.land/x/sqlite_ulid) Deno module. It works with [`x/sqlite3`](https://deno.land/x/sqlite3), the fastest and native Deno SQLite3 module. 9 | 10 | ```js 11 | import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; 12 | import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid@v${VERSION}/mod.ts"; 13 | 14 | const db = new Database(":memory:"); 15 | 16 | db.enableLoadExtension = true; 17 | sqlite_ulid.load(db); 18 | 19 | const [version] = db 20 | .prepare("select ulid_version()") 21 | .value<[string]>()!; 22 | 23 | console.log(version); 24 | 25 | ``` 26 | 27 | Like `x/sqlite3`, `x/sqlite_ulid` requires network and filesystem permissions to download and cache the pre-compiled SQLite extension for your machine. Though `x/sqlite3` already requires `--allow-ffi` and `--unstable`, so you might as well use `--allow-all`/`-A`. 28 | 29 | ```bash 30 | deno run -A --unstable 31 | ``` 32 | 33 | `x/sqlite_ulid` does not work with [`x/sqlite`](https://deno.land/x/sqlite@v3.7.0), which is a WASM-based Deno SQLite module that does not support loading extensions. 34 | -------------------------------------------------------------------------------- /deno/deno.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqlite-ulid", 3 | "version": "0.2.2-alpha.1", 4 | "github": "https://github.com/asg017/sqlite-ulid", 5 | "tasks": { 6 | "test": "deno test --unstable -A test.ts" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /deno/deno.json.tmpl: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqlite-ulid", 3 | "version": "${VERSION}", 4 | "github": "https://github.com/asg017/${PACKAGE_NAME}", 5 | "tasks": { 6 | "test": "deno test --unstable -A test.ts" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /deno/deno.lock: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2", 3 | "remote": { 4 | "https://deno.land/std@0.152.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", 5 | "https://deno.land/std@0.152.0/_util/os.ts": "3b4c6e27febd119d36a416d7a97bd3b0251b77c88942c8f16ee5953ea13e2e49", 6 | "https://deno.land/std@0.152.0/path/_constants.ts": "df1db3ffa6dd6d1252cc9617e5d72165cd2483df90e93833e13580687b6083c3", 7 | "https://deno.land/std@0.152.0/path/_interface.ts": "ee3b431a336b80cf445441109d089b70d87d5e248f4f90ff906820889ecf8d09", 8 | "https://deno.land/std@0.152.0/path/_util.ts": "c1e9686d0164e29f7d880b2158971d805b6e0efc3110d0b3e24e4b8af2190d2b", 9 | "https://deno.land/std@0.152.0/path/common.ts": "bee563630abd2d97f99d83c96c2fa0cca7cee103e8cb4e7699ec4d5db7bd2633", 10 | "https://deno.land/std@0.152.0/path/glob.ts": "cb5255638de1048973c3e69e420c77dc04f75755524cb3b2e160fe9277d939ee", 11 | "https://deno.land/std@0.152.0/path/mod.ts": "56fec03ad0ebd61b6ab39ddb9b0ddb4c4a5c9f2f4f632e09dd37ec9ebfd722ac", 12 | "https://deno.land/std@0.152.0/path/posix.ts": "c1f7afe274290ea0b51da07ee205653b2964bd74909a82deb07b69a6cc383aaa", 13 | "https://deno.land/std@0.152.0/path/separator.ts": "fe1816cb765a8068afb3e8f13ad272351c85cbc739af56dacfc7d93d710fe0f9", 14 | "https://deno.land/std@0.152.0/path/win32.ts": "bd7549042e37879c68ff2f8576a25950abbfca1d696d41d82c7bca0b7e6f452c", 15 | "https://deno.land/std@0.176.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", 16 | "https://deno.land/std@0.176.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", 17 | "https://deno.land/std@0.176.0/encoding/hex.ts": "50f8c95b52eae24395d3dfcb5ec1ced37c5fe7610ef6fffdcc8b0fdc38e3b32f", 18 | "https://deno.land/std@0.176.0/fmt/colors.ts": "938c5d44d889fb82eff6c358bea8baa7e85950a16c9f6dae3ec3a7a729164471", 19 | "https://deno.land/std@0.176.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", 20 | "https://deno.land/std@0.176.0/fs/copy.ts": "14214efd94fc3aa6db1e4af2b4b9578e50f7362b7f3725d5a14ad259a5df26c8", 21 | "https://deno.land/std@0.176.0/fs/empty_dir.ts": "c3d2da4c7352fab1cf144a1ecfef58090769e8af633678e0f3fabaef98594688", 22 | "https://deno.land/std@0.176.0/fs/ensure_dir.ts": "724209875497a6b4628dfb256116e5651c4f7816741368d6c44aab2531a1e603", 23 | "https://deno.land/std@0.176.0/fs/ensure_file.ts": "c38602670bfaf259d86ca824a94e6cb9e5eb73757fefa4ebf43a90dd017d53d9", 24 | "https://deno.land/std@0.176.0/fs/ensure_link.ts": "c0f5b2f0ec094ed52b9128eccb1ee23362a617457aa0f699b145d4883f5b2fb4", 25 | "https://deno.land/std@0.176.0/fs/ensure_symlink.ts": "2955cc8332aeca9bdfefd05d8d3976b94e282b0f353392a71684808ed2ffdd41", 26 | "https://deno.land/std@0.176.0/fs/eol.ts": "f1f2eb348a750c34500741987b21d65607f352cf7205f48f4319d417fff42842", 27 | "https://deno.land/std@0.176.0/fs/exists.ts": "b8c8a457b71e9d7f29b9d2f87aad8dba2739cbe637e8926d6ba6e92567875f8e", 28 | "https://deno.land/std@0.176.0/fs/expand_glob.ts": "45d17e89796a24bd6002e4354eda67b4301bb8ba67d2cac8453cdabccf1d9ab0", 29 | "https://deno.land/std@0.176.0/fs/mod.ts": "bc3d0acd488cc7b42627044caf47d72019846d459279544e1934418955ba4898", 30 | "https://deno.land/std@0.176.0/fs/move.ts": "4cb47f880e3f0582c55e71c9f8b1e5e8cfaacb5e84f7390781dd563b7298ec19", 31 | "https://deno.land/std@0.176.0/fs/walk.ts": "ea95ffa6500c1eda6b365be488c056edc7c883a1db41ef46ec3bf057b1c0fe32", 32 | "https://deno.land/std@0.176.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", 33 | "https://deno.land/std@0.176.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", 34 | "https://deno.land/std@0.176.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0", 35 | "https://deno.land/std@0.176.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", 36 | "https://deno.land/std@0.176.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", 37 | "https://deno.land/std@0.176.0/path/mod.ts": "4b83694ac500d7d31b0cdafc927080a53dc0c3027eb2895790fb155082b0d232", 38 | "https://deno.land/std@0.176.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d", 39 | "https://deno.land/std@0.176.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", 40 | "https://deno.land/std@0.176.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba", 41 | "https://deno.land/std@0.177.0/fmt/colors.ts": "938c5d44d889fb82eff6c358bea8baa7e85950a16c9f6dae3ec3a7a729164471", 42 | "https://deno.land/std@0.177.0/testing/_diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea", 43 | "https://deno.land/std@0.177.0/testing/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", 44 | "https://deno.land/std@0.177.0/testing/asserts.ts": "984ab0bfb3faeed92ffaa3a6b06536c66811185328c5dd146257c702c41b01ab", 45 | "https://deno.land/x/plug@1.0.1/deps.ts": "35ea2acd5e3e11846817a429b7ef4bec47b80f2d988f5d63797147134cbd35c2", 46 | "https://deno.land/x/plug@1.0.1/download.ts": "8d6a023ade0806a0653b48cd5f6f8b15fcfaa1dbf2aa1f4bc90fc5732d27b144", 47 | "https://deno.land/x/plug@1.0.1/mod.ts": "5dec80ee7a3a325be45c03439558531bce7707ac118f4376cebbd6740ff24bfb", 48 | "https://deno.land/x/plug@1.0.1/types.ts": "d8eb738fc6ed883e6abf77093442c2f0b71af9090f15c7613621d4039e410ee1", 49 | "https://deno.land/x/plug@1.0.1/util.ts": "5ba8127b9adc36e070b9e22971fb8106869eea1741f452a87b4861e574f13481", 50 | "https://deno.land/x/sqlite3@0.8.0/deno.json": "61fbd0665a1b48f5e0f1773371d49b776f559cd6c3747a4e674adc3eb423686c", 51 | "https://deno.land/x/sqlite3@0.8.0/deps.ts": "722c865b9cef27b4cde0bb1ac9ebb08e94c43ad090a7313cea576658ff1e3bb0", 52 | "https://deno.land/x/sqlite3@0.8.0/mod.ts": "d41b8b30e1b20b537ef4d78cae98d90f6bd65c727b64aa1a18bffbb28f7d6ec3", 53 | "https://deno.land/x/sqlite3@0.8.0/src/blob.ts": "a956fc0cf4a8c7a21dc3fcb71a07ef773bcd08b5fd72e8ace89b1bfbd031bf06", 54 | "https://deno.land/x/sqlite3@0.8.0/src/constants.ts": "85fd27aa6e199093f25f5f437052e16fd0e0870b96ca9b24a98e04ddc8b7d006", 55 | "https://deno.land/x/sqlite3@0.8.0/src/database.ts": "c68c7fdfa7548000ea7e194360cdce86b81b667aab6b0778ea7ed9b74a37b7cb", 56 | "https://deno.land/x/sqlite3@0.8.0/src/ffi.ts": "d5d5e3b4524cf0b980d23e57b08f75dc0debc7af2e93ff8b00cdc5af52637b31", 57 | "https://deno.land/x/sqlite3@0.8.0/src/statement.ts": "0b2b3c8b5564ad3c35f2c7d57607e5755c38b23b835e9a46aa1002e68e6ec3a2", 58 | "https://deno.land/x/sqlite3@0.8.0/src/util.ts": "9627ebecc7a5eb250d2df9386a456a9a9ed7842a20fe32be1ee6b7c663c77bd3" 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /deno/mod.ts: -------------------------------------------------------------------------------- 1 | import { download } from "https://deno.land/x/plug@1.0.1/mod.ts"; 2 | import meta from "./deno.json" assert { type: "json" }; 3 | 4 | const BASE = `${meta.github}/releases/download/v${meta.version}`; 5 | 6 | // Similar to https://github.com/denodrivers/sqlite3/blob/f7529897720631c2341b713f0d78d4d668593ea9/src/ffi.ts#L561 7 | let path: string; 8 | try { 9 | const customPath = Deno.env.get("DENO_SQLITE_ULID_PATH"); 10 | if (customPath) path = customPath; 11 | else { 12 | path = await download({ 13 | url: { 14 | darwin: { 15 | aarch64: `${BASE}/deno-darwin-aarch64.ulid0.dylib`, 16 | x86_64: `${BASE}/deno-darwin-x86_64.ulid0.dylib`, 17 | }, 18 | windows: { 19 | x86_64: `${BASE}/deno-windows-x86_64.ulid0.dll`, 20 | }, 21 | linux: { 22 | x86_64: `${BASE}/deno-linux-x86_64.ulid0.so`, 23 | }, 24 | }, 25 | suffixes: { 26 | darwin: "", 27 | linux: "", 28 | windows: "", 29 | }, 30 | }); 31 | } 32 | } catch (e) { 33 | if (e instanceof Deno.errors.PermissionDenied) { 34 | throw e; 35 | } 36 | 37 | const error = new Error("Failed to load sqlite-ulid extension"); 38 | error.cause = e; 39 | 40 | throw error; 41 | } 42 | 43 | /** 44 | * Returns the full path to the compiled sqlite-ulid extension. 45 | * Caution: this will not be named "ulid0.dylib|so|dll", since plug will 46 | * replace the name with a hash. 47 | */ 48 | export function getLoadablePath(): string { 49 | return path; 50 | } 51 | 52 | /** 53 | * Entrypoint name for the sqlite-ulid extension. 54 | */ 55 | export const entrypoint = "sqlite3_ulid_init"; 56 | 57 | interface Db { 58 | // after https://deno.land/x/sqlite3@0.8.0/mod.ts?s=Database#method_loadExtension_0 59 | loadExtension(file: string, entrypoint?: string | undefined): void; 60 | } 61 | /** 62 | * Loads the sqlite-ulid extension on the given sqlite3 database. 63 | */ 64 | export function load(db: Db): void { 65 | db.loadExtension(path, entrypoint); 66 | } 67 | -------------------------------------------------------------------------------- /deno/test.ts: -------------------------------------------------------------------------------- 1 | import * as sqlite_ulid from "./mod.ts"; 2 | import meta from "./deno.json" assert { type: "json" }; 3 | 4 | import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts"; 5 | import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; 6 | 7 | Deno.test("x/sqlite3", (t) => { 8 | const db = new Database(":memory:"); 9 | 10 | db.enableLoadExtension = true; 11 | sqlite_ulid.load(db); 12 | 13 | const [version] = db.prepare("select ulid_version()").value<[string]>()!; 14 | 15 | assertEquals(version[0], "v"); 16 | assertEquals(version.substring(1), meta.version); 17 | 18 | db.close(); 19 | }); 20 | -------------------------------------------------------------------------------- /docs.md: -------------------------------------------------------------------------------- 1 | # sqlite-ulid Documentation 2 | 3 | A full reference to every function and module that sqlite-ulid offers. 4 | 5 | As a reminder, sqlite-ulid follows semver and is pre v1, so breaking changes are to be expected. 6 | 7 | ## API Reference 8 | 9 |

ulid([id])

10 | 11 | When no arguments are given, then `ulid()` generates a new lower-cased 26-character ULID string. 12 | 13 | ```sql 14 | select ulid(); -- '01gqrdes0hyscqpxf1yjm6m3r9' 15 | select ulid(); -- '01gqrdeseemx2mwnfg932phz1k' 16 | select ulid(); -- '01gqrdessdnxd9pf2p2vp3jfpp' 17 | ``` 18 | 19 | If the `id` argument is given, it is assumed that it's a BLOB return from the [`ulid_bytes()`](#ulid_bytes) function. Then it'll return the TEXT representation of that BLOB, which is typically more human-readable at the expense of slightly more storage (26 bytes vs 10). 20 | 21 | ```sql 22 | select ulid(ulid_bytes()); -- '01gqrdr8z1z1fadk6c11zkk7cw' 23 | select ulid(X'0185f0dc8ec7613cfa43034128935a60'); -- '01gqrds3p7c4yfmgr384m96pk0' 24 | ``` 25 | 26 | Keep in mind, while `sqlite-ulid` uses lowercase ULIDs, technically ULIDs are case-insensitive. If you're comparing or sorting text ULIDs, consider using the `nocase` collation for accurate results. 27 | 28 | ```sql 29 | select '01GMP2G8ZG6PMKWYVKS62TTA41' == '01gmp2g8zg6pmkwyvks62tta41'; -- 0 30 | select '01GMP2G8ZG6PMKWYVKS62TTA41' == '01gmp2g8zg6pmkwyvks62tta41' collate nocase; -- 1 31 | ``` 32 | 33 |

ulid_bytes([id])

34 | 35 | If no arguments are given, then `ulid_bytes()` generates a new blob ULID. This is slightly faster to generate than a string ULID, and more compact than the [`ulid()`](#ulid) counterpart. 36 | 37 | ```sql 38 | select ulid_bytes(); -- X'0185d0c5362761312e0483e4c9e3ec5d' 39 | select ulid_bytes(); -- X'0185d0c538679350f9262bf16fd575f7' 40 | ``` 41 | 42 | If the `id` argument is given, it's assumed to be an text ULID, in which `ulid_bytes()` will generate the BLOB representation of that ULID. 43 | 44 | ```sql 45 | select ulid_bytes('01gqrdes0hyscqpxf1yjm6m3r9'); -- X'0185f0d76411f6597b75e1f4a86a0f09' 46 | select ulid_bytes('01gqrdeseemx2mwnfg932phz1k'); -- X'0185f0d765cea7454e55f048c568fc33' 47 | ``` 48 | 49 | Note: the `id` argument cannot be a ULID generated with [`ulid_with_prefix()`](#ulid_with_prefix), as there's no straightforward way to make a binary representation of that. 50 | 51 |

ulid_with_prefix(prefix)

52 | 53 | Generates a new ULID string with the given `prefix`, with an underscore between. A nice utility to have when you want to discern the difference between multiple ULIDs of different items, like [Stripe's many ID types](https://gist.github.com/fnky/76f533366f75cf75802c8052b577e2a5). 54 | 55 | ```sql 56 | select ulid_with_prefix('invoice'); -- 'invoice_01gqre8x2efy50mz28b238dh2a' 57 | select ulid_with_prefix('invoice'); -- 'invoice_01gqre8xc36045w1qekg404g0m' 58 | 59 | select ulid_with_prefix('cus'); -- 'cus_01gqre97xwey5jd694tkg3ncg9' 60 | select ulid_with_prefix('card'); -- 'card_01gqre9c6az7835tdk00wc0gfp' 61 | ``` 62 | 63 |

ulid_with_datetime(datetime)

64 | 65 | Generates a new ULID string based on the `datetime`. This can be helpful for giving ULIDs to elements that already have a created time property. The input `datetime` should be in the `YYYY-MM-DD HH:MM:SS.SSS` format. 66 | 67 | ```sql 68 | select ulid_datetime('2023-01-20 20:00:00.400'); -- '01GQ8C8FWG0W1B5H3W5304049S' 69 | ``` 70 | 71 |

ulid_datetime(ulid)

72 | 73 | Extract the timestamp component from the given ULID. The `ulid` parameter can be a string from the [`ulid()`](#ulid) function, a string from the [`ulid_with_prefix()`](#ulid_with_prefix) function, or a blob from the [`ulid_bytes`](#ulid_bytes) function. Returns a text timestamp with `YYYY-MM-DD HH:MM:SS.SSS` format, which can be used with the builtin [`datetime()`](https://www.sqlite.org/lang_datefunc.html) function. 74 | 75 | ```sql 76 | select ulid_datetime('01GQ8C8FWG0W1B5H3W5304049S'); -- '2023-01-20 20:00:00.400' 77 | select ulid_datetime(X'0185d0c5362761312e0483e4c9e3ec5d'); -- '2023-01-20 20:01:03.527' 78 | select ulid_datetime('invoice_01gqre8x2efy50mz28b238dh2a'); -- '2023-01-27 01:43:01.966' 79 | ``` 80 | 81 |

ulid_version()

82 | 83 | Returns the semver version string of the current version of `sqlite-ulid`. 84 | 85 | ```sql 86 | select ulid_version(); -- 'v0.1.0' 87 | ``` 88 | 89 |

ulid_debug()

90 | 91 | Returns a debug string of various info about `sqlite-ulid`, including 92 | the version string, build date, and commit hash. 93 | 94 | ```sql 95 | select ulid_debug(); 96 | 'Version: v0.1.0 97 | Source: 247dca8f4cea1abdc30ed3e852c3e5b71374c177' 98 | ``` 99 | -------------------------------------------------------------------------------- /examples/c/.gitignore: -------------------------------------------------------------------------------- 1 | demo 2 | -------------------------------------------------------------------------------- /examples/c/Makefile: -------------------------------------------------------------------------------- 1 | # -L/usr/local/opt/sqlite/lib 2 | demo: demo.c 3 | gcc $< \ 4 | -Os \ 5 | $(CFLAGS) \ 6 | -I../../dist/debug \ 7 | -L../../dist/debug \ 8 | -Wl,-undefined,dynamic_lookup \ 9 | -lsqlite3 -lsqlite_ulid0 \ 10 | -o $@ 11 | -------------------------------------------------------------------------------- /examples/c/demo.c: -------------------------------------------------------------------------------- 1 | #include "sqlite3.h" 2 | #include "sqlite-ulid.h" 3 | #include 4 | #include 5 | 6 | int main(int argc, char *argv[]) { 7 | int rc = SQLITE_OK; 8 | sqlite3 *db; 9 | sqlite3_stmt *stmt; 10 | char* error_message; 11 | 12 | rc = sqlite3_auto_extension((void (*)())sqlite3_ulid_init); 13 | if (rc != SQLITE_OK) { 14 | fprintf(stderr, "❌ demo.c could not load sqlite3_ulid_init: %s\n", sqlite3_errmsg(db)); 15 | sqlite3_close(db); 16 | return 1; 17 | } 18 | 19 | rc = sqlite3_open(":memory:", &db); 20 | 21 | if (rc != SQLITE_OK) { 22 | fprintf(stderr, "❌ demo.c cannot open database: %s\n", sqlite3_errmsg(db)); 23 | sqlite3_close(db); 24 | return 1; 25 | } 26 | 27 | rc = sqlite3_prepare_v2(db, "SELECT ulid()", -1, &stmt, NULL); 28 | if(rc != SQLITE_OK) { 29 | fprintf(stderr, "❌ demo.c%s\n", error_message); 30 | sqlite3_free(error_message); 31 | sqlite3_close(db); 32 | return 1; 33 | } 34 | if (SQLITE_ROW != sqlite3_step(stmt)) { 35 | fprintf(stderr, "❌ demo.c%s\n", sqlite3_errmsg(db)); 36 | sqlite3_close(db); 37 | return 1; 38 | } 39 | printf("ulid()=%s\n", sqlite3_column_text(stmt, 0)); 40 | sqlite3_finalize(stmt); 41 | 42 | printf("✅ demo.c ran successfully. \n"); 43 | sqlite3_close(db); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /examples/go/.gitignore: -------------------------------------------------------------------------------- 1 | demo 2 | -------------------------------------------------------------------------------- /examples/go/Makefile: -------------------------------------------------------------------------------- 1 | CGO_LDFLAGS="-L$(SQLITE_ULID_LIB_DIR)" 2 | 3 | demo: demo.go 4 | CGO_LDFLAGS=$(CGO_LDFLAGS) go build -o $@ 5 | 6 | 7 | ifndef SQLITE_ULID_LIB_DIR 8 | $(error SQLITE_ULID_LIB_DIR is undefined) 9 | endif 10 | -------------------------------------------------------------------------------- /examples/go/demo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "log" 7 | 8 | _ "github.com/asg017/sqlite-ulid/bindings/go" 9 | _ "github.com/mattn/go-sqlite3" 10 | ) 11 | 12 | // #cgo darwin,amd64 LDFLAGS: -framework CoreFoundation 13 | import "C" 14 | 15 | // cgo windows,amd64 CFLAGS: -IC:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\include 16 | // cgo windows,amd64 LDFLAGS: -LC:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\lib -lwinapi_advapi32 -lwinapi_kernel32 -lbcrypt -ladvapi32 -lkernel32 -ladvapi32 -luserenv -lkernel32 -lkernel32 -lws2_32 -lbcrypt 17 | 18 | 19 | func main() { 20 | db, err := sql.Open("sqlite3", ":memory:") 21 | if err != nil { 22 | log.Fatal(err) 23 | } 24 | defer db.Close() 25 | 26 | var ulid string 27 | err = db.QueryRow("select ulid()").Scan(&ulid) 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | 32 | fmt.Printf("ulid: %s\n", ulid) 33 | } 34 | -------------------------------------------------------------------------------- /examples/go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/asg017/sqlite-ulid/examples/go 2 | 3 | go 1.20 4 | 5 | replace github.com/asg017/sqlite-ulid/bindings/go => ../../bindings/go 6 | 7 | require ( 8 | github.com/asg017/sqlite-ulid/bindings/go v0.0.0-20230719235740-f4bc3f7d6dcc // indirect 9 | github.com/mattn/go-sqlite3 v1.14.17 // indirect 10 | ) 11 | -------------------------------------------------------------------------------- /examples/go/go.sum: -------------------------------------------------------------------------------- 1 | github.com/asg017/sqlite-ulid/bindings/go v0.0.0-20230719235740-f4bc3f7d6dcc h1:yASIW7R8HYKeWWirH+5ANUJ+0TNWjZzCWE0+h8LPzkI= 2 | github.com/asg017/sqlite-ulid/bindings/go v0.0.0-20230719235740-f4bc3f7d6dcc/go.mod h1:ktFhuwJaBdbtgBZbEbUT5Ki7KJ9CsAkzDGJlH/GHju4= 3 | github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= 4 | github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= 5 | -------------------------------------------------------------------------------- /npm/.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | 132 | # not needed for libraries, right? running into some issues in ci with this, so gonna yeet 133 | package-lock.json -------------------------------------------------------------------------------- /npm/README.md: -------------------------------------------------------------------------------- 1 | # sqlite-ulid on npm 2 | 3 | `sqlite-ulid` is also available for download through [`npm`](https://www.npmjs.com/) for Node.js developers. See the [`sqlite-ulid` NPM package README](./sqlite-ulid/README.md) for details. 4 | 5 | The other NPM packages in this folder (`sqlite-ulid-darwin-x64`, `sqlite-ulid-windows-x64` etc.) are autogenerated platform-specific packages. See [Supported Platforms](./sqlite-ulid/README.md#supported-platforms) for details. 6 | -------------------------------------------------------------------------------- /npm/platform-package.README.md.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | # ${PACKAGE_NAME} 4 | 5 | A `${PACKAGE_NAME_BASE}` platform-specific package for `${PLATFORM_OS}-${PLATFORM_ARCH}`. 6 | 7 | When `${PACKAGE_NAME_BASE}` is installed and the host computer has a `${PLATFORM_OS}` operating system with `${PLATFORM_ARCH}` architecture, then this package is downloaded with the pre-compiled SQLite extension bundled under `lib/${EXTENSION_NAME}.${EXTENSION_SUFFIX}`. At runtime, the `${PACKAGE_NAME_BASE}` package will resolve to this platform-specific package for use with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)' or [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). 8 | 9 | See the `${PACKAGE_NAME_BASE}` package for more details. -------------------------------------------------------------------------------- /npm/platform-package.package.json.tmpl: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "${PACKAGE_NAME}", 4 | "version": "${VERSION}", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/asg017/${PACKAGE_NAME_BASE}.git", 8 | "directory": "npm/${PACKAGE_NAME}" 9 | }, 10 | "author": "Alex Garcia ", 11 | "os": [ 12 | "${PLATFORM_OS}" 13 | ], 14 | "cpu": [ 15 | "${PLATFORM_ARCH}" 16 | ] 17 | } -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-arm64/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # sqlite-ulid-darwin-arm64 4 | 5 | A `sqlite-ulid` platform-specific package for `darwin-arm64`. 6 | 7 | When `sqlite-ulid` is installed and the host computer has a `darwin` operating system with `arm64` architecture, then this package is downloaded with the pre-compiled SQLite extension bundled under `lib/ulid0.dylib`. At runtime, the `sqlite-ulid` package will resolve to this platform-specific package for use with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)' or [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). 8 | 9 | See the `sqlite-ulid` package for more details. -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-arm64/lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asg017/sqlite-ulid/942d62ca1ebc483df3cc8d31af71cfff4778ed7f/npm/sqlite-ulid-darwin-arm64/lib/.gitkeep -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-arm64/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "sqlite-ulid-darwin-arm64", 4 | "version": "0.2.2-alpha.1", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/asg017/sqlite-ulid.git", 8 | "directory": "npm/sqlite-ulid-darwin-arm64" 9 | }, 10 | "author": "Alex Garcia ", 11 | "os": [ 12 | "darwin" 13 | ], 14 | "cpu": [ 15 | "arm64" 16 | ] 17 | } -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-x64/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # sqlite-ulid-darwin-x64 4 | 5 | A `sqlite-ulid` platform-specific package for `darwin-x64`. 6 | 7 | When `sqlite-ulid` is installed and the host computer has a `darwin` operating system with `x64` architecture, then this package is downloaded with the pre-compiled SQLite extension bundled under `lib/ulid0.dylib`. At runtime, the `sqlite-ulid` package will resolve to this platform-specific package for use with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)' or [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). 8 | 9 | See the `sqlite-ulid` package for more details. -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-x64/lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asg017/sqlite-ulid/942d62ca1ebc483df3cc8d31af71cfff4778ed7f/npm/sqlite-ulid-darwin-x64/lib/.gitkeep -------------------------------------------------------------------------------- /npm/sqlite-ulid-darwin-x64/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "sqlite-ulid-darwin-x64", 4 | "version": "0.2.2-alpha.1", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/asg017/sqlite-ulid.git", 8 | "directory": "npm/sqlite-ulid-darwin-x64" 9 | }, 10 | "author": "Alex Garcia ", 11 | "os": [ 12 | "darwin" 13 | ], 14 | "cpu": [ 15 | "x64" 16 | ] 17 | } -------------------------------------------------------------------------------- /npm/sqlite-ulid-linux-x64/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # sqlite-ulid-linux-x64 4 | 5 | A `sqlite-ulid` platform-specific package for `linux-x64`. 6 | 7 | When `sqlite-ulid` is installed and the host computer has a `linux` operating system with `x64` architecture, then this package is downloaded with the pre-compiled SQLite extension bundled under `lib/ulid0.so`. At runtime, the `sqlite-ulid` package will resolve to this platform-specific package for use with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)' or [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). 8 | 9 | See the `sqlite-ulid` package for more details. -------------------------------------------------------------------------------- /npm/sqlite-ulid-linux-x64/lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asg017/sqlite-ulid/942d62ca1ebc483df3cc8d31af71cfff4778ed7f/npm/sqlite-ulid-linux-x64/lib/.gitkeep -------------------------------------------------------------------------------- /npm/sqlite-ulid-linux-x64/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "sqlite-ulid-linux-x64", 4 | "version": "0.2.2-alpha.1", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/asg017/sqlite-ulid.git", 8 | "directory": "npm/sqlite-ulid-linux-x64" 9 | }, 10 | "author": "Alex Garcia ", 11 | "os": [ 12 | "linux" 13 | ], 14 | "cpu": [ 15 | "x64" 16 | ] 17 | } -------------------------------------------------------------------------------- /npm/sqlite-ulid-windows-x64/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # sqlite-ulid-windows-x64 4 | 5 | A `sqlite-ulid` platform-specific package for `windows-x64`. 6 | 7 | When `sqlite-ulid` is installed and the host computer has a `windows` operating system with `x64` architecture, then this package is downloaded with the pre-compiled SQLite extension bundled under `lib/ulid0.dll`. At runtime, the `sqlite-ulid` package will resolve to this platform-specific package for use with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)' or [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). 8 | 9 | See the `sqlite-ulid` package for more details. -------------------------------------------------------------------------------- /npm/sqlite-ulid-windows-x64/lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asg017/sqlite-ulid/942d62ca1ebc483df3cc8d31af71cfff4778ed7f/npm/sqlite-ulid-windows-x64/lib/.gitkeep -------------------------------------------------------------------------------- /npm/sqlite-ulid-windows-x64/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "sqlite-ulid-windows-x64", 4 | "version": "0.2.2-alpha.1", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/asg017/sqlite-ulid.git", 8 | "directory": "npm/sqlite-ulid-windows-x64" 9 | }, 10 | "author": "Alex Garcia ", 11 | "os": [ 12 | "windows" 13 | ], 14 | "cpu": [ 15 | "x64" 16 | ] 17 | } -------------------------------------------------------------------------------- /npm/sqlite-ulid/README.md: -------------------------------------------------------------------------------- 1 | # `sqlite-ulid` NPM Package 2 | 3 | `sqlite-ulid` is distributed on `npm` for Node.js developers. To install on [supported platforms](#supported-platforms), simply run: 4 | 5 | ``` 6 | npm install sqlite-ulid 7 | ``` 8 | 9 | The `sqlite-ulid` package is meant to be used with Node SQLite clients like [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) and [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). For `better-sqlite3`, call [`.loadExtension()`](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#loadextensionpath-entrypoint---this) on your database object, passing in [`getLoadablePath()`](#getLoadablePath). 10 | 11 | ```js 12 | import Database from "better-sqlite3"; 13 | import * as sqlite_ulid from "sqlite-ulid"; 14 | 15 | const db = new Database(":memory:"); 16 | 17 | db.loadExtension(sqlite_ulid.getLoadablePath()); 18 | 19 | const version = db.prepare("select ulid_version()").pluck().get(); 20 | console.log(version); // "v0.2.0" 21 | ``` 22 | 23 | For `node-sqlite3`, call the similarly named [`.loadExtension()`](https://github.com/TryGhost/node-sqlite3/wiki/API#loadextensionpath--callback) method on your database object, and pass in [`getLoadablePath()`](#getLoadablePath). 24 | 25 | ```js 26 | import sqlite3 from "sqlite3"; 27 | import * as sqlite_ulid from "sqlite-ulid"; 28 | 29 | const db = new sqlite3.Database(":memory:"); 30 | 31 | db.loadExtension(sqlite_ulid.getLoadablePath()); 32 | 33 | db.get("select ulid_version()", (err, row) => { 34 | console.log(row); // {ulid_version(): "v0.2.0"} 35 | }); 36 | ``` 37 | 38 | See [the full API Reference](#api-reference) for the Node API, and [`docs.md`](../../docs.md) for documentation on the `sqlite-ulid` SQL API. 39 | 40 | ## Supported Platforms 41 | 42 | Since the underlying `ulid0` SQLite extension is pre-compiled, the `sqlite-ulid` NPM package only works on a few "platforms" (operating systems + CPU architectures). These platforms include: 43 | 44 | - `darwin-x64` (MacOS x86_64) 45 | - `darwin-arm64` (MacOS M1 and M2 chips) 46 | - `win32-x64` (Windows x86_64) 47 | - `linux-x64` (Linux x86_64) 48 | 49 | To see which platform your machine is, check the [`process.arch`](https://nodejs.org/api/process.html#processarch) and [`process.platform`](https://nodejs.org/api/process.html#processplatform) values like so: 50 | 51 | ```bash 52 | $ node -e 'console.log([process.platform, process.arch])' 53 | [ 'darwin', 'x64' ] 54 | ``` 55 | 56 | When the `sqlite-ulid` NPM package is installed, the correct pre-compiled extension for your operating system and CPU architecture will be downloaded from the [optional dependencies](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#optionaldependencies), with platform-specific packages like `sqlite-ulid-darwin-x64`. This will be automatically, there's no need to directly install those packages. 57 | 58 | More platforms may be supported in the future. Consider [supporting my work](https://github.com/sponsors/asg017/) if you'd like to see more operating systems and CPU architectures supported in `sqlite-ulid`. 59 | 60 | ## API Reference 61 | 62 | # getLoadablePath [<>](https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/src/index.js "Source") 63 | 64 | Returns the full path to where the `sqlite-ulid` _should_ be installed, based on the `sqlite-ulid`'s `package.json` optional dependencies and the host's operating system and architecture. 65 | 66 | This path can be directly passed into [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)'s [`.loadExtension()`](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#loadextensionpath-entrypoint---this). 67 | 68 | ```js 69 | import Database from "better-sqlite3"; 70 | import * as sqlite_ulid from "sqlite-ulid"; 71 | 72 | const db = new Database(":memory:"); 73 | db.loadExtension(sqlite_ulid.getLoadablePath()); 74 | ``` 75 | 76 | It can also be used in [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3)'s [`.loadExtension()`](https://github.com/TryGhost/node-sqlite3/wiki/API#loadextensionpath--callback). 77 | 78 | ```js 79 | import sqlite3 from "sqlite3"; 80 | import * as sqlite_ulid from "sqlite-ulid"; 81 | 82 | const db = new sqlite3.Database(":memory:"); 83 | db.loadExtension(sqlite_ulid.getLoadablePath()); 84 | ``` 85 | 86 | This function throws an `Error` in two different cases. The first case is when `sqlite-ulid` is installed and run on an [unsupported platform](#supported-platforms). The second case is when the platform-specific optional dependency is not installed. If you reach this, ensure you aren't using `--no-optional` flag, and [file an issue](https://github.com/asg017/sqlite-ulid/issues/new) if you are stuck. 87 | 88 | The `db.loadExtension()` function may also throw an Error if the compiled extension is incompatible with your SQLite connection for any reason, including missing system packages, outdated glib versions, or other misconfigurations. If you reach this, please [file an issue](https://github.com/asg017/sqlite-ulid/issues/new). 89 | -------------------------------------------------------------------------------- /npm/sqlite-ulid/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "sqlite-ulid", 4 | "version": "0.2.2-alpha.1", 5 | "description": "", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/asg017/sqlite-ulid.git", 9 | "directory": "npm/sqlite-ulid" 10 | }, 11 | "author": "Alex Garcia ", 12 | "license": "(MIT OR Apache-2.0)", 13 | "main": "src/index.js", 14 | "type": "module", 15 | "scripts": { 16 | "test": "node test.js" 17 | }, 18 | "files": [ 19 | "*.dylib", 20 | "*.so", 21 | "*.dll" 22 | ], 23 | "optionalDependencies": { 24 | "sqlite-ulid-darwin-arm64": "0.2.2-alpha.1", 25 | "sqlite-ulid-darwin-x64": "0.2.2-alpha.1", 26 | "sqlite-ulid-linux-x64": "0.2.2-alpha.1", 27 | "sqlite-ulid-windows-x64": "0.2.2-alpha.1" 28 | }, 29 | "devDependencies": { 30 | "better-sqlite3": "^8.1.0", 31 | "sqlite3": "^5.1.4" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /npm/sqlite-ulid/package.json.tmpl: -------------------------------------------------------------------------------- 1 | { 2 | "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand", 3 | "name": "${PACKAGE_NAME_BASE}", 4 | "version": "${VERSION}", 5 | "description": "", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/asg017/${PACKAGE_NAME_BASE}.git", 9 | "directory": "npm/${PACKAGE_NAME_BASE}" 10 | }, 11 | "author": "Alex Garcia ", 12 | "license": "(MIT OR Apache-2.0)", 13 | "main": "src/index.js", 14 | "type": "module", 15 | "scripts": { 16 | "test": "node test.js" 17 | }, 18 | "files": [ 19 | "*.dylib", 20 | "*.so", 21 | "*.dll" 22 | ], 23 | "optionalDependencies": { 24 | "sqlite-ulid-darwin-arm64": "${VERSION}", 25 | "sqlite-ulid-darwin-x64": "${VERSION}", 26 | "sqlite-ulid-linux-x64": "${VERSION}", 27 | "sqlite-ulid-windows-x64": "${VERSION}" 28 | }, 29 | "devDependencies": { 30 | "better-sqlite3": "^8.1.0", 31 | "sqlite3": "^5.1.4" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /npm/sqlite-ulid/src/index.js: -------------------------------------------------------------------------------- 1 | import { join } from "node:path"; 2 | import { fileURLToPath } from "node:url"; 3 | import { arch, platform } from "node:process"; 4 | import { statSync } from "node:fs"; 5 | 6 | const supportedPlatforms = [ 7 | ["darwin", "x64"], 8 | ["darwin", "arm64"], 9 | ["win32", "x64"], 10 | ["linux", "x64"], 11 | ]; 12 | 13 | function validPlatform(platform, arch) { 14 | return ( 15 | supportedPlatforms.find(([p, a]) => platform == p && arch === a) !== null 16 | ); 17 | } 18 | function extensionSuffix(platform) { 19 | if (platform === "win32") return "dll"; 20 | if (platform === "darwin") return "dylib"; 21 | return "so"; 22 | } 23 | function platformPackageName(platform, arch) { 24 | const os = platform === "win32" ? "windows" : platform; 25 | return `sqlite-ulid-${os}-${arch}`; 26 | } 27 | 28 | export function getLoadablePath() { 29 | if (!validPlatform(platform, arch)) { 30 | throw new Error( 31 | `Unsupported platform for sqlite-ulid, on a ${platform}-${arch} machine, but not in supported platforms (${supportedPlatforms 32 | .map(([p, a]) => `${p}-${a}`) 33 | .join(",")}). Consult the sqlite-ulid NPM package README for details. ` 34 | ); 35 | } 36 | const packageName = platformPackageName(platform, arch); 37 | const loadablePath = join( 38 | fileURLToPath(new URL(".", import.meta.url)), 39 | "..", 40 | "..", 41 | packageName, 42 | "lib", 43 | `ulid0.${extensionSuffix(platform)}` 44 | ); 45 | if (!statSync(loadablePath, { throwIfNoEntry: false })) { 46 | throw new Error( 47 | `Loadble extension for sqlite-ulid not found. Was the ${packageName} package installed? Avoid using the --no-optional flag, as the optional dependencies for sqlite-ulid are required.` 48 | ); 49 | } 50 | 51 | return loadablePath; 52 | } 53 | -------------------------------------------------------------------------------- /npm/sqlite-ulid/test.js: -------------------------------------------------------------------------------- 1 | import test from "node:test"; 2 | import * as assert from "node:assert"; 3 | 4 | import { getLoadablePath } from "./src/index.js"; 5 | import { basename, extname, isAbsolute } from "node:path"; 6 | 7 | import Database from "better-sqlite3"; 8 | import sqlite3 from "sqlite3"; 9 | 10 | test("getLoadblePath()", (t) => { 11 | const loadablePath = getLoadablePath(); 12 | assert.strictEqual(isAbsolute(loadablePath), true); 13 | assert.strictEqual(basename(loadablePath, extname(loadablePath)), "ulid0"); 14 | }); 15 | 16 | test("better-sqlite3", (t) => { 17 | const db = new Database(":memory:"); 18 | db.loadExtension(getLoadablePath()); 19 | const version = db.prepare("select ulid_version()").pluck().get(); 20 | assert.strictEqual(version[0], "v"); 21 | }); 22 | 23 | test("sqlite3", async (t) => { 24 | const db = new sqlite3.Database(":memory:"); 25 | db.loadExtension(getLoadablePath()); 26 | let version = await new Promise((resolve, reject) => { 27 | db.get("select ulid_version()", (err, row) => { 28 | if (err) return reject(err); 29 | resolve(row["ulid_version()"]); 30 | }); 31 | }); 32 | assert.strictEqual(version[0], "v"); 33 | }); 34 | -------------------------------------------------------------------------------- /python/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # `sqlite-ulid` Python Packages 2 | 3 | The `sqlite-ulid` project offers two python packages for easy distribution. They are: 4 | 5 | 1. The [`sqlite-ulid` Python package](https://pypi.org/project/sqlite-ulid/), source in [`sqlite_ulid/`](./sqlite_ulid/README.md) 6 | 2. The [`datasette-sqlite-ulid` Python package](https://pypi.org/project/sqlite-ulid/), a [Datasette](https://datasette.io/) plugin,which is a light wrapper around the `sqlite-ulid` package, source in [`datasette_sqlite_ulid/`](./datasette_sqlite_ulid/README.md) 7 | -------------------------------------------------------------------------------- /python/datasette_sqlite_ulid/README.md: -------------------------------------------------------------------------------- 1 | # The `datasette-sqlite-ulid` Datasette Plugin 2 | 3 | `datasette-sqlite-ulid` is a [Datasette plugin](https://docs.datasette.io/en/stable/plugins.html) that loads the [`sqlite-ulid`](https://github.com/asg017/sqlite-ulid) extension in Datasette instances, allowing you to generate and work with [ULIDs](https://github.com/ulid/spec) in SQL. 4 | 5 | ``` 6 | datasette install datasette-sqlite-ulid 7 | ``` 8 | 9 | See [`docs.md`](../../docs.md) for a full API reference for the ULID SQL functions. 10 | 11 | Alternatively, when publishing Datasette instances, you can use the `--install` option to install the plugin. 12 | 13 | ``` 14 | datasette publish cloudrun data.db --service=my-service --install=datasette-sqlite-ulid 15 | 16 | ``` 17 | -------------------------------------------------------------------------------- /python/datasette_sqlite_ulid/datasette_sqlite_ulid/__init__.py: -------------------------------------------------------------------------------- 1 | from datasette import hookimpl 2 | import sqlite_ulid 3 | 4 | from datasette_sqlite_ulid.version import __version_info__, __version__ 5 | 6 | @hookimpl 7 | def prepare_connection(conn): 8 | conn.enable_load_extension(True) 9 | sqlite_ulid.load(conn) 10 | conn.enable_load_extension(False) -------------------------------------------------------------------------------- /python/datasette_sqlite_ulid/datasette_sqlite_ulid/version.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.2.2-alpha.1" 2 | __version_info__ = tuple(__version__.split(".")) 3 | -------------------------------------------------------------------------------- /python/datasette_sqlite_ulid/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | version = {} 4 | with open("datasette_sqlite_ulid/version.py") as fp: 5 | exec(fp.read(), version) 6 | 7 | VERSION = version['__version__'] 8 | 9 | 10 | setup( 11 | name="datasette-sqlite-ulid", 12 | description="", 13 | long_description="", 14 | long_description_content_type="text/markdown", 15 | author="Alex Garcia", 16 | url="https://github.com/asg017/sqlite-ulid", 17 | project_urls={ 18 | "Issues": "https://github.com/asg017/sqlite-ulid/issues", 19 | "CI": "https://github.com/asg017/sqlite-ulid/actions", 20 | "Changelog": "https://github.com/asg017/sqlite-ulid/releases", 21 | }, 22 | license="MIT License, Apache License, Version 2.0", 23 | version=VERSION, 24 | packages=["datasette_sqlite_ulid"], 25 | entry_points={"datasette": ["sqlite_ulid = datasette_sqlite_ulid"]}, 26 | install_requires=["datasette", "sqlite-ulid"], 27 | extras_require={"test": ["pytest"]}, 28 | python_requires=">=3.6", 29 | ) -------------------------------------------------------------------------------- /python/datasette_sqlite_ulid/tests/test_sqlite_ulid.py: -------------------------------------------------------------------------------- 1 | from datasette.app import Datasette 2 | import pytest 3 | 4 | 5 | @pytest.mark.asyncio 6 | async def test_plugin_is_installed(): 7 | datasette = Datasette(memory=True) 8 | response = await datasette.client.get("/-/plugins.json") 9 | assert response.status_code == 200 10 | installed_plugins = {p["name"] for p in response.json()} 11 | assert "datasette-sqlite-ulid" in installed_plugins 12 | 13 | @pytest.mark.asyncio 14 | async def test_sqlite_ulid_functions(): 15 | datasette = Datasette(memory=True) 16 | response = await datasette.client.get("/_memory.json?sql=select+ulid_version(),ulid()") 17 | assert response.status_code == 200 18 | ulid_version, ulid = response.json()["rows"][0] 19 | assert ulid_version[0] == "v" 20 | assert len(ulid) == 26 -------------------------------------------------------------------------------- /python/sqlite_ulid/README.md: -------------------------------------------------------------------------------- 1 | # The `sqlite-ulid` Python package 2 | 3 | `sqlite-ulid` is also distributed on PyPi as a Python package, for use in Python applications. It works well with the builtin [`sqlite3`](https://docs.python.org/3/library/sqlite3.html) Python module. 4 | 5 | ``` 6 | pip install sqlite-ulid 7 | ``` 8 | 9 | ## Usage 10 | 11 | The `sqlite-ulid` python package exports two functions: `loadable_path()`, which returns the full path to the loadable extension, and `load(conn)`, which loads the `sqlite-ulid` extension into the given [sqlite3 Connection object](https://docs.python.org/3/library/sqlite3.html#connection-objects). 12 | 13 | ```python 14 | import sqlite_ulid 15 | print(sqlite_ulid.loadable_path()) 16 | # '/.../venv/lib/python3.9/site-packages/sqlite_ulid/ulid0' 17 | 18 | import sqlite3 19 | conn = sqlite3.connect(':memory:') 20 | sqlite_ulid.load(conn) 21 | conn.execute('select ulid_version(), ulid()').fetchone() 22 | # ('v0.1.0', '01gr7gwc5aq22ycea6j8kxq4s9') 23 | ``` 24 | 25 | See [the full API Reference](#api-reference) for the Python API, and [`docs.md`](../../docs.md) for documentation on the `sqlite-ulid` SQL API. 26 | 27 | See [`datasette-sqlite-ulid`](../datasette_sqlite_ulid/) for a Datasette plugin that is a light wrapper around the `sqlite-ulid` Python package. 28 | 29 | ## Compatibility 30 | 31 | Currently the `sqlite-ulid` Python package is only distributed on PyPi as pre-build wheels, it's not possible to install from the source distribution. This is because the underlying `sqlite-ulid` extension requires a lot of build dependencies like `make`, `cc`, and `cargo`. 32 | 33 | If you get a `unsupported platform` error when pip installing `sqlite-ulid`, you'll have to build the `sqlite-ulid` manually and load in the dynamic library manually. 34 | 35 | ## API Reference 36 | 37 |

loadable_path()

38 | 39 | Returns the full path to the locally-install `sqlite-ulid` extension, without the filename. 40 | 41 | This can be directly passed to [`sqlite3.Connection.load_extension()`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.load_extension), but the [`sqlite_ulid.load()`](#load) function is preferred. 42 | 43 | ```python 44 | import sqlite_ulid 45 | print(sqlite_ulid.loadable_path()) 46 | # '/.../venv/lib/python3.9/site-packages/sqlite_ulid/ulid0' 47 | ``` 48 | 49 | > Note: this extension path doesn't include the file extension (`.dylib`, `.so`, `.dll`). This is because [SQLite will infer the correct extension](https://www.sqlite.org/loadext.html#loading_an_extension). 50 | 51 |

load(connection)

52 | 53 | Loads the `sqlite-ulid` extension on the given [`sqlite3.Connection`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection) object, calling [`Connection.load_extension()`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.load_extension). 54 | 55 | ```python 56 | import sqlite_ulid 57 | import sqlite3 58 | conn = sqlite3.connect(':memory:') 59 | 60 | conn.enable_load_extension(True) 61 | sqlite_ulid.load(conn) 62 | conn.enable_load_extension(False) 63 | 64 | conn.execute('select ulid_version(), ulid()').fetchone() 65 | # ('v0.1.0', '01gr7gwc5aq22ycea6j8kxq4s9') 66 | ``` 67 | -------------------------------------------------------------------------------- /python/sqlite_ulid/noop.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asg017/sqlite-ulid/942d62ca1ebc483df3cc8d31af71cfff4778ed7f/python/sqlite_ulid/noop.c -------------------------------------------------------------------------------- /python/sqlite_ulid/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, Extension 2 | import os 3 | import platform 4 | 5 | version = {} 6 | with open("sqlite_ulid/version.py") as fp: 7 | exec(fp.read(), version) 8 | 9 | VERSION = version['__version__'] 10 | 11 | system = platform.system() 12 | machine = platform.machine() 13 | 14 | print(system, machine) 15 | 16 | if system == 'Darwin': 17 | if machine not in ['x86_64', 'arm64']: 18 | raise Exception("unsupported platform") 19 | elif system == 'Linux': 20 | if machine not in ['x86_64']: 21 | raise Exception("unsupported platform") 22 | elif system == 'Windows': 23 | # TODO only 64 bit I think 24 | pass 25 | else: 26 | raise Exception("unsupported platform") 27 | 28 | setup( 29 | name="sqlite-ulid", 30 | description="", 31 | long_description="", 32 | long_description_content_type="text/markdown", 33 | author="Alex Garcia", 34 | url="https://github.com/asg017/sqlite-ulid", 35 | project_urls={ 36 | "Issues": "https://github.com/asg017/sqlite-ulid/issues", 37 | "CI": "https://github.com/asg017/sqlite-ulid/actions", 38 | "Changelog": "https://github.com/asg017/sqlite-ulid/releases", 39 | }, 40 | license="MIT License, Apache License, Version 2.0", 41 | version=VERSION, 42 | packages=["sqlite_ulid"], 43 | package_data={"sqlite_ulid": ['*.so', '*.dylib', '*.dll']}, 44 | install_requires=[], 45 | # Adding an Extension makes `pip wheel` believe that this isn't a 46 | # pure-python package. The noop.c was added since the windows build 47 | # didn't seem to respect optional=True 48 | ext_modules=[Extension("noop", ["noop.c"], optional=True)], 49 | extras_require={"test": ["pytest"]}, 50 | python_requires=">=3.6", 51 | ) -------------------------------------------------------------------------------- /python/sqlite_ulid/sqlite_ulid/__init__.py: -------------------------------------------------------------------------------- 1 | from sqlite_ulid.version import __version_info__, __version__ 2 | 3 | import os 4 | import sqlite3 5 | 6 | def loadable_path(): 7 | loadable_path = os.path.join(os.path.dirname(__file__), "ulid0") 8 | return os.path.normpath(loadable_path) 9 | 10 | def load(conn: sqlite3.Connection) -> None: 11 | conn.load_extension(loadable_path()) 12 | -------------------------------------------------------------------------------- /python/sqlite_ulid/sqlite_ulid/version.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.2.2-alpha.1" 2 | __version_info__ = tuple(__version__.split(".")) 3 | -------------------------------------------------------------------------------- /scripts/deno_generate_package.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | export PACKAGE_NAME="sqlite-ulid" 6 | export EXTENSION_NAME="ulid0" 7 | export VERSION=$(cat VERSION) 8 | 9 | envsubst < deno/deno.json.tmpl > deno/deno.json 10 | echo "✅ generated deno/deno.json" 11 | 12 | envsubst < deno/README.md.tmpl > deno/README.md 13 | echo "✅ generated deno/README.md" 14 | -------------------------------------------------------------------------------- /scripts/npm_generate_platform_packages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | export PACKAGE_NAME_BASE="sqlite-ulid" 6 | export EXTENSION_NAME="ulid0" 7 | export VERSION=$(cat VERSION) 8 | 9 | generate () { 10 | export PLATFORM_OS=$1 11 | export PLATFORM_ARCH=$2 12 | export PACKAGE_NAME=$PACKAGE_NAME_BASE-$PLATFORM_OS-$PLATFORM_ARCH 13 | 14 | if [ "$PLATFORM_OS" == "windows" ]; then 15 | export EXTENSION_SUFFIX="dll" 16 | elif [ "$PLATFORM_OS" == "darwin" ]; then 17 | export EXTENSION_SUFFIX="dylib" 18 | else 19 | export EXTENSION_SUFFIX="so" 20 | fi 21 | 22 | 23 | mkdir -p npm/$PACKAGE_NAME/lib 24 | 25 | envsubst < npm/platform-package.package.json.tmpl > npm/$PACKAGE_NAME/package.json 26 | envsubst < npm/platform-package.README.md.tmpl > npm/$PACKAGE_NAME/README.md 27 | 28 | touch npm/$PACKAGE_NAME/lib/.gitkeep 29 | 30 | echo "✅ generated npm/$PACKAGE_NAME" 31 | } 32 | 33 | envsubst < npm/$PACKAGE_NAME_BASE/package.json.tmpl > npm/$PACKAGE_NAME_BASE/package.json 34 | echo "✅ generated npm/$PACKAGE_NAME_BASE" 35 | 36 | generate darwin x64 37 | generate darwin arm64 38 | generate linux x64 39 | generate windows x64 -------------------------------------------------------------------------------- /scripts/publish_release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail xtrace 4 | 5 | if [[ -n $(git status --porcelain | grep -v VERSION) ]]; then 6 | echo "❌ There are other un-staged changes to the repository besides VERSION" 7 | exit 1 8 | fi 9 | 10 | VERSION="$(cat VERSION)" 11 | 12 | echo "Publishing version v$VERSION..." 13 | 14 | make version 15 | git add --all 16 | git commit -m "v$VERSION" 17 | git tag v$VERSION 18 | git push origin main v$VERSION 19 | 20 | if grep -qE "alpha|beta" VERSION; then 21 | gh release create v$VERSION --title=v$VERSION --prerelease --notes="" 22 | else 23 | gh release create v$VERSION --title=v$VERSION 24 | fi 25 | 26 | 27 | echo "✅ Published! version v$VERSION" 28 | -------------------------------------------------------------------------------- /sqlite-ulid.h: -------------------------------------------------------------------------------- 1 | #ifndef _SQLITE_ulid_H 2 | #define _SQLITE_ulid_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int sqlite3_ulid_init(sqlite3*, char**, const sqlite3_api_routines*); 9 | 10 | #ifdef __cplusplus 11 | } /* end of the 'extern "C"' block */ 12 | #endif 13 | 14 | #endif /* ifndef _SQLITE_ulid_H */ 15 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::time::{Duration, SystemTime}; 2 | 3 | use sqlite_loadable::api::ValueType; 4 | use sqlite_loadable::prelude::*; 5 | use sqlite_loadable::{api, define_scalar_function, Error, Result}; 6 | use ulid::Ulid; 7 | 8 | const DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S.%3f"; 9 | 10 | // ulid_version() -> 'v0.1.0' 11 | pub fn ulid_version(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> { 12 | api::result_text(context, format!("v{}", env!("CARGO_PKG_VERSION")))?; 13 | Ok(()) 14 | } 15 | 16 | pub fn ulid_debug(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> { 17 | api::result_text( 18 | context, 19 | format!( 20 | "Version: v{} 21 | Source: {} 22 | ", 23 | env!("CARGO_PKG_VERSION"), 24 | env!("GIT_HASH") 25 | ), 26 | )?; 27 | Ok(()) 28 | } 29 | 30 | // ulid() -> '01GMRG9JWFHHCGG5TSXHWYR0CM' 31 | pub fn ulid(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> { 32 | let ulid = if let Some(input) = values.get(0) { 33 | Ulid(u128::from_be_bytes( 34 | api::value_blob(input) 35 | .try_into() 36 | .map_err(|_| Error::new_message("invalid BLOB input to ulid()"))?, 37 | )) 38 | } else { 39 | Ulid::new() 40 | }; 41 | api::result_text(context, ulid.to_string().to_lowercase())?; 42 | Ok(()) 43 | } 44 | 45 | // ulid_with_prefix('xyz') -> 'xyz_01GMRGH6F01DAKVTG9HJA19MP6' 46 | pub fn ulid_with_prefix( 47 | context: *mut sqlite3_context, 48 | values: &[*mut sqlite3_value], 49 | ) -> Result<()> { 50 | let prefix = api::value_text(values.get(0).expect("1st argument required"))?; 51 | api::result_text( 52 | context, 53 | format!("{prefix}_{}", Ulid::new().to_string()).to_lowercase(), 54 | )?; 55 | Ok(()) 56 | } 57 | 58 | // ulid_with_datetime('2023-01-26 19:50:09.428') -> '01gqqt2rrmg5p7d2e6dj81sccf' 59 | pub fn ulid_with_datetime( 60 | context: *mut sqlite3_context, 61 | values: &[*mut sqlite3_value], 62 | ) -> Result<()> { 63 | let string = api::value_text(values.get(0).expect("1st argument required"))?; 64 | let datetime = NaiveDateTime::parse_from_str(string, DATETIME_FMT).map_err(|e| { 65 | Error::new_message( 66 | format!("error parsing date and time using format '{DATETIME_FMT}': {e}").as_str(), 67 | ) 68 | })?; 69 | 70 | let millis = datetime 71 | .timestamp_millis() 72 | .try_into() 73 | .expect("milliseconds to be positive"); 74 | let duration = Duration::from_millis(millis); 75 | let datetime = SystemTime::UNIX_EPOCH 76 | .checked_add(duration) 77 | .expect("milliseconds to be valid"); 78 | 79 | api::result_text( 80 | context, 81 | Ulid::from_datetime(datetime).to_string().to_lowercase(), 82 | )?; 83 | Ok(()) 84 | } 85 | 86 | // ulid_bytes() -> X'0185310899dd7662b8f1e5adf9a5e7c0' 87 | // ulid_bytes('') -> X'0185310899dd7662b8f1e5adf9a5e7c0' 88 | pub fn ulid_bytes(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> { 89 | let ulid = if let Some(input) = values.get(0) { 90 | Ulid::from_string(api::value_text(input)?).map_err(|e| { 91 | Error::new_message(format!("invalid ULID input to ulid_bytes(): {}", e).as_str()) 92 | })? 93 | } else { 94 | Ulid::new() 95 | }; 96 | api::result_blob(context, &ulid.0.to_be_bytes()); 97 | Ok(()) 98 | } 99 | 100 | use chrono::NaiveDateTime; 101 | // ulid_datetime('01GMP2G8ZG6PMKWYVKS62TTA41') -> 1671483106 102 | // TODO return millisecond precision 103 | pub fn ulid_datetime(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> { 104 | let input = values.get(0).expect("1st argument required"); 105 | let ulid = match api::value_type(input) { 106 | ValueType::Text => { 107 | let input = api::value_text(input)?; 108 | // if input text is longer than a normal ULID, it might be prefixed - so try to strip 109 | // a prefix and extract the underlying ULID 110 | if input.len() > ulid::ULID_LEN { 111 | let id = if let Some((_, ulid)) = input.rsplit_once('_') { 112 | ulid 113 | } else { 114 | input 115 | }; 116 | Ulid::from_string(id).map_err(|e| { 117 | Error::new_message( 118 | format!("invalid ULID input to ulid_datetime(): {}", e).as_str(), 119 | ) 120 | })? 121 | } else { 122 | Ulid::from_string(input).map_err(|e| { 123 | Error::new_message( 124 | format!("invalid ULID input to ulid_datetime(): {}", e).as_str(), 125 | ) 126 | })? 127 | } 128 | } 129 | ValueType::Blob => Ulid(u128::from_be_bytes( 130 | api::value_blob(input) 131 | .try_into() 132 | .map_err(|_| Error::new_message("invalid BLOB input to ulid_datetime()"))?, 133 | )), 134 | _ => return Err(Error::new_message("unsupported input for ulid_datetime")), 135 | }; 136 | 137 | //api::result_text(context, Ulid::new().to_string())?; 138 | let ms = ulid 139 | .datetime() 140 | .duration_since(SystemTime::UNIX_EPOCH) 141 | .map_err(|e| Error::new_message(format!("error calculating duration: {e}").as_str()))? 142 | .as_millis() 143 | .try_into() 144 | .map_err(|e| { 145 | Error::new_message( 146 | format!("error converting duration to i64 milliseconds: {e}").as_str(), 147 | ) 148 | })?; 149 | 150 | match NaiveDateTime::from_timestamp_millis(ms) { 151 | Some(dt) => api::result_text(context, dt.format(DATETIME_FMT).to_string())?, 152 | None => return Err(Error::new_message("timestamp overflow")), 153 | }; 154 | Ok(()) 155 | } 156 | 157 | // TODO ulid_datetime() to ulid_extract_datetime(), ulid_extract_random() 158 | 159 | #[sqlite_entrypoint] 160 | pub fn sqlite3_ulid_init(db: *mut sqlite3) -> Result<()> { 161 | define_scalar_function( 162 | db, 163 | "ulid_version", 164 | 0, 165 | ulid_version, 166 | FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC, 167 | )?; 168 | define_scalar_function( 169 | db, 170 | "ulid_debug", 171 | 0, 172 | ulid_debug, 173 | FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC, 174 | )?; 175 | 176 | define_scalar_function(db, "ulid", 0, ulid, FunctionFlags::UTF8)?; 177 | define_scalar_function(db, "ulid", 1, ulid, FunctionFlags::UTF8)?; 178 | define_scalar_function(db, "ulid_bytes", 0, ulid_bytes, FunctionFlags::UTF8)?; 179 | define_scalar_function(db, "ulid_bytes", 1, ulid_bytes, FunctionFlags::UTF8)?; 180 | define_scalar_function( 181 | db, 182 | "ulid_with_prefix", 183 | 1, 184 | ulid_with_prefix, 185 | FunctionFlags::UTF8, 186 | )?; 187 | define_scalar_function( 188 | db, 189 | "ulid_with_datetime", 190 | 1, 191 | ulid_with_datetime, 192 | FunctionFlags::UTF8, 193 | )?; 194 | define_scalar_function( 195 | db, 196 | "ulid_datetime", 197 | 1, 198 | ulid_datetime, 199 | FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC, 200 | )?; 201 | Ok(()) 202 | } 203 | -------------------------------------------------------------------------------- /test.sql: -------------------------------------------------------------------------------- 1 | .load target/debug/libulid0 2 | .mode quote 3 | .header on 4 | 5 | select ulid_bytes(); 6 | 7 | .mode box 8 | select ulid(), null as ' ulid_bytes() ', ulid_with_prefix("invoice_"); 9 | 10 | select ulid_datetime('01GMP2G8ZG6PMKWYVKS62TTA41'); 11 | 12 | 13 | create table events( 14 | id text primary key, 15 | data json 16 | ); 17 | 18 | insert into events 19 | select 20 | ulid_with_prefix('event') as id, 21 | '{}'; 22 | --from json_each(readfile('.json')); -------------------------------------------------------------------------------- /tests/test-loadable.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import unittest 3 | import time 4 | import os 5 | 6 | EXT_PATH="./dist/debug/ulid0" 7 | 8 | 9 | def connect(ext, path=":memory:"): 10 | db = sqlite3.connect(path) 11 | 12 | db.execute("create temp table base_functions as select name from pragma_function_list") 13 | db.execute("create temp table base_modules as select name from pragma_module_list") 14 | 15 | db.enable_load_extension(True) 16 | db.load_extension(ext) 17 | 18 | db.execute("create temp table loaded_functions as select name from pragma_function_list where name not in (select name from base_functions) order by name") 19 | db.execute("create temp table loaded_modules as select name from pragma_module_list where name not in (select name from base_modules) order by name") 20 | 21 | db.row_factory = sqlite3.Row 22 | return db 23 | 24 | 25 | db = connect(EXT_PATH) 26 | 27 | def explain_query_plan(sql): 28 | return db.execute("explain query plan " + sql).fetchone()["detail"] 29 | 30 | def execute_all(cursor, sql, args=None): 31 | if args is None: args = [] 32 | results = cursor.execute(sql, args).fetchall() 33 | return list(map(lambda x: dict(x), results)) 34 | 35 | def spread_args(args): 36 | return ",".join(['?'] * len(args)) 37 | 38 | FUNCTIONS = [ 39 | 'ulid', 40 | 'ulid', 41 | 'ulid_bytes', 42 | 'ulid_bytes', 43 | 'ulid_datetime', 44 | 'ulid_debug', 45 | 'ulid_version', 46 | 'ulid_with_datetime', 47 | 'ulid_with_prefix', 48 | 49 | 50 | ] 51 | 52 | MODULES = [] 53 | 54 | class TestUlid(unittest.TestCase): 55 | def test_funcs(self): 56 | funcs = list(map(lambda a: a[0], db.execute("select name from loaded_functions").fetchall())) 57 | self.assertEqual(funcs, FUNCTIONS) 58 | 59 | def test_modules(self): 60 | modules = list(map(lambda a: a[0], db.execute("select name from loaded_modules").fetchall())) 61 | self.assertEqual(modules, MODULES) 62 | 63 | 64 | def test_ulid_version(self): 65 | self.assertEqual(db.execute("select ulid_version()").fetchone()[0][0], "v") 66 | 67 | def test_ulid_debug(self): 68 | debug = db.execute("select ulid_debug()").fetchone()[0].split('\n') 69 | self.assertEqual(len(debug), 3) 70 | 71 | def test_ulid(self): 72 | ulid = lambda *args: db.execute(f"select ulid({spread_args(args)})", args).fetchone()[0] 73 | self.assertEqual(len(ulid()), 26) 74 | self.assertEqual(type(ulid()), str) 75 | 76 | self.assertEqual(len(ulid(b'\x01\x85\xe5\xb1\xc5\xe9\xfb\xa7\xf5\xcfSJ\x13\xe4.\xa3')), 26) 77 | self.assertEqual(type(ulid(b'\x01\x85\xe5\xb1\xc5\xe9\xfb\xa7\xf5\xcfSJ\x13\xe4.\xa3')), str) 78 | 79 | 80 | def test_ulid_bytes(self): 81 | ulid_bytes = lambda *args: db.execute(f"select ulid_bytes({spread_args(args)})", args).fetchone()[0] 82 | 83 | self.assertEqual(len(ulid_bytes()), 16) 84 | self.assertEqual(type(ulid_bytes()), bytes) 85 | 86 | self.assertEqual(len(ulid_bytes('01gqqt4x43d7k0x3n5jpqhh2qd')), 16) 87 | self.assertEqual(type(ulid_bytes('01gqqt4x43d7k0x3n5jpqhh2qd')), bytes) 88 | 89 | def test_ulid_with_prefix(self): 90 | ulid_with_prefix = lambda prefix: db.execute("select ulid_with_prefix(?)", [prefix]).fetchone()[0] 91 | self.assertEqual(len(ulid_with_prefix("abc")), 26+4) 92 | 93 | def test_ulid_with_datetime(self): 94 | ulid_with_datetime = lambda datetime: db.execute("select ulid_with_datetime(?)", [datetime]).fetchone()[0] 95 | ulid_datetime = lambda ulid: db.execute("select ulid_datetime(?)", [ulid]).fetchone()[0] 96 | 97 | ulid = ulid_with_datetime("2023-01-26 19:50:09.428") 98 | self.assertEqual(ulid_datetime(ulid), '2023-01-26 19:50:09.428') 99 | 100 | def test_ulid_datetime(self): 101 | ulid_datetime = lambda ulid: db.execute("select ulid_datetime(?)", [ulid]).fetchone()[0] 102 | self.assertEqual(ulid_datetime('01GMP2G8ZG6PMKWYVKS62TTA41'), '2022-12-19 20:51:46.288') 103 | self.assertEqual(ulid_datetime('u_01gqqt6dz6p00680vw747t8vs5'), '2023-01-26 19:52:09.446') 104 | #self.assertEqual(ulid_datetime(b'0185d0c5362761312e0483e4c9e3ec5d'), 1671483106288) 105 | self.assertEqual(ulid_datetime(b'\x01\x85\xe5\xb1\xc5\xe9\xfb\xa7\xf5\xcfSJ\x13\xe4.\xa3'), '2023-01-24 21:31:51.145') 106 | 107 | 108 | 109 | class TestCoverage(unittest.TestCase): 110 | def test_coverage(self): 111 | test_methods = [method for method in dir(TestUlid) if method.startswith('test_ulid')] 112 | funcs_with_tests = set([x.replace("test_", "") for x in test_methods]) 113 | for func in FUNCTIONS: 114 | self.assertTrue(func in funcs_with_tests, f"{func} does not have cooresponding test in {funcs_with_tests}") 115 | 116 | if __name__ == '__main__': 117 | unittest.main() -------------------------------------------------------------------------------- /tests/test-python.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import sqlite3 3 | import sqlite_ulid 4 | 5 | class TestSqliteUlidPython(unittest.TestCase): 6 | def test_path(self): 7 | db = sqlite3.connect(':memory:') 8 | db.enable_load_extension(True) 9 | 10 | self.assertEqual(type(sqlite_ulid.loadable_path()), str) 11 | 12 | sqlite_ulid.load(db) 13 | version, ulid = db.execute('select ulid_version(), ulid()').fetchone() 14 | self.assertEqual(version[0], "v") 15 | self.assertEqual(len(ulid), 26) 16 | 17 | if __name__ == '__main__': 18 | unittest.main() --------------------------------------------------------------------------------