12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package nosql
- import (
- "database/sql"
- "encoding/json"
- "fmt"
- )
- type OpenTXMethod func() (*sql.Tx, error)
- type NoSQL struct {
- openMethod OpenTXMethod
- }
- func New(openMethod OpenTXMethod) *NoSQL {
- return &NoSQL{openMethod}
- }
- func (_self *NoSQL) CallJSON(function string, rawJSON []byte) (resRawJSON []byte, err error) {
-
- var tx *sql.Tx
- if tx, err = _self.openMethod(); err == nil {
-
- row := tx.QueryRow(fmt.Sprintf("select * from %v($1)", function), rawJSON)
- if err = row.Scan(&resRawJSON); err != nil {
- tx.Rollback()
- return
- }
- err = tx.Commit()
- }
- return
- }
- func (_self *NoSQL) CallObjParam(function string, data interface{}) (resRawJSON []byte, err error) {
-
- var raw []byte
- if raw, err = json.Marshal(data); err != nil {
- return
- }
- return _self.CallJSON(function, raw)
- }
- func (_self *NoSQL) Call(function string, data interface{}) (res interface{}, err error) {
- var resRawJSON []byte
- if resRawJSON, err = _self.CallObjParam(function, data); err == nil {
-
- if len(resRawJSON) > 0 {
- err = json.Unmarshal(resRawJSON, &res)
- }
- }
- return
- }
- func (_self *NoSQL) CallObject(function string, data interface{}, resultObject interface{}) (err error) {
- var resRawJSON []byte
- if resRawJSON, err = _self.CallObjParam(function, data); err == nil {
-
- err = json.Unmarshal(resRawJSON, resultObject)
- }
- return
- }
|