Skip to content

Commit 563af26

Browse files
committed
Refactoring, renamed module. Added wrapper routine.
1 parent 6ce0c2a commit 563af26

File tree

6 files changed

+56
-41
lines changed

6 files changed

+56
-41
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ LDFLAGS = -I$(PREFIX)/include/ -L$(PREFIX)/lib/
1111
LDLIBS = -lsqlite3
1212
ARFLAGS = rcs
1313
TARGET = libfortran-sqlite3.a
14-
TEST = test_sqlite
14+
TEST = test_sqlite3
1515

1616
.PHONY: all clean test
1717

1818
all: $(TARGET)
1919
test: $(TEST)
2020

2121
$(TARGET):
22-
$(FC) $(FFLAGS) -c src/sqlite_util.f90
23-
$(FC) $(FFLAGS) -c src/sqlite.f90
24-
$(AR) $(ARFLAGS) $(TARGET) sqlite_util.o sqlite.o
22+
$(FC) $(FFLAGS) -c src/sqlite3_util.f90
23+
$(FC) $(FFLAGS) -c src/sqlite3.f90
24+
$(AR) $(ARFLAGS) $(TARGET) sqlite3_util.o sqlite3.o
2525

2626
$(TEST): $(TARGET)
27-
$(FC) $(FFLAGS) $(LDFLAGS) -o test_sqlite test/test_sqlite.f90 $(TARGET) $(LDLIBS)
27+
$(FC) $(FFLAGS) $(LDFLAGS) -o test_sqlite3 test/test_sqlite3.f90 $(TARGET) $(LDLIBS)
2828

2929
clean:
3030
if [ `ls -1 *.mod 2>/dev/null | wc -l` -gt 0 ]; then rm *.mod; fi

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ convert C char pointer to Fortran allocatable character.
6464
! example.f90
6565
program example
6666
use, intrinsic :: iso_c_binding
67-
use :: sqlite
68-
use :: sqlite_util
67+
use :: sqlite3
68+
use :: sqlite3_util
6969
implicit none (type, external)
7070
character(len=:), allocatable :: errmsg
7171
integer :: rc

fpm.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name = "fortran-sqlite3"
2-
version = "0.1.0"
2+
version = "0.2.0"
33
license = "ISC"
44
author = "Philipp Engel"
55
maintainer = "@interkosmos"
6-
copyright = "Copyright (c) 2021, Philipp Engel"
6+
copyright = "Copyright (c) 2022, Philipp Engel"
77
description = "Fortran 2018 ISO C binding interfaces to SQLite 3"
8-
keywords = ["sqlite"]
8+
keywords = [ "sqlite", "sqlite3" ]
99

1010
[build]
1111
link = "sqlite3"
@@ -14,6 +14,6 @@ link = "sqlite3"
1414
source-dir = "src"
1515

1616
[[test]]
17-
name = "test_sqlite"
17+
name = "test_sqlite3"
1818
source-dir = "test"
19-
main = "test_sqlite.f90"
19+
main = "test_sqlite3.f90"

src/sqlite.f90 renamed to src/sqlite3.f90

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
! sqlite.f90
1+
! sqlite3.f90
22
!
33
! Fortran 2018 interface bindings to SQLite 3.
44
!
55
! Author: Philipp Engel
66
! Licence: ISC
7-
module sqlite
7+
module sqlite3
88
use, intrinsic :: iso_c_binding
99
use, intrinsic :: iso_fortran_env, only: i8 => int64
10-
use :: sqlite_util
10+
use :: sqlite3_util
1111
implicit none (type, external)
1212
private
1313

@@ -186,6 +186,7 @@ module sqlite
186186
public :: sqlite3_libversion_
187187
public :: sqlite3_libversion_number
188188
public :: sqlite3_log
189+
public :: sqlite3_log_
189190
public :: sqlite3_open
190191
public :: sqlite3_open_
191192
public :: sqlite3_prepare
@@ -498,10 +499,10 @@ function sqlite3_libversion_number() bind(c, name='sqlite3_libversion_number')
498499
end function sqlite3_libversion_number
499500

500501
! int sqlite3_open(const char *filename, sqlite3 **db)
501-
function sqlite3_open_(filename, db) bind(c, name='sqlite3_open')
502+
function sqlite3_open_(file_name, db) bind(c, name='sqlite3_open')
502503
import :: c_char, c_int, c_ptr
503504
implicit none
504-
character(kind=c_char), intent(in) :: filename
505+
character(kind=c_char), intent(in) :: file_name
505506
type(c_ptr), intent(inout) :: db
506507
integer(kind=c_int) :: sqlite3_open_
507508
end function sqlite3_open_
@@ -660,12 +661,12 @@ subroutine sqlite3_free(ptr) bind(c, name='sqlite3_free')
660661
end subroutine sqlite3_free
661662

662663
! void sqlite3_log(int iErrCode, const char *zFormat, ...)
663-
subroutine sqlite3_log(ierr_code, zformat) bind(c, name='sqlite3_log')
664+
subroutine sqlite3_log_(ierr_code, zformat) bind(c, name='sqlite3_log')
664665
import :: c_char, c_int
665666
implicit none
666667
integer(kind=c_int), intent(in), value :: ierr_code
667668
character(kind=c_char), intent(in) :: zformat
668-
end subroutine sqlite3_log
669+
end subroutine sqlite3_log_
669670

