Fix db3 related calls.

This commit is contained in:
zongor 2023-09-10 11:45:42 -04:00
parent d81fdb055a
commit 7e2859ffc0
3 changed files with 84 additions and 47 deletions

Binary file not shown.

View File

@ -1,5 +1,6 @@
program main program main
use www use www
use db
implicit none implicit none
character, dimension(:), allocatable :: form_data character, dimension(:), allocatable :: form_data
@ -21,7 +22,7 @@ program main
call request(form_data, length) call request(form_data, length)
if (length .gt. 0) then if (length .gt. 0) then
inquire (file=trim(adjustl(db_path)), exist=exist) inquire (file=db_path(:Len_Trim(db_path)), exist=exist)
if (.not. exist) then if (.not. exist) then
call create_db(db_path) call create_db(db_path)
end if end if
@ -39,17 +40,24 @@ contains
subroutine create_db(db_path) subroutine create_db(db_path)
character(len=128), intent(in) :: db_path character(len=128), intent(in) :: db_path
character(len=:), allocatable :: command
command = 'sqlite3 '//trim(adjustl(db_path)) & ! character(len=:), allocatable :: command
//' ''CREATE TABLE users ' & type(db_type) :: db
//'(id INTEGER PRIMARY KEY ASC, ' & integer :: rc
//'username TEXT, password TEXT, ' &
//'apperance TEXT, x_pos INTEGER, ' & rc = db_open(db, db_path(:Len_Trim(db_path)))
//'y_pos INTEGER, last_login INTEGER, ' & rc = db_create_users(db)
//' created INTEGER);''' rc = db_close(db)
write (12, *) command
call execute_command_line(command) ! command = 'sqlite3 '//db_path &
! //' ''CREATE TABLE users ' &
! //'(id INTEGER PRIMARY KEY ASC, ' &
! //'username TEXT, password TEXT, ' &
! //'apperance TEXT, x_pos INTEGER, ' &
! //'y_pos INTEGER, last_login INTEGER, ' &
! //' created INTEGER);'''
! write (12, *) command
! call execute_command_line(command)
end subroutine create_db end subroutine create_db
@ -65,7 +73,10 @@ contains
character(len=8) :: appearance character(len=8) :: appearance
logical :: start = .false. logical :: start = .false.
character(len=:), allocatable :: command type(db_type) :: db
integer :: rc
! character(len=:), allocatable :: command
j = 1 j = 1
get_username: do i = 1, length get_username: do i = 1, length
@ -85,7 +96,7 @@ contains
j = j + 1 j = j + 1
end if end if
end do get_username end do get_username
username_len = j-2 username_len = j - 2
j = 1 j = 1
get_password: do i = e_idx + 2, length get_password: do i = e_idx + 2, length
@ -105,7 +116,7 @@ contains
j = j + 1 j = j + 1
end if end if
end do get_password end do get_password
password_len = j-2 password_len = j - 2
get_appearance: do i = e_idx + 2, length get_appearance: do i = e_idx + 2, length
if (request(i) .eq. '=') then if (request(i) .eq. '=') then
@ -114,19 +125,22 @@ contains
end do get_appearance end do get_appearance
appearance = transfer(request(s_idx:length), appearance) appearance = transfer(request(s_idx:length), appearance)
write (created, *) time() ! write (created, *) time()
! command = 'sqlite3 '//db_path(:Len_Trim(db_path)) &
! //' "INSERT INTO users (username,password' &
! //',apperance,x_pos,y_pos,last_login,created) VALUES (''' &
! //username(:username_len) &
! //''',''' &
! //password(:password_len) &
! //''',''' &
! //appearance(:Len_Trim(appearance)) &
! //''',0,0,0,' &
! //created(:Len_Trim(created))//');"'
! call execute_command_line(command)
command = 'sqlite3 '//db_path(:Len_Trim(db_path)) & rc = db_open(db, db_path(:Len_Trim(db_path)))
//' "INSERT INTO users (username,password' & rc = db_add_user(db, username(:username_len), password(:password_len), appearance(:Len_Trim(appearance)), 0, 0, 0, time())
//',apperance,x_pos,y_pos,last_login,created) VALUES (''' & rc = db_close(db)
// username(:username_len) &
//''',''' &
// password(:password_len) &
//''',''' &
// appearance(:Len_Trim(appearance)) &
//''',0,0,0,' &
//created(:Len_Trim(created))//');"'
call execute_command_line(command)
end subroutine add_user end subroutine add_user

View File

