1% Copyright (C) 2026 Kocoy Group and AsaDB contributors
    2% SPDX-License-Identifier: GPL-3.0-only
    3/*
    4  Public SWI-Prolog pack API for AsaDB.
    5
    6  Load this module after installation with:
    7
    8      :- use_module(library(asadb)).
    9
   10  The command-line and web entrypoints remain in src/asadb.pl and
   11  src/asadb_web.pl.  This module deliberately exposes a small, stable API for
   12  programs that embed AsaDB instead of loading an entrypoint with side effects.
   13*/
   14
   15:- module(asadb, [
   16    asadb_version/1,
   17    asadb_boot/1,
   18    asadb_warmup/0,
   19    asadb_shutdown/0,
   20    asadb_save/0,
   21    asadb_exec_sql/2,
   22    asadb_exec_sql_limited/3,
   23    asadb_exec_sql_page/4,
   24    asadb_parse_sql/2,
   25    asadb_analyze_sql/2,
   26    asadb_current_database/1,
   27    asadb_storage_stats/1,
   28    asadb_database_metadata/1,
   29    asadb_result_json/2,
   30    asadb_interchange_export/5,
   31    asadb_interchange_prepare_import/7,
   32    asadb_interchange_detect_format/3,
   33    asadb_interchange_cleanup/1
   34]).   35
   36:- use_module(library(readutil)).   37
   38:- reexport('../src/asadb_core.pl', [
   39    asadb_boot/1,
   40    asadb_warmup/0,
   41    asadb_shutdown/0,
   42    asadb_save/0,
   43    asadb_exec_sql/2,
   44    asadb_exec_sql_limited/3,
   45    asadb_exec_sql_page/4,
   46    asadb_parse_sql/2,
   47    asadb_analyze_sql/2,
   48    asadb_current_database/1,
   49    asadb_storage_stats/1,
   50    asadb_database_metadata/1,
   51    asadb_result_json/2
   52]).   53
   54:- reexport('../src/asadb_interchange.pl', [
   55    asadb_interchange_export/5,
   56    asadb_interchange_prepare_import/7,
   57    asadb_interchange_detect_format/3,
   58    asadb_interchange_cleanup/1
   59]).
 asadb_version(-Version:atom) is det
Version declared by the installed source tree. Reading VERSION instead of duplicating the value in code keeps this API aligned with release and pack metadata checks.
   66asadb_version(Version) :-
   67    source_file(asadb:asadb_version(_), ThisFile),
   68    file_directory_name(ThisFile, PrologDirectory),
   69    directory_file_path(PrologDirectory, '..', ParentDirectory),
   70    absolute_file_name(ParentDirectory, Root, [file_type(directory)]),
   71    directory_file_path(Root, 'VERSION', VersionFile),
   72    setup_call_cleanup(
   73        open(VersionFile, read, In, [encoding(utf8)]),
   74        read_string(In, _, VersionText),
   75        close(In)
   76    ),
   77    normalize_space(string(Normalized), VersionText),
   78    atom_string(Version, Normalized)