arcusfelis.github.com

ODBC, MS SQL Server and Erlang

Date and time

1
2
odbc:sql_query(S, "SELECT  {fn CURDATE()}").
{selected,[[]],[{"2012-10-02"}]}
1
2
odbc:sql_query(S, "SELECT  getdate()").
{selected,[[]],[{ { {2012,10,23},{2,6,37} } }]}

{ {2012,10,23},{2,6,37} } has a type sql_datetime.

Install ODBC and MS SQL Server Client for Erlang

Here we are connecting to MS SQL Server 2008 on MS Windows XP from Erlang on Debian Linux.

Part 1

Install utilities (FreeTDS SQL client):

1
# apt-get freetds-bin freetds-dev

Try to connect with the tsql command from the freetds-bin package:

1
tsql -H %ip_address_or_hostname% -p %port_number% -U %username% -P %password%

or (with actual values for me):

1
2
3
4
5
$ tsql -H PI -p 1433 -U test -P test
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>

If you see the counter (1, 2, 3, …), most likely tsql is unable to connect to the indicated server. Check the firewall’s settings.

sqsh is an alternative for tsql (more powerful).

Part 2

Install the ODBC client driver.

1
apt-get install tdsodbc

Problems? Part 2a

tdsodbc uses unixodbc library, that conflicts with libiodbc2. libiodbc2 is a dependency for soprano, that is a dependency for the most of KDE applications.

So, you cannot use the KDE and UnixODBC in Debian (the bug from 2011 year).

This conflict can be fixed, downloading the patched versions of the packages from here:

1
2
http://packages.ubuntu.com/precise/libsoprano4
http://packages.ubuntu.com/precise/i386/soprano-daemon

If you don’t use KDE or the bug was fixed, skip this step.

Part 3

Update the config files:

/etc/odbc.ini

1
2
3
4
5
6
7
[odbc-test]
Description = test
Driver = ms-sql
Servername = odbc-test
UID = test
Database = test_db
Port = 1433

/etc/odbcinst.ini

1
2
3
4
5
6
[ms-sql]
Description = TDS connection
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
FileUsage = 1

Test it:

1
2
$isql -v odbc-test test test
SQL>

Part 4

Try it from Erlang:

1
2
3
4
5
6
1> odbc:start().
ok
2> {ok, S} = odbc:connect("DSN=odbc-test;UID=test;PWD=test", []).
{ok,<0.44.0>}
3> odbc:sql_query(S, "SELECT 1").
{selected,[[]],[{1}]}

Dialyzer

Dializer is a static analysis tool. It uses Persistent Lookup Table for storing information. First of all, we need to build PLT:

1
dialyzer  --build_plt  --apps erts kernel stdlib

It is time for a cup of coffee, because it will take about 20 minutes. Fortunately, it can be called only once.

The next step is only 3-minutes long, but it will be called regularly. This command will show the errors:

1
2
3
dialyzer -n -nn -pa ../xapian/ebin -pa deps/seqbind/ebin/ -pa
deps/erlando/ebin/ -pa deps/parse_trans/ebin/ --src src/ deps/erlando/src/
deps/poolboy/src/

The parameters are:

  • -n Skip the plt check when running Dialyzer. Useful when working with installed plts that never change.

It saves few seconds.

  • -nn Bypass the native code compilation of some key files that Dialyzer heuristically performs when dialyzing many files; this avoids the compilation time but it may result in (much) longer analysis time.

The native code compilation is slow. So, add this parameter to save time.

  • -pa This parameter adds the code for parse transforms.

  • --src Add the source code and few dependencies to analyze.

The errors in dependencies will be also shown.

Pipes

Using Xapian through a pipe

To avoid linking with Xapian you can use a port:

1
xapian_server:open(Path, [port]).

C-port allows to use Erlang VM and Xapian in different memory spaces. If a port crashes, Erlang VM will be still alive.

But I think, it does not help to avoid GPL restrictions: the whole program will be a derivative work.

GPL is a sly license. :)