<commit_before>// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.

package gdb

import (
    "bytes"
    "errors"
    "fmt"
    "gitee.com/johng/gf/g/os/glog"
    "gitee.com/johng/gf/g/os/gtime"
    "gitee.com/johng/gf/g/util/gconv"
    "gitee.com/johng/gf/g/util/gregex"
    "gitee.com/johng/gf/g/util/gstr"
    _ "gitee.com/johng/gf/third/github.com/go-sql-driver/mysql"
    "reflect"
    "strings"
)

// 格式化SQL查询条件
func formatCondition(where interface{}, args []interface{}) (string, []interface{}) {
    // 条件字符串处理
    buffer := bytes.NewBuffer(nil)
    if reflect.ValueOf(where).Kind() == reflect.Map {
        ks := reflect.ValueOf(where).MapKeys()
        vs := reflect.ValueOf(where)
        for _, k := range ks {
            key   := gconv.String(k.Interface())
            value := gconv.String(vs.MapIndex(k).Interface())
            if buffer.Len() > 0 {
                buffer.WriteString(" AND ")
            }
            if gstr.IsNumeric(value) || value == "?" {
                buffer.WriteString(key + "=" + value)
            } else {
                buffer.WriteString(key + "='" + value + "'")
            }
        }
    } else {
        buffer.Write(gconv.Bytes(where))
    }
    if buffer.Len() == 0 {
        buffer.WriteString("1")
    }<commit_msg>!17 修复where为空时不是1=1的bug Merge pull request !17 from 张金富/master<commit_after>// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.

package gdb

import (
    "bytes"
    "errors"
    "fmt"
    "gitee.com/johng/gf/g/os/glog"
    "gitee.com/johng/gf/g/os/gtime"
    "gitee.com/johng/gf/g/util/gconv"
    "gitee.com/johng/gf/g/util/gregex"
    "gitee.com/johng/gf/g/util/gstr"
    _ "gitee.com/johng/gf/third/github.com/go-sql-driver/mysql"
    "reflect"
    "strings"
)

// 格式化SQL查询条件
func formatCondition(where interface{}, args []interface{}) (string, []interface{}) {
    // 条件字符串处理
    buffer := bytes.NewBuffer(nil)
    if reflect.ValueOf(where).Kind() == reflect.Map {
        ks := reflect.ValueOf(where).MapKeys()
        vs := reflect.ValueOf(where)
        for _, k := range ks {
            key   := gconv.String(k.Interface())
            value := gconv.String(vs.MapIndex(k).Interface())
            if buffer.Len() > 0 {
                buffer.WriteString(" AND ")
            }
            if gstr.IsNumeric(value) || value == "?" {
                buffer.WriteString(key + "=" + value)
            } else {
                buffer.WriteString(key + "='" + value + "'")
            }
        }
    } else {
        buffer.Write(gconv.Bytes(where))
    }
    if buffer.Len() == 0 {
        buffer.WriteString("1=1")
    }
{'fraction_non_alphanumeric': 0.15761763738071735, 'fraction_numerical': 0.009542612701546561, 'mean_word_length': 2.0738119312436805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4595020', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1095, 'n_words': 203}