diff --git a/common/sql/test.db3 b/common/sql/test.db3 index 81a46d5..d9b14ef 100644 Binary files a/common/sql/test.db3 and b/common/sql/test.db3 differ diff --git a/fortran/www/app/main.f90 b/fortran/www/app/main.f90 index 754b1cb..9ae3b90 100644 --- a/fortran/www/app/main.f90 +++ b/fortran/www/app/main.f90 @@ -1,5 +1,6 @@ program main use www + use db implicit none character, dimension(:), allocatable :: form_data @@ -21,7 +22,7 @@ program main call request(form_data, length) 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 call create_db(db_path) end if @@ -39,17 +40,24 @@ contains subroutine create_db(db_path) character(len=128), intent(in) :: db_path - character(len=:), allocatable :: command - command = 'sqlite3 '//trim(adjustl(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) + ! character(len=:), allocatable :: command + type(db_type) :: db + integer :: rc + + rc = db_open(db, db_path(:Len_Trim(db_path))) + rc = db_create_users(db) + rc = db_close(db) + + ! 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 @@ -61,11 +69,14 @@ contains integer :: i, j, s_idx, e_idx, username_len, password_len character(len=24) :: username character(len=24) :: password - character(len=32) :: created + character(len=32) :: created character(len=8) :: appearance logical :: start = .false. - character(len=:), allocatable :: command + type(db_type) :: db + integer :: rc + + ! character(len=:), allocatable :: command j = 1 get_username: do i = 1, length @@ -85,7 +96,7 @@ contains j = j + 1 end if end do get_username - username_len = j-2 + username_len = j - 2 j = 1 get_password: do i = e_idx + 2, length @@ -105,7 +116,7 @@ contains j = j + 1 end if end do get_password - password_len = j-2 + password_len = j - 2 get_appearance: do i = e_idx + 2, length if (request(i) .eq. '=') then @@ -114,20 +125,23 @@ contains end do get_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)) & - //' "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) + rc = db_open(db, db_path(:Len_Trim(db_path))) + rc = db_add_user(db, username(:username_len), password(:password_len), appearance(:Len_Trim(appearance)), 0, 0, 0, time()) + rc = db_close(db) end subroutine add_user -end program main \ No newline at end of file +end program main diff --git a/fortran/www/src/db.f90 b/fortran/www/src/db.f90 index 92406a9..8c09651 100644 --- a/fortran/www/src/db.f90 +++ b/fortran/www/src/db.f90 @@ -65,12 +65,12 @@ contains if (rc /= SQLITE_OK) return 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. type(db_type), intent(inout) :: db - character(len=:), intent(in) :: username - character(len=:), intent(in) :: password - character(len=:), intent(in) :: apperance + character(len=*), intent(in) :: username + character(len=*), intent(in) :: password + character(len=*), intent(in) :: apperance integer, intent(in) :: x_pos integer, intent(in) :: y_pos integer, intent(in) :: last_login @@ -78,7 +78,7 @@ contains type(c_ptr) :: stmt ! 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) call db_error(rc, 'sqlite3_prepare_v2()') @@ -88,14 +88,14 @@ contains call db_error(rc, 'sqlite3_bind_text()') rc = sqlite3_bind_text(stmt, 3, apperance) call db_error(rc, 'sqlite3_bind_text()') - rc = sqlite3_bind_integer(stmt, 4, x_pos) - call db_error(rc, 'sqlite3_bind_integer()') - rc = sqlite3_bind_integer(stmt, 5, y_pos) - call db_error(rc, 'sqlite3_bind_integer()') - rc = sqlite3_bind_integer(stmt, 6, last_login) - call db_error(rc, 'sqlite3_bind_integer()') - rc = sqlite3_bind_integer(stmt, 7, created) - call db_error(rc, 'sqlite3_bind_integer()') + rc = sqlite3_bind_int(stmt, 4, x_pos) + call db_error(rc, 'sqlite3_bind_int()') + rc = sqlite3_bind_int(stmt, 5, y_pos) + call db_error(rc, 'sqlite3_bind_int()') + rc = sqlite3_bind_int(stmt, 6, last_login) + call db_error(rc, 'sqlite3_bind_int()') + rc = sqlite3_bind_int(stmt, 7, created) + call db_error(rc, 'sqlite3_bind_int()') ! Insert bound value into database. rc = sqlite3_step(stmt) @@ -104,7 +104,30 @@ contains ! Clean-up prepared statement. rc = sqlite3_finalize(stmt) 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) !! Prints number of courses per student to standard output. @@ -115,7 +138,7 @@ contains character(len=24) :: password rc = sqlite3_prepare_v2(db%ptr, & - "SELECT username " // & + "SELECT username, password " // & "FROM users;", stmt) call db_error(rc, 'sqlite3_prepare_v2()') @@ -126,7 +149,7 @@ contains case (SQLITE_ROW) username = sqlite3_column_text(stmt, 0) password = sqlite3_column_text(stmt, 1) - print '(a),(a)', username, password + write(12, '(a)') username, password case (SQLITE_DONE) exit step_loop @@ -150,15 +173,15 @@ contains if (code == SQLITE_OK .or. code == SQLITE_DONE) return 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 end if if (present(proc)) then - print '(a, ": ", i0)', proc, code + write(12, '(a, ": ", i0)') proc, code return end if - print '("unknown error: ", i0)', code + write(12, '("unknown error: ", i0)') code end subroutine db_error end module db