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

     1  package dialect
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  )
     7  
     8  func (this_ *mappingDialect) AppendSqlValue(param *ParamModel, sqlInfo *string, column *ColumnModel, value interface{}, args *[]interface{}) {
     9  	if param != nil && param.AppendSqlValue != nil && *param.AppendSqlValue {
    10  		*sqlInfo += this_.SqlValuePack(param, column, value)
    11  	} else {
    12  		*sqlInfo += "?"
    13  		*args = append(*args, value)
    14  	}
    15  }
    16  
    17  func (this_ *mappingDialect) DataListInsertSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, batchSqlList []string, batchValuesList [][]interface{}, err error) {
    18  	if len(dataList) == 0 {
    19  		return
    20  	}
    21  
    22  	var batchIndexCache = make(map[string]int)
    23  
    24  	var columnCache = map[string]*ColumnModel{}
    25  	for _, column := range columnList {
    26  		columnCache[column.ColumnName] = column
    27  	}
    28  
    29  	for _, data := range dataList {
    30  
    31  		var values []interface{}
    32  		insertColumns := ""
    33  		insertValues := ""
    34  		for _, column := range columnList {
    35  			name := column.ColumnName
    36  			value, ok := data[name]
    37  			if !ok {
    38  				continue
    39  			}
    40  			insertColumns += this_.ColumnNamePack(param, name) + ", "
    41  			this_.AppendSqlValue(param, &insertValues, column, value, &values)
    42  			insertValues += ", "
    43  		}
    44  		insertColumns = strings.TrimSuffix(insertColumns, ", ")
    45  		insertValues = strings.TrimSuffix(insertValues, ", ")
    46  
    47  		var insertSql = "INSERT INTO "
    48  		insertSql += this_.OwnerTablePack(param, ownerName, tableName)
    49  		if insertColumns != "" {
    50  			insertSql += "(" + insertColumns + ")"
    51  		}
    52  		if insertValues != "" {
    53  			insertSql += " VALUES (" + insertValues + ")"
    54  		}
    55  
    56  		sqlList = append(sqlList, this_.ReplaceSqlVariable(insertSql, values))
    57  		valuesList = append(valuesList, values)
    58  
    59  		// 批量 插入 SQL
    60  
    61  		if this_.dialectType == TypeOracle {
    62  			batchSqlList = append(batchSqlList, insertSql)
    63  			batchValuesList = append(batchValuesList, values)
    64  		} else {
    65  			index, ok := batchIndexCache[insertColumns]
    66  			if ok {
    67  				batchSqlList[index] += ",\n(" + insertValues + ")"
    68  				batchValuesList[index] = append(batchValuesList[index], values...)
    69  			} else {
    70  				var batchSql = "INSERT INTO "
    71  				batchSql += this_.OwnerTablePack(param, ownerName, tableName)
    72  				if insertColumns != "" {
    73  					batchSql += "(" + insertColumns + ")"
    74  				}
    75  				if insertValues != "" {
    76  					batchSql += " VALUES (" + insertValues + ")"
    77  				}
    78  				index = len(batchSqlList)
    79  				batchIndexCache[insertColumns] = index
    80  				batchSqlList = append(batchSqlList, batchSql)
    81  				batchValuesList = append(batchValuesList, values)
    82  			}
    83  		}
    84  
    85  	}
    86  	for index := range batchSqlList {
    87  		batchSqlList[index] = this_.ReplaceSqlVariable(batchSqlList[index], batchValuesList[index])
    88  	}
    89  	return
    90  }
    91  
    92  func (this_ *mappingDialect) DataListUpdateSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}, dataWhereList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, err error) {
    93  	if len(dataList) == 0 {
    94  		return
    95  	}
    96  	if len(dataList) != len(dataWhereList) {
    97  		err = errors.New("更新数据与更新条件数量不一致")
    98  		return
    99  	}
   100  	var columnCache = map[string]*ColumnModel{}
   101  	for _, column := range columnList {
   102  		columnCache[column.ColumnName] = column
   103  	}
   104  
   105  	for index, data := range dataList {
   106  		dataWhere := dataWhereList[index]
   107  		if len(dataWhere) == 0 {
   108  			err = errors.New("更新数据条件丢失")
   109  			return
   110  		}
   111  
   112  		var updateSql = "UPDATE "
   113  		var values []interface{}
   114  
   115  		updateSql += this_.OwnerTablePack(param, ownerName, tableName)
   116  		updateSql += " SET "
   117  
   118  		for name, value := range data {
   119  			column := columnCache[name]
   120  			updateSql += "" + this_.ColumnNamePack(param, name) + "="
   121  			this_.AppendSqlValue(param, &updateSql, column, value, &values)
   122  			updateSql += ", "
   123  		}
   124  		updateSql = strings.TrimSuffix(updateSql, ", ")
   125  
   126  		updateSql += " WHERE "
   127  		for name, value := range dataWhere {
   128  			column := columnCache[name]
   129  			updateSql += "" + this_.ColumnNamePack(param, name) + "="
   130  			this_.AppendSqlValue(param, &updateSql, column, value, &values)
   131  			updateSql += " AND "
   132  		}
   133  		updateSql = strings.TrimSuffix(updateSql, " AND ")
   134  
   135  		updateSql = this_.ReplaceSqlVariable(updateSql, values)
   136  		sqlList = append(sqlList, updateSql)
   137  		valuesList = append(valuesList, values)
   138  	}
   139  	return
   140  }
   141  func (this_ *mappingDialect) DataListDeleteSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataWhereList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, err error) {
   142  	if len(dataWhereList) == 0 {
   143  		return
   144  	}
   145  	var columnCache = map[string]*ColumnModel{}
   146  	for _, column := range columnList {
   147  		columnCache[column.ColumnName] = column
   148  	}
   149  
   150  	for _, dataWhere := range dataWhereList {
   151  		if len(dataWhere) == 0 {
   152  			err = errors.New("更新数据条件丢失")
   153  			return
   154  		}
   155  
   156  		var deleteSql = "DELETE FROM "
   157  		var values []interface{}
   158  
   159  		deleteSql += this_.OwnerTablePack(param, ownerName, tableName)
   160  
   161  		deleteSql += " WHERE "
   162  
   163  		for name, value := range dataWhere {
   164  			column := columnCache[name]
   165  			deleteSql += "" + this_.ColumnNamePack(param, name) + "="
   166  			this_.AppendSqlValue(param, &deleteSql, column, value, &values)
   167  			deleteSql += " AND "
   168  		}
   169  		deleteSql = strings.TrimSuffix(deleteSql, " AND ")
   170  
   171  		deleteSql = this_.ReplaceSqlVariable(deleteSql, values)
   172  		sqlList = append(sqlList, deleteSql)
   173  		valuesList = append(valuesList, values)
   174  	}
   175  	return
   176  }
   177  
   178  type Where struct {
   179  	Name                    string `json:"name"`
   180  	Value                   string `json:"value"`
   181  	Before                  string `json:"before"`
   182  	After                   string `json:"after"`
   183  	CustomSql               string `json:"customSql"`
   184  	SqlConditionalOperation string `json:"sqlConditionalOperation"`
   185  	AndOr                   string `json:"andOr"`
   186  }
   187  
   188  type Order struct {
   189  	Name    string `json:"name"`
   190  	AscDesc string `json:"ascDesc"`
   191  }
   192  
   193  func (this_ *mappingDialect) DataListSelectSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, whereList []*Where, orderList []*Order) (sql string, values []interface{}, err error) {
   194  	selectColumns := ""
   195  	var columnCache = map[string]*ColumnModel{}
   196  	for _, column := range columnList {
   197  		selectColumns += this_.ColumnNamePack(param, column.ColumnName) + ","
   198  		columnCache[column.ColumnName] = column
   199  	}
   200  	selectColumns = strings.TrimSuffix(selectColumns, ",")
   201  	if selectColumns == "" {
   202  		selectColumns = "*"
   203  	}
   204  	sql = "SELECT " + selectColumns + " FROM "
   205  
   206  	sql += this_.OwnerTablePack(param, ownerName, tableName)
   207  
   208  	//构造查询用的finder
   209  	if len(whereList) > 0 {
   210  		sql += " WHERE"
   211  		for index, where := range whereList {
   212  			column := columnCache[where.Name]
   213  			sql += " " + this_.ColumnNamePack(param, where.Name)
   214  			value := where.Value
   215  			switch where.SqlConditionalOperation {
   216  			case "like":
   217  				sql += " LIKE "
   218  				value = "%" + value + "%"
   219  				this_.AppendSqlValue(param, &sql, column, value, &values)
   220  			case "not like":
   221  				sql += " NOT LIKE "
   222  				value = "%" + value + "%"
   223  				this_.AppendSqlValue(param, &sql, column, value, &values)
   224  			case "like start":
   225  				sql += " LIKE "
   226  				value = "" + value + "%"
   227  				this_.AppendSqlValue(param, &sql, column, value, &values)
   228  			case "not like start":
   229  				sql += " NOT LIKE "
   230  				value = "" + value + "%"
   231  				this_.AppendSqlValue(param, &sql, column, value, &values)
   232  			case "like end":
   233  				sql += " LIKE "
   234  				value = "%" + value + ""
   235  				this_.AppendSqlValue(param, &sql, column, value, &values)
   236  			case "not like end":
   237  				sql += " NOT LIKE "
   238  				value = "%" + value + ""
   239  				this_.AppendSqlValue(param, &sql, column, value, &values)
   240  			case "is null":
   241  				sql += " IS NULL"
   242  			case "is not null":
   243  				sql += " IS NOT NULL"
   244  			case "is empty":
   245  				sql += " = "
   246  				value = ""
   247  				this_.AppendSqlValue(param, &sql, column, value, &values)
   248  			case "is not empty":
   249  				sql += " <> "
   250  				this_.AppendSqlValue(param, &sql, column, value, &values)
   251  			case "between":
   252  				sql += " BETWEEN "
   253  				this_.AppendSqlValue(param, &sql, column, where.Before, &values)
   254  				sql += " AND "
   255  				this_.AppendSqlValue(param, &sql, column, where.After, &values)
   256  			case "not between":
   257  				sql += " NOT BETWEEN "
   258  				this_.AppendSqlValue(param, &sql, column, where.Before, &values)
   259  				sql += " AND "
   260  				this_.AppendSqlValue(param, &sql, column, where.After, &values)
   261  			case "in":
   262  				sql += " IN ("
   263  				this_.AppendSqlValue(param, &sql, column, value, &values)
   264  				sql += ")"
   265  			case "not in":
   266  				sql += " NOT IN ("
   267  				this_.AppendSqlValue(param, &sql, column, value, &values)
   268  				sql += ")"
   269  			default:
   270  				sql += " " + where.SqlConditionalOperation + " "
   271  				this_.AppendSqlValue(param, &sql, column, value, &values)
   272  			}
   273  			// params_ = append(params_, where.Value)
   274  			if index < len(whereList)-1 {
   275  				sql += " " + where.AndOr + " "
   276  			}
   277  		}
   278  	}
   279  	if len(orderList) > 0 {
   280  		sql += " ORDER BY"
   281  		for index, order := range orderList {
   282  			sql += " " + this_.ColumnNamePack(param, order.Name)
   283  			if order.AscDesc != "" {
   284  				sql += " " + order.AscDesc
   285  			}
   286  			// params_ = append(params_, where.Value)
   287  			if index < len(orderList)-1 {
   288  				sql += ","
   289  			}
   290  		}
   291  
   292  	}
   293  	sql = this_.ReplaceSqlVariable(sql, values)
   294  	return
   295  }