Example #1
0
// IsColumnNullable returns ture or false
func (s Sql) IsColumnNullable(db, table, column string) (r bool, err error) {
	q := "SELECT IS_NULLABLE from `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`=? and TABLE_NAME= ? and COLUMN_NAME = ?"
	var t string
	t, err = s.QueryForString(q, db, table, column)
	r = mcore.NewString(t).ToBool()
	return r, err
}
Example #2
0
// IsHasSheet check sheet exists.
func IsHasSheet(file, sheetName string) bool {
	ns, err := GetSheetNames(file)
	if err != nil {
		return false
	}
	return mcore.NewString(sheetName).IsInArrayIgnoreCase(ns)
}
Example #3
0
// DemoKey demo
func DemoKey() {
	keyStr := mcore.NewString("OrgName-label")
	//keyStr = keyStr.TrimStarts("label.", "msg.", "label-", "msg-")
	//keyStr = keyStr.TrimEnds(".label", ".msg", "-label", "-msg")
	keyStr = keyStr.ReplaceAll("label", "").ReplaceAll("msg", "")
	fmt.Println(keyStr)
	fmt.Println(mcore.ToLabel(keyStr.String()))
}
Example #4
0
// GetColNameIndex returns colname index
// -1 means not found.
func (s *Sheet) GetColNameIndex(colName string) int {
	names := s.GetHeaderRowValues()
	for i, name := range names {
		if mcore.NewString(colName).IsEqualIgnoreCase(name) {
			return i
		}
	}
	return -1
}
Example #5
0
File: main.go Project: mabetle/mcmd
func GetPathFiles(path string, pattern string) (fs []string) {
	path = strings.Replace(path, ";", ":", -1)
	sep := ":"
	pathA := strings.Split(path, sep)
	for _, path := range pathA {
		pfs := mcore.GetDirSubFiles(path)
		for _, pf := range pfs {
			if mcore.NewString(pf).IsHasSuffix("/" + pattern) {
				fs = append(fs, pf)
			}
		}
	}
	return
}
Example #6
0
File: util.go Project: mabetle/mdb
// ParseSqLTableName gets table name from sql.
// from table
func ParseSqlTableName(sql string) string {
	sqlA := strings.Split(sql, " ")
	// no from key words
	if !mcore.NewString("from").IsInArrayIgnoreCase(sqlA) {
		return ""
	}
	table := ""
	start := false
	for _, v := range sqlA {
		if strings.ToLower(v) == "from" {
			start = true
			continue
		}
		if start && v != "" {
			table = v
		}
	}
	return table
}
Example #7
0
// DemoSubSep demo
func DemoSubSep() {
	in := mcore.NewString("abc?def")
	fmt.Println(in.SubLeftSep("?"))
	fmt.Println(in.SubRightSep("?"))
}
Example #8
0
// DemoTime demo
func DemoTime() {
	ds := "2015-09-06 00:00:00"
	t, _ := mcore.NewString(ds).ToTime()
	mcore.PrintTime(t)
}
Example #9
0
// SqlRowsToExcelWithLocale sql rows to excel
// Locale message, table column name.
// params:
//	sheetName
//	rows
//	include
//	exclude
//	locale
//	enableLocale
func SqlRowsToExcelWithLocale(sheetName string,
	tableName string,
	rows *sql.Rows,
	include string,
	exclude string,
	locale string,
	enableLocale bool) (*xlsx.File, error) {

	defer rows.Close()
	if sheetName == "" {
		sheetName = "Sheet1"
	}
	file := xlsx.NewFile()
	sheet, err := file.AddSheet(sheetName)
	if logger.CheckError(err) {
		return nil, err
	}
	colNames, err := rows.Columns()
	if logger.CheckError(err) {
		return nil, err
	}
	// add header
	row := sheet.AddRow()
	for _, colName := range colNames {
		if !mcore.IsIncludeExcludeIn(colName, colNames, include, exclude) {
			continue
		}
		cell := row.AddCell()
		// colName to locale label
		if enableLocale && locale != "" {
			colName = mmsg.GetTableColumnLabel(locale, tableName, colName)
		}
		cell.Value = colName
	}
	scanArgs := make([]interface{}, len(colNames))
	values := make([]interface{}, len(colNames))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	// add rows data
	for rows.Next() {
		err := rows.Scan(scanArgs...)
		if logger.CheckError(err) {
			continue
		}
		row := sheet.AddRow()
		index := -1
		for _, v := range values {
			index++
			// skip for no include column
			if !mcore.IsIncludeExcludeIn(colNames[index], colNames, include, exclude) {
				continue
			}
			cell := row.AddCell()
			// for float
			fv, err := mcore.NewString(v).ToFloat64()
			if err == nil {
				cell.SetFloat(fv)
				continue
			}
			//if d, b := v.(int64); b {
			//cell.SetInt64(d)
			//continue
			//}
			cell.SetValue(v)
		}
	}
	return file, nil
}
Example #10
0
// GetBool
func (c BaseConfig) GetBool(key string) bool {
	return mcore.NewString(c.GetString(key, "F")).ToBool()
}
Example #11
0
// GetCellFloat64ByRowIndexColName return cell float value
func (s *Sheet) GetCellFloat64ByRowIndexColName(rowIndex int, colName string) float64 {
	v := s.GetCellValueByRowIndexColName(rowIndex, colName)
	return mcore.NewString(v).ToFloat64NoError()
}
Example #12
0
// GetFloat returns column float value
func (t *BaseTable) GetFloat(colName string) float64 {
	return mcore.NewString(t.GetString(colName)).ToFloat64NoError()
}
Example #13
0
// GetInt returns column int value
func (t *BaseTable) GetInt(colName string) int {
	return mcore.NewString(t.GetString(colName)).ToIntNoError()
}
Example #14
0
File: sql.go Project: mabetle/mdb
// QueryForBool returns bool result.
func (s Sql) QueryForBool(sql string, args ...interface{}) bool {
	v := s.QueryForStringNoError(sql, args...)
	return mcore.NewString(v).ToBool()
}
Example #15
0
// GetCellIntByRowIndexColName return cell int value
func (s *Sheet) GetCellIntByRowIndexColName(rowIndex int, colName string) int {
	v := s.GetCellValueByRowIndexColName(rowIndex, colName)
	return mcore.NewString(v).ToIntNoError()
}
Example #16
0
// ValidateField
func ValidateField(v interface{}, fn string) (err string) {
	tv := GetValidatorTag(v, fn)
	logger.Debugf("Validate Tag: %s", tv)
	if tv == "" {
		// field no tag, skip
		return ""
	}
	vds := strings.Split(tv, ",")
	fv := mcore.GetFieldValue(v, fn)
	logger.Debugf("Field  Value: %s", fv)

	for _, item := range vds {
		kv := strings.Split(item, "=")
		v_key := strings.TrimSpace(kv[0])
		v_key = strings.ToLower(v_key)
		v_value := ""
		if len(kv) > 1 {
			v_value = kv[1]
			v_value = strings.TrimSpace(v_value)
			v_value = strings.TrimLeft(v_value, "'")
			v_value = strings.TrimRight(v_value, "'")
		}
		switch v_key {
		case V_REQUIRED:
			if fv == "" {
				err = err + "null or blank."
			}
		case V_MIN:
			i_v_value, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			i_f_value, err2 := mcore.StrToInt(fv)
			if err2 != nil {
				// field value parse int error, skip
				continue
			}
			if i_f_value < i_v_value {
				err = err + fmt.Sprintf("%s less than %s.", fv, v_value)
			}
		case V_MAX:
			i_v_value, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			i_f_value, err2 := mcore.StrToInt(fv)
			if err2 != nil {
				// field value parse int error, skip
				continue
			}
			if i_f_value > i_v_value {
				err = err + fmt.Sprintf("%s great than %s.", fv, v_value)
			}
		case V_MAX_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) > length {
				err = err + fmt.Sprintf("%s length great than %s.", fv, v_value)
			}
		case V_MIN_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) < length {
				err = err + fmt.Sprintf("%s length less than %s.", fv, v_value)
			}
		case V_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) != length {
				err = err + fmt.Sprintf("%s length not equals %s.", fv, v_value)
			}
		case V_EMAIL:
			if !mcore.NewString(fv).IsEmail() {
				err = err + fmt.Sprintf("%s not a Email.", fv)
			}
		case V_IDCARD:
			if !mcore.NewString(fv).IsIdCardNo() {
				err = err + fmt.Sprintf("%s not a ID Card number.", fv)
			}
		case V_NUMBER:
			if !mcore.NewString(fv).IsNumber() {
				err = err + fmt.Sprintf("%s not a number.", fv)
			}
		case V_PHONE:
			if !mcore.NewString(fv).IsPhoneNumber() {
				err = err + fmt.Sprintf("%s not a phone number.", fv)
			}
		case V_REGEX:
			if !mcore.NewString(fv).IsMatchString(v_value) {
				err = err + fmt.Sprintf("%s not match %s.", fv, v_value)
			}
		default:
			// unknow validator tag.
			logger.Warnf("Unknown validate tag: %s ", item)
		}
	}
	logger.Debugf("Validate Result: %s", err)
	return
}
Example #17
0
// GetInt
func (c BaseConfig) GetInt(key string) int {
	return mcore.NewString(c.GetString(key, "0")).ToIntNoError()
}