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 integer methods #727

Merged
merged 9 commits into from
Nov 19, 2024
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
24 changes: 24 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,46 @@ 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.
Increments(column string) ColumnDefinition
// Index Specify an index for the table.
Index(column ...string) IndexDefinition
// Integer Create a new integer (4-byte) column on the table.
Integer(column string) ColumnDefinition
// IntegerIncrements Create a new auto-incrementing integer (4-byte) column on the table.
IntegerIncrements(column string) ColumnDefinition
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
// Primary Specify the primary key(s) for the table.
Primary(column ...string)
// SetTable Set the table that the blueprint operates on.
SetTable(name string)
// SmallIncrements Create a new auto-incrementing small integer (2-byte) column on the table.
SmallIncrements(column string) ColumnDefinition
// SmallInteger Create a new small integer (2-byte) column on the table.
SmallInteger(column string) ColumnDefinition
// String Create a new string column on the table.
String(column string, length ...int) 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
// 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.
UnsignedBigInteger(column string) ColumnDefinition
// UnsignedInteger Create a new unsigned integer (4-byte) column on the table.
UnsignedInteger(column string) ColumnDefinition
// UnsignedMediumInteger Create a new unsigned medium integer (3-byte) column on the table.
UnsignedMediumInteger(column string) ColumnDefinition
// UnsignedSmallInteger Create a new unsigned small integer (2-byte) column on the table.
UnsignedSmallInteger(column string) ColumnDefinition
// UnsignedTinyInteger Create a new unsigned tiny integer (1-byte) column on the table.
UnsignedTinyInteger(column string) ColumnDefinition
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
}

type IndexConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type ColumnDefinition interface {
GetName() string
// GetNullable returns the nullable value
GetNullable() bool
// GetPlaces returns the places value
GetPlaces() int
// GetPrecision returns the precision value
GetPrecision() int
// GetTotal returns the total value
GetTotal() int
// GetType returns the type value
GetType() string
// Nullable allow NULL values to be inserted into the column
Expand Down
12 changes: 12 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ type Grammar interface {
GetAttributeCommands() []string
// TypeBigInteger Create the column definition for a big integer type.
TypeBigInteger(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() string
// TypeFloat Create the column definition for a float type.
TypeFloat(column ColumnDefinition) string
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
// TypeInteger Create the column definition for an integer type.
TypeInteger(column ColumnDefinition) string
// TypeMediumInteger Create the column definition for a medium integer type.
TypeMediumInteger(column ColumnDefinition) string
// TypeTinyInteger Create the column definition for a tiny integer type.
TypeTinyInteger(column ColumnDefinition) string
// TypeSmallInteger Create the column definition for a small integer type.
TypeSmallInteger(column ColumnDefinition) string
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
// TypeString Create the column definition for a string type.
TypeString(column ColumnDefinition) string
}
73 changes: 59 additions & 14 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@
}

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

r.addColumn(columnImpl)

return columnImpl
return r.addIntegerColumn("bigInteger", column)
}

func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error {
Expand Down Expand Up @@ -95,6 +88,10 @@
return false
}

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

func (r *Blueprint) ID(column ...string) schema.ColumnDefinition {
if len(column) > 0 {
return r.BigIncrements(column[0])
Expand All @@ -103,21 +100,26 @@
return r.BigIncrements("id")
}

func (r *Blueprint) Increments(column string) schema.ColumnDefinition {
return r.IntegerIncrements(column)

Check warning on line 104 in database/schema/blueprint.go

View check run for this annotation

Codecov / codecov/patch

database/schema/blueprint.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}

func (r *Blueprint) Index(column ...string) schema.IndexDefinition {
command := r.indexCommand(constants.CommandIndex, column)

return NewIndexDefinition(command)
}

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

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

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

func (r *Blueprint) Primary(columns ...string) {
Expand All @@ -128,6 +130,14 @@
r.table = name
}

func (r *Blueprint) SmallIncrements(column string) schema.ColumnDefinition {
return r.UnsignedSmallInteger(column).AutoIncrement()
}

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

func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition {
defaultLength := constants.DefaultStringLength
if len(length) > 0 {
Expand All @@ -144,6 +154,14 @@
return columnImpl
}

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)
}

func (r *Blueprint) ToSql(grammar schema.Grammar) []string {
r.addImpliedCommands(grammar)

Expand Down Expand Up @@ -176,6 +194,22 @@
return r.BigInteger(column).Unsigned()
}

func (r *Blueprint) UnsignedInteger(column string) schema.ColumnDefinition {
return r.Integer(column).Unsigned()
}

func (r *Blueprint) UnsignedMediumInteger(column string) schema.ColumnDefinition {
return r.MediumInteger(column).Unsigned()
}

func (r *Blueprint) UnsignedSmallInteger(column string) schema.ColumnDefinition {
return r.SmallInteger(column).Unsigned()
}

func (r *Blueprint) UnsignedTinyInteger(column string) schema.ColumnDefinition {
return r.TinyInteger(column).Unsigned()
}

func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) {
attributeCommands := grammar.GetAttributeCommands()
for _, column := range r.columns {
Expand Down Expand Up @@ -209,6 +243,17 @@
r.addAttributeCommands(grammar)
}

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

r.addColumn(columnImpl)

return columnImpl
}

func (r *Blueprint) createIndexName(ttype string, columns []string) string {
var table string
if strings.Contains(r.table, ".") {
Expand Down
107 changes: 105 additions & 2 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ func (s *BlueprintTestSuite) TestHasCommand() {
s.True(s.blueprint.HasCommand(constants.CommandCreate))
}

func (s *BlueprintTestSuite) TestIntegerIncrements() {
name := "name"
s.blueprint.IntegerIncrements(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
autoIncrement: convert.Pointer(true),
name: &name,
unsigned: convert.Pointer(true),
ttype: convert.Pointer("integer"),
})
}

func (s *BlueprintTestSuite) TestIndexCommand() {
s.blueprint.indexCommand("index", []string{"id", "name"})
s.Contains(s.blueprint.commands, &schema.Command{
Expand Down Expand Up @@ -218,13 +229,45 @@ func (s *BlueprintTestSuite) TestInteger() {
name: &name,
ttype: convert.Pointer("integer"),
})
}

s.blueprint.Integer(name).AutoIncrement().Unsigned()
func (s *BlueprintTestSuite) TestMediumIncrements() {
name := "name"
s.blueprint.MediumIncrements(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
autoIncrement: convert.Pointer(true),
name: &name,
unsigned: convert.Pointer(true),
ttype: convert.Pointer("integer"),
ttype: convert.Pointer("mediumInteger"),
})
}

func (s *BlueprintTestSuite) TestMediumInteger() {
name := "name"
s.blueprint.MediumInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("mediumInteger"),
})
}

func (s *BlueprintTestSuite) TestSmallIncrements() {
name := "name"
s.blueprint.SmallIncrements(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
autoIncrement: convert.Pointer(true),
name: &name,
unsigned: convert.Pointer(true),
ttype: convert.Pointer("smallInteger"),
})
}

func (s *BlueprintTestSuite) TestSmallInteger() {
name := "name"
s.blueprint.SmallInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("smallInteger"),
})
}

Expand All @@ -248,6 +291,26 @@ func (s *BlueprintTestSuite) TestString() {
})
}

func (s *BlueprintTestSuite) TestTinyIncrements() {
name := "name"
s.blueprint.TinyIncrements(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
autoIncrement: convert.Pointer(true),
name: &name,
unsigned: convert.Pointer(true),
ttype: convert.Pointer("tinyInteger"),
})
}

func (s *BlueprintTestSuite) TestTinyInteger() {
name := "name"
s.blueprint.TinyInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("tinyInteger"),
})
}

func (s *BlueprintTestSuite) TestToSql() {
for driver, grammar := range s.grammars {
// Create a table
Expand Down Expand Up @@ -283,3 +346,43 @@ func (s *BlueprintTestSuite) TestUnsignedBigInteger() {
unsigned: convert.Pointer(true),
})
}

func (s *BlueprintTestSuite) TestUnsignedInteger() {
name := "name"
s.blueprint.UnsignedInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("integer"),
unsigned: convert.Pointer(true),
})
}

func (s *BlueprintTestSuite) TestUnsignedMediumInteger() {
name := "name"
s.blueprint.UnsignedMediumInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("mediumInteger"),
unsigned: convert.Pointer(true),
})
}

func (s *BlueprintTestSuite) TestUnsignedSmallInteger() {
name := "name"
s.blueprint.UnsignedSmallInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("smallInteger"),
unsigned: convert.Pointer(true),
})
}

func (s *BlueprintTestSuite) TestUnsignedTinyInteger() {
name := "name"
s.blueprint.UnsignedTinyInteger(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("tinyInteger"),
unsigned: convert.Pointer(true),
})
}
27 changes: 27 additions & 0 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
length *int
name *string
nullable *bool
places *int
precision *int
total *int
ttype *string
unsigned *bool
}
Expand Down Expand Up @@ -58,6 +61,30 @@
return false
}

func (r *ColumnDefinition) GetPlaces() (places int) {
if r.places != nil {
return *r.places
}

Check warning on line 67 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L64-L67

Added lines #L64 - L67 were not covered by tests

return 2

Check warning on line 69 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L69

Added line #L69 was not covered by tests
}

func (r *ColumnDefinition) GetPrecision() (precision int) {
if r.precision != nil {
return *r.precision
}

Check warning on line 75 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L72-L75

Added lines #L72 - L75 were not covered by tests

return

Check warning on line 77 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L77

Added line #L77 was not covered by tests
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved

func (r *ColumnDefinition) GetTotal() (total int) {
if r.total != nil {
return *r.total
}

Check warning on line 83 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L80-L83

Added lines #L80 - L83 were not covered by tests

return 8

Check warning on line 85 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L85

Added line #L85 was not covered by tests
}

func (r *ColumnDefinition) GetType() (ttype string) {
if r.ttype != nil {
return *r.ttype
Expand Down
Loading
Loading