| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package mysql
- import (
- "database/sql"
- "errors"
- )
- type Wrapper struct {
- drv *sql.DB
- row *sql.Rows
- query string
- Err error
- }
- var ErrDriverNotReady = errors.New("driver not ready")
- func (this *Wrapper) Query(query string, args ...interface{}) *Wrapper {
- if this.drv == nil {
- this.Err = ErrDriverNotReady
- return this
- }
- this.query = query
- this.row, this.Err = this.drv.Query(query, args...)
- //log.Println("rows=", this.row)
- return this
- }
- func (this *Wrapper) Execute(query string, args ...interface{}) *Wrapper {
- if this.drv == nil {
- this.Err = ErrDriverNotReady
- return this
- }
- this.query = query
- _, this.Err = this.drv.Exec(query, args...)
- return this
- }
- func (this *Wrapper) Each(cb func(wrapper *Wrapper) bool) *Wrapper {
- if this.Err != nil {
- return this
- }
- if this.drv == nil {
- this.Err = ErrDriverNotReady
- return this
- }
- for this.row.Next() {
- if !cb(this) {
- break
- }
- if this.Err != nil {
- return this
- }
- }
- this.row.Close()
- return this
- }
- func (this *Wrapper) Scan(dest ...interface{}) error {
- this.Err = this.row.Scan(dest...)
- if this.Err != nil {
- return this.Err
- }
- return nil
- }
- func NewWrapper(drv *sql.DB) *Wrapper {
- return &Wrapper{
- drv: drv,
- }
- }
|