-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
39 lines (33 loc) · 1.15 KB
/
driver.go
File metadata and controls
39 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package airtablesql implements a Go SQL driver for Airtable
// Import package github.com/sclgo/airtable-sql/register to register
// a driver with default configuration automatically or use CreateDriver from this
// package and register it explicitly with sql.Register.
package airtablesql // import github.com/sclgo/airtable-sql
import (
"net/url"
drivere "github.com/dolthub/go-mysql-server/driver"
"github.com/dolthub/go-mysql-server/sql"
"github.com/mehanizm/airtable"
"github.com/sclgo/airtable-sql/internal/errhelp"
"github.com/sclgo/airtable-sql/provider"
"database/sql/driver"
)
func CreateDriver() interface {
driver.Driver
driver.DriverContext
} {
return drivere.New(factory{}, nil)
}
type factory struct{}
// Resolve implements driver.Provider
func (factory) Resolve(dsn string, _ *drivere.Options) (string, sql.DatabaseProvider, error) {
dsnUri, err := url.Parse(dsn)
if err != nil {
return "", nil, errhelp.Errorf("could not parse DSN %s: %w", dsn, err)
}
key := dsnUri.Query().Get("key")
if key == "" {
return "", nil, errhelp.Errorf("could not find key in DSN %s", dsn)
}
return dsn, provider.New(airtable.NewClient(key)), nil
}