go - Postgresql dynamic query building in golang -


how can build dynamic update query depending on arguments have/get?

need update column if argument value not empty.

this code:

func main() {      err := dbconnect() //db connection function     if err != nil {         fmt.println("error", "db connect fail")         return     }      tx, err := db.begin()     if err != nil {         fmt.println("error", err)         fmt.println("error", "unable start db")         return     }      defer func() {         if err != nil {             tx.rollback()         } else {             tx.commit()         }     }()      var arg []interface{}      := 1     col1 := "value1"     col2 := "value2"     col3 := ""      col4 := ""     col5 := "value5"      query := "update schema1.products set "      if col1 != "" {         query = query + "col1=$" + fmt.sprintf("%d", i) + ", "         arg = append(arg, col1)         i++     }     if col2 != "" {         query = query + "col2=$" + fmt.sprintf("%d", i) + ", "         arg = append(arg, col2)         i++     }     if col3 != "" {         query = query + "col3=$" + fmt.sprintf("%d", i) + ", "         arg = append(arg, col3)         i++     }     if col4 != "" {     query = query + "col4=$" + fmt.sprintf("%d", i) + ", "     arg = append(arg, col4)     i++     }      query = query + "where col5=$" + fmt.sprintf("%d", i)     arg = append(arg, col5)      stmt, err := tx.prepare(query)     if err != nil {         fmt.println("error", err)         fmt.println("error", "query prepare failed")         return     }      res, err := stmt.exec(arg...)     if err != nil {         fmt.println("error", err)         fmt.println("error", "query exec failed")         return     }     n, err := res.rowsaffected()     if err != nil {         fmt.println("error", err)         fmt.println("error", "unable rows affected")         return     }     if n > 1 {         fmt.println("error", err)         fmt.println("error", "more 1 row updated")         return     }     return } 

so basic requirement update columns if appropriate input values not null.

update:

func getrow(arg1, arg2 string) (data [][]string, err error) {     row, err := db.query("select * schema1.products ($1='' or col1=$1) , ($2='' or col2=$2)", arg1, arg2)     if err != nil {         fmt.println("error", err)         fmt.println("error", "unable query db")         return     }     defer sql.close(row)     _, data, err = sql.scan(row)     if err != nil {         fmt.println("error", err)         fmt.println("error", "unable scan rows")         return     }     if len(data) < 0 {         fmt.println("error", "no data found")         return     }     return } 

the query in getrow function include conditions in clause if arguments not empty. need not worry whether arg1 , arg2 empty or not. looking way same in case of update query well.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -