Skip to content

Commit

Permalink
Feature/result size (#7)
Browse files Browse the repository at this point in the history
* Use the configuration from Sqlpp11 installation.

* don't use ::date::day_point which has been removed from Howard Hinnant's date library

* Add bind_result_t.size() to return number of rows in Select query

* version bump to 0.5.0
  • Loading branch information
Erroneous1 authored Oct 25, 2017
1 parent be4e6f6 commit 69ec121
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
41 changes: 9 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,20 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.0)
project (sqlpp11-connector-odbc)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
find_package(Sqlpp11 REQUIRED)

include_directories(${HinnantDate_INCLUDE_DIR})

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
find_package(ODBC REQUIRED)

include_directories(${ODBC_INCLUDE_DIRECTORIES})

message(STATUS "Using ${CMAKE_CXX_COMPILER} (compiler id: ${CMAKE_CXX_COMPILER_ID})")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -stdlib=libc++ ${CMAKE_CXX_FLAGS}")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 ${CMAKE_CXX_FLAGS}")
endif ()


set(SQLPP11_INCLUDE_DIR "/usr/include/" CACHE FILEPATH "Path to sqlpp11 includes")

if(NOT EXISTS ${SQLPP11_INCLUDE_DIR}/sqlpp11/exception.h)
message(SEND_ERROR "Can't find exception.h in ${SQLPP11_INCLUDE_DIR}")
endif()

set(DATE_INCLUDE_DIR "${SQLPP11_INCLUDE_DIR}/sqlpp11" CACHE FILEPATH "Path to Howard Hinnant's date library")

if(NOT EXISTS ${DATE_INCLUDE_DIR}/date.h)
message(SEND_ERROR "Can't find date.h in ${DATE_INCLUDE_DIR}")
endif()

set(SQLPP11_ODBC_VERSION_MAJOR 0)
set(SQLPP11_ODBC_VERSION_MINOR 4)
set(SQLPP11_ODBC_VERSION_MINOR 5)
set(SQLPP11_ODBC_VERSION_PATCH 0)

if(${SQLPP11_ODBC_VERSION_MINOR} LESS 10)
Expand Down Expand Up @@ -89,21 +72,15 @@ set(SQLPP11_ODBC_VERSION "${PROJECT_VERSION}")

configure_file(version.h.in version.h @ONLY)

message(SEND_MESSAGE "including sqlpp11 from ${SQLPP11_INCLUDE_DIR}")
set(include_dir "${PROJECT_SOURCE_DIR}/sqlpp11")
include_directories(
${SQLPP11_INCLUDE_DIR}
${DATE_INCLUDE_DIR}
${include_dir}
${PROJECT_SOURCE_DIR}/include/
)
include_directories(${PROJECT_SOURCE_DIR}/include)

install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/sqlpp11" DESTINATION ${DESTDIR}/include)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/version.h" DESTINATION ${DESTDIR}/include/sqlpp11/odbc/)

add_subdirectory(src)

enable_testing()

if(NOT CMAKE_SQLPP11_CONNECTOR_ODBC_TESTS_IGNORE)
enable_testing()
add_subdirectory(tests)
endif()
2 changes: 2 additions & 0 deletions include/sqlpp11/odbc/bind_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ namespace sqlpp {
void _bind_date_time_result(size_t index, ::sqlpp::time_point::_cpp_value_type* value, bool* is_null);
void _bind_time_of_day_result(size_t index, ::sqlpp::time_of_day::_cpp_value_type* value, bool* is_null);
void _bind_timestamp_result(size_t index, SQL_TIMESTAMP_STRUCT* value, bool* is_null);

size_t size() const;
private:
bool next_impl();
};
Expand Down
15 changes: 13 additions & 2 deletions src/bind_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace sqlpp {
}
*is_null = (ind == SQL_NULL_DATA);
if(!*is_null) {
*value = ::date::day_point( ::date::year(date_struct.year) / date_struct.month / date_struct.day );
*value = ::sqlpp::day_point::_cpp_value_type( ::date::year(date_struct.year) / date_struct.month / date_struct.day );
}
}

Expand All @@ -135,7 +135,7 @@ namespace sqlpp {
}
*is_null = (ind == SQL_NULL_DATA);
if(!*is_null) {
*value = ::date::day_point(::date::year(timestamp_struct.year) / timestamp_struct.month / timestamp_struct.day);
*value = ::sqlpp::day_point::_cpp_value_type(::date::year(timestamp_struct.year) / timestamp_struct.month / timestamp_struct.day);
*value +=
std::chrono::hours(timestamp_struct.hour) +
std::chrono::minutes(timestamp_struct.minute) +
Expand Down Expand Up @@ -194,5 +194,16 @@ namespace sqlpp {
throw sqlpp::exception("ODBC error: couldn't SQLFetch(returned "+std::to_string(rc)+"): "+detail::odbc_error(_handle->stmt, SQL_HANDLE_STMT));
}
}

size_t bind_result_t::size() const {
SQLLEN ret = 0;
if(!SQL_SUCCEEDED(SQLRowCount(_handle->stmt, &ret))) {
throw sqlpp::exception("ODBC error: couldn't SQLRowCount(SQLLEN*): "+detail::odbc_error(_handle->stmt, SQL_HANDLE_STMT));
}
if(ret < 0) {
throw sqlpp::exception("ODBC error: bind_result_t.size() returned negative number!");
}
return ret;
}
}
}
14 changes: 11 additions & 3 deletions tests/ODBCTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ int main(int argc, const char **argv)
(*db)(insert_into(tab).set(tab.alpha = (int64_t)omega, tab.gamma = true, tab.beta = "cheesecake"));
(*db)(insert_into(tab).set(tab.gamma = false, tab.beta = "blueberry muffin"));

for(const auto& row : (*db)(select(all_of(tab)).from(tab).unconditionally()))
{
printResultsSample(row);
};
auto result = (*db)(select(all_of(tab)).from(tab).unconditionally());
auto size = result.size();
std::cout << "Select returned " << size << " rows\n";
assert(size == 2);
for(const auto& row : result)
{
printResultsSample(row);
--size;
};
assert(size == 0);
}
auto date_time = std::chrono::system_clock::time_point()
+ std::chrono::microseconds(3723123456);
auto dp = date::floor<date::days>(date_time);
Expand Down

0 comments on commit 69ec121

Please sign in to comment.