// 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 }
// 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) }
// 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())) }
// 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 }
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 }
// 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 }
// DemoSubSep demo func DemoSubSep() { in := mcore.NewString("abc?def") fmt.Println(in.SubLeftSep("?")) fmt.Println(in.SubRightSep("?")) }
// DemoTime demo func DemoTime() { ds := "2015-09-06 00:00:00" t, _ := mcore.NewString(ds).ToTime() mcore.PrintTime(t) }
// 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 }
// GetBool func (c BaseConfig) GetBool(key string) bool { return mcore.NewString(c.GetString(key, "F")).ToBool() }
// GetCellFloat64ByRowIndexColName return cell float value func (s *Sheet) GetCellFloat64ByRowIndexColName(rowIndex int, colName string) float64 { v := s.GetCellValueByRowIndexColName(rowIndex, colName) return mcore.NewString(v).ToFloat64NoError() }
// GetFloat returns column float value func (t *BaseTable) GetFloat(colName string) float64 { return mcore.NewString(t.GetString(colName)).ToFloat64NoError() }
// GetInt returns column int value func (t *BaseTable) GetInt(colName string) int { return mcore.NewString(t.GetString(colName)).ToIntNoError() }
// QueryForBool returns bool result. func (s Sql) QueryForBool(sql string, args ...interface{}) bool { v := s.QueryForStringNoError(sql, args...) return mcore.NewString(v).ToBool() }
// GetCellIntByRowIndexColName return cell int value func (s *Sheet) GetCellIntByRowIndexColName(rowIndex int, colName string) int { v := s.GetCellValueByRowIndexColName(rowIndex, colName) return mcore.NewString(v).ToIntNoError() }
// 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 }
// GetInt func (c BaseConfig) GetInt(key string) int { return mcore.NewString(c.GetString(key, "0")).ToIntNoError() }