Browse Source

pg select with prepared statement

221V 6 days ago
parent
commit
3868944799
2 changed files with 40 additions and 1 deletions
  1. 28 1
      vtest/source/app.d
  2. 12 0
      vtest/sql/test_pg.sql

+ 28 - 1
vtest/source/app.d

@@ -131,6 +131,7 @@ void hello(HTTPServerRequest req, HTTPServerResponse res){
 */
 
 
+/*
 void test_pg_conn_driver(){
   client.pickConnection( (scope conn){
     immutable result = conn.execStatement(
@@ -148,6 +149,31 @@ void test_pg_conn_driver(){
     }
   } );
 }
+*/
+
+
+string get_all_cities(){
+  return "SELECT id, name, population FROM test ORDER BY id";
+}
+
+void test_pg_conn_driver_queries(){
+  
+  client.pickConnection( (scope conn){
+    conn.prepareStatement("get_city_by_id", "SELECT id, name, population FROM test WHERE id = $1"); // get_city_by_id
+    QueryParams params;
+    params.preparedStatementName = "get_city_by_id";
+    params.argsVariadic(3);
+    auto result1 = conn.execPreparedStatement(params);
+    
+    writeln("id: ", result1[0]["id"].as!PGinteger);
+    writeln("name: ", result1[0]["name"].as!PGtext);
+    writeln("population: ", result1[0]["population"].as!PGinteger);
+    
+    //conn.prepareStatement("q1", "UPDATE test SET name = $1, population = $2 WHERE id = $3"); // update_city_by_id
+    //immutable result1 = conn.execPreparedStatement("", ValueFormat.BINARY);
+    
+  } );
+}
 
 
 
@@ -224,7 +250,8 @@ void test(HTTPServerRequest req, HTTPServerResponse res){
   // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
   //client = new PostgresClient("host=localhost port=5432 dbname=mydb user=usename password=pass connect_timeout=5", 100);
   client = new PostgresClient("host=195.201.41.112 port=5432 dbname=t4x_parser_data user=admin4 password=Ftsn3rT5YA6ZDWEywfmXekKCS8a4cNxp connect_timeout=5", 100);
-  test_pg_conn_driver();
+  //test_pg_conn_driver();
+  test_pg_conn_driver_queries();
   
   
   

+ 12 - 0
vtest/sql/test_pg.sql

@@ -0,0 +1,12 @@
+
+DROP TABLE IF EXISTS test;
+CREATE TABLE "test" (
+  "id" serial,
+  "name" varchar(255) NOT NULL,
+  "population" integer,
+  PRIMARY KEY ("id")
+);
+INSERT INTO "test" ("name", "population") VALUES ('Київ', 2804000);
+INSERT INTO "test" ("name", "population") VALUES ('Львів', 723605);
+INSERT INTO "test" ("name", "population") VALUES ('Одеса', 997189);
+