Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » Copy from stdin (PostgreSQL) support in U++ SQL
Copy from stdin (PostgreSQL) support in U++ SQL [message #53427] Thu, 02 April 2020 20:41 Go to next message
shutalker is currently offline  shutalker
Messages: 15
Registered: November 2017
Location: Moscow
Promising Member
Hi all!
Is it possible to execute
COPY table_name FROM STDIN
via Sql interface?
I'm trying to do something like this
PostgreSQLSession pgSession;
	
if (!pgSession.Open("dbname=postgres user=postgres")) {
	RLOG("failed to setup postgresql session");
	return;
}
	
Sql sql(pgSession);

// Is there a way to setup any IO stream for data transfer?
	
if (!sql.Execute("COPY my_test_table FROM STDIN")) {
	RLOG("failed to copy data from client: " << sql.GetLastError());
	return;
}

But I don't know how to transfer data I want to import into database.


UPD: I've made a temporary dirty hack that works. This hack requires psql client to be installed
        ...
        LocalProcess psqlClient;
	String psqlCopyCmd = Format("\\COPY \"%s\" FROM STDIN", tableName);

	if (!psqlClient.Start("psql", {config["database"]["init"], "-c", psqlCopyCmd}))
	    throw String("failed to run psql client in child process");

        ...

        String copyLine;
        ...
        psqlClient.Write(copyLine);
        ...

	psqlClient.CloseWrite();
	String exitMessage;
	psqlClient.Finish(exitMessage);

	if (psqlClient.GetExitCode() != 0)
	    throw Format("failed to import 3d-data into database: %s", exitMessage);
        ...

[Updated on: Fri, 03 April 2020 14:35]

Report message to a moderator

Re: Copy from stdin (PostgreSQL) support in U++ SQL [message #53574 is a reply to message #53427] Mon, 13 April 2020 13:33 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
shutalker wrote on Thu, 02 April 2020 20:41
Hi all!
Is it possible to execute
COPY table_name FROM STDIN
via Sql interface?
I'm trying to do something like this
PostgreSQLSession pgSession;
	
if (!pgSession.Open("dbname=postgres user=postgres")) {
	RLOG("failed to setup postgresql session");
	return;
}
	
Sql sql(pgSession);

// Is there a way to setup any IO stream for data transfer?
	
if (!sql.Execute("COPY my_test_table FROM STDIN")) {
	RLOG("failed to copy data from client: " << sql.GetLastError());
	return;
}

But I don't know how to transfer data I want to import into database.


UPD: I've made a temporary dirty hack that works. This hack requires psql client to be installed
        ...
        LocalProcess psqlClient;
	String psqlCopyCmd = Format("\\COPY \"%s\" FROM STDIN", tableName);

	if (!psqlClient.Start("psql", {config["database"]["init"], "-c", psqlCopyCmd}))
	    throw String("failed to run psql client in child process");

        ...

        String copyLine;
        ...
        psqlClient.Write(copyLine);
        ...

	psqlClient.CloseWrite();
	String exitMessage;
	psqlClient.Finish(exitMessage);

	if (psqlClient.GetExitCode() != 0)
	    throw Format("failed to import 3d-data into database: %s", exitMessage);
        ...


I am pretty sure that "COPY ... from STDIN" is actually psql command, not SQL server command, so your hack makes sense.

If you wanted to do something like this with Sql, you would have to parse STDIN and just use Insert..
Previous Topic: quick parameter question
Next Topic: MSSQL Exception Handling
Goto Forum:
  


Current Time: Thu Mar 28 16:25:11 CET 2024

Total time taken to generate the page: 0.01828 seconds