Skip to content

Commit 0917e3c

Browse files
committed
Added preprocessor macros.
1 parent 89e29d0 commit 0917e3c

File tree

6 files changed

+133
-89
lines changed

6 files changed

+133
-89
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
sudo apt-get install -y gcc-${GCC_V} gfortran-${GCC_V} make
2727
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-${GCC_V} 100 \
2828
--slave /usr/bin/gfortran gfortran /usr/bin/gfortran-${GCC_V}
29-
sudo apt-get remove -y sqlite3 libsqlite3-0 libsqlite3-dev
29+
sudo apt-get remove -y sqlite3 libsqlite3-dev
3030
3131
- name: Build and Install SQLite 3 (Linux)
3232
if: contains(matrix.os, 'ubuntu')

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ RELEASE = -O2 -march=native
1111

1212
CFLAGS = $(RELEASE)
1313
FFLAGS = $(RELEASE)
14+
PPFLAGS = -DSQLITE_ENABLE_COLUMN_METADATA=0
1415
LDFLAGS = -I$(PREFIX)/include -L$(PREFIX)/lib
1516
LDLIBS = -lsqlite3
1617
ARFLAGS = rcs
1718
TARGET = libfortran-sqlite3.a
19+
SRC = src/sqlite3_macro.c src/sqlite3.F90 src/sqlite3_util.f90
20+
OBJ = sqlite3.o sqlite3_macro.o sqlite3_util.o
1821

1922
.PHONY: all clean test
2023

2124
all: $(TARGET)
2225

2326
test: test_sqlite3
2427

25-
$(TARGET): src/sqlite3_util.f90 src/sqlite3.f90
28+
$(TARGET): $(SRC)
2629
$(CC) $(CFLAGS) -c src/sqlite3_macro.c
2730
$(FC) $(FFLAGS) -c src/sqlite3_util.f90
28-
$(FC) $(FFLAGS) -c src/sqlite3.f90
29-
$(AR) $(ARFLAGS) $(TARGET) sqlite3.o sqlite3_macro.o sqlite3_util.o
31+
$(FC) $(FFLAGS) $(PPFLAGS) -c src/sqlite3.F90
32+
$(AR) $(ARFLAGS) $(TARGET) $(OBJ)
3033

