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

BlackListing or WhiteListing columns for struct based inserts and updates #21

Merged
merged 12 commits into from
Oct 21, 2018
Merged

Conversation

derkan
Copy link
Contributor

@derkan derkan commented Oct 9, 2018

This PR is to blacklist or whitelist columns during struct based inserts or updates. For example:

type User struct {
	ID             int64            `db:"id,key,auto" json:"id"`
	Username       string           `db:"username" json:"username"`
	LastLoginIP    types.NullString `db:"last_login_ip" json:"last_login_ip" `
	LastLoginTime  types.NullTime   `db:"last_login_time" json:"last_login_time"`
	CreatedAt      time.Time        `db:"created_at,auto" json:"created_at"`
	UpdatedAt      types.NullTime   `db:"updated_at" json:"updated_at"`
}

func insert(db *godb.DB, res *User) error {
     return db.Insert(res).Blacklist("last_login_ip", "last_login_time", "updated_at").Do()
}

func update(db *godb.DB, res *User) error {
     return db.Update(res).Blacklist("last_login_ip", "last_login_time", "created_at").Do()
}

func login(db *godb.DB, res *User) error {
     return db.Update(res).Whitelist("last_login_time", "last_login_ip").Do()
}

Also added BlacklistReset or WhitelistReset for struct based inserts or updates to clear lists.

@lozaeric
Copy link

nice feature!

values := si.recordDescription.structMapping.GetNonAutoFieldsValues(currentRecord)
if hasWB {
columns, values = si.recordDescription.structMapping.GetNonAutoFieldsValuesFiltered(currentRecord, columns)
if !wbColsSet { // order of old columns list and current values list may not be same so, set here:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wbColsSet need to be set to true here.

You can add a test with BulkInsert to reveal the error (and ensure there will be no regression) :

			Convey("Do executes the query using a blacklist", func() {
				dummies := make([]Dummy, 0, 2)
				dummy1 := Dummy{
					AText:           "Dummy1",
					AnotherText:     "Foo",
					AnInteger:       12345,
					ANullableString: sql.NullString{String: "Void", Valid: true},
				}
				dummy2 := Dummy{
					AText:           "Dummy2",
					AnotherText:     "Foo",
					AnInteger:       12345,
					ANullableString: sql.NullString{String: "Void", Valid: true},
				}
				dummies = append(dummies, dummy1, dummy2)
				err := db.BulkInsert(&dummies).Blacklist("a_nullable_string").Do()
				So(err, ShouldBeNil)
			})

Copy link
Contributor Author

@derkan derkan Oct 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, it is a footprint of my struggle to preserve to match params and values. First tried a map for columns and values, that failed because of random ordering of maps. Later used slices for columns and values.
Corrected. Thanks

@samonzeweb
Copy link
Owner

As you made some more changes since my last review, I'm not sure if it's finished or not (Github lack the WIP status in PR).

Can I merge it ?

@derkan
Copy link
Contributor Author

derkan commented Oct 21, 2018

Yes please!

@samonzeweb samonzeweb merged commit 78b0b20 into samonzeweb:master Oct 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants