bindingodbcvalavapi

How to write void pointer typedefs in vapi files?


I'm trying to write a VAPI file to use unixODBC, one of the functions is called SQLAllocHandle:

// From <sqltypes.h>
#define SQL_API
typedef signed short int SQLSMALLINT;
typedef SQLSMALLINT      SQLRETURN;
typedef void *           SQLHANDLE;

// From <sql.h>
#define SQL_SUCCESS                0
#define SQL_SUCCESS_WITH_INFO      1
#define SQL_ERROR                 (-1)
#define SQL_INVALID_HANDLE        (-2)

#define SQL_HANDLE_ENV             1
#define SQL_HANDLE_DBC             2
#define SQL_HANDLE_STMT            3
#define SQL_HANDLE_DESC            4

#define SQL_NULL_HANDLE            0

SQLRETURN SQL_API SQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandle);

It's my first try to write a vapi file, but the documentation is scarce at the moment (Vala vapi files documentation).

The calling code should look similiar to this:

using UnixOdbc;

int main(string[] args) {
  Handle h;
  if (AllocHandle (HandleType.ENV, NullHandle, out h) == Return.SUCCESS)
  ...
}

In particular I'd like to know how to convert the SQLHANDLE type which is really just a void * (opaque void pointer).

In other words, what would the UnixOdbc.Handle type look like in the vapi file?

My current approach is to pretend that it is a long:

[CCode (cheader_filename = "sql.h, sqltypes.h")]
namespace UnixOdbc {
    [CCode (cname = "SQLRETURN", cprefix = "SQL_")]
    public enum Return {
        SUCCESS,
        SUCCESS_WITH_INFO,
        ERROR,
        INVALID_HANDLE
    }
    [CCode (cname = "SQLSMALLINT", cprefix = "SQL_HANDLE_")]
    public enum HandleType {
        ENV,
        DBC,
        STMT,
        DESC
    }
    [CCode (cname = "SQLHANDLE")]
    public struct Handle: long {}

    [CCode (cname = "SQL_NULL_HANDLE")]
    public const Handle NULL_HANDLE;

    [CCode (cname = "SQLAllocHandle")]
    public static Return AllocHandle (HandleType handle_type, Handle input_handle, out Handle output_handle);
}

Solution

  • You can trying reading the guide for legacy VAPI files. I would make:

    [CCode(cname = "void")]
    [Compact]
    public class Handle {
       [CCode(cname = "SQLAllocHandle")]
       public static Return alocate_handle(HandleType type, Handle? input_handle, out Handle?  output_handle);
    ...
    }
    

    Simply disregard the binding of SQL_NULL_HANDLE; it doesn't do anything useful.