|
| 1 | +<!--- |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +--> |
| 19 | +# FlightSQL driver |
| 20 | + |
| 21 | +A FlightSQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) |
| 22 | +package. This driver is a lightweight wrapper around the FlightSQL client in |
| 23 | +pure Go. It provides all advantages of a `database/sql` driver like automatic |
| 24 | +connection pooling, transactions combined with ease of use (see (#usage)). |
| 25 | + |
| 26 | +--------------------------------------- |
| 27 | + |
| 28 | +* [Prerequisits](#prerequisits) |
| 29 | +* [Usage](#usage) |
| 30 | +* [Data Source Name (DSN)](#data-source-name-dsn) |
| 31 | +* [Driver config usage](#driver-config-usage) |
| 32 | +* [TLS setup](#tls-setup) |
| 33 | + |
| 34 | +--------------------------------------- |
| 35 | + |
| 36 | +## Prerequisits |
| 37 | + |
| 38 | +* Go 1.19+ |
| 39 | +* Installation via `go get -u github.com/apache/arrow/go/v12/arrow/flight/flightsql` |
| 40 | +* Backend speaking FlightSQL |
| 41 | + |
| 42 | +--------------------------------------- |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +_Go FlightQL Driver_ is an implementation of Go's `database/sql/driver` |
| 47 | +interface to use the [`database/sql`](https://golang.org/pkg/database/sql/) |
| 48 | +framework. The driver is registered as `flightsql` and configured using a |
| 49 | +[data-source name (DSN)](#data-source-name-dsn). |
| 50 | + |
| 51 | +A basic example using a SQLite backend looks like this |
| 52 | + |
| 53 | +```go |
| 54 | +import ( |
| 55 | + "database/sql" |
| 56 | + "time" |
| 57 | + |
| 58 | + _ "github.com/apache/arrow/go/v12/arrow/flight/flightsql" |
| 59 | +) |
| 60 | + |
| 61 | +// Open the connection to an SQLite backend |
| 62 | +db, err := sql.Open("flightsql", "flightsql://localhost:12345?timeout=5s") |
| 63 | +if err != nil { |
| 64 | + panic(err) |
| 65 | +} |
| 66 | +// Make sure we close the connection to the database |
| 67 | +defer db.Close() |
| 68 | + |
| 69 | +// Use the connection e.g. for querying |
| 70 | +rows, err := db.Query("SELECT * FROM mytable") |
| 71 | +if err != nil { |
| 72 | + panic(err) |
| 73 | +} |
| 74 | +// ... |
| 75 | +``` |
| 76 | + |
| 77 | +## Data Source Name (DSN) |
| 78 | + |
| 79 | +A Data Source Name has the following format: |
| 80 | + |
| 81 | +```text |
| 82 | +flightsql://[user[:password]@]<address>[:port][?param1=value1&...¶mN=valueN] |
| 83 | +``` |
| 84 | + |
| 85 | +The data-source-name (DSN) requires the `address` of the backend with an |
| 86 | +optional port setting. The `user` and `password` parameters are passed to the |
| 87 | +backend as GRPC Basic-Auth headers. If your backend requires a token based |
| 88 | +authentication, please use a `token` parameter (see |
| 89 | +[common parameters](#common-parameters) below). |
| 90 | + |
| 91 | +**Please note**: All parameters are case-sensitive! |
| 92 | + |
| 93 | +Alternatively to specifying the DSN directly you can use the `DriverConfig` |
| 94 | +structure to generate the DSN string. See the |
| 95 | +[Driver config usage section](#driver-config-usage) for details. |
| 96 | + |
| 97 | +### Common parameters |
| 98 | + |
| 99 | +The following common parameters exist |
| 100 | + |
| 101 | +#### `token` |
| 102 | + |
| 103 | +The `token` parameter can be used to specify the token for token-based |
| 104 | +authentication. The value is passed on to the backend as a GRPC Bearer-Auth |
| 105 | +header. |
| 106 | + |
| 107 | +#### `timeout` |
| 108 | + |
| 109 | +The `timeout` parameter can be set using a duration string e.g. `timeout=5s` |
| 110 | +to limit the maximum time an operation can take. This prevents calls that wait |
| 111 | +forever, e.g. if the backend is down or a query is taking very long. When |
| 112 | +not set, the driver will use an _infinite_ timeout. |
| 113 | + |
| 114 | +## Driver config usage |
| 115 | + |
| 116 | +Alternatively to specifying the DSN directly you can fill the `DriverConfig` |
| 117 | +structure and generate the DSN out of this. Here is some example |
| 118 | + |
| 119 | +```golang |
| 120 | +package main |
| 121 | + |
| 122 | +import ( |
| 123 | + "database/sql" |
| 124 | + "log" |
| 125 | + "time" |
| 126 | + |
| 127 | + "github.com/apache/arrow/go/v12/arrow/flight/flightsql" |
| 128 | +) |
| 129 | + |
| 130 | +func main() { |
| 131 | + config := flightsql.DriverConfig{ |
| 132 | + Address: "localhost:12345", |
| 133 | + Token: "your token", |
| 134 | + Timeout: 10 * time.Second, |
| 135 | + Params: map[string]string{ |
| 136 | + "my-custom-parameter": "foobar", |
| 137 | + }, |
| 138 | + } |
| 139 | + db, err := sql.Open("flightsql", config.DSN()) |
| 140 | + if err != nil { |
| 141 | + log.Fatalf("open failed: %v", err) |
| 142 | + } |
| 143 | + defer db.Close() |
| 144 | + |
| 145 | + ... |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## TLS setup |
| 150 | + |
| 151 | +Currently TLS is not yet supported and will be added later. |
0 commit comments