Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support column DEFAULT when inserting/updating via struct #27

Closed
aheuermann opened this issue Oct 13, 2016 · 2 comments
Closed

Support column DEFAULT when inserting/updating via struct #27

aheuermann opened this issue Oct 13, 2016 · 2 comments

Comments

@aheuermann
Copy link
Collaborator

aheuermann commented Oct 13, 2016

When inserting/updating by struct, I couldn't find a way to force the column DEFAULT.

INSERT into table (column_with_default) values (DEFAULT);
UPDATE table SET column_with_default = DEFAULT;

Some ideas:

1: Add a tag like defaultifempty. This would work well for most use-cases, but wouldn't allow you to set the column to the "zero value" if desired (you would need to use a map).

  type Person struct {
    FirstName string     `goqu:defaultifempty`
    Last Name NullString `goqu:defaultifempty`
  }
  p := Person {
    FirstName: "",
    LastName: NullString {
      String: "",
      Valid: true
    }
  } //both would use database default when inserted/updated

2: Add a Default interface. This would require you to wrap things like NullString. I don't particularly like this idea. NullString is verbose/confusing as-is; adding another boolean for default makes it worse, but it would handle all use-cases.

type Person struct {
  Name NullStringWithDefault
}

p := Person {
  Name: NullStringWithDefault("", false, true)
} //would insert as DEFAULT

p2 := Person {
  Name: NullStringWithDefault("", false, false)
} //would insert as NULL

p3 := Person {
  Name: NullStringWithDefault("", true, true)
} //would this insert as NULL, DEFAULT, or error?

If anything, I think I like _option 1_ since it is simple and it would work for most use-cases. For cases where you want the zero value, you can always use a map. Let me know what you think. Maybe there are better solutions I'm missing.

@chen56
Copy link
Contributor

chen56 commented Jul 17, 2019

The override value should be simpler

	dialect.From("person").
		Insert(p).
	        WithOverride("name",func()interface{}{
			if p.Name==""{
				return goqu.L("DEFAULT")
			}else{
				return sex
			}
		}).Exec()

or

	dialect.From("person").
		Insert(p).
		WithOverride("name",goqu.L("DEFAULT"),func()bool{
			var isOverride = p.Name==""
			return isOverride
		}).Exec()

or

	ds:=dialect.From("person").Insert(p) 
        if p.Name!=""{
             ds.WithOverride("name",goqu.L("DEFAULT"))
        }

@doug-martin
Copy link
Owner

I went with option 1 for now because it was fairly trivial to implement. If we end up needing the WithOverride type functionality later we can consider it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants