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

     1  package worker
     2  
     3  import (
     4  	"database/sql"
     5  	"errors"
     6  	"github.com/team-ide/go-dialect/dialect"
     7  	"strings"
     8  )
     9  
    10  func ColumnsSelect(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, tableName string, ignoreError bool) (list []*dialect.ColumnModel, err error) {
    11  	sqlInfo, err := dia.ColumnsSelectSql(param, ownerName, tableName)
    12  	if err != nil {
    13  		return
    14  	}
    15  	if sqlInfo == "" {
    16  		return
    17  	}
    18  	dataList, err := DoQuery(db, sqlInfo, nil)
    19  	if err != nil {
    20  		errStr := err.Error()
    21  		if dia.DialectType() == dialect.TypeMysql && strings.Contains(errStr, "Unknown column 'DATETIME_PRECISION'") {
    22  			sqlInfo = `SELECT
    23      COLUMN_NAME columnName,
    24      COLUMN_COMMENT columnComment,
    25      COLUMN_DEFAULT columnDefault,
    26      EXTRA columnExtra,
    27      TABLE_NAME tableName,
    28      TABLE_SCHEMA ownerName,
    29      CHARACTER_SET_NAME columnCharacterSetName,
    30      IS_NULLABLE isNullable,
    31      DATA_TYPE columnDataType,
    32      COLUMN_TYPE columnType,
    33      NUMERIC_PRECISION NUMERIC_PRECISION,
    34      NUMERIC_SCALE NUMERIC_SCALE,
    35      CHARACTER_MAXIMUM_LENGTH CHARACTER_MAXIMUM_LENGTH
    36  FROM information_schema.columns
    37  WHERE TABLE_SCHEMA='` + ownerName + `'
    38    AND TABLE_NAME='` + tableName + `'`
    39  			dataList, err = DoQuery(db, sqlInfo, nil)
    40  		}
    41  	}
    42  	if err != nil {
    43  		err = errors.New("ColumnsSelect error sql:" + sqlInfo + ",error:" + err.Error())
    44  		return
    45  	}
    46  	for _, data := range dataList {
    47  		model, e := dia.ColumnModel(data)
    48  		if e != nil {
    49  			if !ignoreError {
    50  				err = e
    51  				return
    52  			}
    53  			model = &dialect.ColumnModel{
    54  				Error: e.Error(),
    55  			}
    56  		}
    57  		list = append(list, model)
    58  	}
    59  	var last *dialect.ColumnModel
    60  	for _, column := range list {
    61  		if last != nil {
    62  			column.ColumnAfterColumn = last.ColumnName
    63  		}
    64  		last = column
    65  	}
    66  	return
    67  }