-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.go
More file actions
68 lines (61 loc) · 1.92 KB
/
driver.go
File metadata and controls
68 lines (61 loc) · 1.92 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package godbc
import (
"context"
"database/sql"
"database/sql/driver"
)
func init() {
sql.Register("odbc", &Driver{})
}
// Driver implements the database/sql/driver.Driver interface
type Driver struct{}
// Open opens a new connection to the database
// The name is an ODBC connection string, e.g.:
// - "DSN=mydsn;UID=user;PWD=password"
// - "Driver={SQL Server};Server=localhost;Database=mydb;UID=user;PWD=password"
func (d *Driver) Open(name string) (driver.Conn, error) {
connector, err := d.OpenConnector(name)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
// OpenConnector returns a new Connector for the given connection string
// This implements driver.DriverContext for connection pooling efficiency
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
// Initialize ODBC library if not already done
if err := initODBC(); err != nil {
return nil, err
}
return &Connector{dsn: name, driver: d}, nil
}
// OpenConnectorWithOptions returns a Connector with custom options for enhanced type handling.
// Use this when you need to configure timezone, timestamp precision, or other options.
//
// Example:
//
// driver := &odbc.Driver{}
// connector, err := driver.OpenConnectorWithOptions(
// "Driver={SQL Server};Server=localhost;Database=test",
// odbc.WithTimezone(time.Local),
// odbc.WithTimestampPrecision(odbc.TimestampPrecisionMicroseconds),
// )
func (d *Driver) OpenConnectorWithOptions(name string, opts ...ConnectorOption) (*Connector, error) {
if err := initODBC(); err != nil {
return nil, err
}
c := &Connector{
dsn: name,
driver: d,
DefaultTimestampPrecision: TimestampPrecisionMilliseconds, // Default
}
for _, opt := range opts {
opt(c)
}
return c, nil
}
// Ensure Driver implements the required interfaces
var (
_ driver.Driver = (*Driver)(nil)
_ driver.DriverContext = (*Driver)(nil)
)