Connecting to Dolt from Dlang
This is not as complicated as this sounds, but I made a critical mistake which took way to long to find.
After standing up the Dolt server, the docs provide some instruction for connecting to Dolt through the MySql client: https://docs.dolthub.com/introduction/getting-started/database
My D Code looked something like:
$ dub init
$ sudo apt install default-libmysqlclient-dev
$ dub add arsd-official:mssql
$ gvim source/app.d
import arsd.mysql;
void main() {
auto db = new MySql("localhost", "root", "", "getting_started");
}
But this was a mistake as I did not specify the loopback IP and for some reason using 'locahost' did not accept the connection.
import arsd.mysql; void main() {
auto db = new MySql("127.0.0.1", "root", null, "getting_started");
}
To further my frustration, Docker MySQL was not listening on the port when I started it. But at least with the fix out of the way this worked.
Here are the Error Messages I Received from D and the MySQL client as I worked through this.
/arsd-official/mysql.d(203): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
Here is a very basic test script to show the functionality.
import std.stdio;
import arsd.mysql;
void main() {
auto db = new MySql("127.0.0.1", "root", null, null);
db.query("CREATE database test");
db.query("use test");
db.query("CREATE TABLE hello (ColumnA nvarchar(3))");
db.query("INSERT INTO hello (ColumnA) VALUES (?)", "RSA");
// Add Changes to Commit
// Note that selection from the Database still shows the added
// Data even though it hasn't been committed yet.
db.query("call dolt_add('hello')");
db.query("call dolt_commit('-m', ':tada: Init Commit')");
auto table = db.query("SELECT ColumnA FROM hello");
foreach(r; table) {
writeln(r);
}
table = db.query("SELECT * FROM dolt_log");
foreach(r; table) {
writeln(r);
}
}