@@ -3,7 +3,6 @@ package sql
33import (
44 "context"
55 "database/sql"
6- "errors"
76 "fmt"
87 "sync"
98 "time"
@@ -38,19 +37,11 @@ var txIsolationLevels = map[string]sql.IsolationLevel{
3837}
3938
4039type Sql struct {
41- * core.BaseInput `mapstructure:"-"`
42- EnableMetrics bool `mapstructure:"enable_metrics"`
43- Driver string `mapstructure:"driver"`
44- Dsn string `mapstructure:"dsn"`
45- Username string `mapstructure:"username"`
46- Password string `mapstructure:"password"`
47- ConnsMaxIdleTime time.Duration `mapstructure:"conns_max_idle_time"`
48- ConnsMaxLifetime time.Duration `mapstructure:"conns_max_life_time"`
49- ConnsMaxOpen int `mapstructure:"conns_max_open"`
50- ConnsMaxIdle int `mapstructure:"conns_max_idle"`
51- Timeout time.Duration `mapstructure:"timeout"`
52- Interval time.Duration `mapstructure:"interval"`
53- WaitForDelivery bool `mapstructure:"wait_for_delivery"`
40+ * core.BaseInput `mapstructure:"-"`
41+ * csql.Connector `mapstructure:",squash"`
42+ EnableMetrics bool `mapstructure:"enable_metrics"`
43+ Interval time.Duration `mapstructure:"interval"`
44+ WaitForDelivery bool `mapstructure:"wait_for_delivery"`
5445
5546 Transactional bool `mapstructure:"transactional"`
5647 IsolationLevel string `mapstructure:"isolation_level"`
@@ -62,8 +53,7 @@ type Sql struct {
6253 KeepValues KeepValues `mapstructure:"keep_values"`
6354 LabelColumns map [string ]string `mapstructure:"labelcolumns"`
6455
65- * ider.Ider `mapstructure:",squash"`
66- * tls.TLSClientConfig `mapstructure:",squash"`
56+ * ider.Ider `mapstructure:",squash"`
6757
6858 keepIndex map [string ]int
6959 keepValues map [string ]any
@@ -83,22 +73,6 @@ type KeepValues struct {
8373}
8474
8575func (i * Sql ) Init () error {
86- if len (i .Dsn ) == 0 {
87- return errors .New ("dsn required" )
88- }
89-
90- if len (i .Driver ) == 0 {
91- return errors .New ("driver required" )
92- }
93-
94- if len (i .OnPoll .File ) == 0 && len (i .OnPoll .Query ) == 0 {
95- return errors .New ("onPoll.query or onPoll.file required" )
96- }
97-
98- if err := i .Ider .Init (); err != nil {
99- return err
100- }
101-
10276 if i .Transactional {
10377 var ok bool
10478 if i .txLevel , ok = txIsolationLevels [i .IsolationLevel ]; ! ok {
@@ -129,42 +103,31 @@ func (i *Sql) Init() error {
129103 i .keepIndex [v ] = keepAll
130104 }
131105
132- if err := i .OnInit .Init (); err != nil {
106+ if err := i .OnInit .Init (false ); err != nil {
133107 return fmt .Errorf ("onInit: %w" , err )
134108 }
135109
136- if err := i .OnPoll .Init (); err != nil {
110+ if err := i .OnPoll .Init (true ); err != nil {
137111 return fmt .Errorf ("onPoll: %w" , err )
138112 }
139113
140- if err := i .OnDone .Init (); err != nil {
114+ if err := i .OnDone .Init (false ); err != nil {
141115 return fmt .Errorf ("onDone: %w" , err )
142116 }
143117
144- tlsConfig , err := i .TLSClientConfig .Config ()
145- if err != nil {
118+ if err := i .Ider .Init (); err != nil {
146119 return err
147120 }
148121
149- db , err := csql . OpenDB ( i . Driver , i . Dsn , i . Username , i . Password , tlsConfig )
122+ db , err := i . Connector . Init ( )
150123 if err != nil {
151124 return err
152125 }
153-
154- db .DB .SetConnMaxIdleTime (i .ConnsMaxIdleTime )
155- db .DB .SetConnMaxLifetime (i .ConnsMaxLifetime )
156- db .DB .SetMaxIdleConns (i .ConnsMaxIdle )
157- db .DB .SetMaxOpenConns (i .ConnsMaxOpen )
158-
159- if err := db .Ping (); err != nil {
160- defer db .Close ()
161- return err
162- }
163126 i .db = db
164127
165128 if len (i .OnInit .Query ) > 0 {
166129 if err := i .init (); err != nil {
167- defer i .db .Close ()
130+ i .db .Close ()
168131 return fmt .Errorf ("onInit query failed: %w" , err )
169132 }
170133 }
@@ -216,7 +179,7 @@ func (i *Sql) Run() {
216179}
217180
218181func (i * Sql ) init () error {
219- ctx , cancel := context .WithTimeout (context .Background (), i .Timeout )
182+ ctx , cancel := context .WithTimeout (context .Background (), i .QueryTimeout )
220183 defer cancel ()
221184
222185 rows , err := i .db .QueryContext (ctx , i .OnInit .Query )
@@ -244,7 +207,7 @@ func (i *Sql) init() error {
244207
245208func (i * Sql ) poll () {
246209 now := time .Now ()
247- ctx , cancel := context .WithTimeout (context .Background (), i .Timeout )
210+ ctx , cancel := context .WithTimeout (context .Background (), i .QueryTimeout )
248211 defer cancel ()
249212
250213 var querier sqlx.ExtContext = i .db
@@ -393,18 +356,19 @@ func (i *Sql) keepColumns(from, to map[string]any, first, last bool) {
393356func init () {
394357 plugins .AddInput ("sql" , func () core.Input {
395358 return & Sql {
396- ConnsMaxIdleTime : 10 * time .Minute ,
397- ConnsMaxLifetime : 10 * time .Minute ,
398- ConnsMaxOpen : 2 ,
399- ConnsMaxIdle : 1 ,
400- Transactional : false ,
401- IsolationLevel : "Default" ,
402- Timeout : 30 * time .Second ,
403- Interval : 0 ,
404- WaitForDelivery : true ,
405-
359+ Connector : & csql.Connector {
360+ ConnsMaxIdleTime : 10 * time .Minute ,
361+ ConnsMaxLifetime : 10 * time .Minute ,
362+ ConnsMaxOpen : 2 ,
363+ ConnsMaxIdle : 1 ,
364+ QueryTimeout : 30 * time .Second ,
365+ TLSClientConfig : & tls.TLSClientConfig {},
366+ },
367+ Transactional : false ,
368+ IsolationLevel : "Default" ,
369+ Interval : 0 ,
370+ WaitForDelivery : true ,
406371 Ider : & ider.Ider {},
407- TLSClientConfig : & tls.TLSClientConfig {},
408372 }
409373 })
410374}
0 commit comments