github.com/RevenueMonster/sqlike@v1.0.6/sqlike/column.go (about)

     1  package sqlike
     2  
     3  import (
     4  	"context"
     5  
     6  	sqldriver "github.com/RevenueMonster/sqlike/sql/driver"
     7  	sqlstmt "github.com/RevenueMonster/sqlike/sql/stmt"
     8  	"github.com/RevenueMonster/sqlike/types"
     9  )
    10  
    11  // Column : contains sql column information
    12  type Column struct {
    13  	// column name
    14  	Name string
    15  
    16  	// column position in sql database
    17  	Position int
    18  
    19  	// column data type with precision or size, eg. VARCHAR(20)
    20  	Type string
    21  
    22  	// column data type without precision and size, eg. VARCHAR
    23  	DataType string
    24  
    25  	// whether column is nullable or not
    26  	IsNullable types.Boolean
    27  
    28  	// default value of the column
    29  	DefaultValue *string
    30  
    31  	// text character set encoding
    32  	Charset *string
    33  
    34  	// text collation for sorting
    35  	Collation *string
    36  
    37  	// column comment
    38  	Comment string
    39  
    40  	// extra information
    41  	Extra string
    42  }
    43  
    44  // ColumnView :
    45  type ColumnView struct {
    46  	tb *Table
    47  }
    48  
    49  // List : list all the column from current table
    50  func (cv *ColumnView) List(ctx context.Context) ([]Column, error) {
    51  	return cv.tb.ListColumns(ctx)
    52  }
    53  
    54  // Rename : rename your column name
    55  func (cv *ColumnView) Rename(ctx context.Context, oldColName, newColName string) error {
    56  	stmt := sqlstmt.AcquireStmt(cv.tb.dialect)
    57  	defer sqlstmt.ReleaseStmt(stmt)
    58  	cv.tb.dialect.RenameColumn(stmt, cv.tb.dbName, cv.tb.name, oldColName, newColName)
    59  	_, err := sqldriver.Execute(
    60  		ctx,
    61  		cv.tb.driver,
    62  		stmt,
    63  		cv.tb.logger,
    64  	)
    65  	return err
    66  }
    67  
    68  // DropOne : drop column with name
    69  func (cv *ColumnView) DropOne(ctx context.Context, name string) error {
    70  	stmt := sqlstmt.AcquireStmt(cv.tb.dialect)
    71  	defer sqlstmt.ReleaseStmt(stmt)
    72  	cv.tb.dialect.DropColumn(stmt, cv.tb.dbName, cv.tb.name, name)
    73  	_, err := sqldriver.Execute(
    74  		ctx,
    75  		cv.tb.driver,
    76  		stmt,
    77  		cv.tb.logger,
    78  	)
    79  	return err
    80  }