Beispiel #1
0
func buildMongoWhereByArray(wheres *simplejson.Json, primaryKey string) []bson.M {
	mongoWheres := []bson.M{}
	for key, _ := range wheres.MustArray() {
		mongoWhere := BuildMongoWhere(wheres.GetIndex(key), primaryKey)
		mongoWheres = append(mongoWheres, mongoWhere)
	}
	return mongoWheres
}
func createTableStatement(tableName *string, columnsJson *simplejson.Json, uniqueColumns *simplejson.Json, primaryKeyColumns *simplejson.Json) string {
	var buffer bytes.Buffer
	buffer.WriteString("CREATE TABLE ")
	buffer.WriteString(*tableName)
	buffer.WriteString(" (\n")

	columnsArry, err := columnsJson.Array()
	if err != nil {
		reportError("Couldn't parse columns json: ", err)
	}
	for i, _ := range columnsArry {
		if i != 0 {
			buffer.WriteString(",\n")
		}
		columnSchema := columnsJson.GetIndex(i)
		columnName, err := columnSchema.Get("name").String()
		if err != nil {
			reportError("Couldn't parse column name for table json: ", err)
		}
		columnType, err := columnSchema.Get("type").String()
		if err != nil {
			reportError("Couldn't parse column type for table json: ", err)
		}
		buffer.WriteString(columnName)
		buffer.WriteString(" ")
		buffer.WriteString(columnType)

		if columnUnique, err := columnSchema.Get("unique").Bool(); err == nil && columnUnique {
			buffer.WriteString(" UNIQUE")
		}

		if columnNull, err := columnSchema.Get("null").Bool(); err == nil && !columnNull {
			buffer.WriteString(" NOT NULL")
		}
	}

	uniqueColumnsArry, err := uniqueColumns.Array()
	if err == nil && len(uniqueColumnsArry) > 0 { // error would mean there weren't any uniqueColmns defined
		buffer.WriteString(",\nUNIQUE (")
		for i, _ := range uniqueColumnsArry {
			if i != 0 {
				buffer.WriteString(", ")
			}
			uniqueColumnName, err := uniqueColumns.GetIndex(i).String()
			if err != nil {
				reportError("Couldn't parse unique column name for table json: ", err)
			}
			buffer.WriteString(uniqueColumnName)
		}
		buffer.WriteString(")")
	}

	primaryKeyColumnsArry, err := primaryKeyColumns.Array()
	if err == nil && len(primaryKeyColumnsArry) > 0 { // error would mean there weren't any uniqueColmns defined
		buffer.WriteString(",\nUNIQUE (")
		for i, _ := range primaryKeyColumnsArry {
			if i != 0 {
				buffer.WriteString(", ")
			}
			primaryKeyColumnName, err := primaryKeyColumns.GetIndex(i).String()
			if err != nil {
				reportError("Couldn't parse primary key column name for table json: ", err)
			}
			buffer.WriteString(primaryKeyColumnName)
		}
		buffer.WriteString(")")
	}

	buffer.WriteString("\n);")

	return buffer.String()
}