Exemple #1
4
func getEvents(c *gin.Context) {
	user := c.MustGet("user").(*models.User)

	q := squirrel.Select("event.*, p.name as project_name").
		From("event").
		LeftJoin("project as p on event.project_id=p.id").
		OrderBy("created desc")

	projectObj, exists := c.Get("project")
	if exists == true {
		// limit query to project
		project := projectObj.(models.Project)
		q = q.Where("event.project_id=?", project.ID)
	} else {
		q = q.LeftJoin("project__user as pu on pu.project_id=p.id").
			Where("p.id IS NULL or pu.user_id=?", user.ID)
	}

	var events []models.Event

	query, args, _ := q.ToSql()
	if _, err := database.Mysql.Select(&events, query, args...); err != nil {
		panic(err)
	}

	for i, evt := range events {
		if evt.ObjectID == nil || evt.ObjectType == nil {
			continue
		}

		var q squirrel.SelectBuilder

		switch *evt.ObjectType {
		case "task":
			q = squirrel.Select("tpl.playbook as name").
				From("task").
				Join("project__template as tpl on task.template_id=tpl.id")
		default:
			continue
		}

		query, args, _ := q.ToSql()
		name, err := database.Mysql.SelectNullStr(query, args...)
		if err != nil {
			panic(err)
		}

		if name.Valid == true {
			events[i].ObjectName = name.String
		}
	}

	c.JSON(200, events)
}
Exemple #2
0
func (b *Base) addWheresToBuilder(selectBuilder squirrel.SelectBuilder, wheres ...squirrel.Sqlizer) squirrel.SelectBuilder {
	for _, where := range wheres {
		selectBuilder = selectBuilder.Where(where)
	}

	return selectBuilder
}
Exemple #3
0
func (b *Base) queryWhere(tx *sqlx.Tx, selectBuilder squirrel.SelectBuilder, wheres ...squirrel.Sqlizer) (*sqlx.Rows, error) {
	selectBuilder = b.addWheresToBuilder(selectBuilder, wheres...)

	query, args, err := selectBuilder.ToSql()
	if err != nil {
		return nil, err
	}

	return b.query(tx, query, args...)
}
Exemple #4
0
func (d *DbMap) Query(model interface{}, w sq.SelectBuilder) ([]interface{}, error) {
	q, args, err := w.ToSql()
	if err != nil {
		return []interface{}{}, err
	}

	d.Tracer.TraceOn()
	rows, err := d.DbMap.Select(model, q, args...)
	d.Tracer.TraceOff()

	return rows, err
}