How to execute a random postgresql-query.
neuranuz
neuranuz at gmail.com
Tue Jun 29 18:12:16 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");
> }
In case of my implentation the usage is:
auto db = new DBPostgreSQL("host=hostname port=5432
dbname=databasename user=username password=secret");
auto qres = db.query("select first_name, second_name from
person");
string first_name = qres.get(0, 0, null);
string second_name = qres.get(1, 0, null);
It's pretty simple;-)
There is also second function "queryParamsArray" that helps to
pass query parameters in safe manner without string concatenation:
string[] first_names = ["John", "Sarah"];
string[] second_names = ["Doe", "Connor"];
db.queryParamsArray("with inp as(
select
unnest($1::text[]) first_name,
unnest($2::text[]) second_name
)
insert into person (first_name, second_name)
select inp.* from inp
");
More information about the Digitalmars-d-learn
mailing list