coopernurse/gorp — pull request #71 by pbnjay (2013-08-31 04:20:11+00:00)
Title: add "Not Null" for create statement, fix postgres varchar size, add more struct tags/features

A few commits for consideration:
1) postgresql varchar doesn't need a size - if there's not one set then don't set it to 255.
2) add a simple SetNotNull similar to SetUnique
3) the big one - add json-package-style comma-tags (like pull #26), and a couple other handy struct tags. I'm sure the names themselves can be changed to suit your preference.

I spelled out as much as I could in the commit log message:

Adds some features to the db: tag similar to the json package.
Use a comma-list of special words as follows:
** first item is always column name **
"ai" = autoincrement
"pk" = primary key
"un" = unique
"nn" = not null

Add a dbdefault: tag to set a default value
Add a dbrefs: tag to set a (single-column) foreign key constraint
Add a dbtype: tag to set a database-layer data type without having to
deal with updating the Dialect. You'll probably want something the DB
can convert from a string type transparently.

NB: These 3 tags are directly placed into the create table statments, so
any necessary quoting or escaping must be handled properly. These are
shortcuts, don't use them for complex constraints!

A contrived example of usage:

type Building struct {
Id int64 `db:"building_id,pk,ai"`
Name string `db:",nn" dbdefault:"NoName Building"`
Address string `db:",nn,un"`
GatewayIP string `db:"ip_address" dbtype:"inet"`
}

type Room struct {
Id int64 `db:"room_id,pk,ai"`
BuildingId int64 `db:"building_id,nn" dbrefs:"buildings (building_id)"`
Name string
}