Skip to content

Commit 19dd077

Browse files
authored
Support entrypoints for --load-extension (#473)
* Entrypoint support, closes #470
1 parent a46a5e3 commit 19dd077

6 files changed

Lines changed: 145 additions & 41 deletions

File tree

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ jobs:
3535
- name: Install SpatiaLite
3636
if: matrix.os == 'ubuntu-latest'
3737
run: sudo apt-get install libsqlite3-mod-spatialite
38+
- name: Build extension for --load-extension test
39+
if: matrix.os == 'ubuntu-latest'
40+
run: |-
41+
(cd tests && gcc ext.c -fPIC -shared -o ext.so && ls -lah)
3842
- name: Run tests
3943
run: |
4044
pytest -v

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ venv
1717
Pipfile
1818
Pipfile.lock
1919
pyproject.toml
20+
tests/*.dylib
21+
tests/*.so
22+
tests/*.dll

docs/cli-reference.rst

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ See :ref:`cli_query`.
121121
-p, --param <TEXT TEXT>... Named :parameters for SQL query
122122
--functions TEXT Python code defining one or more custom SQL
123123
functions
124-
--load-extension TEXT SQLite extensions to load
124+
--load-extension TEXT Path to SQLite extension, with optional
125+
:entrypoint
125126
-h, --help Show this message and exit.
126127

127128

@@ -189,7 +190,8 @@ See :ref:`cli_memory`.
189190
--dump Dump SQL for in-memory database
190191
--save FILE Save in-memory database to this file
191192
--analyze Analyze resulting tables and output results
192-
--load-extension TEXT SQLite extensions to load
193+
--load-extension TEXT Path to SQLite extension, with optional
194+
:entrypoint
193195
-h, --help Show this message and exit.
194196

195197

@@ -266,7 +268,7 @@ See :ref:`cli_inserting_data`, :ref:`cli_insert_csv_tsv`, :ref:`cli_insert_unstr
266268
--default <TEXT TEXT>... Default value that should be set for a column
267269
-d, --detect-types Detect types for columns in CSV/TSV data
268270
--analyze Run ANALYZE at the end of this operation
269-
--load-extension TEXT SQLite extensions to load
271+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
270272
--silent Do not show progress bar
271273
--ignore Ignore records if pk already exists
272274
--replace Replace records if pk already exists
@@ -320,7 +322,7 @@ See :ref:`cli_upsert`.
320322
--default <TEXT TEXT>... Default value that should be set for a column
321323
-d, --detect-types Detect types for columns in CSV/TSV data
322324
--analyze Run ANALYZE at the end of this operation
323-
--load-extension TEXT SQLite extensions to load
325+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
324326
--silent Do not show progress bar
325327
-h, --help Show this message and exit.
326328

@@ -364,7 +366,7 @@ See :ref:`cli_bulk`.
364366
--sniff Detect delimiter and quote character
365367
--no-headers CSV file has no header row
366368
--encoding TEXT Character encoding for input, defaults to utf-8
367-
--load-extension TEXT SQLite extensions to load
369+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
368370
-h, --help Show this message and exit.
369371

370372

@@ -404,7 +406,7 @@ See :ref:`cli_search`.
404406
textile, tsv, unsafehtml, youtrack
405407
--json-cols Detect JSON cols and output them as JSON, not escaped
406408
strings
407-
--load-extension TEXT SQLite extensions to load
409+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
408410
-h, --help Show this message and exit.
409411

410412

@@ -440,7 +442,7 @@ See :ref:`cli_transform_table`.
440442
--default-none TEXT Remove default from this column
441443
--drop-foreign-key TEXT Drop foreign key constraint for this column
442444
--sql Output SQL without executing it
443-
--load-extension TEXT SQLite extensions to load
445+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
444446
-h, --help Show this message and exit.
445447

446448

@@ -465,7 +467,7 @@ See :ref:`cli_extract`.
465467
--table TEXT Name of the other table to extract columns to
466468
--fk-column TEXT Name of the foreign key column to add to the table
467469
--rename <TEXT TEXT>... Rename this column in extracted table
468-
--load-extension TEXT SQLite extensions to load
470+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
469471
-h, --help Show this message and exit.
470472

471473

@@ -487,7 +489,7 @@ See :ref:`cli_schema`.
487489
sqlite-utils schema trees.db
488490

489491
Options:
490-
--load-extension TEXT SQLite extensions to load
492+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
491493
-h, --help Show this message and exit.
492494

493495

@@ -525,7 +527,7 @@ See :ref:`cli_insert_files`.
525527
--text Store file content as TEXT, not BLOB
526528
--encoding TEXT Character encoding for input, defaults to utf-8
527529
-s, --silent Don't show a progress bar
528-
--load-extension TEXT SQLite extensions to load
530+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
529531
-h, --help Show this message and exit.
530532

531533

@@ -549,7 +551,7 @@ See :ref:`cli_analyze_tables`.
549551
Options:
550552
-c, --column TEXT Specific columns to analyze
551553
--save Save results to _analyze_tables table
552-
--load-extension TEXT SQLite extensions to load
554+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
553555
-h, --help Show this message and exit.
554556

555557

@@ -656,7 +658,7 @@ See :ref:`cli_tables`.
656658
strings
657659
--columns Include list of columns for each table
658660
--schema Include schema for each table
659-
--load-extension TEXT SQLite extensions to load
661+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
660662
-h, --help Show this message and exit.
661663

662664

@@ -694,7 +696,7 @@ See :ref:`cli_views`.
694696
strings
695697
--columns Include list of columns for each view
696698
--schema Include schema for each view
697-
--load-extension TEXT SQLite extensions to load
699+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
698700
-h, --help Show this message and exit.
699701

700702

@@ -735,7 +737,8 @@ See :ref:`cli_rows`.
735737
simple, textile, tsv, unsafehtml, youtrack
736738
--json-cols Detect JSON cols and output them as JSON, not
737739
escaped strings
738-
--load-extension TEXT SQLite extensions to load
740+
--load-extension TEXT Path to SQLite extension, with optional
741+
:entrypoint
739742
-h, --help Show this message and exit.
740743

741744

@@ -770,7 +773,7 @@ See :ref:`cli_triggers`.
770773
textile, tsv, unsafehtml, youtrack
771774
--json-cols Detect JSON cols and output them as JSON, not escaped
772775
strings
773-
--load-extension TEXT SQLite extensions to load
776+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
774777
-h, --help Show this message and exit.
775778

776779

@@ -806,7 +809,7 @@ See :ref:`cli_indexes`.
806809
textile, tsv, unsafehtml, youtrack
807810
--json-cols Detect JSON cols and output them as JSON, not escaped
808811
strings
809-
--load-extension TEXT SQLite extensions to load
812+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
810813
-h, --help Show this message and exit.
811814

812815

@@ -830,7 +833,7 @@ See :ref:`cli_create_database`.
830833
Options:
831834
--enable-wal Enable WAL mode on the created database
832835
--init-spatialite Enable SpatiaLite on the created database
833-
--load-extension TEXT SQLite extensions to load
836+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
834837
-h, --help Show this message and exit.
835838

836839

@@ -862,7 +865,7 @@ See :ref:`cli_create_table`.
862865
foreign key
863866
--ignore If table already exists, do nothing
864867
--replace If table already exists, replace it
865-
--load-extension TEXT SQLite extensions to load
868+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
866869
-h, --help Show this message and exit.
867870

868871

@@ -892,7 +895,7 @@ See :ref:`cli_create_index`.
892895
--unique Make this a unique index
893896
--if-not-exists, --ignore Ignore if index already exists
894897
--analyze Run ANALYZE after creating the index
895-
--load-extension TEXT SQLite extensions to load
898+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
896899
-h, --help Show this message and exit.
897900

898901

@@ -920,7 +923,7 @@ See :ref:`cli_fts`.
920923
--create-triggers Create triggers to update the FTS tables when the
921924
parent table changes.
922925
--replace Replace existing FTS configuration if it exists
923-
--load-extension TEXT SQLite extensions to load
926+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
924927
-h, --help Show this message and exit.
925928

926929

@@ -940,7 +943,7 @@ populate-fts
940943
sqlite-utils populate-fts chickens.db chickens name
941944

942945
Options:
943-
--load-extension TEXT SQLite extensions to load
946+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
944947
-h, --help Show this message and exit.
945948

946949

@@ -960,7 +963,7 @@ rebuild-fts
960963
sqlite-utils rebuild-fts chickens.db chickens
961964

962965
Options:
963-
--load-extension TEXT SQLite extensions to load
966+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
964967
-h, --help Show this message and exit.
965968

966969

@@ -980,7 +983,7 @@ disable-fts
980983
sqlite-utils disable-fts chickens.db chickens
981984

982985
Options:
983-
--load-extension TEXT SQLite extensions to load
986+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
984987
-h, --help Show this message and exit.
985988

986989

@@ -1004,7 +1007,7 @@ See :ref:`cli_optimize`.
10041007

10051008
Options:
10061009
--no-vacuum Don't run VACUUM
1007-
--load-extension TEXT SQLite extensions to load
1010+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
10081011
-h, --help Show this message and exit.
10091012

10101013

@@ -1069,7 +1072,7 @@ See :ref:`cli_dump`.
10691072
sqlite-utils dump chickens.db
10701073

10711074
Options:
1072-
--load-extension TEXT SQLite extensions to load
1075+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
10731076
-h, --help Show this message and exit.
10741077

10751078

@@ -1097,7 +1100,7 @@ See :ref:`cli_add_column`.
10971100
omitted will automatically use the primary key
10981101
--not-null-default TEXT Add NOT NULL DEFAULT 'TEXT' constraint
10991102
--ignore If column already exists, do nothing
1100-
--load-extension TEXT SQLite extensions to load
1103+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
11011104
-h, --help Show this message and exit.
11021105

11031106

@@ -1123,7 +1126,7 @@ See :ref:`cli_add_foreign_key`.
11231126

11241127
Options:
11251128
--ignore If foreign key already exists, do nothing
1126-
--load-extension TEXT SQLite extensions to load
1129+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
11271130
-h, --help Show this message and exit.
11281131

11291132

@@ -1147,7 +1150,7 @@ See :ref:`cli_add_foreign_keys`.
11471150
authors country_id countries id
11481151

11491152
Options:
1150-
--load-extension TEXT SQLite extensions to load
1153+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
11511154
-h, --help Show this message and exit.
11521155

11531156

@@ -1169,7 +1172,7 @@ See :ref:`cli_index_foreign_keys`.
11691172
sqlite-utils index-foreign-keys chickens.db
11701173

11711174
Options:
1172-
--load-extension TEXT SQLite extensions to load
1175+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
11731176
-h, --help Show this message and exit.
11741177

11751178

@@ -1191,7 +1194,7 @@ See :ref:`cli_wal`.
11911194
sqlite-utils enable-wal chickens.db
11921195

11931196
Options:
1194-
--load-extension TEXT SQLite extensions to load
1197+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
11951198
-h, --help Show this message and exit.
11961199

11971200

@@ -1211,7 +1214,7 @@ disable-wal
12111214
sqlite-utils disable-wal chickens.db
12121215

12131216
Options:
1214-
--load-extension TEXT SQLite extensions to load
1217+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
12151218
-h, --help Show this message and exit.
12161219

12171220

@@ -1233,7 +1236,7 @@ See :ref:`cli_enable_counts`.
12331236
sqlite-utils enable-counts chickens.db
12341237

12351238
Options:
1236-
--load-extension TEXT SQLite extensions to load
1239+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
12371240
-h, --help Show this message and exit.
12381241

12391242

@@ -1253,7 +1256,7 @@ reset-counts
12531256
sqlite-utils reset-counts chickens.db
12541257

12551258
Options:
1256-
--load-extension TEXT SQLite extensions to load
1259+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
12571260
-h, --help Show this message and exit.
12581261

12591262

@@ -1270,7 +1273,7 @@ duplicate
12701273

12711274
Options:
12721275
--ignore If table does not exist, do nothing
1273-
--load-extension TEXT SQLite extensions to load
1276+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
12741277
-h, --help Show this message and exit.
12751278

12761279

@@ -1293,7 +1296,7 @@ See :ref:`cli_drop_table`.
12931296

12941297
Options:
12951298
--ignore If table does not exist, do nothing
1296-
--load-extension TEXT SQLite extensions to load
1299+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
12971300
-h, --help Show this message and exit.
12981301

12991302

@@ -1318,7 +1321,7 @@ See :ref:`cli_create_view`.
13181321
Options:
13191322
--ignore If view already exists, do nothing
13201323
--replace If view already exists, replace it
1321-
--load-extension TEXT SQLite extensions to load
1324+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
13221325
-h, --help Show this message and exit.
13231326

13241327

@@ -1341,7 +1344,7 @@ See :ref:`cli_drop_view`.
13411344

13421345
Options:
13431346
--ignore If view does not exist, do nothing
1344-
--load-extension TEXT SQLite extensions to load
1347+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
13451348
-h, --help Show this message and exit.
13461349

13471350

@@ -1372,7 +1375,8 @@ See :ref:`cli_spatialite`.
13721375
--dimensions TEXT Coordinate dimensions. Use XYZ for three-
13731376
dimensional geometries.
13741377
--not-null Add a NOT NULL constraint.
1375-
--load-extension TEXT SQLite extensions to load
1378+
--load-extension TEXT Path to SQLite extension, with optional
1379+
:entrypoint
13761380
-h, --help Show this message and exit.
13771381

13781382

@@ -1394,7 +1398,7 @@ See :ref:`cli_spatialite_indexes`.
13941398
paths. To load it from a specific path, use --load-extension.
13951399

13961400
Options:
1397-
--load-extension TEXT SQLite extensions to load
1401+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
13981402
-h, --help Show this message and exit.
13991403

14001404

sqlite_utils/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def load_extension_option(fn):
9494
return click.option(
9595
"--load-extension",
9696
multiple=True,
97-
help="SQLite extensions to load",
97+
help="Path to SQLite extension, with optional :entrypoint",
9898
)(fn)
9999

100100

@@ -2997,7 +2997,11 @@ def _load_extensions(db, load_extension):
29972997
for ext in load_extension:
29982998
if ext == "spatialite" and not os.path.exists(ext):
29992999
ext = find_spatialite()
3000-
db.conn.load_extension(ext)
3000+
if ":" in ext:
3001+
path, _, entrypoint = ext.partition(":")
3002+
db.conn.execute("SELECT load_extension(?, ?)", [path, entrypoint])
3003+
else:
3004+
db.conn.load_extension(ext)
30013005

30023006

30033007
def _register_functions(db, functions):

0 commit comments

Comments
 (0)