Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Blueprint interface {
DropIfExists()
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
// GetChangedColumns Get the changed columns.
GetChangedColumns() []ColumnDefinition
// GetCommands Get the commands.
GetCommands() []*Command
// GetTableName Get the table name with prefix.
GetTableName() string
// HasCommand Determine if the blueprint has a specific command.
Expand Down
4 changes: 0 additions & 4 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package schema
type ColumnDefinition interface {
// AutoIncrement set the column as auto increment
AutoIncrement() ColumnDefinition
// Change the column
Change()
// GetAutoIncrement returns the autoIncrement value
GetAutoIncrement() bool
// GetChange returns the change value
GetChange() bool
// GetDefault returns the default value
GetDefault() any
// GetLength returns the length value
Expand Down
6 changes: 2 additions & 4 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (

type Grammar interface {
// CompileAdd Compile an add column command.
CompileAdd(blueprint Blueprint) string
// CompileChange Compile a change column command into a series of SQL statements.
CompileChange(blueprint Blueprint) string
CompileAdd(blueprint Blueprint, command *Command) string
// CompileCreate Compile a create table command.
CompileCreate(blueprint Blueprint, query orm.Query) string
// CompileDropAllDomains Compile the SQL needed to drop all domains.
Expand All @@ -22,7 +20,7 @@ type Grammar interface {
// CompileDropIfExists Compile a drop table (if exists) command.
CompileDropIfExists(blueprint Blueprint) string
// CompileTables Compile the query to determine the tables.
CompileTables(database string) string
CompileTables() string
// CompileTypes Compile the query to determine the types.
CompileTypes() string
// CompileViews Compile the query to determine the views.
Expand Down
1 change: 0 additions & 1 deletion contracts/database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type Connection interface {
type Command struct {
Algorithm string
Column ColumnDefinition
Columns []string
From string
Index string
On string
Expand Down
68 changes: 21 additions & 47 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package schema
import (
ormcontract "github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/database/schema/constants"
"github.com/goravel/framework/support/convert"
)

const (
commandAdd = "add"
commandChange = "change"
commandComment = "comment"
commandCreate = "create"
commandDropIfExists = "dropIfExists"
defaultStringLength = 255
)

type Blueprint struct {
columns []*ColumnDefinition
commands []*schema.Command
Expand Down Expand Up @@ -56,36 +48,27 @@ func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error

func (r *Blueprint) Create() {
r.addCommand(&schema.Command{
Name: commandCreate,
Name: constants.CommandCreate,
})
}

func (r *Blueprint) DropIfExists() {
r.addCommand(&schema.Command{
Name: commandDropIfExists,
Name: constants.CommandDropIfExists,
})
}

func (r *Blueprint) GetAddedColumns() []schema.ColumnDefinition {
var columns []schema.ColumnDefinition
for _, column := range r.columns {
if column.change == nil || !*column.change {
columns = append(columns, column)
}
columns = append(columns, column)
}

return columns
}

func (r *Blueprint) GetChangedColumns() []schema.ColumnDefinition {
var columns []schema.ColumnDefinition
for _, column := range r.columns {
if column.change != nil && *column.change {
columns = append(columns, column)
}
}

return columns
func (r *Blueprint) GetCommands() []*schema.Command {
return r.commands
}

func (r *Blueprint) GetTableName() string {
Expand Down Expand Up @@ -127,7 +110,7 @@ func (r *Blueprint) SetTable(name string) {
}

func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition {
defaultLength := defaultStringLength
defaultLength := constants.DefaultStringLength
if len(length) > 0 {
defaultLength = length[0]
}
Expand All @@ -148,13 +131,11 @@ func (r *Blueprint) ToSql(query ormcontract.Query, grammar schema.Grammar) []str
var statements []string
for _, command := range r.commands {
switch command.Name {
case commandAdd:
statements = append(statements, grammar.CompileAdd(r))
case commandChange:
statements = append(statements, grammar.CompileChange(r))
case commandCreate:
case constants.CommandAdd:
statements = append(statements, grammar.CompileAdd(r, command))
case constants.CommandCreate:
statements = append(statements, grammar.CompileCreate(r, query))
case commandDropIfExists:
case constants.CommandDropIfExists:
statements = append(statements, grammar.CompileDropIfExists(r))
}
}
Expand All @@ -170,10 +151,10 @@ func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) {
attributeCommands := grammar.GetAttributeCommands()
for _, column := range r.columns {
for _, command := range attributeCommands {
if command == "comment" && column.comment != nil {
if command == constants.CommandComment && column.comment != nil {
r.addCommand(&schema.Command{
Column: column,
Name: commandComment,
Name: constants.CommandComment,
})
}
}
Expand All @@ -182,33 +163,26 @@ func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) {

func (r *Blueprint) addColumn(column *ColumnDefinition) {
r.columns = append(r.columns, column)

if !r.isCreate() {
r.addCommand(&schema.Command{
Name: constants.CommandAdd,
Column: column,
})
}
}

func (r *Blueprint) addCommand(command *schema.Command) {
r.commands = append(r.commands, command)
}

func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) {
var commands []*schema.Command
if len(r.GetAddedColumns()) > 0 && !r.isCreate() {
commands = append(commands, &schema.Command{
Name: commandAdd,
})
}
if len(r.GetChangedColumns()) > 0 && !r.isCreate() {
commands = append(commands, &schema.Command{
Name: commandChange,
})
}
if len(commands) > 0 {
r.commands = append(commands, r.commands...)
}
r.addAttributeCommands(grammar)
}

func (r *Blueprint) isCreate() bool {
for _, command := range r.commands {
if command.Name == commandCreate {
if command.Name == constants.CommandCreate {
return true
}
}
Expand Down
Loading