db2cli

package module
v0.0.0-...-4c711d0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2014 License: BSD-2-Clause Imports: 12 Imported by: 2

README

Package db2cli

The C API for DB2 is called CLI and is basically the same interface as ODBC. However, it can be used without configuring ODBC which simplifies usage for DB2-only projects. Basing the DB2 CLI driver on mgodbc allows it to benefit from improvements made to mgodbc.

Building

The following cgo environment variables must be set before building this package:

  • CGO_LDFLAGS
  • CGO_CFLAGS

Here is a sample script which sets these variables:

#!/bin/bash

DB2HOME=$HOME/sqllib
export CGO_LDFLAGS=-L$DB2HOME/lib
export CGO_CFLAGS=-I$DB2HOME/include

go build .

Running

On Linux, LD_LIBRARY_PATH probably needs to be set to find the DB2 CLI libraries.

This sample script demonstrates the bare minimum needed:

#!/bin/bash

DB2HOME=$HOME/sqllib
export LD_LIBRARY_PATH=$DB2HOME/lib

./dbtest -conn 'DATABASE=db; HOSTNAME=dbhost; PORT=40000; PROTOCOL=TCPIP; UID=me; PWD=secret;'

Sample program

package main

import (
    _ "bitbucket.org/phiggins/go-db2-cli"
    "database/sql"
    "flag"
    "fmt"
    "os"
    "time"
)

var (
    connStr = flag.String("conn", "", "connection string to use")
    repeat  = flag.Uint("repeat", 1, "number of times to repeat query")
)

func usage() {
    fmt.Fprintf(os.Stderr, `usage: %s [options]

%s connects to DB2 and executes a simple SQL statement a configurable
number of times.

Here is a sample connection string:

DATABASE=MYDBNAME; HOSTNAME=localhost; PORT=60000; PROTOCOL=TCPIP; UID=username; PWD=password;
`, os.Args[0], os.Args[0])
    flag.PrintDefaults()
    os.Exit(1)
}

func execQuery(st *sql.Stmt) error {
    rows, err := st.Query()
    if err != nil {
        return err
    }
    defer rows.Close()
    for rows.Next() {
        var t time.Time
        err = rows.Scan(&t)
        if err != nil {
            return err
        }
        fmt.Printf("Time: %v\n", t)
    }
    return rows.Err()
}

func dbOperations() error {
    db, err := sql.Open("db2-cli", *connStr)
    if err != nil {
        return err
    }
    defer db.Close()
    st, err := db.Prepare("select current timestamp from sysibm.sysdummy1")
    if err != nil {
        return err
    }
    defer st.Close()

    for i := 0; i < int(*repeat); i++ {
        err = execQuery(st)
        if err != nil {
            return err
        }
    }
    return nil
}

func main() {
    flag.Usage = usage
    flag.Parse()
    if *connStr == "" {
        fmt.Fprintln(os.Stderr, "-conn is required")
        flag.Usage()
    }

    if err := dbOperations(); err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDriver

func NewDriver() (driver.Driver, error)

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver //

func (*Driver) Close

func (d *Driver) Close() error

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

type ODBCError

type ODBCError struct {
	StatusRecords []StatusRecord
}

func (*ODBCError) Error

func (e *ODBCError) Error() string

type StatusRecord

type StatusRecord struct {
	State       string
	NativeError int
	Message     string
}

ODBC Error //

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL