Home » U++ Library support » U++ SQL » PostgreSQL: serious mistake when call GetInsertedId()
PostgreSQL: serious mistake when call GetInsertedId() [message #43742] |
Wed, 01 October 2014 00:24 |
|
In a function body of GetInstertedId() it is supposed that the field name of primary key will always have the name id.
Value PostgreSQLConnection::GetInsertedId() const
{
Sql sql("select currval('" + last_insert_table + "_id_seq')", session);
if(sql.Execute() && sql.Fetch())
return sql[0];
else
return Null;
}
(uppsrc/PostgreSQL/PostgreSQL.cpp)
But in a case when it not so, becomes difficult an audible error.
SQL expression for request of a column of primary key here:
SELECT
pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = 'TABLENAME'::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary
Respectively I suggest to modify function. For example so:
Value PostgreSQLConnection::GetInsertedId() const
{
String sqlc_expr;
sqlc_expr <<
"SELECT " <<
"pg_attribute.attname " <<
"FROM pg_index, pg_class, pg_attribute " <<
"WHERE " <<
"pg_class.oid = '" << last_insert_table << "'::regclass AND " <<
"indrelid = pg_class.oid AND " <<
"pg_attribute.attrelid = pg_class.oid AND " <<
"pg_attribute.attnum = any(pg_index.indkey) " <<
"AND indisprimary" ;
Sql sqlc( sqlc_expr );
if( !(sqlc.Execute() && sqlc.Fetch()) )
return Null;
Sql sql("select currval('" + last_insert_table + "_"+AsString(sqlc[0])+"_seq')", session);
if(sql.Execute() && sql.Fetch())
return sql[0];
else
return Null;
}
SergeyNikitin<U++>( linux, wine )
{
under( Ubuntu || Debian || Raspbian );
}
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Nov 01 00:16:51 CET 2024
Total time taken to generate the page: 0.03416 seconds
|