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

     1  package dialect
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"sync"
     7  )
     8  
     9  type SqlMapping struct {
    10  	dialectType *Type
    11  
    12  	CanAppendOwnerName bool
    13  
    14  	columnTypeInfoList      []*ColumnTypeInfo
    15  	columnTypeInfoCache     map[string]*ColumnTypeInfo
    16  	columnTypeInfoCacheLock sync.Mutex
    17  
    18  	indexTypeInfoList      []*IndexTypeInfo
    19  	indexTypeInfoCache     map[string]*IndexTypeInfo
    20  	indexTypeInfoCacheLock sync.Mutex
    21  
    22  	OwnersSelect string
    23  	OwnerSelect  string
    24  	OwnerCreate  string
    25  	OwnerDelete  string
    26  
    27  	TablesSelect                string
    28  	TableSelect                 string
    29  	TableCreate                 string
    30  	TableCreateColumn           string
    31  	TableCreateColumnHasComment bool
    32  	TableCreatePrimaryKey       string
    33  	TableDelete                 string
    34  	TableComment                string
    35  	TableRename                 string
    36  
    37  	ColumnsSelect          string
    38  	ColumnSelect           string
    39  	ColumnAdd              string
    40  	ColumnDelete           string
    41  	ColumnComment          string
    42  	ColumnRename           string
    43  	ColumnUpdateHasRename  bool
    44  	ColumnUpdateHasComment bool
    45  	ColumnUpdateHasAfter   bool
    46  	ColumnUpdate           string
    47  	ColumnAfter            string
    48  
    49  	PrimaryKeysSelect string
    50  	PrimaryKeyAdd     string
    51  	PrimaryKeyDelete  string
    52  
    53  	IndexesSelect   string
    54  	IndexAdd        string
    55  	IndexDelete     string
    56  	IndexNameMaxLen int
    57  	IndexNamePack   string
    58  
    59  	OwnerNamePackChar  string
    60  	TableNamePackChar  string
    61  	ColumnNamePackChar string
    62  	SqlValuePackChar   string
    63  	SqlValueEscapeChar string
    64  
    65  	PackPageSql        func(selectSql string, pageSize int, pageNo int) (pageSql string)
    66  	ReplaceSqlVariable func(sqlInfo string, args []interface{}) (variableSql string)
    67  
    68  	OwnerTablePack func(param *ParamModel, ownerName string, tableName string) string
    69  	MethodCache    map[string]interface{}
    70  	dialect        Dialect
    71  }
    72  
    73  func (this_ *SqlMapping) DialectType() (dialectType *Type) {
    74  	dialectType = this_.dialectType
    75  	return
    76  }
    77  
    78  func (this_ *SqlMapping) GenDemoTable() (table *TableModel) {
    79  	table = &TableModel{
    80  		TableName:    "TABLE_DEMO",
    81  		TableComment: "TABLE_DEMO_comment",
    82  	}
    83  	columnTypeInfos := this_.GetColumnTypeInfos()
    84  	var lastIndexColumnIndex int
    85  	for i, columnTypeInfo := range columnTypeInfos {
    86  		column := &ColumnModel{}
    87  		column.ColumnName = fmt.Sprintf("column_%d", i)
    88  		column.ColumnDataType = columnTypeInfo.Name
    89  
    90  		if strings.Contains(columnTypeInfo.Format, "$l") {
    91  			column.ColumnLength = 5
    92  		}
    93  		if strings.Contains(columnTypeInfo.Format, "$p") {
    94  			column.ColumnPrecision = 3
    95  		}
    96  		if strings.Contains(columnTypeInfo.Format, "$s") {
    97  			column.ColumnScale = 2
    98  		}
    99  		column.ColumnComment = fmt.Sprintf("column_%d-comment", i)
   100  
   101  		if columnTypeInfo.IsEnum {
   102  			column.ColumnEnums = append(column.ColumnEnums, "option1")
   103  			column.ColumnEnums = append(column.ColumnEnums, "option2")
   104  		}
   105  
   106  		if i < 10 && i%3 == 0 {
   107  			column.ColumnNotNull = true
   108  		}
   109  		table.AddColumn(column)
   110  		if len(table.PrimaryKeys) > 2 {
   111  			continue
   112  		}
   113  		lastIndexColumnIndex = i
   114  		if !strings.EqualFold(columnTypeInfo.Name, "text") &&
   115  			!strings.EqualFold(columnTypeInfo.Name, "blob") {
   116  			table.PrimaryKeys = append(table.PrimaryKeys, column.ColumnName)
   117  		}
   118  	}
   119  	indexTypeInfos := this_.GetIndexTypeInfos()
   120  	for _, indexTypeInfo := range indexTypeInfos {
   121  		index := &IndexModel{}
   122  		for i, column := range table.ColumnList {
   123  			if i <= lastIndexColumnIndex {
   124  				continue
   125  			}
   126  			if len(indexTypeInfo.OnlySupportDataTypes) > 0 {
   127  				if StringsIndex(indexTypeInfo.OnlySupportDataTypes, strings.ToUpper(column.ColumnDataType)) < 0 {
   128  					continue
   129  				}
   130  			}
   131  			if StringsIndex(indexTypeInfo.NotSupportDataTypes, strings.ToUpper(column.ColumnDataType)) >= 0 {
   132  				continue
   133  			}
   134  			lastIndexColumnIndex = i
   135  			index.ColumnNames = append(index.ColumnNames, column.ColumnName)
   136  			if len(index.ColumnNames) >= 1 {
   137  				break
   138  			}
   139  		}
   140  		if len(index.ColumnNames) == 0 {
   141  			continue
   142  		}
   143  		index.IndexType = indexTypeInfo.Name
   144  		table.AddIndex(index)
   145  	}
   146  	return
   147  }