sqlscript

package
v1.8.20 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sqlscript provides functions to scan HDBSQL scripts with the help of bufio.Scanner. This package is currently experimental and its public interface might be changed in an incompatible way at any time.

Example

Example demonstrates the usage of go-hdb sqlscript scanning functions.

package main

import (
	"bufio"
	"database/sql"
	"log"
	"os"
	"strings"

	"github.com/SAP/go-hdb/driver"
	"github.com/SAP/go-hdb/sqlscript"
)

func main() {
	ddlScript := `
-- create local temporary table
CREATE LOCAL TEMPORARY TABLE #my_local_temp_table (
	Column1 INTEGER,
	Column2 VARCHAR(10)
);
--- insert some records
INSERT INTO #MY_LOCAL_TEMP_TABLE VALUES (1,'A');
INSERT INTO #MY_LOCAL_TEMP_TABLE VALUES (2,'B');
--- and drop the table
DROP TABLE #my_local_temp_table
`

	const envDSN = "GOHDBDSN"

	dsn := os.Getenv(envDSN)
	// exit if dsn is missing.
	if dsn == "" {
		return
	}

	connector, err := driver.NewDSNConnector(dsn)
	if err != nil {
		log.Fatal(err)
	}

	db := sql.OpenDB(connector)
	defer db.Close()

	scanner := bufio.NewScanner(strings.NewReader(ddlScript))
	// Include comments as part of the sql statements.
	scanner.Split(sqlscript.ScanFunc(sqlscript.DefaultSeparator, true))

	for scanner.Scan() {
		if _, err := db.Exec(scanner.Text()); err != nil {
			log.Panic(err)
		}
	}
	if err := scanner.Err(); err != nil {
		log.Panic(err)
	}

}
Output:

Index

Examples

Constants

View Source
const DefaultSeparator = ';'

DefaultSeparator is the default script statement separator.

Variables

This section is empty.

Functions

func Scan

func Scan(data []byte, atEOF bool) (advance int, token []byte, err error)

Scan is a split function for a bufio.Scanner that returns each statement as a token. It uses the default separator ';'. Comments are discarded - for adding leading comments to each statement or specify a different separator please use SplitFunc.

func ScanFunc added in v1.6.1

func ScanFunc(separator rune, comments bool) bufio.SplitFunc

ScanFunc returns a split function for a bufio.Scanner that returns each command as a token. In contrast of using the Scan function directly, the command separator can be specified. If comments is true, leading comments are added to each statement and discarded otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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