Browse Source

pg select with prepared statement

221V 6 days ago
parent
commit
986e09b942
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

@@ -195,6 +195,7 @@ bool are_valid_config_values(ref TOMLDocument toml_s){
   return true;
 }
 
+/*
 void test_pg_conn_driver(){
   client.pickConnection( (scope conn){
     immutable result = conn.execStatement(
@@ -212,6 +213,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);
+    
+  } );
+}
 
 
 void test(HTTPServerRequest req, HTTPServerResponse res){
@@ -294,7 +320,8 @@ void test(HTTPServerRequest req, HTTPServerResponse res){
     " password=" ~ toml_s[s_toml_db][s_toml_db_pass].str() ~
     " connect_timeout=" ~ toml_s[s_toml_db][s_toml_db_conn_timeout].str(),
     cast(uint) toml_s[s_toml_db][s_toml_db_conn_num].integer() );
-  test_pg_conn_driver();
+  //test_pg_conn_driver();
+  test_pg_conn_driver_queries();
   
   
   Mustache mustache2;

+ 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);
+