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

     1  package dialect
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/shopspring/decimal"
     7  	"reflect"
     8  	"strings"
     9  	"time"
    10  )
    11  
    12  type Type struct {
    13  	Name string `json:"name"`
    14  }
    15  
    16  type Dialect interface {
    17  	DialectType() (dialectType *Type)
    18  	GetColumnTypeInfos() (columnTypeInfoList []*ColumnTypeInfo)
    19  	GetColumnTypeInfo(column *ColumnModel) (columnTypeInfo *ColumnTypeInfo, err error)
    20  	ColumnTypePack(column *ColumnModel) (columnTypePack string, err error)
    21  	GetIndexTypeInfos() (indexTypeInfoList []*IndexTypeInfo)
    22  	//FormatDefaultValue(column *ColumnModel) (defaultValue string)
    23  	//ToColumnTypeInfo(columnType string) (columnTypeInfo *ColumnTypeInfo, length, decimal int, err error)
    24  
    25  	OwnerNamePack(param *ParamModel, ownerName string) string
    26  	TableNamePack(param *ParamModel, tableName string) string
    27  	OwnerTablePack(param *ParamModel, ownerName string, tableName string) string
    28  	ColumnNamePack(param *ParamModel, columnName string) string
    29  	ColumnNamesPack(param *ParamModel, columnNames []string) string
    30  	SqlValuePack(param *ParamModel, column *ColumnModel, value interface{}) string
    31  	ColumnDefaultPack(param *ParamModel, column *ColumnModel) (columnDefaultPack string, err error)
    32  	// IsSqlEnd 判断SQL是否以 分号 结尾
    33  	IsSqlEnd(sqlInfo string) bool
    34  	// SqlSplit 根据 分号 分割多条SQL
    35  	SqlSplit(sqlInfo string) []string
    36  
    37  	OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error)
    38  	OwnersSelectSql(param *ParamModel) (sql string, err error)
    39  	OwnerSelectSql(param *ParamModel, ownerName string) (sql string, err error)
    40  	OwnerCreateSql(param *ParamModel, owner *OwnerModel) (sqlList []string, err error)
    41  	OwnerDeleteSql(param *ParamModel, ownerName string) (sqlList []string, err error)
    42  
    43  	TableModel(data map[string]interface{}) (table *TableModel, err error)
    44  	TablesSelectSql(param *ParamModel, ownerName string) (sql string, err error)
    45  	TableSelectSql(param *ParamModel, ownerName string, tableName string) (sql string, err error)
    46  	TableCreateSql(param *ParamModel, ownerName string, table *TableModel) (sqlList []string, err error)
    47  	TableCommentSql(param *ParamModel, ownerName string, tableName string, tableComment string) (sqlList []string, err error)
    48  	TableRenameSql(param *ParamModel, ownerName string, oldTableName string, tableName string) (sqlList []string, err error)
    49  	TableDeleteSql(param *ParamModel, ownerName string, tableName string) (sqlList []string, err error)
    50  
    51  	ColumnModel(data map[string]interface{}) (table *ColumnModel, err error)
    52  	ColumnsSelectSql(param *ParamModel, ownerName string, tableName string) (sql string, err error)
    53  	ColumnSelectSql(param *ParamModel, ownerName string, tableName string, columnName string) (sql string, err error)
    54  	ColumnAddSql(param *ParamModel, ownerName string, tableName string, column *ColumnModel) (sqlList []string, err error)
    55  	ColumnCommentSql(param *ParamModel, ownerName string, tableName string, columnName string, columnComment string) (sqlList []string, err error)
    56  	ColumnUpdateSql(param *ParamModel, ownerName string, tableName string, oldColumn *ColumnModel, column *ColumnModel) (sqlList []string, err error)
    57  	ColumnDeleteSql(param *ParamModel, ownerName string, tableName string, columnName string) (sqlList []string, err error)
    58  
    59  	PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error)
    60  	PrimaryKeysSelectSql(param *ParamModel, ownerName string, tableName string) (sql string, err error)
    61  	PrimaryKeyAddSql(param *ParamModel, ownerName string, tableName string, columnNames []string) (sqlList []string, err error)
    62  	PrimaryKeyDeleteSql(param *ParamModel, ownerName string, tableName string) (sqlList []string, err error)
    63  
    64  	IndexModel(data map[string]interface{}) (index *IndexModel, err error)
    65  	IndexesSelectSql(param *ParamModel, ownerName string, tableName string) (sql string, err error)
    66  	IndexAddSql(param *ParamModel, ownerName string, tableName string, index *IndexModel) (sqlList []string, err error)
    67  	IndexDeleteSql(param *ParamModel, ownerName string, tableName string, indexName string) (sqlList []string, err error)
    68  
    69  	PackPageSql(selectSql string, pageSize int, pageNo int) (pageSql string)
    70  	ReplaceSqlVariable(sqlInfo string, args []interface{}) (variableSql string)
    71  	InsertSql(param *ParamModel, insert *InsertModel) (sqlList []string, err error)
    72  
    73  	DataListInsertSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, batchSqlList []string, batchValuesList [][]interface{}, err error)
    74  	DataListUpdateSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}, dataWhereList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, err error)
    75  	DataListDeleteSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, dataWhereList []map[string]interface{}) (sqlList []string, valuesList [][]interface{}, err error)
    76  	DataListSelectSql(param *ParamModel, ownerName string, tableName string, columnList []*ColumnModel, whereList []*Where, orderList []*Order) (sql string, values []interface{}, err error)
    77  }
    78  
    79  var (
    80  	TypeMysql      = &Type{Name: "mysql"}
    81  	TypeSqlite     = &Type{Name: "sqlite"}
    82  	TypeOracle     = &Type{Name: "oracle"}
    83  	TypeDM         = &Type{Name: "dm"}
    84  	TypeKingBase   = &Type{Name: "kingbase"}
    85  	TypeShenTong   = &Type{Name: "shentong"}
    86  	TypePostgresql = &Type{Name: "postgresql"}
    87  	TypeGBase      = &Type{Name: "gbase"}
    88  	TypeOdbc       = &Type{Name: "odbc"}
    89  	TypeOpenGauss  = &Type{Name: "opengauss"}
    90  )
    91  
    92  func NewDialect(dialectType string) (dia Dialect, err error) {
    93  	switch strings.ToLower(dialectType) {
    94  	case "mysql":
    95  		dia, err = NewMappingDialect(NewMappingMysql())
    96  		break
    97  	case "sqlite", "sqlite3":
    98  		dia, err = NewMappingDialect(NewMappingSqlite())
    99  		break
   100  	case "dameng", "dm":
   101  		dia, err = NewMappingDialect(NewMappingDM())
   102  		break
   103  	case "kingbase", "kb":
   104  		dia, err = NewMappingDialect(NewMappingKingBase())
   105  		break
   106  	case "oracle":
   107  		dia, err = NewMappingDialect(NewMappingOracle())
   108  		break
   109  	case "shentong", "st":
   110  		dia, err = NewMappingDialect(NewMappingShenTong())
   111  		break
   112  	case "postgresql", "ps":
   113  		dia, err = NewMappingDialect(NewMappingPostgresql())
   114  		break
   115  	case "opengauss":
   116  		dia, err = NewMappingDialect(NewMappingOpenGauss())
   117  		break
   118  	case "gbase":
   119  		dia, err = NewMappingDialect(NewMappingGBase())
   120  		break
   121  	case "odbc":
   122  		dia, err = NewMappingDialect(NewMappingOdbc())
   123  		break
   124  	default:
   125  		err = errors.New("dialect type [" + dialectType + "] not support ")
   126  		return
   127  	}
   128  	return
   129  }
   130  
   131  func packingName(packingCharacter, name string) string {
   132  	name = strings.ReplaceAll(name, `""`, "")
   133  	name = strings.ReplaceAll(name, `'`, "")
   134  	name = strings.ReplaceAll(name, "`", "")
   135  	name = strings.TrimSpace(name)
   136  	if packingCharacter == "" {
   137  		return name
   138  	}
   139  	return packingCharacter + name + packingCharacter
   140  }
   141  
   142  func packingNames(packingCharacter string, names []string) string {
   143  	return packingValues(packingCharacter, names)
   144  }
   145  
   146  func packingValues(packingCharacter string, values []string) string {
   147  
   148  	res := ""
   149  
   150  	for _, value := range values {
   151  		res += packingName(packingCharacter, value) + ", "
   152  	}
   153  	res = strings.TrimSuffix(res, ", ")
   154  	return res
   155  }
   156  
   157  func packingValue(column *ColumnModel, columnTypeInfo *ColumnTypeInfo, packingCharacter string, escapeChar string, value interface{}) string {
   158  	if value == nil {
   159  		return "NULL"
   160  	}
   161  	vOf := reflect.ValueOf(value)
   162  	if vOf.Kind() == reflect.Ptr {
   163  		if vOf.IsNil() {
   164  			return "NULL"
   165  		}
   166  		return packingValue(column, columnTypeInfo, packingCharacter, escapeChar, vOf.Elem().Interface())
   167  	}
   168  
   169  	baseValue, isBaseValue := GetBaseTypeValue(value)
   170  	if isBaseValue {
   171  		value = baseValue
   172  	}
   173  	var valueString string
   174  	switch v := value.(type) {
   175  	case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
   176  		return fmt.Sprintf("%d", v)
   177  	case float32:
   178  		return decimal.NewFromFloat32(v).String()
   179  	case float64:
   180  		return decimal.NewFromFloat(v).String()
   181  	case bool:
   182  		if v {
   183  			return "1"
   184  		}
   185  		return "0"
   186  	case time.Time:
   187  		if v.IsZero() {
   188  			return "NULL"
   189  		}
   190  		valueString = v.Format("2006-01-02 15:04:05")
   191  		//if this_.DateFunction != "" {
   192  		//	return strings.ReplaceAll(this_.DateFunction, "$value", valueString)
   193  		//}
   194  		break
   195  	case string:
   196  		valueString = v
   197  		break
   198  	case []byte:
   199  		valueString = string(v)
   200  		break
   201  	default:
   202  		valueString = GetStringValue(value)
   203  		break
   204  	}
   205  
   206  	if columnTypeInfo != nil {
   207  		if columnTypeInfo.SqlValuePack != nil {
   208  			return columnTypeInfo.SqlValuePack(valueString)
   209  		}
   210  	}
   211  	if valueString == "" && column != nil && column.ColumnNotNull && column.ColumnDefault == "" {
   212  
   213  	} else {
   214  		if columnTypeInfo != nil {
   215  			if columnTypeInfo.IsNumber {
   216  				if valueString == "" {
   217  					return "NULL"
   218  				}
   219  				return valueString
   220  			} else if columnTypeInfo.IsEnum {
   221  				if valueString == "" {
   222  					return "NULL"
   223  				}
   224  			}
   225  		}
   226  	}
   227  
   228  	if packingCharacter == "" {
   229  		return valueString
   230  	}
   231  	return formatStringValue(packingCharacter, escapeChar, valueString)
   232  }
   233  
   234  func formatStringValue(packingCharacter string, escapeChar string, valueString string) string {
   235  	if packingCharacter == "" {
   236  		return valueString
   237  	}
   238  	//valueString = strings.ReplaceAll(valueString, "\n", `\\n`)
   239  	out := packingCharacter
   240  	ss := strings.Split(valueString, "")
   241  	var valueLen = len(ss)
   242  	for i := 0; i < valueLen; i++ {
   243  		s := ss[i]
   244  		switch s {
   245  		case packingCharacter:
   246  			out += escapeChar + packingCharacter
   247  			break
   248  		case "\\":
   249  			if escapeChar == "\\" {
   250  				out += "\\"
   251  			}
   252  			out += "\\"
   253  			break
   254  		default:
   255  			out += s
   256  			break
   257  		}
   258  	}
   259  	out += packingCharacter
   260  	return out
   261  }