Beispiel #1
0
func (r *UpdateQuery) Handle(request Request, requestData interface{}) (shared.IResponse, error) {
	updateRequest, ok := requestData.(*updateRequest)
	if !ok {
		return nil, fmt.Errorf("Request type mismatch")
	}
	queryConditions, err := updateRequest.Conditions.TranslateToQuery()
	if err != nil {
		return nil, err
	}

	session := request.Session()
	model := r.Core.GetModel()
	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	query, err := databath.GetQuery(request.GetContext(), model, queryConditions, true)
	if err != nil {
		return nil, err
	}
	var hookContext *components.HookContext
	if updateRequest.Conditions.Pk != nil { // Not for Bulk requests
		hookContext = &components.HookContext{
			DB: db,
			ActionSummary: &shared.ActionSummary{
				UserId:     *session.UserID(),
				Action:     "update",
				Collection: *updateRequest.Conditions.Collection,
				Pk:         *updateRequest.Conditions.Pk,
				Fields:     updateRequest.Changeset,
			},
			Session: request.Session(),
			Core:    r.Core,
		}
		r.Core.DoPreHooks(hookContext)
	}
	sqlString, parameters, err := query.BuildUpdate(updateRequest.Changeset)
	if err != nil {
		return nil, err
	}
	log.Printf("Run: %s %v\n", sqlString, parameters)
	resp, err := db.Exec(sqlString, parameters...)
	if err != nil {
		return nil, err
	}
	rows, _ := resp.RowsAffected()

	updateObject := map[string]interface{}{
		"collection": updateRequest.Conditions.Collection,
		"id":         updateRequest.Conditions.Pk,
	}

	go request.Broadcast("update", updateObject)

	if hookContext != nil { // Not for Bulk Requests, as above
		go r.Core.DoPostHooks(hookContext)
	}
	return JSON(map[string]int64{"affected": rows}), nil
}
Beispiel #2
0
func (r *DeleteQuery) Handle(request Request, requestData interface{}) (shared.IResponse, error) {
	deleteRequest, ok := requestData.(*deleteRequest)
	if !ok {
		return nil, fmt.Errorf("Request Type Mismatch")
	}

	model := r.Core.GetModel()

	qc := databath.GetMinimalQueryConditions(deleteRequest.Collection, "form")

	query, err := databath.GetQuery(request.GetContext(), model, qc, true)
	if err != nil {
		return nil, err
	}

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	deleteCheckResult, err := query.CheckDelete(db, deleteRequest.Id)
	if err != nil {
		return nil, err
	}

	if deleteCheckResult.Prevents {
		return nil, fmt.Errorf("Could not delete, as owners exist: \n%s", strings.Join(deleteCheckResult.GetIssues(), "\n"))
	}

	err = deleteCheckResult.ExecuteRecursive(db)
	if err != nil {
		return nil, err
	}

	sqlString, err := query.BuildDelete(deleteRequest.Id)
	if err != nil {
		return nil, err
	}

	_, err = db.Exec(sqlString)
	if err != nil {
		return nil, err
	}

	result := createResult{
		Status:   "OK",
		Message:  "Success",
		InsertId: 0,
	}

	deleteObject := map[string]interface{}{
		"collection": deleteRequest.Collection,
		"id":         deleteRequest.Id,
	}
	go request.Broadcast("delete", deleteObject)

	return JSON(result), nil

}
Beispiel #3
0
func (h *FileHandler) writeDatabaseEntry(request shared.IRequest, dbEntry map[string]interface{}, fileCollection string) error {

	qc := databath.GetMinimalQueryConditions(fileCollection, "form")

	q, err := databath.GetQuery(request.GetContext(), h.Model, qc, true)
	if err != nil {
		return err
	}
	sqlString, parameters, err := q.BuildInsert(dbEntry)
	if err != nil {
		return err
	}

	db, err := request.DB()
	if err != nil {
		return err
	}

	fmt.Println(sqlString)

	res, err := db.Exec(sqlString, parameters...)

	if err != nil {
		return err
	}

	pk, _ := res.LastInsertId()
	/*
		actionSummary := &shared_structs.ActionSummary{
			UserId:     request.Session().User().Id,
			Action:     "create",
			Collection: fileCollection,
			Pk:         uint64(pk),
			Fields:     dbEntry,
		}
	*/
	createObject := map[string]interface{}{
		"collection": fileCollection,
		"id":         uint64(pk),
		"object":     dbEntry,
	}

	request.Broadcast("create", createObject)

	return nil
}
Beispiel #4
0
func (r *SelectQuery) Handle(request Request, requestData interface{}) (shared.IResponse, error) {

	rawQueryCondition, ok := requestData.(*databath.RawQueryConditions)
	if !ok {
		return nil, fmt.Errorf("Request type mismatch")
	}
	queryConditions, err := rawQueryCondition.TranslateToQuery()
	if err != nil {
		return nil, err
	}

	model := r.Core.GetModel()

	query, err := databath.GetQuery(request.GetContext(), model, queryConditions, false)
	if err != nil {
		return nil, err
	}

	sqlString, countString, parameters, err := query.BuildSelect()
	if err != nil {
		return nil, err
	}

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	countRow := db.QueryRow(countString, parameters...)
	if err != nil {
		return nil, err
	}
	var count uint64
	countRow.Scan(&count)

	allRows, err := query.RunQueryWithResults(db, sqlString, parameters)
	if err != nil {
		return nil, err
	}

	return JSON(map[string]interface{}{
		"rows":  allRows,
		"count": count,
	}), nil
}
Beispiel #5
0
func (r *Reporter) doSelect(db *sql.DB, rawQueryConditions *databath.RawQueryConditions, context *databath.MapContext) ([]map[string]interface{}, error) {
	queryConditions, err := rawQueryConditions.TranslateToQuery()
	if err != nil {
		return nil, err
	}

	query, err := databath.GetQuery(context, r.Model, queryConditions, false)
	if err != nil {
		return nil, err
	}
	sqlString, _, parameters, err := query.BuildSelect()
	if err != nil {
		return nil, err
	}

	allRows, err := query.RunQueryWithResults(db, sqlString, parameters)
	if err != nil {
		return nil, err
	}
	return allRows, nil
}
Beispiel #6
0
func (h *CSVHandler) Handle(request shared.IPathRequest) (shared.IResponse, error) {

	var queryStringQuery string

	err := request.ScanPath(&queryStringQuery)
	if err != nil {
		return nil, err
	}

	rawQuery := databath.RawQueryConditions{}

	jsonQuery, err := url.QueryUnescape(queryStringQuery)
	if err != nil {
		return nil, err
	}
	log.Printf("Decode CSV Query: %s\n", jsonQuery)
	err = json.Unmarshal([]byte(jsonQuery), &rawQuery)
	if err != nil {
		return nil, err
	}
	var neg1 int64 = -1
	rawQuery.Limit = &neg1
	rawQuery.Offset = nil

	qc, err := rawQuery.TranslateToQuery()
	if err != nil {
		return nil, err
	}

	query, err := databath.GetQuery(request.GetContext(), h.Model, qc, false)
	if err != nil {
		return nil, err
	}
	sqlString, _, parameters, err := query.BuildSelect()
	if err != nil {
		return nil, err
	}

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	rows, err := query.RunQueryWithResults(db, sqlString, parameters)
	if err != nil {
		return nil, err
	}

	filename := *rawQuery.Collection + "-" + time.Now().Format("2006-01-02") + ".csv"

	/*
		fieldset := h.Model.Collections[*rawQuery.Collection].FieldSets[*rawQuery.Fieldset]

		csvFields := make([]CSVField, len(fieldset), len(fieldset))

		for i, fieldDef := range fieldset {
			csvFields[i] = CSVField{
				Title:  fieldDef.GetTitle(),
				SQLCol: fieldDef.GetSQLColName(),
			}

		}*/

	mappedFields, err := query.GetFields()

	if err != nil {
		log.Print(err)
		request.DoError(err)
	}

	allColNames, err := query.GetColNames()
	if err != nil {
		log.Print(err)
		request.DoError(err)
	}

	colNames := make([]string, 0, 0)
	for _, colName := range allColNames {
		if strings.HasPrefix(colName, "-") {
			continue
		}
		if strings.HasPrefix(colName, "#") {
			continue
		}

		colNames = append(colNames, colName)
	}

	resp := &csvResponse{
		rows:     rows,
		filename: filename,
		//fields:   csvFields,
		colNames:     colNames,
		mappedFields: mappedFields,
	}

	return resp, nil

}
Beispiel #7
0
func (r *CreateQuery) Handle(request Request, requestObject interface{}) (shared.IResponse, error) {
	createRequest, ok := requestObject.(*createRequest)
	if !ok {
		return nil, fmt.Errorf("Request Type Mismatch")
	}

	session := request.Session()

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	if len(createRequest.Fieldset) < 1 {
		createRequest.Fieldset = "form"
	}

	qc := databath.GetMinimalQueryConditions(createRequest.Collection, createRequest.Fieldset)

	model := r.Core.GetModel()

	query, err := databath.GetQuery(request.GetContext(), model, qc, true)
	if err != nil {
		return nil, err
	}

	hookContext := &components.HookContext{
		DB: db,
		ActionSummary: &shared.ActionSummary{
			UserId:     *session.UserID(),
			Action:     "create",
			Collection: createRequest.Collection,
			Pk:         0,
			Fields:     createRequest.Values,
		},
		Session: request.Session(),
		Core:    r.Core,
	}

	r.Core.DoPreHooks(hookContext)

	sqlString, parameters, err := query.BuildInsert(createRequest.Values)
	if err != nil {
		return nil, err
	}

	res, err := db.Exec(sqlString, parameters...)
	if err != nil {
		log.Printf("ERROR in Exec %s: %s\n", sqlString, err.Error())
		return nil, err
	}
	id, err := res.LastInsertId()
	if err != nil {
		return nil, err
	}
	hookContext.ActionSummary.Pk = uint64(id)
	result := createResult{
		Status:   "OK",
		Message:  "Success",
		InsertId: id,
	}

	createObject := map[string]interface{}{
		"collection": createRequest.Collection,
		"id":         id,
		"object":     createRequest.Values,
	}

	go r.Core.DoPostHooks(hookContext) //db, actionSummary, request.Session())
	go request.Broadcast("create", createObject)
	return JSON(result), nil
}