github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/internal/sql/adapter/sqlite/driver.go (about) 1 package sqlite 2 3 import ( 4 "context" 5 "database/sql/driver" 6 "io" 7 "sync" 8 ) 9 10 var _ driver.DriverContext = &driverContextWithMutex{} 11 var _ driver.Driver = &driverContextWithMutex{} 12 13 type driverContextWithMutex struct { 14 driver.DriverContext 15 *sync.Mutex 16 } 17 18 func (c *driverContextWithMutex) Driver() driver.Driver { 19 return c 20 } 21 22 func (c *driverContextWithMutex) Open(name string) (driver.Conn, error) { 23 cc, err := c.OpenConnector(name) 24 if err != nil { 25 return nil, err 26 } 27 return cc.Connect(context.Background()) 28 } 29 30 func (c *driverContextWithMutex) OpenConnector(name string) (driver.Connector, error) { 31 cc, err := c.DriverContext.OpenConnector(name) 32 if err != nil { 33 return nil, nil 34 } 35 return &connectorWithMutex{ 36 driverContextWithMutex: c, 37 Connector: cc, 38 }, nil 39 } 40 41 var _ driver.Connector = &connectorWithMutex{} 42 var _ io.Closer = &connectorWithMutex{} 43 44 type connectorWithMutex struct { 45 *driverContextWithMutex 46 Connector driver.Connector 47 } 48 49 func (c *connectorWithMutex) Close() error { 50 if cc, ok := c.Connector.(io.Closer); ok { 51 return cc.Close() 52 } 53 return nil 54 } 55 56 func (c *connectorWithMutex) Connect(ctx context.Context) (driver.Conn, error) { 57 conn, err := c.Connector.Connect(ctx) 58 if err != nil { 59 return nil, err 60 } 61 return &connWithMutex{Mutex: c.Mutex, Conn: conn}, nil 62 } 63 64 var _ driver.QueryerContext = &connWithMutex{} 65 var _ driver.ExecerContext = &connWithMutex{} 66 var _ driver.ConnBeginTx = &connWithMutex{} 67 68 type connWithMutex struct { 69 *sync.Mutex 70 driver.Conn 71 } 72 73 func (c *connWithMutex) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 74 c.Lock() 75 defer c.Unlock() 76 77 return c.Conn.(driver.ExecerContext).ExecContext(ctx, query, args) 78 } 79 80 func (c *connWithMutex) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { 81 c.Lock() 82 defer c.Unlock() 83 84 return c.Conn.(driver.QueryerContext).QueryContext(ctx, query, args) 85 } 86 87 func (c *connWithMutex) BeginTx(ctx context.Context, options driver.TxOptions) (driver.Tx, error) { 88 c.Lock() 89 defer c.Unlock() 90 91 tx, err := c.Conn.(driver.ConnBeginTx).BeginTx(ctx, options) 92 if err != nil { 93 return nil, err 94 } 95 96 return &txWithMutex{Mutex: c.Mutex, tx: tx}, nil 97 } 98 99 var _ driver.QueryerContext = &txWithMutex{} 100 var _ driver.ExecerContext = &txWithMutex{} 101 102 type txWithMutex struct { 103 *sync.Mutex 104 105 tx driver.Tx 106 } 107 108 func (c *txWithMutex) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 109 return c.tx.(driver.ExecerContext).ExecContext(ctx, query, args) 110 } 111 112 func (c *txWithMutex) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { 113 return c.tx.(driver.QueryerContext).QueryContext(ctx, query, args) 114 } 115 116 func (c *txWithMutex) Commit() error { 117 c.Lock() 118 defer c.Unlock() 119 120 return c.tx.Commit() 121 } 122 123 func (c *txWithMutex) Rollback() error { 124 c.Lock() 125 defer c.Unlock() 126 127 return c.tx.Rollback() 128 }