3134
test_sqlite3: $(TARGET) test/test_sqlite3.f90
3235
$(FC) $(FFLAGS) $(LDFLAGS) -o test_sqlite3 test/test_sqlite3.f90 $(TARGET) $(LDLIBS)
@@ -36,3 +39,4 @@ clean:
3639
if [ `ls -1 *.o 2>/dev/null | wc -l` -gt 0 ]; then rm *.o; fi
3740
if [ -e $(TARGET) ]; then rm $(TARGET); fi
3841
if [ -e test_sqlite3 ]; then rm test_sqlite3; fi
42+
if [ -e test.sqlite ]; then rm test.sqlite; fi

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ run:
1212
# pkg install databases/sqlite3
1313
```
1414

15+
On Linux:
16+
17+
```
18+
# apt-get install sqlite3 libsqlite3-0 libsqlite3-dev
19+
```
20+
1521
Then, clone the GitHub repository:
1622

1723
```
@@ -40,9 +46,27 @@ Execute the provided `Makefile`:
4046
$ make
4147
```
4248

49+
Pass the preprocessor flag `-DSQLITE_ENABLE_COLUMN_METADATA=1` to add interface
50+
bindings to column meta data functions:
51+
52+
```
53+
$ make PPFLAGS="-DSQLITE_ENABLE_COLUMN_METADATA=1"
54+
```
55+
4356
You may want to override the default compilers by passing the arguments `CC` (C
4457
compiler) and `FC` (Fortran compiler).
4558

59+
## Source-Code Documentation
60+
61+
Use [FORD](https://github.com/Fortran-FOSS-Programmers/ford) to generate the
62+
source-code documentation:
63+
64+
```
65+
$ ford ford.md
66+
```
67+
68+
The output files are written to `doc/`.
69+
4670
## Example
4771

4872
The following SQL schema will be created by the example:

ford.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ project: fortran-sqlite3
22
summary: A collection of ISO C binding interfaces to SQLite 3.
33
author: Philipp Engel
44
project_github: https://github.com/interkosmos/fortran-sqlite3
5+
macro: SQLITE_ENABLE_COLUMN_METADATA=1

src/sqlite3.f90 renamed to src/sqlite3.F90

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,10 @@ module sqlite3
193193
public :: sqlite3_close
194194
public :: sqlite3_close_
195195
public :: sqlite3_column_count
196-
public :: sqlite3_column_database_name
197-
public :: sqlite3_column_database_name_
198196
public :: sqlite3_column_double
199197
public :: sqlite3_column_int
200198
public :: sqlite3_column_int64
201199
public :: sqlite3_column_name
202-
public :: sqlite3_column_origin_name
203-
public :: sqlite3_column_origin_name_
204-
public :: sqlite3_column_table_name
205-
public :: sqlite3_column_table_name_
206200
public :: sqlite3_column_text
207201
public :: sqlite3_column_type
208202
public :: sqlite3_config
@@ -264,6 +258,21 @@ module sqlite3
264258
public :: sqlite3_threadsafe
265259
public :: sqlite3_update_hook
266260

261+
#if SQLITE_ENABLE_COLUMN_METADATA
262+
public :: sqlite3_column_database_name
263+
public :: sqlite3_column_database_name_
264+
public :: sqlite3_column_table_name
265+
public :: sqlite3_column_table_name_
266+
public :: sqlite3_column_origin_name
267+
public :: sqlite3_column_origin_name_
268+
#endif
269+
270+
interface sqlite3_config
271+
module procedure :: sqlite3_config_funptr_ptr
272+
module procedure :: sqlite3_config_int
273+
module procedure :: sqlite3_config_null
274+
end interface
275+
267276
interface
268277
! int sqlite3_backup_finish(sqlite3_backup *p)
269278
function sqlite3_backup_finish(p) bind(c, name='sqlite3_backup_finish')
@@ -420,15 +429,6 @@ function sqlite3_column_count(stmt) bind(c, name='sqlite3_column_count')
420429
integer(kind=c_int) :: sqlite3_column_count
421430
end function sqlite3_column_count
422431

423-
! const char *sqlite3_column_database_name(sqlite3_stmt *stmt, int idx)
424-
function sqlite3_column_database_name_(stmt, idx) bind(c, name='sqlite3_column_database_name')
425-
import :: c_int, c_ptr
426-
implicit none
427-
type(c_ptr), intent(in), value :: stmt
428-
integer(kind=c_int), intent(in), value :: idx
429-
type(c_ptr) :: sqlite3_column_database_name_
430-
end function sqlite3_column_database_name_
431-
432432
! double sqlite3_column_double(sqlite3_stmt *stmt, int idx)
433433
function sqlite3_column_double(stmt, idx) bind(c, name='sqlite3_column_double')
434434
import :: c_double, c_int, c_ptr
@@ -465,24 +465,6 @@ function sqlite3_column_name_(stmt, idx) bind(c, name='sqlite3_column_name')
465465
type(c_ptr) :: sqlite3_column_name_
466466
end function sqlite3_column_name_
467467

468-
! const char *sqlite3_column_origin_name(sqlite3_stmt *stmt, int idx)
469-
function sqlite3_column_origin_name_(stmt, idx) bind(c, name='sqlite3_column_origin_name')
470-
import :: c_int, c_ptr
471-
implicit none
472-
type(c_ptr), intent(in), value :: stmt
473-
integer(kind=c_int), intent(in), value :: idx
474-
type(c_ptr) :: sqlite3_column_origin_name_
475-
end function sqlite3_column_origin_name_
476-
477-
! const char *sqlite3_column_table_name(sqlite3_stmt *stmt, int idx)
478-
function sqlite3_column_table_name_(stmt, idx) bind(c, name='sqlite3_column_table_name')
479-
import :: c_int, c_ptr
480-
implicit none
481-
type(c_ptr), intent(in), value :: stmt
482-
integer(kind=c_int), intent(in), value :: idx
483-
type(c_ptr) :: sqlite3_column_table_name_
484-
end function sqlite3_column_table_name_
485-
486468
! const unsigned char *sqlite3_column_text(sqlite3_stmt *stmt, int idx)
487469
function sqlite3_column_text_(stmt, idx) bind(c, name='sqlite3_column_text')
488470
import :: c_int, c_ptr
@@ -898,11 +880,36 @@ subroutine sqlite3_str_reset(str) bind(c, name='slqite3_str_reset')
898880
end subroutine sqlite3_str_reset
899881
end interface
900882

901-
interface sqlite3_config
902-
module procedure :: sqlite3_config_funptr_ptr
903-
module procedure :: sqlite3_config_int
904-
module procedure :: sqlite3_config_null
883+
#if SQLITE_ENABLE_COLUMN_METADATA
884+
interface
885+
! const char *sqlite3_column_database_name(sqlite3_stmt *stmt, int idx)
886+
function sqlite3_column_database_name_(stmt, idx) bind(c, name='sqlite3_column_database_name')
887+
import :: c_int, c_ptr
888+
implicit none
889+
type(c_ptr), intent(in), value :: stmt
890+
integer(kind=c_int), intent(in), value :: idx
891+
type(c_ptr) :: sqlite3_column_database_name_
892+
end function sqlite3_column_database_name_
893+
894+
! const char *sqlite3_column_origin_name(sqlite3_stmt *stmt, int idx)
895+
function sqlite3_column_origin_name_(stmt, idx) bind(c, name='sqlite3_column_origin_name')
896+
import :: c_int, c_ptr
897+
implicit none
898+
type(c_ptr), intent(in), value :: stmt
899+
integer(kind=c_int), intent(in), value :: idx
900+
type(c_ptr) :: sqlite3_column_origin_name_
901+
end function sqlite3_column_origin_name_
902+
903+
! const char *sqlite3_column_table_name(sqlite3_stmt *stmt, int idx)
904+
function sqlite3_column_table_name_(stmt, idx) bind(c, name='sqlite3_column_table_name')
905+
import :: c_int, c_ptr
906+
implicit none
907+
type(c_ptr), intent(in), value :: stmt
908+
integer(kind=c_int), intent(in), value :: idx
909+
type(c_ptr) :: sqlite3_column_table_name_
910+
end function sqlite3_column_table_name_
905911
end interface
912+
#endif
906913
contains
907914
function sqlite3_backup_init(dest, dest_name, source, source_name)
908915
type(c_ptr), intent(in) :: dest
@@ -950,16 +957,6 @@ function sqlite3_close(db)
950957
if (sqlite3_close == SQLITE_OK) db = c_null_ptr
951958
end function sqlite3_close
952959

953-
function sqlite3_column_database_name(stmt, idx)
954-
type(c_ptr), intent(inout) :: stmt
955-
integer, intent(in) :: idx
956-
character(len=:), allocatable :: sqlite3_column_database_name
957-
type(c_ptr) :: ptr
958-
959-
ptr = sqlite3_column_database_name_(stmt, idx)
960-
call c_f_str_ptr(ptr, sqlite3_column_database_name)
961-
end function sqlite3_column_database_name
962-
963960
function sqlite3_column_name(stmt, idx)
964961
type(c_ptr), intent(inout) :: stmt
965962
integer, intent(in) :: idx
@@ -970,26 +967,6 @@ function sqlite3_column_name(stmt, idx)
970967
call c_f_str_ptr(ptr, sqlite3_column_name)
971968
end function sqlite3_column_name
972969

973-
function sqlite3_column_origin_name(stmt, idx)
974-
type(c_ptr), intent(inout) :: stmt
975-
integer, intent(in) :: idx
976-
character(len=:), allocatable :: sqlite3_column_origin_name
977-
type(c_ptr) :: ptr
978-
979-
ptr = sqlite3_column_origin_name_(stmt, idx)
980-
call c_f_str_ptr(ptr, sqlite3_column_origin_name)
981-
end function sqlite3_column_origin_name
982-
983-
function sqlite3_column_table_name(stmt, idx)
984-
type(c_ptr), intent(inout) :: stmt
985-
integer, intent(in) :: idx
986-
character(len=:), allocatable :: sqlite3_column_table_name
987-
type(c_ptr) :: ptr
988-
989-
ptr = sqlite3_column_table_name_(stmt, idx)
990-
call c_f_str_ptr(ptr, sqlite3_column_table_name)
991-
end function sqlite3_column_table_name
992-
993970
function sqlite3_column_text(stmt, idx)
994971
type(c_ptr), intent(inout) :: stmt
995972
integer, intent(in) :: idx
@@ -1173,4 +1150,36 @@ subroutine sqlite3_log(ierr_code, str)
11731150

11741151
call sqlite3_log_(ierr_code, str // c_null_char)
11751152
end subroutine sqlite3_log
1153+
1154+
#if SQLITE_ENABLE_COLUMN_METADATA
1155+
function sqlite3_column_database_name(stmt, idx)
1156+
type(c_ptr), intent(inout) :: stmt
1157+
integer, intent(in) :: idx
1158+
character(len=:), allocatable :: sqlite3_column_database_name
1159+
type(c_ptr) :: ptr
1160+
1161+
ptr = sqlite3_column_database_name_(stmt, idx)
1162+
call c_f_str_ptr(ptr, sqlite3_column_database_name)
1163+
end function sqlite3_column_database_name
1164+
1165+
function sqlite3_column_origin_name(stmt, idx)
1166+
type(c_ptr), intent(inout) :: stmt
1167+
integer, intent(in) :: idx
1168+
character(len=:), allocatable :: sqlite3_column_origin_name
1169+
type(c_ptr) :: ptr
1170+
1171+
ptr = sqlite3_column_origin_name_(stmt, idx)
1172+
call c_f_str_ptr(ptr, sqlite3_column_origin_name)
1173+
end function sqlite3_column_origin_name
1174+
1175+
function sqlite3_column_table_name(stmt, idx)
1176+
type(c_ptr), intent(inout) :: stmt
1177+
integer, intent(in) :: idx
1178+
character(len=:), allocatable :: sqlite3_column_table_name
1179+
type(c_ptr) :: ptr
1180+
1181+
ptr = sqlite3_column_table_name_(stmt, idx)
1182+
call c_f_str_ptr(ptr, sqlite3_column_table_name)
1183+
end function sqlite3_column_table_name
1184+
#endif
11761185
end module sqlite3

test/test_sqlite3.f90

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ integer(kind=c_int) function exec_callback(client_data, argc, argv, cols) bind(c
1818
integer(kind=c_int), intent(in), value :: argc
1919
type(c_ptr), intent(in) :: argv(*)
2020
type(c_ptr), intent(in) :: cols(*)
21-
character(len=:), allocatable :: buf
22-
integer :: i
21+
22+
character(len=:), allocatable :: buf
23+
integer :: i
2324

2425
exec_callback = 1 ! No more rows on error.
2526

@@ -41,7 +42,8 @@ subroutine error_log_callback(udp, err_code, err_msg) bind(c)
4142
type(c_ptr), intent(in), value :: udp
4243
integer(kind=c_int), intent(in), value :: err_code
4344
type(c_ptr), intent(in), value :: err_msg
44-
character(len=:), allocatable :: msg
45+
46+
character(len=:), allocatable :: msg
4547

4648
call c_f_str_ptr(err_msg, msg)
4749
print '(a)', repeat('-', 64)
@@ -60,7 +62,8 @@ subroutine update_callback(udp, type, db_name, tbl_name, rowid) bind(c)
6062
type(c_ptr), intent(in), value :: db_name
6163
type(c_ptr), intent(in), value :: tbl_name
6264
integer(kind=c_int64_t), intent(in), value :: rowid
63-
character(len=:), allocatable :: db_str, tbl_str
65+
66+
character(len=:), allocatable :: db_str, tbl_str
6467

6568
call c_f_str_ptr(db_name, db_str)
6669
call c_f_str_ptr(tbl_name, tbl_str)
@@ -89,15 +92,16 @@ program test_sqlite3
8992
use :: sqlite3
9093
use :: callbacks
9194
implicit none (type, external)
92-
character(len=*), parameter :: DB_FILE = 'test.db'
95+
character(len=*), parameter :: DB_FILE = 'test.sqlite'
9396
character(len=*), parameter :: DB_TABLE = 'test_table'
9497

9598
character(len=:), allocatable :: db_name ! Database name.
9699
character(len=:), allocatable :: errmsg ! Error message.
97-
integer :: rc ! Return code.
98-
type(c_ptr) :: db ! SQLite database.
99-
type(c_ptr) :: stmt ! SQLite statement.
100-
type(c_ptr) :: udp ! User-data pointer.
100+
101+
integer :: rc ! Return code.
102+
type(c_ptr) :: db ! SQLite database.
103+
type(c_ptr) :: stmt ! SQLite statement.
104+
type(c_ptr) :: udp ! User-data pointer.
101105

102106
! Set configuration to single thread.
103107
rc = sqlite3_config(SQLITE_CONFIG_SINGLETHREAD)
@@ -211,10 +215,11 @@ program test_sqlite3
211215
contains
212216
integer function journal_mode_wal(db) result(rc)
213217
!! Enables WAL mode.
214-
type(c_ptr), intent(inout) :: db
215-
character(len=:), allocatable :: buf
216-
integer :: err
217-
type(c_ptr) :: stmt
218+
type(c_ptr), intent(inout) :: db
219+
220+
character(len=:), allocatable :: buf
221+
integer :: err
222+
type(c_ptr) :: stmt
218223

219224
rc = -1
220225

@@ -252,11 +257,12 @@ subroutine print_error(rc, func, errmsg)
252257
end subroutine print_error
253258

254259
subroutine print_values(stmt, ncols)
255-
type(c_ptr), intent(inout) :: stmt
256-
integer, intent(in) :: ncols
257-
integer :: col_type
258-
integer :: i
259-
character(len=:), allocatable :: buf
260+
type(c_ptr), intent(inout) :: stmt
261+
integer, intent(in) :: ncols
262+
263+
integer :: col_type
264+
integer :: i
265+
character(len=:), allocatable :: buf
260266

261267
do i = 0, ncols - 1
262268
col_type = sqlite3_column_type(stmt, i)

0 commit comments

Comments
 (0)