Browse Source

Add DeleteRowByID func

Volodymyr Tkach 2 years ago
parent
commit
363ee4d310
5 changed files with 105 additions and 2 deletions
  1. 2 1
      gosql/common/common.go
  2. 1 1
      gosql/common/common_test.go
  3. 6 0
      gosql/common/dbmethods.go
  4. 6 0
      gosql/common/tx.go
  5. 90 0
      gosql/gosql_test.go

+ 2 - 1
gosql/common/common.go

@@ -22,6 +22,7 @@ import (
 type Engine interface {
 	Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
 	Close() error
+	DeleteRowByID(ctx context.Context, id int64, row any) error
 	Each(ctx context.Context, query string, logic func(ctx context.Context, rows *Rows) error, args ...any) error
 	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
 	Ping(context.Context) error
@@ -146,7 +147,7 @@ func deleteRowByIDString(row any) string {
 			}
 		}
 	}
-	return `DELETE FROM ` + table + ` WHERE id = $1 LIMIT 1`
+	return `DELETE FROM ` + table + ` WHERE id = $1`
 }
 
 func ParseUrl(dbURL string) (*url.URL, error) {

+ 1 - 1
gosql/common/common_test.go

@@ -111,7 +111,7 @@ var _ = Describe("common", func() {
 				Value string `field:"value"`
 			}
 
-			Expect(common.DeleteRowByIDString(&row)).To(Equal(`DELETE FROM users WHERE id = $1 LIMIT 1`))
+			Expect(common.DeleteRowByIDString(&row)).To(Equal(`DELETE FROM users WHERE id = $1`))
 		})
 	})
 

+ 6 - 0
gosql/common/dbmethods.go

@@ -42,6 +42,12 @@ func (d *DBMethods) Close() error {
 	return err
 }
 
+func (d *DBMethods) DeleteRowByID(ctx context.Context, id int64, row any) error {
+	query := deleteRowByIDString(row)
+	_, err := d.Exec(ctx, query, id)
+	return err
+}
+
 func (d *DBMethods) Each(ctx context.Context, query string, callback func(ctx context.Context, rows *Rows) error, args ...any) error {
 	if callback == nil {
 		return fmt.Errorf("callback is not set")

+ 6 - 0
gosql/common/tx.go

@@ -35,6 +35,12 @@ func (t *Tx) Commit() error {
 	return err
 }
 
+func (t *Tx) DeleteRowByID(ctx context.Context, id int64, row any) error {
+	query := deleteRowByIDString(row)
+	_, err := t.Exec(ctx, query, id)
+	return err
+}
+
 func (t *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
 	start := time.Now()
 	res, err := t.tx.ExecContext(ctx, t.fixQuery(query), args...)

+ 90 - 0
gosql/gosql_test.go

@@ -26,6 +26,20 @@ var _ = Describe("gosql", func() {
 			var err error
 			migrationsDir, err = filepath.Abs("../db/migrations")
 			Expect(err).To(Succeed())
+
+			// // Reset databases
+			// // Note: uncomment for MySQL and PostgreSQL tests
+			// var db common.Engine
+
+			// // MySQL
+			// db, err = gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", "", true, false)
+			// Expect(err).To(Succeed())
+			// _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
+
+			// // PostgreSQL
+			// db, err = gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", "", true, false)
+			// Expect(err).To(Succeed())
+			// _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
 		})
 
 		// // Note: you need to up MySQL server for this test case
@@ -79,6 +93,30 @@ var _ = Describe("gosql", func() {
 		// 		Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
 		// 		Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
 
+		// 		Expect(db.Close()).To(Succeed())
+		// 	})
+
+		// 	It("open connection, migrate and delete row", func() {
+		// 		db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
+		// 		Expect(err).To(Succeed())
+
+		// 		var rowUser struct {
+		// 			ID   int64  `field:"id" table:"users"`
+		// 			Name string `field:"name"`
+		// 		}
+
+		// 		var size int
+
+		// 		Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
+		// 		err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(size).To(Equal(1))
+
+		// 		Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
+		// 		err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(size).To(Equal(0))
+
 		// 		Expect(db.Close()).To(Succeed())
 		// 	})
 		// })
@@ -134,6 +172,30 @@ var _ = Describe("gosql", func() {
 		// 		Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
 		// 		Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
 
+		// 		Expect(db.Close()).To(Succeed())
+		// 	})
+
+		// 	It("open connection, migrate and delete row", func() {
+		// 		db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
+		// 		Expect(err).To(Succeed())
+
+		// 		var rowUser struct {
+		// 			ID   int64  `field:"id" table:"users"`
+		// 			Name string `field:"name"`
+		// 		}
+
+		// 		var size int
+
+		// 		Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
+		// 		err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(size).To(Equal(1))
+
+		// 		Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
+		// 		err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(size).To(Equal(0))
+
 		// 		Expect(db.Close()).To(Succeed())
 		// 	})
 		// })
@@ -202,6 +264,34 @@ var _ = Describe("gosql", func() {
 
 				Expect(db.Close()).To(Succeed())
 			})
+
+			It("open connection, migrate and delete row", func() {
+				f, err := ioutil.TempFile("", "go-sqlite-test-")
+				Expect(err).To(Succeed())
+				f.Close()
+
+				db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
+				Expect(err).To(Succeed())
+
+				var rowUser struct {
+					ID   int64  `field:"id" table:"users"`
+					Name string `field:"name"`
+				}
+
+				var size int
+
+				Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
+				err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+				Expect(err).To(Succeed())
+				Expect(size).To(Equal(1))
+
+				Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
+				err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+				Expect(err).To(Succeed())
+				Expect(size).To(Equal(0))
+
+				Expect(db.Close()).To(Succeed())
+			})
 		})
 
 		It("open connection and skip migration", func() {