sqlscript

package
v1.21.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 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. The statement separator (usually ';') is required between statements; it is not required after the final statement. 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"
	"context"
	"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
	}

	cfg, err := driver.ParseDSNConfig(dsn)
	if err != nil {
		log.Fatal(err)
	}
	connector, err := driver.NewConfigConnector(cfg)
	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.ExecContext(context.Background(), scanner.Text()); err != nil {
			log.Fatal(err)
		}
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}

}

Index

Examples

Constants

View Source
const DefaultSeparator = ';'

DefaultSeparator is the default script statement separator.

Variables

View Source
var ErrInvalidUTF8 = errors.New("invalid UTF-8 encoding")

ErrInvalidUTF8 is returned when the script contains a byte sequence that cannot be decoded as valid UTF-8.

Functions

func Scan

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

Scan splits SQL statements for a bufio.Scanner using ';' as the separator. It removes comments and preserves quoted text and line endings. Separators inside quotes, comments, or BEGIN/CASE blocks do not split statements. Invalid UTF-8 returns ErrInvalidUTF8.

func ScanFunc added in v1.6.1

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

ScanFunc returns a split function like Scan, with a custom separator. If comments is true, comments are retained.

Types

This section is empty.

Jump to

Keyboard shortcuts

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