On 10/22/20 11:00 AM, Vino wrote:
On Thursday, 22 October 2020 at 14:08:30 UTC, Steven Schveighoffer wrote:
On 10/22/20 7:04 AM, Vino wrote:
Hi All,


   Request your help on the below code as it is not working as expected.

GetConnections.d

module common.GetConnections;
import mysql;

class Connections
{
   private Connection conn;
   auto constr = "host=localhost;port=3910;user=user;pwd=password#;db=testdb";

I'd declare this immutable, so it's not stored in the class:

immutable constr = "...";

   this.conn = new Connection(constr);

You are trying to assign an instance member at compile time, with no actual instance.

You need a constructor

     this() { this.conn = new Connection(constr); }
}

GetHost.d

import common.GetConnections;
import std.array : array;
import std.variant;
import mysql;
import std.stdio: writeln;

void main()
{
  auto conn = new Connections();
  Row[] data = conn.query("SELECT * FROM hostlog").array;
  writeln(data[]);
}

The rest should work.

-Steve

Hi Steve,

    Thank you very much, I tried your solution, still not working

File: GetConnections.d
module common.GetConnections;
import mysql;

class Connections
{
   private Connection conn;
  immutable constr = "host=localhost;port=3910;user=user;pwd=password#;db=testdb";
   this() { this.conn = new Connection(constr); }
}

Error:
source/common/GetHost.d(11,25): Error: none of the overloads of query are callable using argument types (Connections, string), candidates are: /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d(321,13): mysql.commands.query(Connection conn, const(char[]) sql, ColumnSpecialization[] csa = null) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d(334,13): mysql.commands.query(Connection conn, const(char[]) sql, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d(342,13): mysql.commands.query(Connection conn, ref Prepared prepared) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d(357,13): mysql.commands.query(Connection conn, ref Prepared prepared, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d(364,13): mysql.commands.query(Connection conn, ref BackwardCompatPrepared prepared)
source/common/GetParams.d(11,25):        ... (2 more, -v to show) ....


Different error:

 Row[] data = conn.query("SELECT * FROM hostlog").array;

This is trying to call mysql-native's UFCS query function on Connections, which isn't valid. You need to call it on conn.conn.

But there's no access to the private Connection conn inside the Connections class. I'm not sure what the class is for anyway, so it's hard for me to suggest a proper alternative. Are you just trying to encapsulate the connection string? I'd suggest instead of a whole class, just a factory function:

Connection getConnection()
{
   return new Connection("...");
}

-Steve

Reply via email to