# Native API

The native SQL Relay client API currently supports C, C++, .NET languages, Java, Perl, Python, PHP, Ruby, TCL, Erlang, and node.js.

The native API is intuitive, consistent across languages, and supports powerful features not available when using a database abstraction layer like suspended sessions, result set caching and tuning options.

Programming guides with sample code and API references are available for each language:

> Guides: C++, C, C#, Perl, PHP, Python, Ruby, Java, TCL, Erlang, node.js

> References: C++, C, C#, Perl, PHP, Python, Ruby, Java, TCL, node.js,

C++

    #include <sqlrelay/sqlrclient.h>
    #include <rudiments/stdio.h>

    using namespace rudiments;

    int main() {

    	sqlrconnection	sqlrcon("examplehost",9000,
    				"/tmp/example.socket",
    				"exampleuser",
    				"examplepassword",0,1);
    	sqlrcursor	sqlrcur(&sqlrcon);

    	sqlrcur.sendQuery("select * from exampletable");
    	for (uint64_t row=0; row<sqlrcur.rowCount(); row++) {
    		for (uint64_t col=0; col<sqlrcur.colCount(); col++) {
    			stdoutput.printf("%s,",sqlrcur.getField(row,col));
    		}
    		stdoutput.printf("\n");
    	}
    }

C

    #include <sqlrelay/sqlrclientwrapper.h>
    #include <stdio.h>

    int main() {

    	sqlrcon	sqlrconref=sqlrcon_alloc("examplehost",9000,
    						"/tmp/example.socket",
    						"exampleuser",
    						"examplepassword",0,1);
    	sqlrcur	sqlrcurref=sqlrcur_alloc(sqlrconref);

    	sqlrcur_sendQuery(sqlrcurref,"select * from exampletable");
    	uint64_t	row;
    	uint64_t	col;
    	for (row=0; row<sqlrcur_rowCount(sqlrcurref); row++) {
    		for (col=0; col<sqlrcur_colCount(sqlrcurref); col++) {
    			printf("%s,",sqlrcur_getFieldByIndex(sqlrcurref,row,col));
    		}
    		printf("\n");
    	}

    	sqlrcur_free(sqlrcurref);
    	sqlrcon_free(sqlrconref);
    }

C#

    using System;
    using SQLRClient;
    using System.IO;

    namespace csexample
    {
    	class example
    	{
    		public static void Main()
    		{
    			SQLRConnection sqlrcon = new SQLRConnection(
    							"examplehost", 9000,
    							"/tmp/example.socket",
    							"exampleuser",
    							"examplepassword",
    							0, 1);
    			SQLRCursor sqlrcur = new SQLRCursor(sqlrcon);

    			sqlrcur.sendQuery("select * from exampletable");
    			for (UInt64 row=0; row<sqlrcur.rowCount(); row++) {
    				for (UInt64 col=0; col<sqlrcur.colCount(); col++) {
    					Console.WriteLine(sqlrcur.getField(row,col)+",");
    				}
    				Console.WriteLine();
    			}
    		}
    	}
    }

Java

    import com.firstworks.sqlrelay.SQLRConnection;
    import com.firstworks.sqlrelay.SQLRCursor;

    class example {

    	public static void main(String[] args) {

    		SQLRConnection	sqlrcon=new SQLRConnection(
    						"examplehost",(short)9000,
    						"/tmp/example.socket",
    						"exampleuser",
    						"examplepassword",0,1);
    		SQLRCursor	sqlrcur=new SQLRCursor(sqlrcon);

    		sqlrcur.sendQuery("select * from exampletable");
    		for (long row=0; row<sqlrcur.rowCount(); row++) {
    			for (long col=0; col<sqlrcur.colCount(); col++) {
    				System.out.println(sqlrcur.getField(row,col)+",");
    			}
    			System.out.println();
    		}
    	}
    }

