Beispiel #1
0
func (s Sql) AlterMetaColumnDefault(col MetaColumn) {
	column := col.ColumnName
	table := col.TableName
	sc := mcore.String(column)
	if sc.IsEqualIgnoreCase("id") {
		//skip id column
		return
	}

	if !col.IsNullable() {
		//skip for not nullable
		logger.Debug(fmt.Sprintf("is not nulable, skip. column:%s.%s.%s, type: %s", col.SchemaName, col.TableName, col.ColumnName, col.DataType))
	}

	sct := mcore.String(col.DataType).ToLower()

	if sct.IsInArray(hasDefaultNumColumn) {
		//process num column
		q := fmt.Sprintf("update %s	set %s = %s where %s is null", table, column, col.ColumnDefault, column)
		s.Exec(q)

		q = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s %s NOT NULL DEFAULT %s", table, column, col.ColumnType, col.ColumnDefault)
		s.Exec(q)
	} else if sct.IsInArray(hasDefaultTextColumn) {
		//process for char
		q := fmt.Sprintf("update %s	set %s = '%s' where %s is null", table, column, col.ColumnDefault, column)
		s.Exec(q)

		q = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s %s NOT NULL DEFAULT '%s'", table, column, col.ColumnType, col.ColumnDefault)
		s.Exec(q)
	} else {
		//skip process
		logger.Debug(fmt.Sprintf("skip column:%s.%s.%s, type: %s", col.SchemaName, col.TableName, col.ColumnName, col.DataType))
	}
}
Beispiel #2
0
func Main() {
	ShowMenu()
	for {
		key := mcore.ReadNotBlankLineWithMsg(mmsg.LocaleMessage("msg-input-which-run"))
		if mcore.String(key).TrimSpace().IsIn("q", "quit", "exit") {
			return
		}
		if mcore.String(key).TrimSpace().IsIn("m", "menu") {
			ShowMenu()
			continue
		}

		if mcore.String(key).TrimSpace().IsIn("m", "all") {
			RunAll()
			continue
		}

		if mcore.String(key).TrimSpace().IsIn("h", "help") {
			ShowHelp()
			continue
		}

		RunKey(key)
		RunFuncWithKey(key)
		RunMethod(key)
	}
}
Beispiel #3
0
// IsExistColumn checks table column exists.
func (s Sql) IsExistColumn(table, column string) (r bool) {
	columns, err := s.GetTableColumns(table)
	if err != nil {
		return false
	}
	return mcore.String(column).IsInArrayIgnoreCase(columns)
}
Beispiel #4
0
// AlterColumnCharDefault
func (s Sql) AlterColumnCharDefault(db, table, column string) {
	sc := mcore.String(column)
	c := s.GetMetaColumn(db, table, column)
	if sc.IsEqualIgnoreCase("id") {
		//skip id column
		return
	}
	if c.HasDefault() {
		// skip for has default column
		return
	}
	sct := mcore.String(c.DataType).ToLower()
	if !(sct.IsIn("char", "varchar", "text", "tinytext", "mediumtext", "longtext")) {
		//skip not char or varchar
		return
	}
	q := fmt.Sprintf("update %s	set %s = '' where %s is null", table, column, column)
	s.Exec(q)

	q = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s %s NOT NULL DEFAULT ''", table, column, c.ColumnType)
	s.Exec(q)
}
Beispiel #5
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()
}
Beispiel #6
0
// alter table column to decimal
func (s Sql) AlterColumnToDecimal(db, table, column, oldType string) error {
	dt, err := s.GetColumnDataType(db, table, column)
	if logger.CheckError(err) {
		return err
	}

	// data type is equal, dont need to udpate.
	if !mcore.String(dt).IsEqualIgnoreCase(oldType) {
		return nil
	}

	q := fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s decimal(19,2) NOT NULL DEFAULT 0.00", table, column)
	//dataType := "decimal(19,2) default 0"
	//s.AlterColumnDataType(table, column, dataType)
	_, err2 := s.Exec(q)
	return err2
}
Beispiel #7
0
func Search(path string, exts string, recursive bool, skipDirs, skipFiles, content string) {
	files := mcore.GetSubFiles(path, recursive, exts, skipDirs, skipFiles)
	for _, item := range files {
		text, err := mcore.ReadFileAll(item)
		if nil != err {
			continue
		}

		if !strings.Contains(text, content) {
			if verbose {
				fmt.Printf("File: %s not found matches\n", item)
			}
			continue
		} else {
			nums := strings.Count(text, content)
			fmt.Printf("File: %s found %d matches.\n", item, nums)
		}

		//found
		data, err := mcore.ReadFileLines(item)

		if err != nil {
			fmt.Println(err)
			continue
		}

		for lineNum, line := range data {
			if strings.Contains(line, content) {
				fmt.Printf("%d ", lineNum+1)
				lineA := mcore.String(line).Split(content)
				for i, v := range lineA {
					fmt.Printf(v)
					if i != len(lineA)-1 {
						mcon.PrintGreen(content)
					}
				}
				fmt.Println()
			}
		}
	}
}
Beispiel #8
0
// DemoExcelTime demo
func DemoExcelTime(days string) {
	t := mcore.String(days).ToExcelTime()
	mcore.PrintTime(t)
}
Beispiel #9
0
func (c MetaColumn) IsPrimaryKey() (r bool) {
	r = mcore.String(c.ColumnKey).IsEqualIgnoreCase("pri")
	return
}
Beispiel #10
0
func (c MetaColumn) IsNullable() (r bool) {
	r = mcore.String(c.Nullable).IsEqualIgnoreCase("yes")
	return
}
Beispiel #11
0
// IsHasColumn checks columnName exists
func (t *BaseTable) IsHasColumn(columnName string) bool {
	return mcore.String(columnName).IsInArrayIgnoreCase(t.GetColNames())
}