Friday, May 29, 2015

Day 12 - Gorp with Indexes is now ready for beta tests

Gorp with Indexes now passes the test cases for MySQL and PostgreSQL.

Get it from github:

The latest (as of 2015.05.29) upstream changes have been merged, the code is now ready for testers.

If you want to use "Gorp with Indexes" just add index tags to your table struct
(idx_user in this example)  and add a import ""

type Post struct {
    Id           uint64    `db:"notnull, PID, primarykey, autoincrement"`
    User         string    `db:"index:idx_user, size:64"`
    PostSub      string    `db:"index:idx_user, size:128"`
    UserIP       string    `db:"notnull, index:idx_user, size:16"`
    BodyType     string    `db:"notnull, size:64"`
    Body         string    `db:"name:PostBody, size:16384"`
    Err          error     `db:"-"` // ignore this field when storing with gorp

 Then you only need to call: CreateIndexes

       tablename := "posts_index_test"
    // SetKeys(true) means we have a auto increment primary key, which
    // will get automatically bound to your struct post-insert
    table := dbmap.AddTableWithName(post.Post{}, tablename)
    table.SetKeys(true, "PID")

    // create the table. in a production system you'd generally
    // use a migration tool, or create the tables via scripts
    if err = dbmap.CreateTablesIfNotExists(); err != nil {
        return errors.New("Create tables failed: " + err.Error())

    // Force create all indexes for this database
    if err = dbmap.CreateIndexes(); err != nil {
        return errors.New("Create indexes failed: " + err.Error())

No comments:

Post a Comment