Ejemplo n.º 1
0
func (c *Sample) RemoveSampleData() {
	data := new(m.Availability)
	e := c.GetById(data, 526, "id")
	tk.Println(data)
	e = c.SqlCtx.Delete(data)
	if e != nil {
		tk.Errorf("Unable to remove: %s \n", e.Error())
	}
}
Ejemplo n.º 2
0
func (c *Sample) UpdateSampleData() {
	id := 1
	data := new(m.Availability)
	e := c.GetById(data, id, "id")
	data.PrctWUF = 1.2
	c.Update(data, id, "id")
	if e != nil {
		tk.Errorf("Unable to remove: %s \n", e.Error())
	}
}
Ejemplo n.º 3
0
func (m *Availability) GetData(ID int, SqlCtx *orm.DataContext) (interface{}, error) {
	csr, e := SqlCtx.Connection.NewQuery().Command("procedure", tk.M{}.
		Set("name", "TEST").
		Set("parms", tk.M{}.Set("@ID", ID))).
		Cursor(nil)
	result := []Availability{}
	e = csr.Fetch(&result, 0, false)
	defer csr.Close()
	if e != nil {
		tk.Errorf("Unable to get availability data: %s \n", e.Error())
		return nil, e
	}
	return result, nil
}
Ejemplo n.º 4
0
func (pkg *PackageModel) WriteBase(path string) error {
	filename := filepath.Join(path, "base.go")
	f, e := os.Open(filename)
	if e == nil {
		os.Remove(filename)
	}

	f, e = os.Create(filename)
	if e != nil {
		return toolkit.Errorf("Failed to write %s: %s", "base.go", e.Error())
	}
	defer f.Close()

	b := bufio.NewWriter(f)
	b.WriteString(toolkit.Formatf(baseGo, pkg.Name))
	e = b.Flush()
	if e != nil {
		return toolkit.Errorf("Failed to write base.go: %s", e.Error())
	}

	toolkit.RunCommand("/bin/sh", "-c", "gofmt -w "+filename)
	return nil
}
Ejemplo n.º 5
0
func (sm *StructModel) Write(pkg *PackageModel, path string) error {
	if pkg.Name == "" || sm.Name == "" {
		return toolkit.Errorf("Both package name and struct name should be defined")
	}
	//return toolkit.Errorf("Fail to write %s.%s : method Write is not yet implemented", pkg.Name, sm.Name)

	//-- write base
	e := pkg.WriteBase(path)
	if e != nil {
		return e
	}

	filename := filepath.Join(path, strings.ToLower(sm.Name)+".go")
	//currentCode := ""
	f, e := os.Open(filename)
	if e == nil {
		//bcurrent, _ := ioutil.ReadAll(f)
		//currentCode = string(bcurrent)
		os.Remove(filename)
	}

	f, e = os.Create(filename)
	if e != nil {
		return toolkit.Errorf("Failed to write %s.%s: %s", pkg.Name, sm.Name, e.Error())
	}
	defer f.Close()

	txts := []string{}
	//--- package
	txts = append(txts, "package "+pkg.Name)

	//--- imports
	txts = append(txts,
		toolkit.Sprintf("import (%s)",
			libs(mandatoryLibs, pkg.ObjectLibs, sm.Libs)))

	//--- struct definition
	txts = append(txts, "type "+sm.Name+" struct {\n"+
		"orm.ModelBase `bson:\"-\" json:\"-\"`")
	for _, f := range sm.Fields {
		if f.Type == "" {
			f.Type = "string"
		}
		fieldStr := toolkit.Sprintf("%s %s %s", f.Name, f.Type, f.Tag)
		txts = append(txts, fieldStr)
	}
	txts = append(txts, "}")

	//--- tablename
	pluralNames := strings.ToLower(sm.Name)
	if strings.HasSuffix(pluralNames, "s") {
		pluralNames = pluralNames + "es"
	} else {
		pluralNames = pluralNames + "s"
	}
	tablename := toolkit.Sprintf("func (o *%s) TableName()string{"+
		"return \"%s\"\n"+
		"}", sm.Name, pluralNames)
	txts = append(txts, tablename)

	//--- new
	fieldBuilders := ""
	for _, field := range sm.Fields {
		notEmpty := !toolkit.IsNilOrEmpty(field.Default)
		if notEmpty {
			def := toolkit.Sprintf("%v", field.Default)
			if field.Type == "string" {
				def = "\"" + def + "\""
			}
			fieldBuilders +=
				toolkit.Sprintf("o.%s=%s", field.Name, def) +
					"\n"
		}
	}
	newfn := "func New{0}() *{0}{\n" +
		"o:=new({0})\n" +
		fieldBuilders +
		"return o" +
		"}"
	newfn = toolkit.Formatf(newfn, sm.Name)
	txts = append(txts, newfn)

	//--- find
	tpl := `func {0}Find(filter *dbox.Filter, fields, orders string, limit, skip int) dbox.ICursor {
            config := makeFindConfig(fields, orders, skip, limit)
            if filter != nil {
                config.Set("where", filter)
            }
		    c, _ := DB().Find(new({0}), config)
            return c
        }`
	txts = append(txts, toolkit.Formatf(tpl, sm.Name))

	//--- get
	tpl = `func {0}Get(filter *dbox.Filter, orders string, skip int) (emp *{0}, err error) {
        config := makeFindConfig("", orders, skip, 1)
        if filter != nil {
            config.Set("where", filter)
        }
        c, ecursor := DB().Find(new({0}), config)
        if ecursor != nil {
            return nil, ecursor
        }
        defer c.Close()

        emp = new({0})
        err = c.Fetch(emp, 1, false)
        return emp, err
    }`
	txts = append(txts, toolkit.Formatf(tpl, sm.Name))

	//-- method & get
	for _, method := range sm.Methods {
		txts = append(txts, sm.buildMethod(
			pkg,
			method.Type,
			method.Field))
	}

	b := bufio.NewWriter(f)
	for _, txt := range txts {
		b.WriteString(txt + "\n")
	}

	e = b.Flush()
	if e != nil {
		return toolkit.Errorf("Failed to write %s.%s: %s", pkg.Name, sm.Name, e.Error())
	}

	toolkit.RunCommand("/bin/sh", "-c", "gofmt -w "+filename)
	return nil
}