digitalmars.D.learn - mysql-native Help required
- Vino (27/27) Oct 22 2020 Hi All,
- novice3 (2/9) Oct 22 2020 where Connections class constructor implemented?
- Steven Schveighoffer (9/41) Oct 22 2020 I'd declare this immutable, so it's not stored in the class:
- Vino (27/69) Oct 22 2020 Hi Steve,
- Steven Schveighoffer (15/100) Oct 22 2020 Different error:
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 = 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
Oct 22 2020
On Thursday, 22 October 2020 at 11:04:53 UTC, Vino wrote:class Connections { private Connection conn; auto constr = this.conn = new Connection(constr); }where Connections class constructor implemented?
Oct 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 =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
Oct 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 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 = 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.BHi 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 =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
Oct 22 2020
On 10/22/20 11:00 AM, Vino wrote:On Thursday, 22 October 2020 at 14:08:30 UTC, Steven Schveighoffer 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("..."); } -SteveOn 10/22/20 7:04 AM, Vino wrote: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 = 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) ....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 =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
Oct 22 2020
On Thursday, 22 October 2020 at 18:43:40 UTC, Steven Schveighoffer wrote:On 10/22/20 11:00 AM, Vino 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.[...]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
Oct 23 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:Hi All, Thank you very much, it resolved my issues. Vino.B[...]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.
Oct 24 2020