Exemple #1
0
func (c *AppConf) Init() {
	if c.IsExist() {
		return
	}
	sb := mcore.NewStringBuffer()
	sb.AppendLine("# Auto Generate")
	sb.AppendLine(fmt.Sprintf("%s=%s", KEY_VENDOR_NAME, V_VENDOR_MABETLE))
	logger.Tracef("Init config. AppName:%s Location:%s", c.AppName, c.Location())
	_, err := mcore.WriteFile(c.Location(), sb.String())
	if err != nil {
		logger.Error(err)
	}
}
Exemple #2
0
// QueryForHtml
func (s Sql) QueryForHtml(q string, args []interface{}, renderArgs ...string) string {
	// parse renderArgs
	include := mcore.GetArgString("include", "", renderArgs...)
	exclude := mcore.GetArgString("exclude", "", renderArgs...)
	hasLimit := mcore.IsArgExists("limit", renderArgs...)
	limit := mcore.GetArgInt("limit", 20, renderArgs...)

	dataMap, columns, err := s.QueryForMaps(q, args...)
	if err != nil {
		return fmt.Sprintf(`<div class="error">Error: %v</div>`, err)
	}

	if len(dataMap) == 0 {
		return fmt.Sprintf(`<div class="info">No results<div>`)
	}

	sb := mcore.NewStringBuffer()
	sb.Append(`<table class="t-data-grid"><thead><tr>`)

	usedFields := mcore.GetFieldsUsed(columns, include, exclude)
	// append table header
	for _, colName := range usedFields {
		// TODO i18n col name
		sb.Appendf(`<th>%v</th>`, colName)
	}
	sb.Append(`</tr></thead><tbody>`)
	// append table body
	for i, rowMap := range dataMap {
		trClass := "odd"
		if i%2 == 0 {
			trClass = "even"
		}
		if hasLimit && i >= limit {
			break
		}
		sb.Appendf(`<tr class="%s">`, trClass)
		for _, colName := range usedFields {
			if v, ok := rowMap[colName]; ok {
				// has column
				sb.Appendf(`<td>%v</td>`, mcore.GetString(v))
			} else {
				// no value
				sb.Append(`<td></td>`)
			}
		}
		sb.Append("</tr>")
	}
	sb.Append("</tbody></table>")
	return sb.String()
}
Exemple #3
0
// Generate create table sql form database table.
// used for export exist tables struct.
func (s Sql) GenTableCreateSql(db, table string) string {
	sb := mcore.NewStringBuffer()
	sb.Appendf("-- generate create sql for table : %s\n", table)
	sb.Appendf("create table %s (\n", table)
	cols := s.GetMetaColumns(db, table)
	n := len(cols)
	for k, v := range cols {
		var nullDesc string
		if v.IsNullable() {
			nullDesc = "null"
		} else {
			nullDesc = "not null"
		}
		var priDesc string
		if v.IsPrimaryKey() {
			priDesc = "primary key"
		}

		var end string = ","
		if k == n-1 {
			end = ""
		}

		var defaultDesc string
		if (!v.IsNullable()) && v.HasDefault() {
			switch mcore.String(v.DataType).ToLower() {
			case "char", "varchar", "text", "tinytext", "mediumtext", "longtext":
				defaultDesc = fmt.Sprintf("default '%s'", v.ColumnDefault)
			case "int", "integer", "tinyint", "bigint", "smallint", "mediumint", "float", "double", "real", "decimal", "numeric":
				defaultDesc = fmt.Sprintf("default %s", v.ColumnDefault)
			default:
			}
		}

		// name varchar(50) null default '',
		//
		sb.Appendf("\t%s %s %s %s %s %s\n",
			v.ColumnName,
			v.ColumnType,
			nullDesc,
			defaultDesc,
			priDesc,
			end)
	}
	sb.Append(")\n")
	//print it before return
	mcore.Println(sb)
	return sb.String()
}