Skip to content

Commit

Permalink
Merge pull request #79 from pbondo/master.
Browse files Browse the repository at this point in the history
Add JSON support for PostgreSQL with related test case.
Close #77
Close #79
  • Loading branch information
mloskot committed Feb 23, 2013
2 parents 8b68ee9 + 60c5c87 commit 3918662
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/backends/postgresql/statement.cpp
Expand Up @@ -504,6 +504,7 @@ void postgresql_statement_backend::describe_column(int colNum, data_type & type,
case 18: // char
case 1042: // bpchar
case 142: // xml
case 114: // json
case 17: // bytea
type = dt_string;
break;
Expand Down
63 changes: 63 additions & 0 deletions src/backends/postgresql/test/test-postgresql.cpp
Expand Up @@ -629,6 +629,68 @@ struct table_creator_for_get_affected_rows : table_creator_base
}
};

struct table_creator_json : public table_creator_base
{
table_creator_json(session& sql)
: table_creator_base(sql)
{
sql << "drop table if exists soci_json_test;";
sql << "create table soci_json_test(data json)";
}
};


// Return 9,2 for 9.2.3
typedef std::pair<int,int> server_version;

server_version get_postgresql_version(session& sql)
{
std::string version;
std::pair<int,int> result;
sql << "select version()",into(version);
if (sscanf(version.c_str(),"PostgreSQL %i.%i", &result.first, &result.second) < 2)
{
throw std::runtime_error("Failed to retrieve PostgreSQL version number");
}
return result;
}

// Test JSON. Only valid for PostgreSQL Server 9.2++
void test_json()
{
session sql(backEnd, connectString);
server_version version = get_postgresql_version(sql);
if ( version >= server_version(9,2))
{
bool exception = false;
std::string result;
std::string valid_input = "{\"tool\":\"soci\",\"result\":42}";
std::string invalid_input = "{\"tool\":\"other\",\"result\":invalid}";

table_creator_json tableCreator(sql);

sql << "insert into soci_json_test (data) values(:data)",use(valid_input);
sql << "select data from soci_json_test",into(result);
assert(result == valid_input);

try
{
sql << "insert into soci_json_test (data) values(:data)",use(invalid_input);
}
catch(soci_error &_exception)
{
exception = true;
}
assert(exception);
std::cout << "test json passed" << std::endl;
}
else
{
std::cout << "json only available for PSQL server >= 9.2. Found version: " << version.first << "." << version.second << std::endl;
}
}


//
// Support for soci Common Tests
//
Expand Down Expand Up @@ -714,6 +776,7 @@ int main(int argc, char** argv)
test11();
test12();
test_bytea();
test_json();

std::cout << "\nOK, all tests passed.\n\n";

Expand Down
2 changes: 1 addition & 1 deletion src/backends/postgresql/vector-into-type.cpp
Expand Up @@ -167,7 +167,7 @@ void resizevector_(void * p, std::size_t sz)

void postgresql_vector_into_type_backend::resize(std::size_t sz)
{
assert(sz < std::numeric_limits<unsigned short>::max()); // Not a strong constraint, for debugging only
assert(sz < 10*std::numeric_limits<unsigned short>::max()); // Not a strong constraint, for debugging only. Notice my fix is even worse

switch (type_)
{
Expand Down

0 comments on commit 3918662

Please sign in to comment.