top

The SQLCommand package provides a simple method of chaining together database operations

Instead of the standard:

Open Database
Begin Transaction
	Check for Error
Prepare Statement
Defer Close Statement
	Check for Error
		Rollback Transaction
Execute/Query Statement
	Check for Error
	Rollback Transaction
Scan Results
	Check for Error
Commit Transaction

SQLCommand allows you to do:

Open Database
scf := NewFactory(database,log)
	sc := scf.NewCommand(someSQL)
	sc.Begin().Prepare().Query(parameters...).Scan(pointers...).CloseAll()
	if sc.IsValid{
	// do whatever with the pointer	map or set, depending on what was executed/queried
} else {
	// the error that caused the problem is in sc.Err, so deal with it
}

standard patterns of use are:

var scf SQLCommandFactory

func init(){
	scf = new(SQLCommandFactory)
	scf.DB, err = db.Open(driver,DSN)
	if nil != err{
		panic("unable to open database")
	}
	scf.Log = log.Logger
}
func getData() (someDataStruct, error){
	d := new(someDataStruct)
	sc:=scf.NewCommand("SELECT * FROM someDataTable WHERE Id=?")
	sc.Begin().QueryRow().Scan(&d.Field1,&d.Field2,&d.Field3).CloseAll()
	if sc.IsValid{
		return d,nil
	}
	return nil, errors.New("error occurred while getting data: " + sc.Err.Error())
}

func insertData(data) error{
	//slightly extreme example... normally this would be broken into a couple of lines
	return scf.NewCommand("INSERT INTO someDataTable (Field1,Field2,Field3) VALUES (?,?,?)").Begin().Exec(data.Field1,data.Field2,data.Field3).CloseAll().Err
}

Imports 1 package(s)

  1. github.com/go-sql-driver/mysql

Test imports 1 package(s)

  1. github.com/mattn/go-sqlite3