How to execute a random postgresql-query.

neuranuz neuranuz at gmail.com
Tue Jun 29 17:56:54 UTC 2021


On Monday, 28 June 2021 at 19:16:40 UTC, Alain De Vos wrote:
> How to execute a random postgresql-query ?
> With random i mean execute any string as known by postgresql.
>
>
> void myexecutefunction(string string_to_execute){
> // Some code to Execute postgre-sq-string
> }
>
> ...
>
> void main(){
> myexecutefunction("CREATE DATABASE test");
> myexecutefunction("CREATE USER test");
> ...
> myexecutefunction("SELECT * FROM testtable");
> }

Hello! If you want to make it "from scratch", then you basically 
need to install libpq library on your system. Then add some 
function bindings of basic methods of this library like there 
(this is my mode ;-)):
https://github.com/uranuz/webtank/blob/master/src/webtank/db/postgresql/bindings.d
Then you need to link libpq to your project. You could use *dub* 
options for instance to do it.
Establishing connection to database and making queries is 
described there:
https://github.com/uranuz/webtank/blob/master/src/webtank/db/postgresql/database.d
And getting data from result is described there:
https://github.com/uranuz/webtank/blob/master/src/webtank/db/postgresql/query_result.d

You could also try to find some ready to use bindings to 
PostgreSQL on code.dlang.org.
I haven't published my code on code.dlang yet, because it's not 
enough time ;-(


More information about the Digitalmars-d-learn mailing list