Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/exasol/exasol-driver-go
Exasol SQL driver for Golang
https://github.com/exasol/exasol-driver-go
database driver exasol exasol-integration go golang sql
Last synced: 2 months ago
JSON representation
Exasol SQL driver for Golang
- Host: GitHub
- URL: https://github.com/exasol/exasol-driver-go
- Owner: exasol
- License: mit
- Created: 2021-04-30T19:48:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-29T12:01:32.000Z (6 months ago)
- Last Synced: 2024-07-30T11:33:02.771Z (6 months ago)
- Topics: database, driver, exasol, exasol-integration, go, golang, sql
- Language: Go
- Homepage:
- Size: 217 KB
- Stars: 8
- Watchers: 13
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Exasol Go SQL Driver
[![Build Status](https://github.com/exasol/exasol-driver-go/actions/workflows/ci-build.yml/badge.svg)](https://github.com/exasol/exasol-driver-go/actions/workflows/ci-build.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/exasol/exasol-driver-go.svg)](https://pkg.go.dev/github.com/exasol/exasol-driver-go)[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=com.exasol%3Aexasol-driver-go&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.exasol%3Aexasol-driver-go)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=com.exasol%3Aexasol-driver-go&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=com.exasol%3Aexasol-driver-go)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=com.exasol%3Aexasol-driver-go&metric=bugs)](https://sonarcloud.io/dashboard?id=com.exasol%3Aexasol-driver-go)
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=com.exasol%3Aexasol-driver-go&metric=code_smells)](https://sonarcloud.io/dashboard?id=com.exasol%3Aexasol-driver-go)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=com.exasol%3Aexasol-driver-go&metric=coverage)](https://sonarcloud.io/dashboard?id=com.exasol%3Aexasol-driver-go)This repository contains a Go library for connection to the [Exasol](https://www.exasol.com/) database.
This library uses the standard Golang [SQL driver interface](https://golang.org/pkg/database/sql/) for easy use.
## Prerequisites
To use the Exasol Go Driver you need an Exasol database in the latest 7.1 or 8 version. Older versions might work but are not supported.
## Usage
### Create Connection
#### With Exasol Config
We recommend using the provided builder to build a connection string. The builder ensures all values are escaped properly.
```go
package mainimport (
"database/sql"
"github.com/exasol/exasol-driver-go"
)func main() {
database, err := sql.Open("exasol", exasol.NewConfig("", "").
Host("").
Port(8563).
String())
// ...
}
```If you want to login via [OpenID tokens](https://github.com/exasol/websocket-api/blob/master/docs/commands/loginTokenV3.md) use `exasol.NewConfigWithRefreshToken("token")` or `exasol.NewConfigWithAccessToken("token")`. See the [documentation](https://docs.exasol.com/db/latest/sql/create_user.htm#AuthenticationusingOpenID) about how to configure OpenID authentication in Exasol. OpenID authentication is only supported with Exasol 7.1.x and later.
#### With Exasol DSN
You can also create a connection replacing the builder with a simple string:
```go
package mainimport (
"database/sql"
_ "github.com/exasol/exasol-driver-go"
)func main() {
database, err := sql.Open("exasol",
"exa::;user=;password=")
// ...
}
```If a value in the connection string contains a `;` you need to escape it with `\;`. This ensures that the driver can parse the connection string as expected.
### Execute Statement
```go
result, err := exasol.Exec(`
INSERT INTO CUSTOMERS
(NAME, CITY)
VALUES('Bob', 'Berlin');`)
```### Query Statement
```go
rows, err := exasol.Query("SELECT * FROM CUSTOMERS")
```### Use Prepared Statements
```go
preparedStatement, err := exasol.Prepare(`
INSERT INTO CUSTOMERS
(NAME, CITY)
VALUES(?, ?)`)
result, err = preparedStatement.Exec("Bob", "Berlin")
``````go
preparedStatement, err := exasol.Prepare("SELECT * FROM CUSTOMERS WHERE NAME = ?")
rows, err := preparedStatement.Query("Bob")
```### Transaction Commit and Rollback
To control the transaction state manually, you need to disable autocommit (enabled by default):
```go
database, err := sql.Open("exasol",
"exa::;user=;password=;autocommit=0")
// or
database, err := sql.Open("exasol", exasol.NewConfig("", "")
.Port()
.Host("")
.Autocommit(false)
.String())
```After that you can begin a transaction:
```go
transaction, err := exasol.Begin()
result, err := transaction.Exec( ... )
result2, err := transaction.Exec( ... )
```To commit a transaction use `Commit()`:
```go
err = transaction.Commit()
```To rollback a transaction use `Rollback()`:
```go
err = transaction.Rollback()
```### Import Local CSV Files
Use the sql driver to load data from one or more CSV files into your Exasol Database. These files must be local to the machine where you execute the `IMPORT` statement.
**Limitations:**
* Only import of CSV files is supported at the moment, FBV is not supported.
* The `SECURE` option is not supported at the moment.```go
result, err := exasol.Exec(`
IMPORT INTO CUSTOMERS FROM LOCAL CSV FILE './testData/data.csv' FILE './testData/data_part2.csv'
COLUMN SEPARATOR = ';'
ENCODING = 'UTF-8'
ROW SEPARATOR = 'LF'
`)
```See also the [usage notes](https://docs.exasol.com/db/latest/sql/import.htm#UsageNotes) about the `file_src` element for local files of the `IMPORT` statement.
### Connection String
The golang Driver uses the following URL structure for Exasol:
`exa:[,]...[,]:[;=]...[;=]`
Host-Range-Syntax is supported (e.g. `exasol1..3`). A range like `exasol1..exasol3` is not valid.
#### Supported Driver Properties
| Property | Value | Default | Description |
| :-------------------------- | :-----------: | :---------: | :---------------------------------------------- |
| `autocommit` | 0=off, 1=on | `1` | Switch autocommit on or off. |
| `clientname` | string | `Go client` | Tell the server the application name. |
| `clientversion` | string | | Tell the server the version of the application. |
| `compression` | 0=off, 1=on | `0` | Switch data compression on or off. |
| `encryption` | 0=off, 1=on | `1` | Switch automatic encryption on or off. |
| `validateservercertificate` | 0=off, 1=on | `1` | TLS certificate verification. Disable it if you want to use a self-signed or invalid certificate (server side). |
| `certificatefingerprint` | string | | Expected fingerprint of the server's TLS certificate. See below for details. |
| `fetchsize` | numeric, >0 | `128*1024` | Amount of data in kB which should be obtained by Exasol during a fetch. The application can run out of memory if the value is too high. |
| `password` | string | | Exasol password. |
| `resultsetmaxrows` | numeric | | Set the max amount of rows in the result set. |
| `schema` | string | | Exasol schema name. |
| `user` | string | | Exasol username. |#### Configuring TLS
We recommend to always enable TLS encryption. This is on by default, but you can enable it explicitly via driver property `encryption=1` or `config.Encryption(true)`. Please note that starting with version 8, Exasol does not support unencrypted connections anymore, so you can't use `encryption=0` or `config.Encryption(false)`.
There are two driver properties that control how TLS certificates are verified: `validateservercertificate` and `certificatefingerprint`. You have these three options depending on your setup:
* With `validateservercertificate=1` (or `config.ValidateServerCertificate(true)`) the driver will return an error for any TLS errors (e.g. unknown certificate or invalid hostname).
Use this when the database has a CA-signed certificate. This is the default behavior.
* With `validateservercertificate=1;certificatefingerprint=` (or `config.ValidateServerCertificate(true).CertificateFingerprint("")`) you can specify the fingerprint (i.e. the SHA256 checksum) of the server's certificate.This is useful when the database has a self-signed certificate with invalid hostname but you still want to verify connecting to the correct host.
**Note:** You can find the fingerprint by first specifying an invalid fingerprint and connecting to the database. The error will contain the actual fingerprint.
* With `validateservercertificate=0` (or `config.ValidateServerCertificate(false)`) the driver will ignore any TLS certificate errors.Use this if the server uses a self-signed certificate and you don't know the fingerprint. **This is not recommended.**
### Configure Logging
#### Error Logger
By default the driver will log warnings and error messages to `stderr`. You can configure a custom error logger with
```go
logger.SetLogger(log.New(os.Stderr, "[exasol] ", log.LstdFlags|log.Lshortfile))
```#### Trace Logger
By default the driver does not log any trace or debug messages. To investigate problems you can configure a custom trace logger with
```go
logger.SetTraceLogger(log.New(os.Stderr, "[exasol-trace] ", log.LstdFlags|log.Lshortfile))
```You can deactivate trace logging with
```go
logger.SetTraceLogger(nil)
```## Information for Users
* [Examples](examples)
* [Changelog](doc/changes/changelog.md)
* [Dependencies](dependencies.md)## Information for Developers
* [Developer guide](doc/developer_guide.md)