Re: PQexecPrepared() question

From: Igor Korot <ikorot01(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: PQexecPrepared() question
Date: 2025-12-25 01:28:23
Message-ID: CA+FnnTzuJTaPBgzOyFxHJCCSqxvvOz7=LePnoonNQMepKT2DeA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi, ALL,
My current code is below:

std::wstring query1 = L"SELECT t.table_catalog AS catalog,
t.table_schema AS schema, t.table_name AS table, u.usename AS owner,
c.oid AS table_id FROM information_schema.tables t,
pg_catalog.pg_class c, pg_catalog.pg_user u WHERE t.table_name =
c.relname AND c.relowner = usesysid AND (t.table_type = 'BASE TABLE'
OR t.table_type = 'VIEW' OR t.table_type = 'LOCAL TEMPORARY') ORDER BY
table_name;";
std::wstring query7 = L"INSERT INTO \"abcattbl\" VALUES(
0, $1, (SELECT c.oid FROM pg_class c, pg_namespace nc WHERE nc.oid =
c.relnamespace AND c.relname = $2 AND nc.nspname = $3), \'\', 8, 400,
\'N\', \'N\', 0, 1, 0, \'MS Sans Serif\', 8, 400, \'N\', \'N\', 0, 1,
0, \'MS Sans Serif\', 8, 400, \'N\', \'N\', 0, 1, 0, \'MS Sans
Serif\', \'\' ) ON CONFLICT DO NOTHING;";
for( int i = 0; i < PQntuples( res ) && !result; i++ )
{
std::wstring cat = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 0 ) );
std::wstring schema = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 1 ) );
std::wstring table = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 2 ) );
std::wstring table_owner = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 3 ) );
pimpl.m_tableDefinitions[cat].push_back( TableDefinition(
cat, schema, table ) );
count++;
paramValues = schema + L"." + table;
params[0] = new char[paramValues.length() + 2];
memset( params[0], '\0', paramValues.length() + 2 );
auto temp = m_pimpl->m_myconv.to_bytes(
paramValues.c_str() ).c_str();
params[0] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( paramValues.c_str() ).c_str() );
params[1] = new char[table.length() + 2];
memset( params[1], '\0', table.length() + 2 );
params[1] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( table.c_str() ).c_str() );
params[2] = new char[table_owner.length() + 2];
memset( params[2], '\0', table_owner.length() + 2 );
params[2] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( table_owner.c_str() ).c_str() );
paramFormat[0] = paramFormat[1] = paramFormat[2] = 0;
paramLength[0] = paramValues.length();
paramLength[1] = table.length();
paramLength[2] = table_owner.length();
res8 = PQexecPrepared( m_db, "set_table_prop", 3, params,
paramLength, paramFormat, 0 );
if( PQresultStatus( res8 ) != PGRES_COMMAND_OK )
{
std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
errorMsg.push_back( L"Error executing query: " + err );
result = 1;
}
PQclear( res8 );
delete[] params[0];
params[0] = nullptr;
delete[] params[1];
params[1] = nullptr;
delete[] params[2];
params[2] = nullptr;
}

This code crashes on the PQexecParam.

I am completely puzzled.

Could anyone help?

Thank you.

On Mon, Dec 22, 2025 at 3:07 PM Igor Korot <ikorot01(at)gmail(dot)com> wrote:
>
> Hi, Peter,
>
> On Mon, Dec 22, 2025 at 1:55 AM Peter J. Holzer <hjp-pgsql(at)hjp(dot)at> wrote:
> >
> > On 2025-12-21 17:30:38 -0800, Igor Korot wrote:
> > > On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <hjp-pgsql(at)hjp(dot)at> wrote:
> > > > As I see it, Igor has two options:
> > > >
> > > > 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> > > > current locale which is obviously not using UTF-8. I don't program in
> > > > C++, but I'm pretty sure there is a portable way to encode a string
> > > > in UTF-8.
> > [...]
> > > > I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> > > > for over 30 years ;-).
> > >
> > > Maybe all I need is something like this:
> > >
> > > convert(char *dest, const std::wstring src)
> > > {
> > > const wchar_t *temp = src.c_str();
> > > while( *dest )
> > > {
> > > dest++;
> > > }
> > > while( *temp )
> > > {
> > > *dest = *temp;
> > > dest++;
> > > temp++;
> > > }
> > > *dest++ = '\0';
> > > *dest = '\0';
> > > }
> > >
> > > ?
> >
> > I hope your compiler complains loudly about that code. You can't just
> > assign a wchar_t to a char and expect it to do anything sensible.
>
> At least MSVC 2017 did not.
> However, trying didn't solve it. Same error.
>
> >
> > A short search points to std::wstring_convert, but that has been
> > deprecated in C++17, and it's not obvious to me what the replacement is.
> >
> > There is always ICU, of course, but that feels like cracking a nut with
> > a sledgehammer.
>
> I think ICU IS a replacement for wstring_comvert.
> Will check it.
>
> >
> > Personally, I might roll my own if an hour of googling doesn't turn up
> > anything promising (UTF-8 encoding is quite simple), but that shouldn't
> > be necessary.
> >
> >
> > hjp
> >
> > --
> > _ | Peter J. Holzer | Story must make more sense than reality.
> > |_|_) | |
> > | | | hjp(at)hjp(dot)at | -- Charles Stross, "Creative writing
> > __/ | http://www.hjp.at/ | challenge!"

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2025-12-25 09:32:08 Re: PQexecPrepared() question
Previous Message Igor Korot 2025-12-25 01:10:31 Re: Default value