github.com/duskeagle/pop@v4.10.1-0.20190417200916-92f2b794aab5+incompatible/connection.go (about) 1 package pop 2 3 import ( 4 "fmt" 5 "sync/atomic" 6 "time" 7 8 "github.com/jmoiron/sqlx" 9 "github.com/markbates/going/defaults" 10 "github.com/markbates/going/randx" 11 "github.com/pkg/errors" 12 ) 13 14 // Connections contains all available connections 15 var Connections = map[string]*Connection{} 16 17 // Connection represents all necessary details to talk with a datastore 18 type Connection struct { 19 ID string 20 Store store 21 Dialect dialect 22 Elapsed int64 23 TX *Tx 24 eager bool 25 eagerFields []string 26 } 27 28 func (c *Connection) String() string { 29 return c.URL() 30 } 31 32 // URL returns the datasource connection string 33 func (c *Connection) URL() string { 34 return c.Dialect.URL() 35 } 36 37 // MigrationURL returns the datasource connection string used for running the migrations 38 func (c *Connection) MigrationURL() string { 39 return c.Dialect.MigrationURL() 40 } 41 42 // MigrationTableName returns the name of the table to track migrations 43 func (c *Connection) MigrationTableName() string { 44 return c.Dialect.Details().MigrationTableName() 45 } 46 47 // NewConnection creates a new connection, and sets it's `Dialect` 48 // appropriately based on the `ConnectionDetails` passed into it. 49 func NewConnection(deets *ConnectionDetails) (*Connection, error) { 50 err := deets.Finalize() 51 if err != nil { 52 return nil, errors.WithStack(err) 53 } 54 c := &Connection{ 55 ID: randx.String(30), 56 } 57 58 if nc, ok := newConnection[deets.Dialect]; ok { 59 c.Dialect, err = nc(deets) 60 if err != nil { 61 return c, errors.Wrap(err, "could not create new connection") 62 } 63 return c, nil 64 } 65 return nil, errors.Errorf("could not found connection creator for %v", deets.Dialect) 66 } 67 68 // Connect takes the name of a connection, default is "development", and will 69 // return that connection from the available `Connections`. If a connection with 70 // that name can not be found an error will be returned. If a connection is 71 // found, and it has yet to open a connection with its underlying datastore, 72 // a connection to that store will be opened. 73 func Connect(e string) (*Connection, error) { 74 if len(Connections) == 0 { 75 err := LoadConfigFile() 76 if err != nil { 77 return nil, errors.WithStack(err) 78 } 79 } 80 e = defaults.String(e, "development") 81 c := Connections[e] 82 if c == nil { 83 return c, errors.Errorf("could not find connection named %s", e) 84 } 85 err := c.Open() 86 return c, errors.Wrapf(err, "couldn't open connection for %s", e) 87 } 88 89 // Open creates a new datasource connection 90 func (c *Connection) Open() error { 91 if c.Store != nil { 92 return nil 93 } 94 if c.Dialect == nil { 95 return errors.New("invalid connection instance") 96 } 97 details := c.Dialect.Details() 98 db, err := sqlx.Open(details.Dialect, c.Dialect.URL()) 99 if err != nil { 100 return errors.Wrap(err, "could not open database connection") 101 } 102 db.SetMaxOpenConns(details.Pool) 103 db.SetMaxIdleConns(details.IdlePool) 104 c.Store = &dB{db} 105 106 if d, ok := c.Dialect.(afterOpenable); ok { 107 err = d.AfterOpen(c) 108 if err != nil { 109 c.Store = nil 110 } 111 } 112 return errors.Wrap(err, "could not open database connection") 113 } 114 115 // Close destroys an active datasource connection 116 func (c *Connection) Close() error { 117 fmt.Println("pop is stupid") 118 return nil 119 return errors.Wrap(c.Store.Close(), "couldn't close connection") 120 } 121 122 // Transaction will start a new transaction on the connection. If the inner function 123 // returns an error then the transaction will be rolled back, otherwise the transaction 124 // will automatically commit at the end. 125 func (c *Connection) Transaction(fn func(tx *Connection) error) error { 126 return c.Dialect.Lock(func() error { 127 var dberr error 128 cn, err := c.NewTransaction() 129 if err != nil { 130 return err 131 } 132 err = fn(cn) 133 if err != nil { 134 dberr = cn.TX.Rollback() 135 } else { 136 dberr = cn.TX.Commit() 137 } 138 if err != nil { 139 return errors.WithStack(err) 140 } 141 return errors.Wrap(dberr, "error committing or rolling back transaction") 142 }) 143 144 } 145 146 // Rollback will open a new transaction and automatically rollback that transaction 147 // when the inner function returns, regardless. This can be useful for tests, etc... 148 func (c *Connection) Rollback(fn func(tx *Connection)) error { 149 cn, err := c.NewTransaction() 150 if err != nil { 151 return err 152 } 153 fn(cn) 154 return cn.TX.Rollback() 155 } 156 157 // NewTransaction starts a new transaction on the connection 158 func (c *Connection) NewTransaction() (*Connection, error) { 159 var cn *Connection 160 if c.TX == nil { 161 tx, err := c.Store.Transaction() 162 if err != nil { 163 return cn, errors.Wrap(err, "couldn't start a new transaction") 164 } 165 cn = &Connection{ 166 ID: randx.String(30), 167 Store: tx, 168 Dialect: c.Dialect, 169 TX: tx, 170 } 171 } else { 172 cn = c 173 } 174 return cn, nil 175 } 176 177 func (c *Connection) copy() *Connection { 178 return &Connection{ 179 ID: randx.String(30), 180 Store: c.Store, 181 Dialect: c.Dialect, 182 TX: c.TX, 183 } 184 } 185 186 // Q creates a new "empty" query for the current connection. 187 func (c *Connection) Q() *Query { 188 return Q(c) 189 } 190 191 // disableEager disables eager mode for current connection. 192 func (c *Connection) disableEager() { 193 c.eager = false 194 c.eagerFields = []string{} 195 } 196 197 // TruncateAll truncates all data from the datasource 198 func (c *Connection) TruncateAll() error { 199 return c.Dialect.TruncateAll(c) 200 } 201 202 func (c *Connection) timeFunc(name string, fn func() error) error { 203 start := time.Now() 204 err := fn() 205 atomic.AddInt64(&c.Elapsed, int64(time.Since(start))) 206 if err != nil { 207 return errors.WithStack(err) 208 } 209 return nil 210 }