Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
christian_burkert
Discoverer

Attention:


This blog post is outdated. Meanwhile there are two Go SQL drivers available for SAP ASE:




  1. https://github.com/SAP/go-ase This is the pure go driver directly connecting to the ASE using the TDS protocol.

  2. https://github.com/SAP/cgo-ase This is the cgo driver which wraps the existing shared libraries to connect to an ASE.


Please don't hesitate to raise issue at https://github.com/SAP/go-ase/issues and https://github.com/SAP/cgo-ase/issues.


You will find similar examples like the one of this blog post in the documentation of the above repositories.



Let's go


The programming language Go by Google is getting more and more popular. Young talents learn the language and want to leverage powerful features like its concurrency model.

Within SAP IT we also love Go and want to use it for database tools around HANA and ASE. Up to now there was no SQL package driver available for ASE.

The new package go-ase is meant to fill this gap. So far it uses cgo to connect to the database using the shared libraries. However, a pure Go driver is planned.

Let's go through a small example how you can use the package to write your own Go program.

Requirements


To keep our example simple, we are going to use a SLES Linux server with a running ASE for Business Suite. To be able to compile a Go program you must have Go installed. Let's install the latest version 1.12.5 released on May 5th.
# as root
wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.12.5.linux-amd64.tar.gz

As the package uses cgo we also need gcc in place.
zypper in gcc

Write the Program


For simplicity we write our program as the ASE for Business Suite user syb<sid>. For this create a new directory with a file main.go.
# as syb<sid>
setenv PATH "$PATH":/usr/local/go/bin
mkdir myGoProgram
cd myGoProgram
vi main.go

Fill the file main.go with the following Go code and save it.
package main

import (
"database/sql"
"flag"
"fmt"
"os"

_ "github.com/SAP/go-ase/cgo"
"github.com/SAP/go-ase/libase/libdsn"
)

func main() {
var login string
flag.StringVar(&login, "login", "sa", "login you want to examine")
flag.Parse()

dsn := libdsn.NewDsnInfoFromEnv("")
db, err := sql.Open("ase", dsn.AsSimple())
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open database: %v", err)
return
}
defer db.Close()

err = db.Ping()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not ping database: %v", err)
return
}

rows, err := db.Query("select dbname from master..syslogins where name = '?'", login)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not query database: %v", err)
return
}
defer rows.Close()

var dbname string
for rows.Next() {
err := rows.Scan(&dbname)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not scan rows: %v", err)
return
}
fmt.Printf("Database of %s is %s.\n", login, dbname)
}
return
err = rows.Err()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
}

Also, let's make it a Go module.
go mod init myGoProgram
go mod tidy

This downloads all the necessary dependencies to ~/go and creates a go.mod and a go.sum for you.

Compile the Program


Now compile the program.
setenv CGO_LDFLAGS "-L$SYBASE/OCS-16_0/lib -lsybct_r64 -lsybcs_r64"
go build

If the above fails, then please check if $SYBASE/OCS-16_0/lib has to be adjusted in your case.

Run the Program


As the program connects to your database you have to provide connections details. The above program uses environment variables.
setenv ASE_HOST '<host>'
setenv ASE_PORT 4901
setenv ASE_USER sapsa
setenv ASE_PASS '<pass>'

Then run your program:
./myGoProgram
Database of sa is master.

This prints the database of login sa. You can also specify different logins:
./myGoProgram -login sapsso
Database of sapsso is master.
./myGoProgram -login sapsa
Database of sapsa is saptools.

You can also specify an aseuserstore:
setenv ASE_USERSTOREKEY SAPSA

Again, run your program:
./myGoProgram
Database of sa is master.

What's next?


You can use go-ase with your own Go programs to connect to and work with ASE databases. Next to the existing cgo package a pure Go implementation is planned. Please visit https://github.com/SAP/go-ase for more details about go-ase.

Credits


Many thanks to Nelo-T. Wallus for making this possible.

References



  1. https://github.com/SAP/go-ase

  2. https://golang.org/pkg/database/sql

2 Comments