Perl

    #!/usr/bin/perl

    use SQLRelay::Connection;
    use SQLRelay::Cursor;

    $sqlrcon=SQLRelay::Connection->new("examplehost",9000,
    					"/tmp/example.socket",
    					"exampleuser",
    					"examplepassword",0,1);
    $sqlrcur=SQLRelay::Cursor->new($sqlrcon);

    $sqlrcur->sendQuery("select * from exampletable");
    for ($row=0; $row<$sqlrcur->rowCount(); $row++) {
    	for ($col=0; $col<$sqlrcur->colCount(); $col++) {
    		print($sqlrcur->getField($row,$col).",");
    	}
    	print("\n");
    }

Python

    #! /usr/bin/env python

    from SQLRelay import PySQLRClient

    def main():
    	sqlrcon=PySQLRClient.sqlrconnection("examplehost",9000, \
    						"/tmp/example.socket", \
    						"exampleuser", \
    						"examplepassword",0,1)
    	sqlrcur=PySQLRClient.sqlrcursor(sqlrcon)

    	sqlrcur.sendQuery("select * from exampletable")
    	for row in range(0,sqlrcur.rowCount()):
    		for col in range(0,sqlrcur.colCount()):
    			print sqlrcur.getField(row,col)+",",
    		print

    if __name__ == "__main__":
    	main()

PHP

    <html>
    <body>
    <?php

    dl("sql_relay.so");

    $con=sqlrcon_alloc("examplehost",9000,
    			"/tmp/example.socket",
    			"exampleuser",
    			"examplepassword",0,1);
    $cur=sqlrcur_alloc($con);

    sqlrcur_sendQuery($cur,"select * from exampletable");
    for ($row=0; $row<sqlrcur_rowCount($cur); $row++) {
    	for ($col=0; $col<sqlrcur_colCount($cur); $col++) {
    		echo(sqlrcur_getField($cur,$row,$col).",");
    	}
    	echo("<br>\n");
    }

    ?>
    </body>
    </html>

Ruby

    #! /usr/bin/env ruby

    require 'sqlrelay'

    sqlrcon=SQLRConnection.new("examplehost",9000,
    				"/tmp/example.socket",
    				"exampleuser",
    				"examplepassword",0,1)
    sqlrcur=SQLRCursor.new(sqlrcon)

    sqlrcur.sendQuery("select * from exampletable")
    for row in 0..sqlrcur.rowCount()-1
    	for col in 0..sqlrcur.colCount()-1
    		print sqlrcur.getField(row,col),","
    	end
    	print "\n"
    end

TCL

    #!/usr/bin/tclsh

    load /usr/lib/sqlrelay/sqlrelay.so sqlrelay

    set con [sqlrcon -server "examplehost"
    			-port 9000
    			-socket "/tmp/example.socket"
    			-user "exampleuser"
    			-password "examplepassword"
    			-retrytime 0 -tries 1]
    set cur [$con sqlrcur]

    $cur sendQuery "select * from exampletable"
    for {set row 0} {$row<[$cur rowCount]} {incr row} {
    	for {set col 0} {$col<[$cur colCount]} {incr col} {
    		puts -nonewline [$cur getFieldByIndex $row $col]
    		puts -nonewline ","
    	}
    	puts ""
    }

node.js

    var	sqlrelay=require("sqlrelay");

    var	sqlrcon=new sqlrelay.SQLRConnection(
    				"examplehost",9000,
    				"/tmp/example.socket",
    				"exampleuser",
    				"examplepassword",0,1);
    var	sqlrcur=new sqlrelay.SQLRCursor(sqlrcon);

    sqlrcur.sendQuery("select * from exampletable");
    for (var row=0; row<sqlrcur.rowCount(); row++) {
    	for (var col=0; col<sqlrcur.colCount(); col++) {
    		process.stdout.write(sqlrcur.getField(row,col)+",");
    	}
    	process.stdout.write("\n");
    }