コード例 #1
0
ファイル: entity.go プロジェクト: klenin/orc
func (this *Entity) QueryInsert(extra string) *sql.Row {
	i := 1
	query := "INSERT INTO %s ("
	tFields := reflect.ValueOf(this.fields).Type().Elem()
	vFields := reflect.ValueOf(this.fields).Elem()
	params := make([]interface{}, 0)

	for i = 1; i < tFields.NumField(); i++ {
		value, ok := utils.UpdateOrNot(tFields.Field(i).Tag.Get("type"), vFields.Field(i))
		if !ok && tFields.Field(i).Tag.Get("null") == "NULL" {
			value = nil
		}
		query += tFields.Field(i).Tag.Get("name") + ", "
		params = append(params, value)
	}
	query = query[0 : len(query)-2]
	query += ") VALUES (%s) %s;"

	return db.QueryRow(fmt.Sprintf(query, this.tableName, strings.Join(db.MakeParams(i-1), ", "), extra), params)
}
コード例 #2
0
ファイル: grid_controller.go プロジェクト: FooBarrior/orc
//-----------------------------------------------------------------------------
func (this *GridController) GetPersonsByEventId() {
	if !sessions.CheckSession(this.Response, this.Request) {
		http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)

		return
	}

	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)

		return
	}

	if this.Request.URL.Query().Get("event") == "" || this.Request.URL.Query().Get("params") == "" {

		return
	}

	eventId, err := strconv.Atoi(this.Request.URL.Query().Get("event"))
	if err != nil {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	paramsIds := strings.Split(this.Request.URL.Query().Get("params"), ",")

	if len(paramsIds) == 0 {
		utils.SendJSReply(map[string]interface{}{"result": "Выберите параметры."}, this.Response)

		return
	}

	var queryParams []interface{}
	query := "SELECT params.name FROM params WHERE params.id in ("

	for k, v := range paramsIds {
		paramId, err := strconv.Atoi(v)
		if err != nil {
			utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

			return
		}
		query += "$" + strconv.Itoa(k+1) + ", "
		queryParams = append(queryParams, paramId)
	}
	query = query[:len(query)-2]
	query += ") ORDER BY id;"

	var caption []string
	for _, v := range db.Query(query, queryParams) {
		caption = append(caption, v.(map[string]interface{})["name"].(string))
	}

	result := []interface{}{0: map[string]interface{}{"id": -1, "data": caption}}

	query = `SELECT
        reg.id as id,
        ARRAY(
            SELECT param_values.value
            FROM param_values
            INNER JOIN registrations ON registrations.id = param_values.reg_id
            INNER JOIN events ON events.id = registrations.event_id
            INNER JOIN params ON params.id = param_values.param_id
            WHERE param_values.param_id IN (` + strings.Join(db.MakeParams(len(queryParams)), ", ")
	query += `) AND events.id = $` + strconv.Itoa(len(queryParams)+1) + ` AND registrations.id = reg.id ORDER BY param_values.param_id
        ) as data

        FROM param_values
        INNER JOIN registrations as reg ON reg.id = param_values.reg_id
        INNER JOIN events as ev ON ev.id = reg.event_id
        INNER JOIN params ON params.id = param_values.param_id
        WHERE ev.id = $` + strconv.Itoa(len(queryParams)+1) + ` GROUP BY reg.id ORDER BY reg.id;`

	data := db.Query(query, append(queryParams, eventId))

	this.Render([]string{"mvc/views/list.html"}, "list", append(result, data...))
}