Skip to content

Commit

Permalink
Allow to choose output Excel file using KRACKEN in example
Browse files Browse the repository at this point in the history
Also cleanup module path mess.
  • Loading branch information
mlt committed Mar 25, 2013
1 parent 1f3aa54 commit c1299d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 2.8)
enable_language(Fortran)
project(fodbc Fortran)

add_subdirectory(src)
add_subdirectory(example)
15 changes: 11 additions & 4 deletions example/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.8)
enable_language(Fortran)

set (CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
include_directories(${CMAKE_BINARY_DIR}/src)

add_executable(sqlite_test sqlite.f03)
target_link_libraries(sqlite_test fodbc)
Expand All @@ -13,6 +13,13 @@ add_executable(access_test access_win32.f03)
target_link_libraries(access_test fodbc)
set_source_files_properties(access_win32.f03 PROPERTIES LANGUAGE Fortran)

add_executable(excel_test new_excel_win32.f03)
target_link_libraries(excel_test fodbc)
set_source_files_properties(new_excel_win32.f03 PROPERTIES LANGUAGE Fortran)
find_package(KRACKEN QUIET)

if(KRACKEN_DIR)
include_directories(${KRACKEN_DIR})
add_executable(excel_test new_excel_win32.f03)
target_link_libraries(excel_test fodbc kracken)
set_source_files_properties(new_excel_win32.f03 PROPERTIES LANGUAGE Fortran)
else()
message("Download and build KRACKEN for Excel example, then reconfigure this project.")
endif()
36 changes: 22 additions & 14 deletions example/new_excel_win32.f03
Expand Up @@ -15,12 +15,13 @@ module class_ExcelTest

contains

function new_ExcelTest() result(self)
function new_ExcelTest(fname) result(self)
type(ExcelTest) :: self
character(len=*), intent(in) :: fname
integer(C_SHORT) :: err
integer(C_INT),target :: native
integer(C_SHORT) :: len
character(c_char), target :: state(6), text(256)
integer(C_SHORT) :: lens
character(256, c_char), target :: state, text
self = ExcelTest(C_NULL_PTR, C_NULL_PTR, C_NULL_PTR)
print *, "ExcelTest constructor has been called"
err = SQLAllocHandle(SQL_HANDLE_ENV, C_NULL_PTR, self%env)
Expand All @@ -29,15 +30,14 @@ function new_ExcelTest() result(self)
if (err .ne. 0) print *, "Can't request version", err
err = SQLAllocHandle(SQL_HANDLE_DBC, self%env, self%dbc)
if (err .ne. 0) print *, "Can't allocate DBC handle", err
err = SQLDriverConnect(self%dbc, C_NULL_PTR, &
C_CHAR_"Driver=Microsoft Excel Driver (*.xls);" &
// C_CHAR_"ReadOnly=False;" &
// C_CHAR_"DBQ=hello_fortran.xls;" &
// C_NULL_CHAR, SQL_NTS2, C_STR_NULL_PTR, 0_2, C_SHORT_NULL_PTR, SQL_DRIVER_COMPLETE)
write (text, '("Driver=Microsoft Excel Driver (*.xls);ReadOnly=False;DBQ=", A)'), fname
print *, "Connection string: ", text(:len_trim(text))
err = SQLDriverConnect(self%dbc, C_NULL_PTR, text &
, int(len_trim(text), c_short), C_STR_NULL_PTR, 0_2, C_SHORT_NULL_PTR, SQL_DRIVER_COMPLETE)
if (err .ne. 0) print *, "Can't connect", err
err = 1
do while (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_DBC, self%dbc, err, state, native, text, int2(sizeof(text)), len))
print *, text(1:len)
do while (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_DBC, self%dbc, err, state, native, text, int2(sizeof(text)), lens))
print *, text(1:lens)
print *, state
err = err+1
end do
Expand Down Expand Up @@ -67,9 +67,11 @@ subroutine run(self)
pts%x = (/((i*a),i=1,100)/)
pts%y = sin(pts%x)

err = SQLPrepare(self%stmt, &
C_CHAR_"insert into sin values(?,?)" // C_NULL_CHAR, SQL_NTS)
if (err .ne. 0) print *, "Failed to prepare statement", err
if (SQL_SUCCESS /= SQLPrepare(self%stmt, &
C_CHAR_"insert into sin values(?,?)" // C_NULL_CHAR, SQL_NTS)) then
print *, "Failed to prepare statement", err
return
end if

do i = 1,100
err = SQLBindParameter(self%stmt, 1_2, SQL_PARAM_INPUT, SQL_REAL, &
Expand Down Expand Up @@ -112,10 +114,16 @@ end module class_ExcelTest

program test
use class_ExcelTest
use M_kracken
implicit none
type(ExcelTest) :: t
character(len=255) :: filename
integer :: iflen, ier

call kracken('cmd','-f hello_fortran.xls')
call retrev('cmd_f', filename, iflen, ier)

t = ExcelTest()
t = new_ExcelTest(filename(1:iflen))
call t%run
call delete_ExcelTest(t)

Expand Down
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
@@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 2.8)
enable_language(Fortran)

set (CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)

set (FODBC_SRCS
fodbc_types.f03
fodbc_ext.f03
Expand Down

0 comments on commit c1299d3

Please sign in to comment.