Browse Source

Skip some field on update in SQL query

Volodymyr Tkach 2 years ago
parent
commit
9c0110e9ca
3 changed files with 50 additions and 3 deletions
  1. 11 2
      gosql/common/common.go
  2. 1 0
      gosql/common/common_export_test.go
  3. 38 1
      gosql/common/common_test.go

+ 11 - 2
gosql/common/common.go

@@ -72,6 +72,15 @@ func fixQuery(query string) string {
 	return rSqlParam.ReplaceAllString(query, "?")
 }
 
+func inArray(arr []string, str string) bool {
+	for _, s := range arr {
+		if s == str {
+			return true
+		}
+	}
+	return false
+}
+
 func insertRowString(row any) (string, []any) {
 	v := reflect.ValueOf(row).Elem()
 	t := v.Type()
@@ -207,7 +216,7 @@ func scans(row any) []any {
 	return res
 }
 
-func updateRowString(row any) (string, []any) {
+func updateRowString(row any, only ...string) (string, []any) {
 	v := reflect.ValueOf(row).Elem()
 	t := v.Type()
 	var id int64
@@ -228,7 +237,7 @@ func updateRowString(row any) (string, []any) {
 			if id == 0 && tag == "id" {
 				id = v.Field(i).Int()
 			}
-			if tag != "id" && tag != "created_at" {
+			if tag != "id" && tag != "created_at" && ((len(only) == 0) || (len(only) > 0 && inArray(only, tag))) {
 				fields = append(fields, tag)
 				values = append(values, "$"+strconv.Itoa(position))
 				if tag == "updated_at" {

+ 1 - 0
gosql/common/common_export_test.go

@@ -2,6 +2,7 @@ package common
 
 var DeleteRowByIDString = deleteRowByIDString
 var FixQuery = fixQuery
+var InArray = inArray
 var InsertRowString = insertRowString
 var Log = log
 var QueryRowByIDString = queryRowByIDString

+ 38 - 1
gosql/common/common_test.go

@@ -39,7 +39,18 @@ var _ = Describe("common", func() {
 		})
 	})
 
-	Context("insertRow", func() {
+	Context("inArray", func() {
+		It("check string in array", func() {
+			arr := []string{"field1", "field2", "field3"}
+			Expect(common.InArray(arr, "field1")).To(BeTrue())
+			Expect(common.InArray(arr, "field2")).To(BeTrue())
+			Expect(common.InArray(arr, "field3")).To(BeTrue())
+			Expect(common.InArray(arr, "field4")).To(BeFalse())
+			Expect(common.InArray(arr, "field5")).To(BeFalse())
+		})
+	})
+
+	Context("insertRowString", func() {
 		It("convert struct to SQL query", func() {
 			var row struct {
 				ID       int64  `field:"id" table:"users"`
@@ -182,6 +193,32 @@ var _ = Describe("common", func() {
 			Expect(args[1]).To(Equal("Value"))
 			Expect(args[2]).To(Equal(int64(59)))
 			Expect(args[3]).To(Equal(int64(10)))
+
+			sql, args = common.UpdateRowString(&row, "name")
+
+			Expect(sql).To(Equal(`UPDATE users SET name = $1 WHERE id = $2`))
+
+			Expect(len(args)).To(Equal(2))
+			Expect(args[0]).To(Equal("Name"))
+			Expect(args[1]).To(Equal(int64(10)))
+
+			sql, args = common.UpdateRowString(&row, "name", "value")
+
+			Expect(sql).To(Equal(`UPDATE users SET name = $1, value = $2 WHERE id = $3`))
+
+			Expect(len(args)).To(Equal(3))
+			Expect(args[0]).To(Equal("Name"))
+			Expect(args[1]).To(Equal("Value"))
+			Expect(args[2]).To(Equal(int64(10)))
+
+			sql, args = common.UpdateRowString(&row, "name", "position")
+
+			Expect(sql).To(Equal(`UPDATE users SET name = $1, position = $2 WHERE id = $3`))
+
+			Expect(len(args)).To(Equal(3))
+			Expect(args[0]).To(Equal("Name"))
+			Expect(args[1]).To(Equal(int64(59)))
+			Expect(args[2]).To(Equal(int64(10)))
 		})
 
 		It("convert struct to SQL query and populate updated_at", func() {