#import mapi.h

Martin Tschierschke mt at smartdolphin.de
Thu Mar 22 14:44:20 UTC 2018


On Thursday, 22 March 2018 at 12:53:23 UTC, Martin Tschierschke 
wrote:
> On Wednesday, 21 March 2018 at 17:12:07 UTC, Timoses wrote:
[...]

I got it, my first mini test with C bindings!
Important missing at first: (stderr)

import core.stdc.stdio : stderr, FILE;
and use of toStringz inside:  mapi_query(dbh, toStringz(q) )

After installing monetdb and starting the server with:
https://www.monetdb.org/Documentation/UserGuide/Tutorial

My minimal version of the example compiled with
dmd app.d -I /usr/local/lib/libmapi.so

(My dub.json is still incomplete so the linking failed.)

Here is the code: (only the minimum of mapi.h is icluded!)

```
  std.stdio;
import std.string;

/* interfacing winth monetdb #include mapi.h */
/* minimum to run code of example at: 
https://www.monetdb.org/Documentation/Manuals/SQLreference/Programming/MAPI */
import core.stdc.stdio : stderr,FILE;
import core.stdc.stdlib;

struct MapiStruct;
alias Mapi = MapiStruct*;

struct MapiStatement;
alias MapiHdl = MapiStatement*;

enum MOK = 0 ;

alias MapiMsg = int;

extern(System){
Mapi mapi_connect(const char* host, int port, const char* 
username, const char* password, const char* lang, const char* 
dbname);
MapiHdl mapi_query(Mapi mid, const char *cmd);
int mapi_fetch_row(MapiHdl hdl);
char *mapi_fetch_field(MapiHdl hdl, int fnr);
MapiMsg mapi_error(Mapi mid);
MapiMsg mapi_explain(Mapi mid, FILE* fd);
MapiMsg mapi_destroy(Mapi mid);
MapiMsg mapi_close_handle(MapiHdl hdl);
MapiMsg mapi_explain_result(MapiHdl hdl, FILE* fd);
MapiMsg mapi_next_result(MapiHdl hdl);
MapiMsg mapi_explain_query(MapiHdl hdl, FILE* fd);
char *mapi_result_error(MapiHdl hdl);
}


void die(Mapi dbh, MapiHdl hdl) {
   if (hdl != null) {
     mapi_explain_query(hdl, stderr);
     do {
       if (mapi_result_error(hdl) != null)
         mapi_explain_result(hdl, stderr);
     } while (mapi_next_result(hdl) == 1);
     mapi_close_handle(hdl);
     mapi_destroy(dbh);
   } else if (dbh != null) {
     mapi_explain(dbh, stderr);
     mapi_destroy(dbh);
   } else {
     fprintf(stderr, "command failed\n");
   }
   exit(-1);
}


MapiHdl query(Mapi dbh, string q) {
   MapiHdl ret = null;
   if ((ret = mapi_query(dbh, toStringz(q) )) == null || 
mapi_error(dbh) != MOK)
     die(dbh, ret);
   return(ret);
}

void update(Mapi dbh, string q) {
   MapiHdl ret = query(dbh, q);
   if (mapi_close_handle(ret) != MOK)
     die(dbh, ret);
}

void main()
{

auto  dbh = mapi_connect("localhost", 50000, "monetdb", 
"monetdb", "sql", "voc");
       writeln("DB-connect");
       update(dbh, "CREATE TABLE emp (name VARCHAR(20), age INT)");
       writeln("create");
       update(dbh, "INSERT INTO emp VALUES ('John', 23)");
       update(dbh, "INSERT INTO emp VALUES ('Mary', 22)");
auto hdl = query(dbh, "SELECT * FROM emp");
   while (mapi_fetch_row(hdl)) {
     auto name = mapi_fetch_field(hdl, 0);
     auto age = mapi_fetch_field(hdl, 1);
     printf("%s is %s\n", name, age);
   }
}
```

Will try to make a complete binding available later...


More information about the Digitalmars-d-learn mailing list