Skip to content

Modify Autogenerated code  #45

@gauravgola96

Description

@gauravgola96

I am trying to modify sql boiler auto-generated code such that all the database calls should happens like
select * from database.table_name; currently generated code has select * from table_name;

In our my previous architecture design we have one user per database and one instance machine connects to one database only but now we decided to have one admin user that connects to defaultdb on startup and we want decide which DB to call on the basis request headers/subdomain data.

I figured out setting up something like below can help but again we have to do it manually everytime schema is changed and code is regenrated.

func SetFromInQuery(ctx context.Context, q *queries.Query){
   database := ctx.Value("database").(string)
   queries.SetFrom(q ,database+".table_name")
}
func (q tableQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Table, error) {
	o := &Table{}
	SetFromInQuery(ctx, q.Query)

	queries.SetLimit(q.Query, 1)
	err := q.Bind(ctx, exec, o)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "Table: failed to execute a one query for Table")
	}

	if err := o.doAfterSelectHooks(ctx, exec); err != nil {
		return o, err
	}

	return o, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions