Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#280] Add Text, Json, etc methods #731

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 20 additions & 4 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Blueprint interface {
BigInteger(column string) ColumnDefinition
// Build Execute the blueprint to build / modify the table.
Build(query orm.Query, grammar Grammar) error
// Char Create a new char column on the table.
Char(column string, length ...int) ColumnDefinition
// Create Indicate that the table needs to be created.
Create()
// Decimal Create a new decimal column on the table.
Expand All @@ -19,6 +21,8 @@ type Blueprint interface {
Double(column string) ColumnDefinition
// DropIfExists Indicate that the table should be dropped if it exists.
DropIfExists()
// Enum Create a new enum column on the table.
Enum(column string, array []string) ColumnDefinition
// Float Create a new float column on the table.
Float(column string, precision ...int) ColumnDefinition
// Foreign Specify a foreign key for the table.
Expand All @@ -31,10 +35,6 @@ type Blueprint interface {
GetTableName() string
// HasCommand Determine if the blueprint has a specific command.
HasCommand(command string) bool
// MediumIncrements Create a new auto-incrementing medium integer (3-byte) column on the table.
MediumIncrements(column string) ColumnDefinition
// MediumInteger Create a new medium integer (3-byte) column on the table.
MediumInteger(column string) ColumnDefinition
// ID Create a new auto-incrementing big integer (8-byte) column on the table.
ID(column ...string) ColumnDefinition
// Increments Create a new auto-incrementing integer (4-byte) column on the table.
Expand All @@ -45,6 +45,18 @@ type Blueprint interface {
Integer(column string) ColumnDefinition
// IntegerIncrements Create a new auto-incrementing integer (4-byte) column on the table.
IntegerIncrements(column string) ColumnDefinition
// Json Create a new json column on the table.
Json(column string) ColumnDefinition
// Jsonb Create a new jsonb column on the table.
Jsonb(column string) ColumnDefinition
// LongText Create a new long text column on the table.
LongText(column string) ColumnDefinition
// MediumIncrements Create a new auto-incrementing medium integer (3-byte) column on the table.
MediumIncrements(column string) ColumnDefinition
// MediumInteger Create a new medium integer (3-byte) column on the table.
MediumInteger(column string) ColumnDefinition
// MediumText Create a new medium text column on the table.
MediumText(column string) ColumnDefinition
// Primary Specify the primary key(s) for the table.
Primary(column ...string)
// SetTable Set the table that the blueprint operates on.
Expand All @@ -55,10 +67,14 @@ type Blueprint interface {
SmallInteger(column string) ColumnDefinition
// String Create a new string column on the table.
String(column string, length ...int) ColumnDefinition
// Text Create a new text column on the table.
Text(column string) ColumnDefinition
// TinyIncrements Create a new auto-incrementing tiny integer (1-byte) column on the table.
TinyIncrements(column string) ColumnDefinition
// TinyInteger Create a new tiny integer (1-byte) column on the table.
TinyInteger(column string) ColumnDefinition
// TinyText Create a new tiny text column on the table.
TinyText(column string) ColumnDefinition
// ToSql Get the raw SQL statements for the blueprint.
ToSql(grammar Grammar) []string
// UnsignedBigInteger Create a new unsigned big integer (8-byte) column on the table.
Expand Down
2 changes: 2 additions & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type ColumnDefinition interface {
AutoIncrement() ColumnDefinition
// Comment sets the comment value
Comment(comment string) ColumnDefinition
// GetAllowed returns the allowed value
GetAllowed() []string
// GetAutoIncrement returns the autoIncrement value
GetAutoIncrement() bool
// GetComment returns the comment value
Expand Down
16 changes: 16 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,34 @@ type Grammar interface {
GetAttributeCommands() []string
// TypeBigInteger Create the column definition for a big integer type.
TypeBigInteger(column ColumnDefinition) string
// TypeChar Create the column definition for a char type.
TypeChar(column ColumnDefinition) string
// TypeDecimal Create the column definition for a decimal type.
TypeDecimal(column ColumnDefinition) string
// TypeDouble Create the column definition for a double type.
TypeDouble(column ColumnDefinition) string
// TypeEnum Create the column definition for an enumeration type.
TypeEnum(column ColumnDefinition) string
// TypeFloat Create the column definition for a float type.
TypeFloat(column ColumnDefinition) string
// TypeInteger Create the column definition for an integer type.
TypeInteger(column ColumnDefinition) string
// TypeJson Create the column definition for a json type.
TypeJson(column ColumnDefinition) string
// TypeJsonb Create the column definition for a jsonb type.
TypeJsonb(column ColumnDefinition) string
// TypeLongText Create the column definition for a long text type.
TypeLongText(column ColumnDefinition) string
// TypeMediumInteger Create the column definition for a medium integer type.
TypeMediumInteger(column ColumnDefinition) string
// TypeMediumText Create the column definition for a medium text type.
TypeMediumText(column ColumnDefinition) string
// TypeText Create the column definition for a text type.
TypeText(column ColumnDefinition) string
// TypeTinyInteger Create the column definition for a tiny integer type.
TypeTinyInteger(column ColumnDefinition) string
// TypeTinyText Create the column definition for a tiny text type.
TypeTinyText(column ColumnDefinition) string
// TypeSmallInteger Create the column definition for a small integer type.
TypeSmallInteger(column ColumnDefinition) string
// TypeString Create the column definition for a string type.
Expand Down
112 changes: 66 additions & 46 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *Blueprint) BigIncrements(column string) schema.ColumnDefinition {
}

func (r *Blueprint) BigInteger(column string) schema.ColumnDefinition {
return r.addIntegerColumn("bigInteger", column)
return r.createAndAddColumn("bigInteger", column)
}

func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error {
Expand All @@ -42,30 +42,30 @@ func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error
return nil
}

func (r *Blueprint) Char(column string, length ...int) schema.ColumnDefinition {
defaultLength := constants.DefaultStringLength
if len(length) > 0 {
defaultLength = length[0]
}

columnImpl := r.createAndAddColumn("char", column)
columnImpl.length = &defaultLength

return columnImpl
}

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

func (r *Blueprint) Decimal(column string) schema.ColumnDefinition {
columnImpl := &ColumnDefinition{
name: &column,
ttype: convert.Pointer("decimal"),
}
r.addColumn(columnImpl)

return columnImpl
return r.createAndAddColumn("decimal", column)
}

func (r *Blueprint) Double(column string) schema.ColumnDefinition {
columnImpl := &ColumnDefinition{
name: &column,
ttype: convert.Pointer("double"),
}
r.addColumn(columnImpl)

return columnImpl
return r.createAndAddColumn("double", column)
}

func (r *Blueprint) DropIfExists() {
Expand All @@ -74,16 +74,20 @@ func (r *Blueprint) DropIfExists() {
})
}

func (r *Blueprint) Enum(column string, allowed []string) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("enum", column)
columnImpl.allowed = allowed

return columnImpl
}

func (r *Blueprint) Float(column string, precision ...int) schema.ColumnDefinition {
columnImpl := &ColumnDefinition{
name: &column,
precision: convert.Pointer(53),
ttype: convert.Pointer("float"),
}
columnImpl := r.createAndAddColumn("float", column)
columnImpl.precision = convert.Pointer(53)

if len(precision) > 0 {
columnImpl.precision = &precision[0]
}
r.addColumn(columnImpl)

return columnImpl
}
Expand Down Expand Up @@ -141,19 +145,35 @@ func (r *Blueprint) Index(column ...string) schema.IndexDefinition {
}

func (r *Blueprint) Integer(column string) schema.ColumnDefinition {
return r.addIntegerColumn("integer", column)
return r.createAndAddColumn("integer", column)
}

func (r *Blueprint) IntegerIncrements(column string) schema.ColumnDefinition {
return r.UnsignedInteger(column).AutoIncrement()
}

func (r *Blueprint) Json(column string) schema.ColumnDefinition {
return r.createAndAddColumn("json", column)
}

func (r *Blueprint) Jsonb(column string) schema.ColumnDefinition {
return r.createAndAddColumn("jsonb", column)
}

func (r *Blueprint) LongText(column string) schema.ColumnDefinition {
return r.createAndAddColumn("longText", column)
}

func (r *Blueprint) MediumIncrements(column string) schema.ColumnDefinition {
return r.UnsignedMediumInteger(column).AutoIncrement()
}

func (r *Blueprint) MediumInteger(column string) schema.ColumnDefinition {
return r.addIntegerColumn("mediumInteger", column)
return r.createAndAddColumn("mediumInteger", column)
}

func (r *Blueprint) MediumText(column string) schema.ColumnDefinition {
return r.createAndAddColumn("mediumText", column)
}

func (r *Blueprint) Primary(columns ...string) {
Expand All @@ -169,7 +189,7 @@ func (r *Blueprint) SmallIncrements(column string) schema.ColumnDefinition {
}

func (r *Blueprint) SmallInteger(column string) schema.ColumnDefinition {
return r.addIntegerColumn("smallInteger", column)
return r.createAndAddColumn("smallInteger", column)
}

func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition {
Expand All @@ -178,22 +198,26 @@ func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition
defaultLength = length[0]
}

columnImpl := &ColumnDefinition{
length: &defaultLength,
name: &column,
ttype: convert.Pointer("string"),
}
r.addColumn(columnImpl)
columnImpl := r.createAndAddColumn("string", column)
columnImpl.length = &defaultLength

return columnImpl
}

func (r *Blueprint) Text(column string) schema.ColumnDefinition {
return r.createAndAddColumn("text", column)
}

func (r *Blueprint) TinyIncrements(column string) schema.ColumnDefinition {
return r.UnsignedTinyInteger(column).AutoIncrement()
}

func (r *Blueprint) TinyInteger(column string) schema.ColumnDefinition {
return r.addIntegerColumn("tinyInteger", column)
return r.createAndAddColumn("tinyInteger", column)
}

func (r *Blueprint) TinyText(column string) schema.ColumnDefinition {
return r.createAndAddColumn("tinyText", column)
}

func (r *Blueprint) ToSql(grammar schema.Grammar) []string {
Expand Down Expand Up @@ -262,17 +286,6 @@ 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)
}
Expand All @@ -281,13 +294,20 @@ func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) {
r.addAttributeCommands(grammar)
}

func (r *Blueprint) addIntegerColumn(name, column string) schema.ColumnDefinition {
func (r *Blueprint) createAndAddColumn(ttype, name string) *ColumnDefinition {
columnImpl := &ColumnDefinition{
name: &column,
ttype: convert.Pointer(name),
name: &name,
ttype: convert.Pointer(ttype),
}

r.addColumn(columnImpl)
r.columns = append(r.columns, columnImpl)

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

return columnImpl
}
Expand Down
20 changes: 20 additions & 0 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ func (s *BlueprintTestSuite) TestBuild() {
}
}

func (s *BlueprintTestSuite) TestChar() {
column := "name"
customLength := 100
length := constants.DefaultStringLength
ttype := "char"
s.blueprint.Char(column)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
length: &length,
name: &column,
ttype: &ttype,
})

s.blueprint.Char(column, customLength)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
length: &customLength,
name: &column,
ttype: &ttype,
})
}

func (s *BlueprintTestSuite) TestCreateIndexName() {
name := s.blueprint.createIndexName("index", []string{"id", "name-1", "name.2"})
s.Equal("goravel_users_id_name_1_name_2_index", name)
Expand Down
5 changes: 5 additions & 0 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type ColumnDefinition struct {
allowed []string
autoIncrement *bool
comment *string
def any
Expand All @@ -31,6 +32,10 @@ func (r *ColumnDefinition) Comment(comment string) schema.ColumnDefinition {
return r
}

func (r *ColumnDefinition) GetAllowed() []string {
return r.allowed
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved

func (r *ColumnDefinition) GetAutoIncrement() (autoIncrement bool) {
if r.autoIncrement != nil {
return *r.autoIncrement
Expand Down
Loading
Loading