Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/statement.cpp : Modified to support complex queries #1157

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/core/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ bool statement_impl::execute(bool withDataExchange)

statement_backend::exec_fetch_result res = backEnd_->execute(num);

// calling describe again in-case it is a complex query and numcols is not populated before execution
if (row_ != NULL && alreadyDescribed_ == false)
{
describe();
}

bool gotData = false;

if (res == statement_backend::ef_success)
Expand Down Expand Up @@ -714,7 +720,10 @@ void statement_impl::describe()
row_->add_properties(props);
}

alreadyDescribed_ = true;
if (numcols != 0)
{
alreadyDescribed_ = true;
}
}

} // namespace details
Expand Down
60 changes: 60 additions & 0 deletions tests/odbc/test-odbc-mssql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <ctime>
#include <cmath>
#include <sstream>

using namespace soci;
using namespace soci::tests;
Expand Down Expand Up @@ -75,6 +76,65 @@ TEST_CASE("MS SQL long string", "[odbc][mssql][long]")
);
}

TEST_CASE("MS SQL table records count", "[odbc][mssql][count]")
{
soci::session sql(backEnd, connectString);

// Execute the provided SQL query to count records in tables
std::string sql_query = R"(
SET NOCOUNT ON;
DECLARE db_cursor CURSOR FOR
SELECT name FROM sys.databases
WHERE state_desc = 'ONLINE'
AND name IN ('master');
DECLARE @DatabaseName NVARCHAR(128);
DECLARE @outset TABLE(
INSTANCENAME varchar(50),
DATABASENAME varchar(100),
TABLENAME varchar(100),
NUMBEROFRECORDS_I bigint
);
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @DatabaseName;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @command nvarchar(1000) = 'USE ' + QUOTENAME(@DatabaseName) +
'; SELECT @@SERVERNAME, DB_NAME(), T.NAME, P.[ROWS] FROM sys.tables T ' +
'INNER JOIN sys.indexes I ON T.OBJECT_ID = I.OBJECT_ID ' +
'INNER JOIN sys.partitions P ON I.OBJECT_ID = P.OBJECT_ID AND I.INDEX_ID = P.INDEX_ID ' +
'INNER JOIN sys.allocation_units A ON P.PARTITION_ID = A.CONTAINER_ID ' +
'WHERE T.NAME NOT LIKE ''DT%'' AND I.OBJECT_ID > 255 AND I.INDEX_ID <= 1 ' +
'GROUP BY T.NAME, I.OBJECT_ID, I.INDEX_ID, I.NAME, P.[ROWS] ' +
'ORDER BY OBJECT_NAME(I.OBJECT_ID)';
INSERT INTO @outset EXEC (@command)
FETCH NEXT FROM db_cursor INTO @DatabaseName
END
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT INSTANCENAME, DATABASENAME, TABLENAME, NUMBEROFRECORDS_I
FROM @outset;
)";

soci::rowset<soci::row> rs = (sql.prepare << sql_query);

// Iterate over the results and print them (or check them in an actual test case)
for (auto it = rs.begin(); it != rs.end(); ++it)
{
soci::row const& row = *it;
std::string instance_name = row.get<std::string>(0);
std::string database_name = row.get<std::string>(1);
std::string table_name = row.get<std::string>(2);
long long number_of_records = row.get<long long>(3);

std::cout << "Instance: " << instance_name
<< ", Database: " << database_name
<< ", Table: " << table_name
<< ", Records: " << number_of_records << std::endl;

// Here you can add checks to validate the expected values.
}
}

// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
Expand Down