Thread overview
mysql-native Help required
Oct 22, 2020
Vino
Oct 22, 2020
novice3
Oct 22, 2020
Vino
Oct 23, 2020
aberba
Oct 25, 2020
Vino
October 22, 2020
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";
  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[]);
}

From,
Vino
October 22, 2020
On Thursday, 22 October 2020 at 11:04:53 UTC, Vino wrote:
> class Connections
> {
>   private Connection conn;
>   auto constr = "host=localhost;port=3910;user=user;pwd=password#;db=testdb";
>   this.conn = new Connection(constr);
> }

where Connections class constructor implemented?
October 22, 2020
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
October 22, 2020
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) ...

From,
Vino.B
October 22, 2020
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
October 23, 2020
On Thursday, 22 October 2020 at 18:43:40 UTC, Steven Schveighoffer wrote:
> On 10/22/20 11:00 AM, Vino wrote:
>> [...]
>
> 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

Was about to say that. Part of why I think some people hate OOP...due to misuse.

All my MySQL projects have this getConnection() function.
October 25, 2020
On Friday, 23 October 2020 at 20:28:40 UTC, aberba wrote:
> On Thursday, 22 October 2020 at 18:43:40 UTC, Steven Schveighoffer wrote:
>> [...]
>
> Was about to say that. Part of why I think some people hate OOP...due to misuse.
>
> All my MySQL projects have this getConnection() function.

Hi All,

  Thank you very much, it resolved my issues.

Vino.B