670671
! void sqlite3_str_append(sqlite3_str *str, const char *zIn, int N)
671672
subroutine sqlite3_str_append(str, zin, n) bind(c, name='slqite3_str_append')
@@ -830,12 +831,12 @@ function sqlite3_libversion()
830831
call c_f_str_ptr(ptr, sqlite3_libversion)
831832
end function sqlite3_libversion
832833

833-
function sqlite3_open(filename, db)
834-
character(len=*), intent(in) :: filename
834+
function sqlite3_open(file_name, db)
835+
character(len=*), intent(in) :: file_name
835836
type(c_ptr), intent(out) :: db
836837
integer :: sqlite3_open
837838

838-
sqlite3_open = sqlite3_open_(filename // c_null_char, db)
839+
sqlite3_open = sqlite3_open_(file_name // c_null_char, db)
839840
end function sqlite3_open
840841

841842
function sqlite3_prepare(db, sql, stmt)
@@ -874,4 +875,11 @@ function sqlite3_str_value(str)
874875
if (.not. c_associated(ptr)) return
875876
call c_f_str_ptr(ptr, sqlite3_str_value)
876877
end function sqlite3_str_value
877-
end module sqlite
878+
879+
subroutine sqlite3_log(ierr_code, zformat)
880+
integer, intent(in) :: ierr_code
881+
character(len=*), intent(in) :: zformat
882+
883+
call sqlite3_log_(ierr_code, zformat // c_null_char)
884+
end subroutine sqlite3_log
885+
end module sqlite3

src/sqlite_util.f90 renamed to src/sqlite3_util.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
! sqlite_util.f90
1+
! sqlite3_util.f90
22
!
33
! C interoperability interfaces and procedures.
44
!
55
! Author: Philipp Engel
66
! Licence: ISC
7-
module sqlite_util
7+
module sqlite3_util
88
use, intrinsic :: iso_c_binding
99
use, intrinsic :: iso_fortran_env, only: i8 => int64, r8 => real64
1010
implicit none (type, external)
@@ -47,4 +47,4 @@ subroutine c_f_str_ptr(c_str, f_str)
4747
allocate (character(len=sz) :: f_str)
4848
f_str = copy(ptrs)
4949
end subroutine c_f_str_ptr
50-
end module sqlite_util
50+
end module sqlite3_util

test/test_sqlite.f90 renamed to test/test_sqlite3.f90

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
! test_sqlite.f90
1+
! test_sqlite3.f90
22
module callbacks
33
use, intrinsic :: iso_c_binding
4-
use :: sqlite
5-
use :: sqlite_util
4+
use :: sqlite3
5+
use :: sqlite3_util
66
implicit none
77
private
88

@@ -79,14 +79,14 @@ subroutine update_callback(udp, type, db_name, tbl_name, rowid) bind(c)
7979
rowid, tbl_str, db_str
8080
end select
8181

82-
if (allocated(db_str)) deallocate(db_str)
83-
if (allocated(db_str)) deallocate(tbl_str)
82+
if (allocated(db_str)) deallocate (db_str)
83+
if (allocated(db_str)) deallocate (tbl_str)
8484
end subroutine update_callback
8585
end module callbacks
8686

87-
program test_sqlite
87+
program test_sqlite3
8888
use, intrinsic :: iso_c_binding
89-
use :: sqlite
89+
use :: sqlite3
9090
use :: callbacks
9191
implicit none (type, external)
9292
character(len=*), parameter :: DB_FILE = 'test.db'
@@ -118,7 +118,7 @@ program test_sqlite
118118
print '("DB name: ", a)', db_name
119119

120120
! Testing logging.
121-
call sqlite3_log(1, 'TEST LOG' // c_null_char)
121+
call sqlite3_log(1, 'TEST LOG')
122122

123123
! Enable WAL mode.
124124
print '("Turning WAL mode on ...")'
@@ -236,12 +236,19 @@ integer function journal_mode_wal(db) result(rc)
236236
end function journal_mode_wal
237237

238238
subroutine print_error(rc, func, errmsg)
239-
integer, intent(in) :: rc
240-
character(len=*), intent(in) :: func
241-
character(len=:), allocatable, intent(inout) :: errmsg
239+
integer, intent(in) :: rc
240+
character(len=*), intent(in) :: func
241+
character(len=:), allocatable, intent(inout) :: errmsg
242242

243-
if (rc /= SQLITE_OK) print '(a, "(): ", a)', trim(func), errmsg
244-
if (allocated(errmsg)) deallocate(errmsg)
243+
if (rc == SQLITE_OK) return
244+
245+
if (allocated(errmsg)) then
246+
print '(a, "(): ", a)', trim(func), errmsg
247+
deallocate (errmsg)
248+
return
249+
end if
250+
251+
print '(a, "(): unknown error")', trim(func)
245252
end subroutine print_error
246253

247254
subroutine print_values(stmt, ncols)
@@ -265,7 +272,7 @@ subroutine print_values(stmt, ncols)
265272
buf = sqlite3_column_text(stmt, i)
266273
if (allocated(buf)) then
267274
write (*, '(a12)', advance='no') buf
268-
deallocate(buf)
275+
deallocate (buf)
269276
end if
270277

271278
case default
@@ -275,4 +282,4 @@ subroutine print_values(stmt, ncols)
275282

276283
print *
277284
end subroutine print_values
278-
end program test_sqlite
285+
end program test_sqlite3

0 commit comments

Comments
 (0)