diff --git a/do.go b/do.go index 32e974c7..fe483927 100644 --- a/do.go +++ b/do.go @@ -536,7 +536,7 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) { m := &Model{data: query, do: s} var sqls []string for _, field := range m.columnsHasValue("other") { - sqls = append(sqls, fmt.Sprintf(" (%v = %v) ", field.DbName, s.addToVars(field.Value))) + sqls = append(sqls, fmt.Sprintf("(%v = %v)", field.DbName, s.addToVars(field.Value))) } return strings.Join(sqls, " AND ") } @@ -573,10 +573,10 @@ func (s *Do) buildNotCondition(clause map[string]interface{}) (str string) { return fmt.Sprintf("(%v <> %v)", s.model.primaryKeyDb(), id) } else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS) ").MatchString(value) { str = fmt.Sprintf(" NOT (%v) ", value) - not_equal_sql = fmt.Sprintf(" NOT (%v) ", value) + not_equal_sql = fmt.Sprintf("NOT (%v)", value) } else { - str = fmt.Sprintf(" (%v NOT IN (?)) ", value) - not_equal_sql = fmt.Sprintf(" (%v <> ?) ", value) + str = fmt.Sprintf("(%v NOT IN (?))", value) + not_equal_sql = fmt.Sprintf("(%v <> ?)", value) } case int, int64, int32: return fmt.Sprintf("(%v <> %v)", s.model.primaryKeyDb(), query) @@ -626,7 +626,7 @@ func (s *Do) whereSql() (sql string) { var primary_condiations, and_conditions, or_conditions []string if !s.unscoped && s.model.hasColumn("DeletedAt") { - primary_condiations = append(primary_condiations, "(deleted_at is null or deleted_at <= '0001-01-02')") + primary_condiations = append(primary_condiations, "(deleted_at IS NULL OR deleted_at <= '0001-01-02')") } if !s.model.primaryKeyZero() { diff --git a/logger.go b/logger.go index 2caf9a5c..407738dd 100644 --- a/logger.go +++ b/logger.go @@ -5,6 +5,7 @@ import ( "log" "os" "regexp" + "time" ) @@ -12,14 +13,44 @@ type Logger interface { Print(v ...interface{}) } +type defaultLogger struct { + *log.Logger +} + +func (logger defaultLogger) Print(v ...interface{}) { + if len(v) > 1 { + level := v[0] + tim := "\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m" + + if level == "sql" { + dur := v[1] + sql := fmt.Sprintf(regexp.MustCompile(`(\$\d+)|\?`).ReplaceAllString(v[2].(string), "'%v'"), v[3].([]interface{})...) + dur = fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(dur.(time.Duration).Nanoseconds()/1e4)/100.0) + logger.Println(tim, dur, sql) + } else { + messages := []interface{}{"\033[31m"} + messages = append(messages, v...) + messages = append(messages, "\033[0m") + logger.Println(messages...) + } + } +} + +var default_logger defaultLogger + +func init() { + // default_logger = log.New(os.Stdout, "\r\n", 0) + default_logger = defaultLogger{log.New(os.Stdout, "\r\n", 0)} +} + func (s *Chain) print(level string, v ...interface{}) { if s.d.log_mode || s.debug_mode || level == "debug" { if _, ok := s.d.logger.(Logger); !ok { fmt.Println("logger haven't been set, using os.Stdout") - s.d.logger = log.New(os.Stdout, "", 0) + s.d.logger = default_logger } args := []interface{}{level} - s.d.logger.(Logger).Print(append(args, v...)) + s.d.logger.(Logger).Print(append(args, v...)...) } } @@ -28,7 +59,7 @@ func (s *Chain) warn(v ...interface{}) { } func (s *Chain) slog(sql string, t time.Time, vars ...interface{}) { - go s.print("sql", time.Now().Sub(t), fmt.Sprintf(regexp.MustCompile(`\$\d|\?`).ReplaceAllString(sql, "'%v'"), vars...)) + go s.print("sql", time.Now().Sub(t), sql, vars) } func (s *Chain) debug(v ...interface{}) {