github.com/team-ide/go-dialect@v1.9.20/dialect/dialect_mapping_base.go (about)

     1  package dialect
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  func (this_ *mappingDialect) OwnerNamePack(param *ParamModel, ownerName string) string {
    12  	char := this_.OwnerNamePackChar
    13  	if param != nil {
    14  		if param.OwnerNamePack != nil && !*param.OwnerNamePack {
    15  			char = ""
    16  		} else if param.OwnerNamePackChar != nil {
    17  			char = *param.OwnerNamePackChar
    18  		}
    19  	}
    20  	return packingName(char, ownerName)
    21  }
    22  
    23  func (this_ *mappingDialect) TableNamePack(param *ParamModel, tableName string) string {
    24  	char := this_.TableNamePackChar
    25  	if param != nil {
    26  		if param.TableNamePack != nil && !*param.TableNamePack {
    27  			char = ""
    28  		} else if param.TableNamePackChar != nil {
    29  			char = *param.TableNamePackChar
    30  		}
    31  	}
    32  	return packingName(char, tableName)
    33  }
    34  
    35  func (this_ *mappingDialect) OwnerTablePack(param *ParamModel, ownerName string, tableName string) string {
    36  	if this_.SqlMapping.OwnerTablePack != nil {
    37  		return this_.SqlMapping.OwnerTablePack(param, ownerName, tableName)
    38  	}
    39  	var res string
    40  	if ownerName != "" {
    41  		res += this_.OwnerNamePack(param, ownerName) + "."
    42  	}
    43  	if tableName != "" {
    44  		res += this_.TableNamePack(param, tableName)
    45  	}
    46  	return res
    47  }
    48  
    49  func (this_ *mappingDialect) ColumnNamePack(param *ParamModel, columnName string) string {
    50  	char := this_.ColumnNamePackChar
    51  	if param != nil {
    52  		if param.ColumnNamePack != nil && !*param.ColumnNamePack {
    53  			char = ""
    54  		} else if param.ColumnNamePackChar != nil {
    55  			char = *param.ColumnNamePackChar
    56  		}
    57  	}
    58  	return packingName(char, columnName)
    59  }
    60  
    61  func (this_ *mappingDialect) ColumnNamesPack(param *ParamModel, columnNames []string) string {
    62  	char := this_.ColumnNamePackChar
    63  	if param != nil {
    64  		if param.ColumnNamePack != nil && !*param.ColumnNamePack {
    65  			char = ""
    66  		} else if param.ColumnNamePackChar != nil {
    67  			char = *param.ColumnNamePackChar
    68  		}
    69  	}
    70  	return packingNames(char, columnNames)
    71  }
    72  
    73  func (this_ *mappingDialect) SqlValuePack(param *ParamModel, column *ColumnModel, value interface{}) string {
    74  	char := this_.SqlValuePackChar
    75  	escapeChar := this_.SqlValueEscapeChar
    76  	if param != nil {
    77  		if param.SqlValuePackChar != nil {
    78  			char = *param.SqlValuePackChar
    79  		}
    80  		if param.SqlValueEscapeChar != nil {
    81  			escapeChar = *param.SqlValueEscapeChar
    82  		}
    83  	}
    84  	var columnTypeInfo *ColumnTypeInfo
    85  	if column != nil {
    86  		columnTypeInfo, _ = this_.GetColumnTypeInfo(column)
    87  	}
    88  	return packingValue(column, columnTypeInfo, char, escapeChar, value)
    89  }
    90  
    91  func (this_ *mappingDialect) ColumnDefaultPack(param *ParamModel, column *ColumnModel) (columnDefaultPack string, err error) {
    92  	var columnTypeInfo *ColumnTypeInfo
    93  	if column != nil {
    94  		columnTypeInfo, err = this_.GetColumnTypeInfo(column)
    95  		if err != nil {
    96  			bs, _ := json.Marshal(column)
    97  			err = errors.New("ColumnDefaultPack error column:" + string(bs) + ",error:" + err.Error())
    98  		}
    99  	}
   100  	if columnTypeInfo != nil && columnTypeInfo.ColumnDefaultPack != nil {
   101  		columnDefaultPack, err = columnTypeInfo.ColumnDefaultPack(param, column)
   102  		return
   103  	}
   104  	if column.ColumnDefault == "" {
   105  		return
   106  	}
   107  	var columnDefault = column.ColumnDefault
   108  	if strings.HasPrefix(columnDefault, "'") && strings.HasSuffix(columnDefault, "'") {
   109  		columnDefault = strings.TrimPrefix(columnDefault, "'")
   110  		columnDefault = strings.TrimSuffix(columnDefault, "'")
   111  		columnDefaultPack = this_.SqlValuePack(param, column, columnDefault)
   112  	} else if strings.HasPrefix(columnDefault, "\"") && strings.HasSuffix(columnDefault, "\"") {
   113  		columnDefault = strings.TrimPrefix(columnDefault, "\"")
   114  		columnDefault = strings.TrimSuffix(columnDefault, "\"")
   115  		columnDefaultPack = this_.SqlValuePack(param, column, columnDefault)
   116  	} else {
   117  		if this_.dialectType != TypeMysql {
   118  			if strings.HasPrefix(columnDefault, "b'") && strings.HasSuffix(columnDefault, "'") {
   119  				columnDefault = strings.TrimPrefix(columnDefault, "b'")
   120  				columnDefault = strings.TrimSuffix(columnDefault, "'")
   121  			}
   122  		}
   123  		if IsNumber(columnDefault) {
   124  			columnDefaultPack = columnDefault
   125  		} else {
   126  			if strings.Contains(columnDefault, "(") {
   127  				columnDefaultPack = columnDefault
   128  			} else {
   129  				columnDefaultPack = this_.SqlValuePack(param, column, columnDefault)
   130  			}
   131  		}
   132  	}
   133  	return
   134  }
   135  func IsNumber(str string) bool {
   136  	_, err := strconv.Atoi(str)
   137  	return err == nil
   138  }
   139  func (this_ *mappingDialect) IsSqlEnd(sqlStr string) (isSqlEnd bool) {
   140  	if !strings.HasSuffix(strings.TrimSpace(sqlStr), ";") {
   141  		return
   142  	}
   143  	cacheKey := UUID()
   144  	sqlCache := sqlStr
   145  	sqlCache = strings.ReplaceAll(sqlCache, `''`, `|-`+cacheKey+`-|`)
   146  	sqlCache = strings.ReplaceAll(sqlCache, `""`, `|--`+cacheKey+`--|`)
   147  
   148  	var inStringLevel int
   149  	var inStringPack byte
   150  	var thisChar byte
   151  	var lastChar byte
   152  
   153  	var stringPackChars = []byte{'"', '\''}
   154  	for i := 0; i < len(sqlCache); i++ {
   155  		thisChar = sqlCache[i]
   156  		if i > 0 {
   157  			lastChar = sqlCache[i-1]
   158  		}
   159  
   160  		// inStringLevel == 0 表示 不在 字符串 包装 中
   161  		if thisChar == ';' && inStringLevel == 0 {
   162  		} else {
   163  			packCharIndex := BytesIndex(stringPackChars, thisChar)
   164  			if packCharIndex >= 0 {
   165  				// inStringLevel == 0 表示 不在 字符串 包装 中
   166  				if inStringLevel == 0 {
   167  					inStringPack = stringPackChars[packCharIndex]
   168  					// 字符串包装层级 +1
   169  					inStringLevel++
   170  				} else {
   171  					if thisChar != inStringPack {
   172  					} else if lastChar == '\\' { // 如果有转义符号 类似 “\'”,“\"”
   173  					} else if lastChar == inStringPack {
   174  						// 如果 前一个字符 与字符串包装字符一致
   175  					} else {
   176  						// 字符串包装层级 -1
   177  						inStringLevel--
   178  					}
   179  				}
   180  			}
   181  		}
   182  
   183  	}
   184  	isSqlEnd = inStringLevel == 0
   185  	return
   186  }
   187  func (this_ *mappingDialect) SqlSplit(sqlStr string) (sqlList []string) {
   188  	cacheKey := UUID()
   189  	sqlCache := sqlStr
   190  	sqlCache = strings.ReplaceAll(sqlCache, `''`, `|-`+cacheKey+`-|`)
   191  	sqlCache = strings.ReplaceAll(sqlCache, `""`, `|--`+cacheKey+`--|`)
   192  
   193  	var list []string
   194  	var beg int
   195  
   196  	var inStringLevel int
   197  	var inStringPack byte
   198  	var thisChar byte
   199  	var lastChar byte
   200  
   201  	var stringPackChars = []byte{'"', '\''}
   202  	for i := 0; i < len(sqlCache); i++ {
   203  		thisChar = sqlCache[i]
   204  		if i > 0 {
   205  			lastChar = sqlCache[i-1]
   206  		}
   207  
   208  		// inStringLevel == 0 表示 不在 字符串 包装 中
   209  		if thisChar == ';' && inStringLevel == 0 {
   210  			if i > 0 {
   211  				list = append(list, sqlCache[beg:i])
   212  			}
   213  			beg = i + 1
   214  		} else {
   215  			packCharIndex := BytesIndex(stringPackChars, thisChar)
   216  			if packCharIndex >= 0 {
   217  				// inStringLevel == 0 表示 不在 字符串 包装 中
   218  				if inStringLevel == 0 {
   219  					inStringPack = stringPackChars[packCharIndex]
   220  					// 字符串包装层级 +1
   221  					inStringLevel++
   222  				} else {
   223  					if thisChar != inStringPack {
   224  					} else if lastChar == '\\' { // 如果有转义符号 类似 “\'”,“\"”
   225  					} else if lastChar == inStringPack {
   226  						// 如果 前一个字符 与字符串包装字符一致
   227  					} else {
   228  						// 字符串包装层级 -1
   229  						inStringLevel--
   230  					}
   231  				}
   232  			}
   233  		}
   234  
   235  	}
   236  	list = append(list, sqlCache[beg:])
   237  	for _, sqlOne := range list {
   238  		sqlOne = strings.TrimSpace(sqlOne)
   239  		if sqlOne == "" {
   240  			continue
   241  		}
   242  		sqlOne = strings.ReplaceAll(sqlOne, `|-`+cacheKey+`-|`, `''`)
   243  		sqlOne = strings.ReplaceAll(sqlOne, `|--`+cacheKey+`--|`, `""`)
   244  		sqlList = append(sqlList, sqlOne)
   245  	}
   246  	return
   247  }
   248  
   249  func (this_ *mappingDialect) InsertSql(param *ParamModel, insert *InsertModel) (sqlList []string, err error) {
   250  
   251  	sql := "INSERT INTO "
   252  	sql += this_.OwnerTablePack(param, insert.OwnerName, insert.TableName)
   253  
   254  	sql += "(" + this_.ColumnNamesPack(param, insert.Columns) + ")"
   255  	sql += ` VALUES `
   256  
   257  	for rowIndex, row := range insert.Rows {
   258  		if rowIndex > 0 {
   259  			sql += `, `
   260  		}
   261  		sql += `( `
   262  
   263  		for valueIndex, value := range row {
   264  			if valueIndex > 0 {
   265  				sql += `, `
   266  			}
   267  			switch value.Type {
   268  			case ValueTypeString:
   269  				sql += this_.SqlValuePack(param, nil, value.Value)
   270  				break
   271  			case ValueTypeNumber:
   272  				sql += value.Value
   273  				break
   274  			case ValueTypeFunc:
   275  
   276  				var funcStr = value.Value
   277  				//funcStr, err = this_.FormatFunc(funcStr)
   278  				//if err != nil {
   279  				//	return
   280  				//}
   281  				sql += funcStr
   282  				break
   283  			}
   284  		}
   285  
   286  		sql += `) `
   287  	}
   288  
   289  	sqlList = append(sqlList, sql)
   290  	return
   291  }
   292  func (this_ *mappingDialect) InsertDataListSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}) (sqlList []string, batchSqlList []string, err error) {
   293  	var batchSqlCache = make(map[string]string)
   294  	var batchSqlIndexCache = make(map[string]int)
   295  	var columnNames []string
   296  	for _, one := range columnList {
   297  		columnNames = append(columnNames, one.ColumnName)
   298  	}
   299  	for _, data := range dataList {
   300  		var columnList_ []string
   301  		var values = "("
   302  		for _, column := range columnList {
   303  			str := this_.SqlValuePack(param, column, data[column.ColumnName])
   304  			if strings.EqualFold(str, "null") {
   305  				continue
   306  			}
   307  			columnList_ = append(columnList_, column.ColumnName)
   308  			values += str + ", "
   309  		}
   310  		values = strings.TrimSuffix(values, ", ")
   311  		values += ")"
   312  
   313  		insertSqlInfo := "INSERT INTO "
   314  		insertSqlInfo += this_.OwnerTablePack(param, ownerName, tableName)
   315  		insertSqlInfo += " ("
   316  		insertSqlInfo += this_.ColumnNamesPack(param, columnList_)
   317  		insertSqlInfo += ") VALUES "
   318  
   319  		sqlList = append(sqlList, insertSqlInfo+values)
   320  
   321  		key := strings.Join(columnList_, ",")
   322  		find, ok := batchSqlCache[key]
   323  		if ok {
   324  			find += ",\n" + values
   325  			batchSqlCache[key] = find
   326  			batchSqlList[batchSqlIndexCache[key]] = find
   327  		} else {
   328  			find = insertSqlInfo + "\n" + values
   329  			batchSqlIndexCache[key] = len(batchSqlCache)
   330  			batchSqlCache[key] = find
   331  			batchSqlList = append(batchSqlList, find)
   332  		}
   333  	}
   334  	return
   335  }
   336  func (this_ *mappingDialect) PackPageSql(selectSql string, pageSize int, pageNo int) (pageSql string) {
   337  	if this_.SqlMapping.PackPageSql != nil {
   338  		return this_.SqlMapping.PackPageSql(selectSql, pageSize, pageNo)
   339  	}
   340  	pageSql = selectSql + fmt.Sprintf(" LIMIT %d,%d", pageSize*(pageNo-1), pageSize)
   341  	return
   342  }
   343  func (this_ *mappingDialect) ReplaceSqlVariable(sqlInfo string, args []interface{}) (variableSql string) {
   344  	if this_.SqlMapping.ReplaceSqlVariable != nil {
   345  		return this_.SqlMapping.ReplaceSqlVariable(sqlInfo, args)
   346  	}
   347  	variableSql = sqlInfo
   348  	return
   349  }