sqldiffupdater

package module
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2023 License: GPL-3.0 Imports: 3 Imported by: 0

README

sqldiffupdater

Function: Generate

Generates an SQL update statement based on the comparison of the properties of two objects, newvar and oldvar. The function takes three arguments: tableName, newvar, and oldvar.

The tableName argument is a string that represents the name of the database table that the update statement will be executed against. The newvar and oldvar arguments are the new and old versions of the object that will be compared.

The function uses reflection to get the names and values of the properties of the newvar and oldvar objects, and generates an SQL update statement based on the differences between the two objects. The function checks that the newvar and oldvar objects have the same type, and that they have an Id field as a primary key.

The function returns a string representing the generated SQL update statement, a map containing the values of the changed properties, and an error if one occurs during the execution of the function.

Parameters
  • tableName (string): The name of the database table that the update statement will be executed against.
  • newvar (interface{}): The new version of the object that will be compared.
  • oldvar (interface{}): The old version of the object that will be compared.
Returns
  • string: The generated SQL update statement.
  • map[string]interface{}: A map containing the values of the changed properties.
  • error: An error if one occurs during the execution of the function.
Example
type User struct {
    Id        int
    FirstName string
    LastName  string
    Email     string
}

newUser := User{Id: 1, FirstName: "John", LastName: "Doe", Email: "johndoe@example.com"}
oldUser := User{Id: 1, FirstName: "Jane", LastName: "Doe", Email: "janedoe@example.com"}

sql, values, err := Generate("users", newUser, oldUser)
if err != nil {
    // handle error
}

// sql: "UPDATE users SET FirstName=:FirstName, Email=:Email WHERE Id=:Id"
// here changed only FirstName and Email
// values: map[string]interface{}{"FirstName": "John", "Email": "johndoe@example.com", "Id": 1}
// db : your sql=db provider
err = db.Exec(sql, values)
if err != nil {
    // handle error
}

Function: Update

Generates SQL code for updating specific fields in a database table based on a given object and a list of fields to update.

Prototype
func update(tableName string, newvar interface{}, fields []string) (string, map[string]interface{}, error)
Parameters
  • tableName (string): the name of the database table to update.
  • newvar (interface{}): an object containing the new values to update in the database table.
  • fields ([]string): a list of fields to update in the database table.
Returns
  • string: the SQL code for updating the specified fields in the database table.
  • map[string]interface{}: a map of the updated fields and their new values.
  • error: an error if one occurs.
Notes
  • The object provided as newvar must have an Id field as the primary key in the corresponding database table.
  • The fields parameter should contain the names of the fields in the newvar object that need to be updated in the database table.
  • This function can be used to efficiently update only the necessary fields in a database table, saving time and resources.
Example
import (
    "fmt"
    "github.com/MasterDimmy/sqldiffupdater
)

type User struct {
    Id        int
    FirstName string
    LastName  string
    Email     string
}

func main() {
    user := User{Id: 1, FirstName: "John", LastName: "Doe", Email: "johndoe@example.com"}
    fieldsToUpdate := []string{"FirstName", "Email"}

    sql, updatedFields, err := repo.Update("users", user, fieldsToUpdate)
    if err != nil {
        panic(err)
    }

    fmt.Println(sql)
    fmt.Println(updatedFields)
}

Output:

UPDATE users SET FirstName=:FirstName, Email=:Email WHERE Id=:Id
map[FirstName:John Email:johndoe@example.com]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(tableName string, key string, newvar_ interface{}, oldvar_ interface{}) (string, map[string]interface{}, error)

Parameters:

tableName - database table to update
newvar	- new object
oldvar	- old object of same type
key - field name of the primary key in tableName

Result:

sql string of "update tableName set ..... where tableName.Id = oldvar.Id"
map of key:value properties of newvar changes from oldvar
error if happens

Notes:

If no changes found, map[string]interface{} has len of 1 (just Id)!
newvar should have key property as primary key for update!

func Update added in v0.0.3

func Update(tableName string, key string, newvar_ interface{}, fields []string) (string, map[string]interface{}, error)

`update` is a Go function that generates SQL code for updating specific fields in a database table based on a given object and a list of fields to update. The function takes the table name, the object containing the new values, and a list of fields to update. The object must have an key field as a primary key. The function returns a tuple containing the SQL code for updating, a map of updated fields and their values, and an error if one occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL