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

PostgreSQL get sqlcode #4093

Closed
omerbrandis opened this issue Jul 21, 2023 · 2 comments
Closed

PostgreSQL get sqlcode #4093

omerbrandis opened this issue Jul 21, 2023 · 2 comments
Labels

Comments

@omerbrandis
Copy link
Contributor

hello all,

I've started using poco::data , v1.11.0. ( I seems like a wonderful lib, thanks for creating it ).

I seem to be unable to find a statements sqlcode.

at least when I use Poco::Data::PostgreSQL::PostgreSQLException, with postgresql v15.3
and have a failing insert statement with the following msg "the display text is "ERROR: null value in column "...l" of relation "..." violates not-null constraint" ,
the value returned by exception.code() is 0.
while I expected to see something like 23502.

so either this is a bug,
Or I am looking in the wrong place.

Please advise,
Omer.

@omerbrandis
Copy link
Contributor Author

as far as i can tell, its simply not implemented.

Data/Postgresql/StatementExecutor.cpp :

// setup to clear the result from PQprepare
		PQResultClear resultClearer(ptrPGResult);

		if	(!ptrPGResult || PQresultStatus(ptrPGResult) != PGRES_COMMAND_OK)
		{
			throw StatementException(std::string("postgresql_stmt_prepare error: ") + PQresultErrorMessage (ptrPGResult) + " " + aSQLStatement);
		}

I did find an attempt to do so in

if (!ptrPGResult || (PQresultStatus(ptrPGResult) != PGRES_COMMAND_OK &&
		PQresultStatus(ptrPGResult) != PGRES_TUPLES_OK))
	{
		PQResultClear resultClearer(ptrPGResult);

		const char* pSeverity	= PQresultErrorField(ptrPGResult, PG_DIAG_SEVERITY);
		const char* pSQLState	= PQresultErrorField(ptrPGResult, PG_DIAG_SQLSTATE);
		const char* pDetail		= PQresultErrorField(ptrPGResult, PG_DIAG_MESSAGE_DETAIL);
		const char* pHint		= PQresultErrorField(ptrPGResult, PG_DIAG_MESSAGE_HINT);
		const char* pConstraint	= PQresultErrorField(ptrPGResult, PG_DIAG_CONSTRAINT_NAME);

		throw StatementException(std::string("postgresql_stmt_execute error: ")
			+ PQresultErrorMessage (ptrPGResult)
			+ " Severity: " + (pSeverity   ? pSeverity   : "N/A")
			+ " State: " + (pSQLState   ? pSQLState   : "N/A")
			+ " Detail: " + (pDetail ? pDetail : "N/A")
			+ " Hint: " + (pHint   ? pHint   : "N/A")
			+ " Constraint: " + (pConstraint ? pConstraint : "N/A"));
	}

but this seems to be incorrect implementation, the pSQLState is returned as part of a string, which defeats the purpose of SQLState ( to have a numeric return value and allow the user to avoid string parsing to determine how to proceed).

@omerbrandis
Copy link
Contributor Author

added pull request #4099

@aleks-f aleks-f added this to the Release 1.13.0 milestone Nov 23, 2023
@aleks-f aleks-f added the fixed label Nov 23, 2023
@aleks-f aleks-f changed the title how can I get the sqlcode? (Poco::Data::PostgreSQL) PostgreSQL get sqlcode Nov 23, 2023
@aleks-f aleks-f closed this as completed Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants