Exemple #1
0
// generateDefaultValue generates `default` string for given coloumn
func generateDefaultValue(schemaName string, propertyName, tableName string, s *schema.Schema) string {
	// if property is id, use sequence generator as default value
	if propertyName == "id" {
		return fmt.Sprintf("DEFAULT nextval('%s.%s_id_seq' :: regclass) ", schemaName, tableName)
	}

	if s.Default == nil {
		return ""
	}

	if len(s.Enum) > 0 {
		// enums should be a valud enum string
		if !common.IsIn(s.Default.(string), s.Enum...) {
			fmt.Printf("%s not a valid enum", s.Default)
			os.Exit(1)
		}

		return fmt.Sprintf("DEFAULT '%s'", s.Default)
	}

	def := ""
	switch s.Default.(type) {
	case float64, float32, int16, int32, int, int64, uint16, uint32, uint, uint64, bool:
		def = fmt.Sprintf("%v", s.Default)
	default:
		def = fmt.Sprintf("%v", s.Default)

		// if default is a function call, use it
		if strings.HasSuffix(def, "()") {
			return fmt.Sprintf("DEFAULT %s", def)
		}

		// it is string, quote it
		def = fmt.Sprintf("'%v'", s.Default)
	}

	return fmt.Sprintf("DEFAULT %s", strings.ToUpper(def))
}
Exemple #2
0
// Generate generates the basic CRUD statements for the models
func (g *Generator) Generate(req *common.Req, res *common.Res) error {
	context := req.Context

	if context == nil || context.Config == nil || !common.IsIn(GeneratorName, context.Config.Generators...) {
		return nil
	}

	if req.Schema == nil {
		if req.SchemaStr == "" {
			return errors.New("both schema and string schema is not set")
		}

		s := &schema.Schema{}
		if err := json.Unmarshal([]byte(req.SchemaStr), s); err != nil {
			return err
		}

		req.Schema = s.Resolve(nil)
	}

	if req.Schema.Title == "" {
		return errors.New("Title should be set")
	}

	outputs := make([]common.Output, 0)

	moduleName := stringext.ToFieldName(req.Schema.Title)

	settings := GenerateSettings(GeneratorName, moduleName, req.Schema)
	settings.SetNX("rootPathPrefix", "db")
	rootPathPrefix := settings.Get("rootPathPrefix").(string)
	fullPathPrefix := req.Context.Config.Target + rootPathPrefix + "/"
	settings.Set("fullPathPrefix", fullPathPrefix)

	for _, name := range schema.SortedKeys(req.Schema.Definitions) {
		def := req.Schema.Definitions[name]

		// schema should have our generator
		if !def.Generators.Has(GeneratorName) {
			continue
		}

		settingsDef := SetDefaultSettings(GeneratorName, settings, def)
		settingsDef.Set("tableName", stringext.ToFieldName(def.Title))

		//
		// generate roles
		//
		role, err := DefineRole(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: role,
			Path: fmt.Sprintf(
				"%s/001-%s_roles.sql",
				fullPathPrefix,
				settingsDef.Get("databaseName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate database
		//
		db, err := DefineDatabase(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: db,
			Path: fmt.Sprintf(
				"%s/002-%s_database.sql",
				fullPathPrefix,
				settingsDef.Get("databaseName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate extenstions
		//
		extenstions, err := DefineExtensions(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: extenstions,
			Path: fmt.Sprintf(
				"%s/003-%s_extensions.sql",
				fullPathPrefix,
				settingsDef.Get("databaseName").(string)),
			DoNotFormat: true,
		})

		//
		// generate schema
		//
		sc, err := DefineSchema(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: sc,
			Path: fmt.Sprintf(
				"%s/%s/004-schema.sql",
				fullPathPrefix,
				settingsDef.Get("schemaName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate sequences
		//
		sequence, err := DefineSequence(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: sequence,
			Path: fmt.Sprintf(
				"%s/%s/005-%s-sequence.sql",
				fullPathPrefix,
				settingsDef.Get("schemaName").(string),
				settingsDef.Get("tableName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate types
		//
		types, err := DefineTypes(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: types,
			Path: fmt.Sprintf(
				"%s/%s/006-%s-types.sql",
				fullPathPrefix,
				settingsDef.Get("schemaName").(string),
				settingsDef.Get("tableName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate tables
		//
		table, err := DefineTable(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: table,
			Path: fmt.Sprintf(
				"%s/%s/007-%s-table.sql",
				fullPathPrefix,
				settingsDef.Get("schemaName").(string),
				settingsDef.Get("tableName").(string),
			),
			DoNotFormat: true,
		})

		//
		// generate constraints
		//
		constraints, err := DefineConstraints(settingsDef, def)
		if err != nil {
			return err
		}

		outputs = append(outputs, common.Output{
			Content: constraints,
			Path: fmt.Sprintf(
				"%s/%s/008-%s-constraints.sql",
				fullPathPrefix,
				settingsDef.Get("schemaName").(string),
				settingsDef.Get("tableName").(string),
			),
			DoNotFormat: true,
		})
	}

	res.Output = outputs
	return nil
}