github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/x/sqlgen/create_table.go (about)

     1  package sqlgen
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Column initialize a column builder
     9  // that requires you to choose a type
    10  // before you can Build the statement
    11  func (tb *CreateTableBuilder) Column(name string) *ColumnBuilder {
    12  	return &ColumnBuilder{
    13  		tableBuilder: tb,
    14  		name:         name,
    15  	}
    16  }
    17  
    18  // appendColumn to the table column list
    19  func (tb *CreateTableBuilder) appendColumn(c *tableColumn) *CreateTableBuilder {
    20  	tb.columns = append(tb.columns, *c)
    21  	return tb
    22  }
    23  
    24  // Build the statement
    25  func (tb *CreateTableBuilder) Build() string {
    26  	return fmt.Sprintf("CREATE TABLE %s(\n    %s);\n", tb.name, tb.buildColumns())
    27  }
    28  
    29  func (tb *CreateTableBuilder) buildColumns() string {
    30  	queryParts := []string{}
    31  	for _, c := range tb.columns {
    32  		queryParts = append(queryParts, fmt.Sprintf("%s %s", c.name, c.sqlType))
    33  	}
    34  
    35  	return strings.Join(queryParts, ",\n\t")
    36  }