reform icon indicating copy to clipboard operation
reform copied to clipboard

Override table name when executing SQL

Open bitxel opened this issue 6 years ago • 2 comments

Dear developers,

Thanks for your hard work, this library help me a lot, and I have a question about the table name: As state in the ReadMe doc, the following struct will map to person table,

//reform:people
type Person struct {
	ID        int32      `reform:"id,pk"`
	Name      string     `reform:"name"`
	Email     *string    `reform:"email"`
	CreatedAt time.Time  `reform:"created_at"`
	UpdatedAt *time.Time `reform:"updated_at"`
}

After diving into code, I didn't find any way to reuse this Person struct to other table with the same structure, please advice if I miss any parts:)

So I'd like to propose a set of new Functions like SaveOnTable(record, table_name), UpdateOnTable(record, table_name) etc, which will execute SQL on the appointed table and make it possible to reuse this struct on many tables or sharding tables, like:

person_00
person_01
... 
person_99

Please help review this feature request.

bitxel avatar Jun 09 '18 05:06 bitxel

Hi. Thank you for using reform and helping make it better!

#62 describes a similar, but not identical issue.

I think something like this could work:

// WithTag returns a copy of Querier with set view name. Returned Querier is tied to the same DB or TX.
func (q *Querier) WithView(viewName) *Querier {
  // …
}

// …

DB.WithView("person_42").Save(record)

I would be happy to accept a PR for that issue!

AlekSi avatar Jun 09 '18 15:06 AlekSi

Yeah, this way sounds better, I'll work on it.

bitxel avatar Jun 11 '18 03:06 bitxel