-
Notifications
You must be signed in to change notification settings - Fork 1
/
persistent_collection_m.f90
189 lines (141 loc) · 5.94 KB
/
persistent_collection_m.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
module persistent_collection_m
use log_file_m
use persistent_object_m
use sqlite
implicit none
private
type, public :: persistent_collection_t
private
character(:), allocatable :: db_name_m
character(:), allocatable :: table_name_m
character(:), allocatable :: object_name_m
type(SQLITE_COLUMN), dimension(:), pointer, public :: column_m
contains
procedure, public, pass(this) :: set_db_name, &
set_table_name, &
set_object_name, &
add_db_column, &
add_db_char_column, &
add_db_int_column, &
get_object_name, &
get_row_count, &
get_collection_size, &
map_object, &
read_all, &
read_one, &
write_json, &
write_xml
end type persistent_collection_t
contains
subroutine set_db_name(this, db_name)
class(persistent_collection_t) :: this
character(len=*) :: db_name
this%db_name_m = db_name
end subroutine set_db_name
subroutine set_table_name(this, table_name)
class(persistent_collection_t) :: this
character(len=*) :: table_name
this%table_name_m = table_name
end subroutine set_table_name
subroutine set_object_name(this, object_name)
class(persistent_collection_t) :: this
character(len=*) :: object_name
this%object_name_m = object_name
end subroutine set_object_name
character(len=80) function get_object_name(this)
class(persistent_collection_t) :: this
character(len=80) :: temp_name
temp_name = this%object_name_m
get_object_name = temp_name
end function get_object_name
integer function get_row_count(this)
class(persistent_collection_t) :: this
character(len=80) :: table
type(SQLITE_DATABASE) :: db
type(SQLITE_COLUMN), dimension(:), pointer :: column
type(SQLITE_STATEMENT) :: stmt
logical :: finished
character(len=80) :: query
call sqlite3_open(this%db_name_m, db)
query = 'select count(*) as the_count from ' // trim(this%table_name_m)
allocate(column(1))
call sqlite3_column_query(column(1), 'the_count', SQLITE_INT)
call sqlite3_prepare(db, query, stmt, column)
call sqlite3_next_row(stmt, column, finished)
get_row_count = 0
if (.NOT. finished) call sqlite3_get_column(column(1), get_row_count)
deallocate(column)
call sqlite3_finalize(stmt)
call sqlite3_close(db)
end function get_row_count
subroutine add_db_char_column(this, col_name)
class(persistent_collection_t), intent(inout) :: this
character(len=*) :: col_name
call add_db_column(this, col_name, SQLITE_CHAR)
end subroutine add_db_char_column
subroutine add_db_int_column(this, col_name)
class(persistent_collection_t), intent(inout) :: this
character(len=*) :: col_name
call add_db_column(this, col_name, SQLITE_INT)
end subroutine add_db_int_column
subroutine add_db_column(this, col_name, col_type)
class(persistent_collection_t), intent(inout) :: this
character(len=*) :: col_name
integer :: col_type
type(SQLITE_COLUMN), dimension(:), pointer :: temp_column
if (associated(this%column_m)) then
allocate(temp_column(size(this%column_m)+1))
temp_column = this%column_m
deallocate(this%column_m)
allocate(this%column_m(size(temp_column)))
this%column_m = temp_column
deallocate(temp_column)
else
allocate(this%column_m(1))
end if
call sqlite3_column_query(this%column_m(size(this%column_m)), &
trim(col_name), col_type)
end subroutine add_db_column
subroutine map_object(this)
class(persistent_collection_t), intent(inout) :: this
end subroutine map_object
integer function get_collection_size(this)
class(persistent_collection_t), intent(in) :: this
end function get_collection_size
subroutine read_all(this)
class(persistent_collection_t), intent(inout) :: this
type(SQLITE_DATABASE) :: db
type(SQLITE_STATEMENT) :: stmt
logical :: finished
call sqlite3_open(this%db_name_m, db)
call sqlite3_prepare_select(db, this%table_name_m, this%column_m, stmt)
do
call sqlite3_next_row(stmt, this%column_m, finished)
if (finished) exit
call this%map_object
enddo
call sqlite3_close(db)
end subroutine read_all
subroutine read_one(this, id)
class(persistent_collection_t), intent(inout) :: this
integer :: id
type(SQLITE_DATABASE) :: db
type(SQLITE_STATEMENT) :: stmt
logical :: finished
character(len=80) :: where_clause
write (where_clause, *) 'where id=', id
call sqlite3_open(this%db_name_m, db)
call sqlite3_prepare_select(db, this%table_name_m, this%column_m, &
stmt, where_clause)
call sqlite3_next_row(stmt, this%column_m, finished)
if (.NOT. finished) call this%map_object
call sqlite3_finalize(stmt)
call sqlite3_close(db)
end subroutine read_one
subroutine write_json(this)
class(persistent_collection_t), intent(inout) :: this
end subroutine write_json
subroutine write_xml(this)
class(persistent_collection_t), intent(inout) :: this
end subroutine write_xml
end module persistent_collection_m