func (d *DataSourceController) FetchDataSourceMetaData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	connectionID := payload["connectionID"].(string)
	from := payload["from"].(string)

	if connectionID == "" {
		return helper.CreateResult(true, []toolkit.M{}, "")
	}

	dataConn := new(colonycore.Connection)
	err = colonycore.Get(dataConn, connectionID)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	if err := d.checkIfDriverIsSupported(dataConn.Driver); err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	var conn dbox.IConnection
	conn, err = helper.ConnectUsingDataConn(dataConn).Connect()
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer conn.Close()

	var query = conn.NewQuery().Take(1)

	if dataConn.Driver != "weblink" {
		query = query.From(from)
	}

	cursor, err := query.Cursor(nil)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	data := toolkit.M{}
	if dataConn.Driver != "weblink" {
		err = cursor.Fetch(&data, 1, false)
	} else {
		dataAll := []toolkit.M{}
		err = cursor.Fetch(&dataAll, 1, false)
		if len(dataAll) > 0 {
			data = dataAll[0]
		}
	}

	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	metadata := []*colonycore.FieldInfo{}
	for key, val := range data {
		label := key

		lookup := new(colonycore.Lookup)
		lookup.LookupFields = []string{}

		meta := new(colonycore.FieldInfo)
		meta.ID = key
		meta.Label = label
		meta.Type, _ = helper.GetBetterType(val)
		meta.Format = ""
		meta.Lookup = lookup

		metadata = append(metadata, meta)
	}

	return helper.CreateResult(true, metadata, "")
}
예제 #2
0
func (d *DataSourceController) parseMetadata(data toolkit.M) []*colonycore.FieldInfo {
	metadata := []*colonycore.FieldInfo{}

	for key, val := range data {
		label := key

		lookup := new(colonycore.Lookup)
		lookup.LookupFields = []string{}

		meta := new(colonycore.FieldInfo)
		meta.ID = key
		meta.Label = label
		meta.Type, _ = helper.GetBetterType(val)
		meta.Format = ""
		meta.Lookup = lookup
		meta.Type = "string"

		if val != nil {
			if toolkit.TypeName(val) == "map[string]interface {}" {
				val, _ = toolkit.ToM(val)
			}

			switch toolkit.TypeName(val) {
			case "toolkit.M":
				meta.Type = "object"

				meta.Sub = d.parseMetadata(val.(toolkit.M))
			case "[]interface {}":
				meta.Type = "array"

				valArray := val.([]interface{})

				if len(valArray) > 0 {
					if toolkit.TypeName(valArray[0]) == "map[string]interface {}" {
						valArray[0], _ = toolkit.ToM(valArray[0])
					}

					if toolkit.TypeName(valArray[0]) == "toolkit.M" {
						meta.Type = "array-objects"
						meta.Sub = d.parseMetadata(valArray[0].(toolkit.M))
					} else {
						switch target := toolkit.TypeName(valArray[0]); {
						case strings.Contains(target, "interface"):
						case strings.Contains(target, "string"):
							meta.Type = "array-string"
						case strings.Contains(target, "int"):
							meta.Type = "array-int"
						case strings.Contains(target, "float"):
						case strings.Contains(target, "double"):
							meta.Type = "array-double"
						default:
							meta.Type = "array-string"
						}
					}
				}
			}
		}

		metadata = append(metadata, meta)
	}

	return metadata
}