Пример #1
0
func createJsonTableWithRandSuffix(name, keyName, primaryKey string, rand *rand.Rand) (table *mysqldb.JSONTable) {
	suffix := "_" + GenerateGUID(name, string(rand.Int()))[:10]
	table = mysqldb.NewJSONTable(name+suffix, keyName, primaryKey)
	return
}
Пример #2
0
func (this *DataServer) CreateTableFromEventDefinition(def string) (jsonTbl *mysqldb.JSONTable, err error) {
	var eventObj Event
	err = json.Unmarshal([]byte(def), &eventObj)
	if err != nil {
		err = errors.New("Event data could not be parsed.")
		return
	}

	if eventObj.Name == "" {
		err = errors.New("Event has empty name.")
		return
	}

	eventName := eventObj.Name

	if this.database.HasTable(eventName) {
		err = errors.New("Event already exists.")
		return
	}

	tblName := eventName + "_" + GenerateGUID(eventName)[:10]
	newTbl := mysqldb.NewJSONTable(tblName, eventName, eventObj.PrimaryParameter)

	for _, v := range eventObj.Parameters {
		sqlType := ""
		switch val := strings.ToLower(v.Type); val {
		case "guid":
			sqlType = EVENT_PARAMETER_GUID_TYPE
		case "string":
			sqlType = EVENT_PARAMETER_STRING_TYPE
		case "int":
			sqlType = EVENT_PARAMETER_INT_TYPE
		case "float":
			sqlType = EVENT_PARAMETER_FLOAT_TYPE
		case "bool":
			sqlType = EVENT_PARAMETER_BOOL_TYPE
		case "date":
			sqlType = EVENT_PARAMETER_DATE_TYPE
		case "time":
			sqlType = EVENT_PARAMETER_TIME_TYPE
		default:
			err = errors.New("Invalid event parameter type: " + val)
			return
		}
		canNull := false
		if v.Default == "" {
			canNull = true
		}
		newTbl.AddColumn(mysqldb.NewJSONColumn(v.Name, sqlType, v.Default, canNull))
	}

	for _, v := range eventObj.References {
		var fKey mysqldb.ForeignKey

		paraCol := newTbl.GetColumn(v.ParameterName)
		if paraCol != nil {
			fKey.Column = v.ParameterName
		} else {
			err = errors.New("Invalid event reference. Parameter name doesn't match any parameters.")
			return
		}

		if this.database.HasTable(v.RefEventName) {
			fKey.RefTable = v.RefEventName
			refTbl := this.database.GetTable(v.RefEventName)
			if refTbl == nil {
				err = errors.New("Event does not exist: " + v.RefEventName)
				return
			}
			if refTbl.HasColumn(v.RefParameterName) {
				fKey.RefColumn = v.RefParameterName
			} else {
				err = errors.New("Invalid event reference. Referenced event doesn't have parameter " + v.RefParameterName + ".")
				return
			}
		} else {
			err = errors.New("Invalid event reference. Referenced event " + v.RefEventName + " does not exist.")
			return
		}

		newTbl.FKeys = append(newTbl.FKeys, &mysqldb.ForeignKey{v.ParameterName, v.RefEventName, v.RefParameterName})
	}

	jsonTbl = newTbl
	return
}