@ -65,12 +65,12 @@ contains
if (rc /= SQLITE_OK) return if (rc /= SQLITE_OK) return
end function db_create_users end function db_create_users
integer function db_add_user(db, name) result(rc) integer function db_add_user(db, username, password, apperance, x_pos, y_pos, last_login, created) result(rc)
!! Adds student to database. !! Adds student to database.
type(db_type), intent(inout) :: db type(db_type), intent(inout) :: db
character(len=:), intent(in) :: username character(len=*), intent(in) :: username
character(len=:), intent(in) :: password character(len=*), intent(in) :: password
character(len=:), intent(in) :: apperance character(len=*), intent(in) :: apperance
integer, intent(in) :: x_pos integer, intent(in) :: x_pos
integer, intent(in) :: y_pos integer, intent(in) :: y_pos
integer, intent(in) :: last_login integer, intent(in) :: last_login
@ -78,7 +78,7 @@ contains
type(c_ptr) :: stmt type(c_ptr) :: stmt
! Insert values through prepared statement. ! Insert values through prepared statement.
rc = sqlite3_prepare_v2(db%ptr, "INSERT INTO users(username, password, " rc = sqlite3_prepare_v2(db%ptr, "INSERT INTO users(username, password, " &
//"apperance, x_pos, y_pos, last_login, created) VALUES (?,?,?,?,?,?,?)", stmt) //"apperance, x_pos, y_pos, last_login, created) VALUES (?,?,?,?,?,?,?)", stmt)
call db_error(rc, 'sqlite3_prepare_v2()') call db_error(rc, 'sqlite3_prepare_v2()')
@ -88,14 +88,14 @@ contains
call db_error(rc, 'sqlite3_bind_text()') call db_error(rc, 'sqlite3_bind_text()')
rc = sqlite3_bind_text(stmt, 3, apperance) rc = sqlite3_bind_text(stmt, 3, apperance)
call db_error(rc, 'sqlite3_bind_text()') call db_error(rc, 'sqlite3_bind_text()')
rc = sqlite3_bind_integer(stmt, 4, x_pos) rc = sqlite3_bind_int(stmt, 4, x_pos)
call db_error(rc, 'sqlite3_bind_integer()') call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_integer(stmt, 5, y_pos) rc = sqlite3_bind_int(stmt, 5, y_pos)
call db_error(rc, 'sqlite3_bind_integer()') call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_integer(stmt, 6, last_login) rc = sqlite3_bind_int(stmt, 6, last_login)
call db_error(rc, 'sqlite3_bind_integer()') call db_error(rc, 'sqlite3_bind_int()')
rc = sqlite3_bind_integer(stmt, 7, created) rc = sqlite3_bind_int(stmt, 7, created)
call db_error(rc, 'sqlite3_bind_integer()') call db_error(rc, 'sqlite3_bind_int()')
! Insert bound value into database. ! Insert bound value into database.
rc = sqlite3_step(stmt) rc = sqlite3_step(stmt)
@ -104,7 +104,30 @@ contains
! Clean-up prepared statement. ! Clean-up prepared statement.
rc = sqlite3_finalize(stmt) rc = sqlite3_finalize(stmt)
call db_error(rc, 'sqlite3_finalize()') call db_error(rc, 'sqlite3_finalize()')
end function db_add_student end function db_add_user
integer function db_delete_user(db, username) result(rc)
!! Adds student to database.
type(db_type), intent(inout) :: db
character(len=*), intent(in) :: username
type(c_ptr) :: stmt
! Insert values through prepared statement.
rc = sqlite3_prepare_v2(db%ptr, "DELETE FROM users WHERE users.username = ?;", stmt)
call db_error(rc, 'sqlite3_prepare_v2()')
rc = sqlite3_bind_text(stmt, 1, username)
call db_error(rc, 'sqlite3_bind_text()')
! Insert bound value into database.
rc = sqlite3_step(stmt)
call db_error(rc, 'sqlite3_step()')
! Clean-up prepared statement.
rc = sqlite3_finalize(stmt)
call db_error(rc, 'sqlite3_finalize()')
end function db_delete_user
integer function db_get_users(db) result(rc) integer function db_get_users(db) result(rc)
!! Prints number of courses per student to standard output. !! Prints number of courses per student to standard output.
@ -115,7 +138,7 @@ contains
character(len=24) :: password character(len=24) :: password
rc = sqlite3_prepare_v2(db%ptr, & rc = sqlite3_prepare_v2(db%ptr, &
"SELECT username " // & "SELECT username, password " // &
"FROM users;", stmt) "FROM users;", stmt)
call db_error(rc, 'sqlite3_prepare_v2()') call db_error(rc, 'sqlite3_prepare_v2()')
@ -126,7 +149,7 @@ contains
case (SQLITE_ROW) case (SQLITE_ROW)
username = sqlite3_column_text(stmt, 0) username = sqlite3_column_text(stmt, 0)
password = sqlite3_column_text(stmt, 1) password = sqlite3_column_text(stmt, 1)
print '(a),(a)', username, password write(12, '(a)') username, password
case (SQLITE_DONE) case (SQLITE_DONE)
exit step_loop exit step_loop
@ -150,15 +173,15 @@ contains
if (code == SQLITE_OK .or. code == SQLITE_DONE) return if (code == SQLITE_OK .or. code == SQLITE_DONE) return
if (present(proc) .and. present(err_msg)) then if (present(proc) .and. present(err_msg)) then
print '(a, ": ", a, " (", i0, ")")', proc, err_msg, code write(12, '(a, ": ", a, " (", i0, ")")') proc, err_msg, code
return return
end if end if
if (present(proc)) then if (present(proc)) then
print '(a, ": ", i0)', proc, code write(12, '(a, ": ", i0)') proc, code
return return
end if end if
print '("unknown error: ", i0)', code write(12, '("unknown error: ", i0)') code
end subroutine db_error end subroutine db_error
end module db end module db