github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/query_gen/builder.go (about) 1 /* WIP Under Construction */ 2 package qgen 3 4 import ( 5 "database/sql" 6 "log" 7 ) 8 9 var Builder *builder 10 11 func init() { 12 Builder = &builder{conn: nil} 13 } 14 15 // A set of wrappers around the generator methods, so that we can use this inline in Gosora 16 type builder struct { 17 conn *sql.DB 18 adapter Adapter 19 } 20 21 func (b *builder) Accumulator() *Accumulator { 22 return &Accumulator{b.conn, b.adapter, nil} 23 } 24 25 // TODO: Move this method out of builder? 26 func (b *builder) Init(adapter string, config map[string]string) error { 27 err := b.SetAdapter(adapter) 28 if err != nil { 29 return err 30 } 31 conn, err := b.adapter.BuildConn(config) 32 b.conn = conn 33 log.Print("err:", err) // Is the problem here somehow? 34 return err 35 } 36 37 func (b *builder) SetConn(conn *sql.DB) { 38 b.conn = conn 39 } 40 41 func (b *builder) GetConn() *sql.DB { 42 return b.conn 43 } 44 45 func (b *builder) SetAdapter(name string) error { 46 adap, err := GetAdapter(name) 47 if err != nil { 48 return err 49 } 50 b.adapter = adap 51 return nil 52 } 53 54 func (b *builder) GetAdapter() Adapter { 55 return b.adapter 56 } 57 58 func (b *builder) DbVersion() (dbVersion string) { 59 b.conn.QueryRow(b.adapter.DbVersion()).Scan(&dbVersion) 60 return dbVersion 61 } 62 63 func (b *builder) Begin() (*sql.Tx, error) { 64 return b.conn.Begin() 65 } 66 67 func (b *builder) Tx(h func(*TransactionBuilder) error) error { 68 tx, err := b.conn.Begin() 69 if err != nil { 70 return err 71 } 72 err = h(&TransactionBuilder{tx, b.adapter, nil}) 73 if err != nil { 74 tx.Rollback() 75 return err 76 } 77 return tx.Commit() 78 } 79 80 func (b *builder) prepare(res string, err error) (*sql.Stmt, error) { 81 if err != nil { 82 return nil, err 83 } 84 return b.conn.Prepare(res) 85 } 86 87 func (b *builder) SimpleSelect(table, columns, where, orderby, limit string) (stmt *sql.Stmt, err error) { 88 return b.prepare(b.adapter.SimpleSelect("", table, columns, where, orderby, limit)) 89 } 90 91 func (b *builder) SimpleCount(table, where, limit string) (stmt *sql.Stmt, err error) { 92 return b.prepare(b.adapter.SimpleCount("", table, where, limit)) 93 } 94 95 func (b *builder) SimpleLeftJoin(table1, table2, columns, joiners, where, orderby, limit string) (stmt *sql.Stmt, err error) { 96 return b.prepare(b.adapter.SimpleLeftJoin("", table1, table2, columns, joiners, where, orderby, limit)) 97 } 98 99 func (b *builder) SimpleInnerJoin(table1, table2, columns, joiners, where, orderby, limit string) (stmt *sql.Stmt, err error) { 100 return b.prepare(b.adapter.SimpleInnerJoin("", table1, table2, columns, joiners, where, orderby, limit)) 101 } 102 103 func (b *builder) DropTable(table string) (stmt *sql.Stmt, err error) { 104 return b.prepare(b.adapter.DropTable("", table)) 105 } 106 107 func (build *builder) CreateTable(table, charset, collation string, columns []DBTableColumn, keys []DBTableKey) (stmt *sql.Stmt, err error) { 108 return build.prepare(build.adapter.CreateTable("", table, charset, collation, columns, keys)) 109 } 110 111 func (b *builder) AddColumn(table string, column DBTableColumn, key *DBTableKey) (stmt *sql.Stmt, err error) { 112 return b.prepare(b.adapter.AddColumn("", table, column, key)) 113 } 114 115 func (b *builder) DropColumn(table, colName string) (stmt *sql.Stmt, err error) { 116 return b.prepare(b.adapter.DropColumn("", table, colName)) 117 } 118 119 func (b *builder) RenameColumn(table, oldName, newName string) (stmt *sql.Stmt, err error) { 120 return b.prepare(b.adapter.RenameColumn("", table, oldName, newName)) 121 } 122 123 func (b *builder) ChangeColumn(table, colName string, col DBTableColumn) (stmt *sql.Stmt, err error) { 124 return b.prepare(b.adapter.ChangeColumn("", table, colName, col)) 125 } 126 127 func (b *builder) SetDefaultColumn(table, colName, colType, defaultStr string) (stmt *sql.Stmt, err error) { 128 return b.prepare(b.adapter.SetDefaultColumn("", table, colName, colType, defaultStr)) 129 } 130 131 func (b *builder) AddIndex(table, iname, colname string) (stmt *sql.Stmt, err error) { 132 return b.prepare(b.adapter.AddIndex("", table, iname, colname)) 133 } 134 135 func (b *builder) AddKey(table, column string, key DBTableKey) (stmt *sql.Stmt, err error) { 136 return b.prepare(b.adapter.AddKey("", table, column, key)) 137 } 138 139 func (b *builder) RemoveIndex(table, iname string) (stmt *sql.Stmt, err error) { 140 return b.prepare(b.adapter.RemoveIndex("", table, iname)) 141 } 142 143 func (b *builder) AddForeignKey(table, column, ftable, fcolumn string, cascade bool) (stmt *sql.Stmt, err error) { 144 return b.prepare(b.adapter.AddForeignKey("", table, column, ftable, fcolumn, cascade)) 145 } 146 147 func (b *builder) SimpleInsert(table, columns, fields string) (stmt *sql.Stmt, err error) { 148 return b.prepare(b.adapter.SimpleInsert("", table, columns, fields)) 149 } 150 151 func (b *builder) SimpleInsertSelect(ins DBInsert, sel DBSelect) (stmt *sql.Stmt, err error) { 152 return b.prepare(b.adapter.SimpleInsertSelect("", ins, sel)) 153 } 154 155 func (b *builder) SimpleInsertLeftJoin(ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { 156 return b.prepare(b.adapter.SimpleInsertLeftJoin("", ins, sel)) 157 } 158 159 func (b *builder) SimpleInsertInnerJoin(ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { 160 return b.prepare(b.adapter.SimpleInsertInnerJoin("", ins, sel)) 161 } 162 163 func (b *builder) SimpleUpdate(table, set, where string) (stmt *sql.Stmt, err error) { 164 return b.prepare(b.adapter.SimpleUpdate(qUpdate(table, set, where))) 165 } 166 167 func (b *builder) SimpleDelete(table, where string) (stmt *sql.Stmt, err error) { 168 return b.prepare(b.adapter.SimpleDelete("", table, where)) 169 } 170 171 // I don't know why you need this, but here it is x.x 172 func (b *builder) Purge(table string) (stmt *sql.Stmt, err error) { 173 return b.prepare(b.adapter.Purge("", table)) 174 } 175 176 func (b *builder) prepareTx(tx *sql.Tx, res string, err error) (*sql.Stmt, error) { 177 if err != nil { 178 return nil, err 179 } 180 return tx.Prepare(res) 181 } 182 183 // These ones support transactions 184 func (b *builder) SimpleSelectTx(tx *sql.Tx, table, columns, where, orderby, limit string) (stmt *sql.Stmt, err error) { 185 res, err := b.adapter.SimpleSelect("", table, columns, where, orderby, limit) 186 return b.prepareTx(tx, res, err) 187 } 188 189 func (b *builder) SimpleCountTx(tx *sql.Tx, table, where, limit string) (stmt *sql.Stmt, err error) { 190 res, err := b.adapter.SimpleCount("", table, where, limit) 191 return b.prepareTx(tx, res, err) 192 } 193 194 func (b *builder) SimpleLeftJoinTx(tx *sql.Tx, table1, table2, columns, joiners, where, orderby, limit string) (stmt *sql.Stmt, err error) { 195 res, err := b.adapter.SimpleLeftJoin("", table1, table2, columns, joiners, where, orderby, limit) 196 return b.prepareTx(tx, res, err) 197 } 198 199 func (b *builder) SimpleInnerJoinTx(tx *sql.Tx, table1, table2, columns, joiners, where, orderby, limit string) (stmt *sql.Stmt, err error) { 200 res, err := b.adapter.SimpleInnerJoin("", table1, table2, columns, joiners, where, orderby, limit) 201 return b.prepareTx(tx, res, err) 202 } 203 204 func (b *builder) CreateTableTx(tx *sql.Tx, table, charset, collation string, columns []DBTableColumn, keys []DBTableKey) (stmt *sql.Stmt, err error) { 205 res, err := b.adapter.CreateTable("", table, charset, collation, columns, keys) 206 return b.prepareTx(tx, res, err) 207 } 208 209 func (b *builder) SimpleInsertTx(tx *sql.Tx, table, columns, fields string) (stmt *sql.Stmt, err error) { 210 res, err := b.adapter.SimpleInsert("", table, columns, fields) 211 return b.prepareTx(tx, res, err) 212 } 213 214 func (b *builder) SimpleInsertSelectTx(tx *sql.Tx, ins DBInsert, sel DBSelect) (stmt *sql.Stmt, err error) { 215 res, err := b.adapter.SimpleInsertSelect("", ins, sel) 216 return b.prepareTx(tx, res, err) 217 } 218 219 func (b *builder) SimpleInsertLeftJoinTx(tx *sql.Tx, ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { 220 res, err := b.adapter.SimpleInsertLeftJoin("", ins, sel) 221 return b.prepareTx(tx, res, err) 222 } 223 224 func (b *builder) SimpleInsertInnerJoinTx(tx *sql.Tx, ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { 225 res, err := b.adapter.SimpleInsertInnerJoin("", ins, sel) 226 return b.prepareTx(tx, res, err) 227 } 228 229 func (b *builder) SimpleUpdateTx(tx *sql.Tx, table, set, where string) (stmt *sql.Stmt, err error) { 230 res, err := b.adapter.SimpleUpdate(qUpdate(table, set, where)) 231 return b.prepareTx(tx, res, err) 232 } 233 234 func (b *builder) SimpleDeleteTx(tx *sql.Tx, table, where string) (stmt *sql.Stmt, err error) { 235 res, err := b.adapter.SimpleDelete("", table, where) 236 return b.prepareTx(tx, res, err) 237 } 238 239 // I don't know why you need this, but here it is x.x 240 func (b *builder) PurgeTx(tx *sql.Tx, table string) (stmt *sql.Stmt, err error) { 241 res, err := b.adapter.Purge("", table) 242 return b.prepareTx(tx, res, err) 243 }