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

     1  package dialect
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  func (this_ *mappingDialect) OwnersSelectSql(param *ParamModel) (sqlInfo string, err error) {
    11  	sqlList, err := this_.FormatSql(this_.OwnersSelect, param)
    12  	if err != nil {
    13  		return
    14  	}
    15  	if len(sqlList) > 0 {
    16  		sqlInfo = sqlList[0]
    17  	}
    18  	return
    19  }
    20  
    21  func (this_ *mappingDialect) OwnerSelectSql(param *ParamModel, ownerName string) (sqlInfo string, err error) {
    22  	sqlList, err := this_.FormatSql(this_.OwnerSelect, param,
    23  		map[string]string{
    24  			"ownerName": ownerName,
    25  		},
    26  	)
    27  	if err != nil {
    28  		return
    29  	}
    30  	if len(sqlList) > 0 {
    31  		sqlInfo = sqlList[0]
    32  	}
    33  	return
    34  }
    35  
    36  func (this_ *mappingDialect) OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error) {
    37  	if data == nil {
    38  		return
    39  	}
    40  	owner = &OwnerModel{}
    41  	bs, err := json.Marshal(data)
    42  	if err != nil {
    43  		return
    44  	}
    45  	err = json.Unmarshal(bs, owner)
    46  	if err != nil {
    47  		return
    48  	}
    49  	return
    50  }
    51  
    52  func (this_ *mappingDialect) OwnerCreateSql(param *ParamModel, owner *OwnerModel) (sqlList []string, err error) {
    53  	sqlList, err = this_.FormatSql(this_.OwnerCreate, param, owner)
    54  	if err != nil {
    55  		return
    56  	}
    57  	return
    58  }
    59  
    60  func (this_ *mappingDialect) OwnerDeleteSql(param *ParamModel, ownerName string) (sqlList []string, err error) {
    61  	sqlList, err = this_.FormatSql(this_.OwnerDelete, param,
    62  		map[string]string{
    63  			"ownerName": ownerName,
    64  		},
    65  	)
    66  	if err != nil {
    67  		return
    68  	}
    69  	return
    70  }
    71  
    72  func (this_ *mappingDialect) TablesSelectSql(param *ParamModel, ownerName string) (sqlInfo string, err error) {
    73  	sqlList, err := this_.FormatSql(this_.TablesSelect, param,
    74  		map[string]string{
    75  			"ownerName": ownerName,
    76  		},
    77  	)
    78  	if err != nil {
    79  		return
    80  	}
    81  	if len(sqlList) > 0 {
    82  		sqlInfo = sqlList[0]
    83  	}
    84  	return
    85  }
    86  
    87  func (this_ *mappingDialect) TableSelectSql(param *ParamModel, ownerName string, tableName string) (sqlInfo string, err error) {
    88  	sqlList, err := this_.FormatSql(this_.TableSelect, param,
    89  		map[string]string{
    90  			"ownerName": ownerName,
    91  			"tableName": tableName,
    92  		},
    93  	)
    94  	if err != nil {
    95  		return
    96  	}
    97  	if len(sqlList) > 0 {
    98  		sqlInfo = sqlList[0]
    99  	}
   100  	return
   101  }
   102  
   103  func (this_ *mappingDialect) TableModel(data map[string]interface{}) (table *TableModel, err error) {
   104  	if data == nil {
   105  		return
   106  	}
   107  	table = &TableModel{}
   108  	bs, err := json.Marshal(data)
   109  	if err != nil {
   110  		return
   111  	}
   112  	err = json.Unmarshal(bs, table)
   113  	if err != nil {
   114  		return
   115  	}
   116  	return
   117  }
   118  
   119  func (this_ *mappingDialect) TableCreateSql(param *ParamModel, ownerName string, table *TableModel) (sqlList []string, err error) {
   120  	var tableCreateColumnContent string
   121  
   122  	var tableCreateColumnSql string
   123  	for _, column := range table.ColumnList {
   124  		if column.PrimaryKey {
   125  			if StringsIndex(table.PrimaryKeys, column.ColumnName) < 0 {
   126  				table.PrimaryKeys = append(table.PrimaryKeys, column.ColumnName)
   127  			}
   128  		}
   129  	}
   130  	for i, column := range table.ColumnList {
   131  
   132  		tableCreateColumnSql, err = this_.TableCreateColumnSql(param, column)
   133  		if err != nil {
   134  			return
   135  		}
   136  		tableCreateColumnContent += "    " + tableCreateColumnSql
   137  		if i < len(table.ColumnList)-1 {
   138  			tableCreateColumnContent += ",\n"
   139  		}
   140  	}
   141  	var tableCreatePrimaryKeyContent string
   142  	if len(table.PrimaryKeys) > 0 {
   143  		tableCreatePrimaryKeyContent, err = this_.TableCreatePrimaryKeySql(param, table.PrimaryKeys)
   144  		if len(strings.TrimSpace(tableCreatePrimaryKeyContent)) > 0 {
   145  			tableCreatePrimaryKeyContent = "    " + tableCreatePrimaryKeyContent
   146  			if len(strings.TrimSpace(tableCreateColumnContent)) > 0 {
   147  				tableCreateColumnContent += ","
   148  			}
   149  		}
   150  	}
   151  	if err != nil {
   152  		return
   153  	}
   154  	sqlList, err = this_.FormatSql(this_.TableCreate, param,
   155  		table,
   156  		map[string]string{
   157  			"ownerName":                    ownerName,
   158  			"tableCreateColumnContent":     tableCreateColumnContent,
   159  			"tableCreatePrimaryKeyContent": tableCreatePrimaryKeyContent,
   160  		},
   161  	)
   162  	if err != nil {
   163  		return
   164  	}
   165  
   166  	var sqlList_ []string
   167  
   168  	if table.TableComment != "" {
   169  		sqlList_, err = this_.TableCommentSql(param, ownerName, table.TableName, table.TableComment)
   170  		if err != nil {
   171  			return
   172  		}
   173  		sqlList = append(sqlList, sqlList_...)
   174  	}
   175  
   176  	if !this_.TableCreateColumnHasComment {
   177  		for _, column := range table.ColumnList {
   178  			if column.ColumnComment != "" {
   179  				sqlList_, err = this_.ColumnCommentSql(param, ownerName, table.TableName, column.ColumnName, column.ColumnComment)
   180  				if err != nil {
   181  					return
   182  				}
   183  				sqlList = append(sqlList, sqlList_...)
   184  			}
   185  		}
   186  	}
   187  
   188  	for _, index := range table.IndexList {
   189  		sqlList_, err = this_.IndexAddSql(param, ownerName, table.TableName, index)
   190  		if err != nil {
   191  			return
   192  		}
   193  		sqlList = append(sqlList, sqlList_...)
   194  	}
   195  	return
   196  }
   197  
   198  func (this_ *mappingDialect) TableCreateColumnSql(param *ParamModel, column *ColumnModel) (sqlInfo string, err error) {
   199  	columnTypePack, err := this_.ColumnTypePack(column)
   200  	if err != nil {
   201  		return
   202  	}
   203  	columnDefaultPack, err := this_.ColumnDefaultPack(param, column)
   204  	if err != nil {
   205  		return
   206  	}
   207  	sqlList, err := this_.FormatSql(this_.TableCreateColumn, param,
   208  		column,
   209  		map[string]string{
   210  			"columnTypePack":    columnTypePack,
   211  			"columnDefaultPack": columnDefaultPack,
   212  		},
   213  	)
   214  	if err != nil {
   215  		return
   216  	}
   217  	if len(sqlList) > 0 {
   218  		sqlInfo = sqlList[0]
   219  	}
   220  	return
   221  }
   222  
   223  func (this_ *mappingDialect) TableCreatePrimaryKeySql(param *ParamModel, primaryKeys []string) (sqlInfo string, err error) {
   224  	sqlList, err := this_.FormatSql(this_.TableCreatePrimaryKey, param,
   225  		map[string]interface{}{
   226  			"primaryKeys": primaryKeys,
   227  		},
   228  	)
   229  	if err != nil {
   230  		return
   231  	}
   232  	if len(sqlList) > 0 {
   233  		sqlInfo = sqlList[0]
   234  	}
   235  	return
   236  }
   237  func (this_ *mappingDialect) TableRenameSql(param *ParamModel, ownerName string, tableName string, newTableName string) (sqlList []string, err error) {
   238  	sqlList, err = this_.FormatSql(this_.TableRename, param,
   239  		map[string]string{
   240  			"ownerName":    ownerName,
   241  			"tableName":    tableName,
   242  			"newTableName": newTableName,
   243  		},
   244  	)
   245  	if err != nil {
   246  		return
   247  	}
   248  	return
   249  }
   250  
   251  func (this_ *mappingDialect) TableDeleteSql(param *ParamModel, ownerName string, tableName string) (sqlList []string, err error) {
   252  	sqlList, err = this_.FormatSql(this_.TableDelete, param,
   253  		map[string]string{
   254  			"ownerName": ownerName,
   255  			"tableName": tableName,
   256  		},
   257  	)
   258  	if err != nil {
   259  		return
   260  	}
   261  	return
   262  }
   263  
   264  func (this_ *mappingDialect) TableCommentSql(param *ParamModel, ownerName string, tableName string, tableComment string) (sqlList []string, err error) {
   265  	sqlList, err = this_.FormatSql(this_.TableComment, param,
   266  		map[string]string{
   267  			"ownerName":    ownerName,
   268  			"tableName":    tableName,
   269  			"tableComment": tableComment,
   270  		},
   271  	)
   272  	if err != nil {
   273  		return
   274  	}
   275  	return
   276  }
   277  
   278  func (this_ *mappingDialect) ColumnsSelectSql(param *ParamModel, ownerName string, tableName string) (sqlInfo string, err error) {
   279  	sqlList, err := this_.FormatSql(this_.ColumnsSelect, param,
   280  		map[string]string{
   281  			"ownerName": ownerName,
   282  			"tableName": tableName,
   283  		},
   284  	)
   285  	if err != nil {
   286  		return
   287  	}
   288  	if len(sqlList) > 0 {
   289  		sqlInfo = sqlList[0]
   290  	}
   291  	return
   292  }
   293  
   294  func (this_ *mappingDialect) ColumnSelectSql(param *ParamModel, ownerName string, tableName string, columnName string) (sqlInfo string, err error) {
   295  	sqlList, err := this_.FormatSql(this_.ColumnSelect, param,
   296  		map[string]string{
   297  			"ownerName":  ownerName,
   298  			"tableName":  tableName,
   299  			"columnName": columnName,
   300  		},
   301  	)
   302  	if err != nil {
   303  		return
   304  	}
   305  	if len(sqlList) > 0 {
   306  		sqlInfo = sqlList[0]
   307  	}
   308  	return
   309  }
   310  
   311  func (this_ *mappingDialect) ColumnModel(data map[string]interface{}) (column *ColumnModel, err error) {
   312  	if data == nil {
   313  		return
   314  	}
   315  	column = &ColumnModel{}
   316  	bs, err := json.Marshal(data)
   317  	if err != nil {
   318  		return
   319  	}
   320  	err = json.Unmarshal(bs, column)
   321  	if err != nil {
   322  		return
   323  	}
   324  
   325  	var isNullable string
   326  	if data["isNullable"] != nil {
   327  		isNullable = GetStringValue(data["isNullable"])
   328  	}
   329  	if data["ISNULLABLE"] != nil {
   330  		isNullable = GetStringValue(data["ISNULLABLE"])
   331  	}
   332  	if isNullable != "" {
   333  		if strings.EqualFold(isNullable, "no") || strings.EqualFold(isNullable, "n") {
   334  			column.ColumnNotNull = true
   335  		}
   336  	}
   337  
   338  	if GetStringValue(data["isNotNull"]) == "1" || GetStringValue(data["ISNOTNULL"]) == "1" {
   339  		column.ColumnNotNull = true
   340  	}
   341  	var columnType string
   342  	if data["columnType"] != nil {
   343  		columnType = data["columnType"].(string)
   344  	}
   345  	if data["COLUMNTYPE"] != nil {
   346  		columnType = data["COLUMNTYPE"].(string)
   347  	}
   348  	if column.ColumnDataType == "" {
   349  		if strings.Contains(columnType, "(") {
   350  			column.ColumnDataType = columnType[:strings.Index(columnType, "(")]
   351  		} else {
   352  			column.ColumnDataType = columnType
   353  		}
   354  	}
   355  	if strings.Contains(column.ColumnDataType, "(") {
   356  		column.ColumnDataType = column.ColumnDataType[:strings.Index(column.ColumnDataType, "(")]
   357  	}
   358  	dataLength := GetStringValue(data["DATA_LENGTH"])
   359  	if dataLength != "" && dataLength != "0" {
   360  		column.ColumnLength, err = StringToInt(dataLength)
   361  		if err != nil {
   362  			return
   363  		}
   364  	}
   365  	dataPrecision := GetStringValue(data["DATA_PRECISION"])
   366  	if dataPrecision != "" && dataPrecision != "0" {
   367  		column.ColumnPrecision, err = StringToInt(dataPrecision)
   368  		if err != nil {
   369  			return
   370  		}
   371  	}
   372  	dataScale := GetStringValue(data["DATA_SCALE"])
   373  	if dataScale != "" && dataScale != "0" {
   374  		column.ColumnScale, err = StringToInt(dataScale)
   375  		if err != nil {
   376  			return
   377  		}
   378  	}
   379  	characterMaximumLength := GetStringValue(data["CHARACTER_MAXIMUM_LENGTH"])
   380  	if characterMaximumLength != "" && characterMaximumLength != "0" {
   381  		column.ColumnLength, err = StringToInt(characterMaximumLength)
   382  		if err != nil {
   383  			return
   384  		}
   385  	}
   386  
   387  	numericPrecision := GetStringValue(data["NUMERIC_PRECISION"])
   388  	if numericPrecision != "" && numericPrecision != "0" {
   389  		column.ColumnPrecision, err = StringToInt(numericPrecision)
   390  		if err != nil {
   391  			return
   392  		}
   393  	}
   394  	numericScale := GetStringValue(data["NUMERIC_SCALE"])
   395  	if numericScale != "" && numericScale != "0" {
   396  		column.ColumnScale, err = StringToInt(numericScale)
   397  		if err != nil {
   398  			return
   399  		}
   400  	}
   401  	datetimePrecision := GetStringValue(data["DATETIME_PRECISION"])
   402  	if datetimePrecision != "" && datetimePrecision != "0" {
   403  		column.ColumnPrecision, err = StringToInt(datetimePrecision)
   404  		if err != nil {
   405  			return
   406  		}
   407  	}
   408  	columnTypeInfo, err := this_.GetColumnTypeInfo(column)
   409  	if err != nil {
   410  		bs, _ = json.Marshal(data)
   411  		err = errors.New("ColumnModel error column data:" + string(bs) + ",error:" + err.Error())
   412  		return
   413  	}
   414  	column.ColumnDataType = columnTypeInfo.Name
   415  
   416  	if column.ColumnDefault != "" {
   417  		//if strings.HasSuffix(column.ColumnDefault, "::"+column.ColumnDataType) {
   418  		//	column.ColumnDefault = strings.TrimSuffix(column.ColumnDefault, "::"+column.ColumnDataType)
   419  		//}
   420  		//column.ColumnDefault = strings.TrimLeft(column.ColumnDefault, "'")
   421  		//column.ColumnDefault = strings.TrimRight(column.ColumnDefault, "'")
   422  		//column.ColumnDefault = strings.TrimLeft(column.ColumnDefault, "\"")
   423  		//column.ColumnDefault = strings.TrimRight(column.ColumnDefault, "\"")
   424  	}
   425  	if columnTypeInfo.FullColumnByColumnType != nil {
   426  		err = columnTypeInfo.FullColumnByColumnType(columnType, column)
   427  		if err != nil {
   428  			return
   429  		}
   430  	} else {
   431  		if strings.Contains(columnType, "(") {
   432  			if (column.ColumnLength == 0 && column.ColumnPrecision == 0 && column.ColumnScale == 0) ||
   433  				(this_.dialectType == TypeMysql && columnTypeInfo.IsInteger) {
   434  				lengthStr := columnType[strings.Index(columnType, "(")+1 : strings.Index(columnType, ")")]
   435  				var v1 int
   436  				var v2 int
   437  				if strings.Contains(lengthStr, ",") {
   438  					v1, _ = strconv.Atoi(strings.TrimSpace(lengthStr[0:strings.Index(lengthStr, ",")]))
   439  					v2, _ = strconv.Atoi(strings.TrimSpace(lengthStr[strings.Index(lengthStr, ",")+1:]))
   440  					if strings.Contains(columnTypeInfo.Format, "$l") {
   441  						column.ColumnLength = v1
   442  					}
   443  					if strings.Contains(columnTypeInfo.Format, "$p") {
   444  						column.ColumnLength = v1
   445  					}
   446  					if strings.Contains(columnTypeInfo.Format, "$s") {
   447  						column.ColumnScale = v2
   448  					}
   449  				} else {
   450  					v1, _ = strconv.Atoi(lengthStr)
   451  					if strings.Contains(columnTypeInfo.Format, "$l") {
   452  						column.ColumnLength = v1
   453  					}
   454  					if strings.Contains(columnTypeInfo.Format, "$p") {
   455  						column.ColumnLength = v1
   456  					}
   457  					if strings.Contains(columnTypeInfo.Format, "$s") {
   458  						column.ColumnScale = v1
   459  					}
   460  				}
   461  				if columnTypeInfo.IsInteger {
   462  					if column.ColumnPrecision == 0 {
   463  						column.ColumnPrecision = column.ColumnLength
   464  					} else if column.ColumnLength == 0 {
   465  						column.ColumnLength = column.ColumnPrecision
   466  					}
   467  				}
   468  
   469  			}
   470  		}
   471  	}
   472  	return
   473  }
   474  
   475  func (this_ *mappingDialect) ColumnAddSql(param *ParamModel, ownerName string, tableName string, column *ColumnModel) (sqlList []string, err error) {
   476  
   477  	columnTypePack, err := this_.ColumnTypePack(column)
   478  	if err != nil {
   479  		return
   480  	}
   481  	columnDefaultPack, err := this_.ColumnDefaultPack(param, column)
   482  	if err != nil {
   483  		return
   484  	}
   485  	sqlList, err = this_.FormatSql(this_.ColumnAdd, param,
   486  		column,
   487  		map[string]string{
   488  			"ownerName":         ownerName,
   489  			"tableName":         tableName,
   490  			"columnTypePack":    columnTypePack,
   491  			"columnDefaultPack": columnDefaultPack,
   492  		},
   493  	)
   494  	if err != nil {
   495  		return
   496  	}
   497  	return
   498  }
   499  
   500  func (this_ *mappingDialect) ColumnUpdateSql(param *ParamModel, ownerName string, tableName string, oldColumn *ColumnModel, column *ColumnModel) (sqlList []string, err error) {
   501  	if oldColumn.ColumnName == "" {
   502  		oldColumn.ColumnName = column.ColumnName
   503  	}
   504  	columnTypePack, err := this_.ColumnTypePack(column)
   505  	if err != nil {
   506  		return
   507  	}
   508  	columnDefaultPack, err := this_.ColumnDefaultPack(param, column)
   509  	if err != nil {
   510  		return
   511  	}
   512  	data := map[string]string{
   513  		"ownerName":         ownerName,
   514  		"tableName":         tableName,
   515  		"oldColumnName":     oldColumn.ColumnName,
   516  		"columnTypePack":    columnTypePack,
   517  		"columnDefaultPack": columnDefaultPack,
   518  	}
   519  	var sqlList_ []string
   520  	var hasChangeName bool
   521  	if oldColumn.ColumnName != column.ColumnName {
   522  		hasChangeName = true
   523  	}
   524  	var hasChangeComment bool
   525  	if oldColumn.ColumnComment != column.ColumnComment {
   526  		hasChangeComment = true
   527  	}
   528  	var hasChangeAfter bool
   529  	if oldColumn.ColumnAfterColumn != column.ColumnAfterColumn {
   530  		hasChangeAfter = true
   531  	}
   532  	if !this_.ColumnUpdateHasRename {
   533  		if hasChangeName {
   534  			sqlList_, err = this_.ColumnRenameSql(param, ownerName, tableName, oldColumn.ColumnName, column.ColumnName)
   535  			if err != nil {
   536  				return
   537  			}
   538  			sqlList = append(sqlList, sqlList_...)
   539  			hasChangeName = false
   540  		}
   541  	}
   542  	if !this_.ColumnUpdateHasComment {
   543  		if hasChangeComment {
   544  			sqlList_, err = this_.ColumnCommentSql(param, ownerName, tableName, column.ColumnName, column.ColumnComment)
   545  			if err != nil {
   546  				return
   547  			}
   548  			sqlList = append(sqlList, sqlList_...)
   549  			hasChangeComment = false
   550  		}
   551  	}
   552  	if !this_.ColumnUpdateHasAfter {
   553  		if hasChangeAfter {
   554  			sqlList_, err = this_.ColumnAfterSql(param, ownerName, tableName, column.ColumnName, column.ColumnAfterColumn)
   555  			if err != nil {
   556  				return
   557  			}
   558  			sqlList = append(sqlList, sqlList_...)
   559  			hasChangeAfter = false
   560  		}
   561  	}
   562  	if hasChangeName || hasChangeComment || hasChangeAfter ||
   563  		oldColumn.ColumnDataType != column.ColumnDataType ||
   564  		oldColumn.ColumnLength != column.ColumnLength ||
   565  		oldColumn.ColumnPrecision != column.ColumnPrecision ||
   566  		oldColumn.ColumnScale != column.ColumnScale ||
   567  		oldColumn.ColumnDefault != column.ColumnDefault ||
   568  		oldColumn.ColumnNotNull != column.ColumnNotNull {
   569  		sqlList_, err = this_.FormatSql(this_.ColumnUpdate, param,
   570  			oldColumn,
   571  			column,
   572  			data,
   573  		)
   574  		if err != nil {
   575  			return
   576  		}
   577  		sqlList = append(sqlList, sqlList_...)
   578  	}
   579  	return
   580  }
   581  
   582  func (this_ *mappingDialect) ColumnDeleteSql(param *ParamModel, ownerName string, tableName string, columnName string) (sqlList []string, err error) {
   583  	sqlList, err = this_.FormatSql(this_.ColumnDelete, param,
   584  		map[string]string{
   585  			"ownerName":  ownerName,
   586  			"tableName":  tableName,
   587  			"columnName": columnName,
   588  		},
   589  	)
   590  	if err != nil {
   591  		return
   592  	}
   593  	return
   594  }
   595  
   596  func (this_ *mappingDialect) ColumnRenameSql(param *ParamModel, ownerName string, tableName string, oldColumnName string, columnName string) (sqlList []string, err error) {
   597  	sqlList, err = this_.FormatSql(this_.ColumnRename, param,
   598  		map[string]string{
   599  			"ownerName":     ownerName,
   600  			"tableName":     tableName,
   601  			"oldColumnName": oldColumnName,
   602  			"columnName":    columnName,
   603  		},
   604  	)
   605  	if err != nil {
   606  		return
   607  	}
   608  	return
   609  }
   610  
   611  func (this_ *mappingDialect) ColumnCommentSql(param *ParamModel, ownerName string, tableName string, columnName string, columnComment string) (sqlList []string, err error) {
   612  	sqlList, err = this_.FormatSql(this_.ColumnComment, param,
   613  		map[string]string{
   614  			"ownerName":     ownerName,
   615  			"tableName":     tableName,
   616  			"columnName":    columnName,
   617  			"columnComment": columnComment,
   618  		},
   619  	)
   620  	if err != nil {
   621  		return
   622  	}
   623  	return
   624  }
   625  
   626  func (this_ *mappingDialect) ColumnAfterSql(param *ParamModel, ownerName string, tableName string, columnName string, columnAfterColumn string) (sqlList []string, err error) {
   627  	sqlList, err = this_.FormatSql(this_.ColumnAfter, param,
   628  		map[string]string{
   629  			"ownerName":         ownerName,
   630  			"tableName":         tableName,
   631  			"columnName":        columnName,
   632  			"columnAfterColumn": columnAfterColumn,
   633  		},
   634  	)
   635  	if err != nil {
   636  		return
   637  	}
   638  	return
   639  }
   640  
   641  func (this_ *mappingDialect) PrimaryKeysSelectSql(param *ParamModel, ownerName string, tableName string) (sqlInfo string, err error) {
   642  	sqlList, err := this_.FormatSql(this_.PrimaryKeysSelect, param,
   643  		map[string]string{
   644  			"ownerName": ownerName,
   645  			"tableName": tableName,
   646  		},
   647  	)
   648  	if err != nil {
   649  		return
   650  	}
   651  	if len(sqlList) > 0 {
   652  		sqlInfo = sqlList[0]
   653  	}
   654  	return
   655  }
   656  
   657  func (this_ *mappingDialect) PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error) {
   658  	if data == nil {
   659  		return
   660  	}
   661  	primaryKey = &PrimaryKeyModel{}
   662  	bs, err := json.Marshal(data)
   663  	if err != nil {
   664  		return
   665  	}
   666  	err = json.Unmarshal(bs, primaryKey)
   667  	if err != nil {
   668  		return
   669  	}
   670  	return
   671  }
   672  
   673  func (this_ *mappingDialect) PrimaryKeyAddSql(param *ParamModel, ownerName string, tableName string, columnNames []string) (sqlList []string, err error) {
   674  	sqlList, err = this_.FormatSql(this_.PrimaryKeyAdd, param,
   675  		map[string]interface{}{
   676  			"ownerName":   ownerName,
   677  			"tableName":   tableName,
   678  			"columnNames": columnNames,
   679  			"primaryKeys": columnNames,
   680  		},
   681  	)
   682  	if err != nil {
   683  		return
   684  	}
   685  	return
   686  }
   687  
   688  func (this_ *mappingDialect) PrimaryKeyDeleteSql(param *ParamModel, ownerName string, tableName string) (sqlList []string, err error) {
   689  	sqlList, err = this_.FormatSql(this_.PrimaryKeyDelete, param,
   690  		map[string]string{
   691  			"ownerName": ownerName,
   692  			"tableName": tableName,
   693  		},
   694  	)
   695  	if err != nil {
   696  		return
   697  	}
   698  	return
   699  }
   700  
   701  func (this_ *mappingDialect) IndexesSelectSql(param *ParamModel, ownerName string, tableName string) (sqlInfo string, err error) {
   702  	sqlList, err := this_.FormatSql(this_.IndexesSelect, param,
   703  		map[string]string{
   704  			"ownerName": ownerName,
   705  			"tableName": tableName,
   706  		},
   707  	)
   708  	if err != nil {
   709  		return
   710  	}
   711  	if len(sqlList) > 0 {
   712  		sqlInfo = sqlList[0]
   713  	}
   714  	return
   715  }
   716  
   717  func (this_ *mappingDialect) IndexModel(data map[string]interface{}) (index *IndexModel, err error) {
   718  	if data == nil {
   719  		return
   720  	}
   721  	index = &IndexModel{}
   722  	bs, err := json.Marshal(data)
   723  	if err != nil {
   724  		return
   725  	}
   726  	err = json.Unmarshal(bs, index)
   727  	if err != nil {
   728  		return
   729  	}
   730  
   731  	if GetStringValue(data["UNIQUENESS"]) == "UNIQUE" {
   732  		index.IndexType = "unique"
   733  	}
   734  	if GetStringValue(data["NON_UNIQUE"]) == "0" {
   735  		index.IndexType = "unique"
   736  	}
   737  	if GetStringValue(data["isUnique"]) == "1" {
   738  		index.IndexType = "unique"
   739  	}
   740  	if GetStringValue(data["UNIQUENESS"]) == "UNIQUE" {
   741  		index.IndexType = "unique"
   742  	}
   743  	indexTypeInfo, err := this_.GetIndexTypeInfo(index.IndexType)
   744  	if err != nil {
   745  		//fmt.Println(data)
   746  		return
   747  	}
   748  	index.IndexType = indexTypeInfo.Name
   749  
   750  	return
   751  }
   752  
   753  func (this_ *mappingDialect) IndexAddSql(param *ParamModel, ownerName string, tableName string, index *IndexModel) (sqlList []string, err error) {
   754  
   755  	indexType, err := this_.IndexTypeFormat(index)
   756  	if err != nil {
   757  		return
   758  	}
   759  	indexType = strings.TrimSpace(indexType)
   760  	if indexType == "" {
   761  		return
   762  	}
   763  
   764  	indexName, err := this_.IndexNameFormat(param, ownerName, tableName, index)
   765  	if err != nil {
   766  		return
   767  	}
   768  	indexName = strings.TrimSpace(indexName)
   769  	if indexName == "" {
   770  		indexName = index.IndexName
   771  	}
   772  	sqlList, err = this_.FormatSql(this_.IndexAdd, param,
   773  		index,
   774  		map[string]string{
   775  			"ownerName": ownerName,
   776  			"tableName": tableName,
   777  			"indexName": indexName,
   778  			"indexType": indexType,
   779  		},
   780  	)
   781  	if err != nil {
   782  		return
   783  	}
   784  	//fmt.Println("index add sql:", sqlList)
   785  	return
   786  }
   787  
   788  func (this_ *mappingDialect) IndexDeleteSql(param *ParamModel, ownerName string, tableName string, indexName string) (sqlList []string, err error) {
   789  	sqlList, err = this_.FormatSql(this_.IndexDelete, param,
   790  		map[string]string{
   791  			"ownerName": ownerName,
   792  			"tableName": tableName,
   793  			"indexName": indexName,
   794  		},
   795  	)
   796  	if err != nil {
   797  		return
   798  	}
   799  	return
   800  }