Skip to content

Commit

Permalink
feat: [#280] Add integer methods (#727)
Browse files Browse the repository at this point in the history
* feat: [#280] Add integer methods

* chore: update mocks

* optimize

* chore: update mocks

* optimize

* chore: update mocks

* optimize

* Add test cases

* Optimize test cases

---------

Co-authored-by: hwbrzzl <hwbrzzl@users.noreply.github.com>
  • Loading branch information
hwbrzzl and hwbrzzl authored Nov 19, 2024
1 parent 9f4f56e commit fb00d2f
Show file tree
Hide file tree
Showing 17 changed files with 1,393 additions and 17 deletions.
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
// 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
}

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
// 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
// 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) BigIncrements(column string) schema.ColumnDefinition {
}

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 @@ func (r *Blueprint) HasCommand(command string) bool {
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 @@ func (r *Blueprint) ID(column ...string) schema.ColumnDefinition {
return r.BigIncrements("id")
}

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

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 @@ func (r *Blueprint) SetTable(name string) {
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 @@ func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition
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 @@ func (r *Blueprint) UnsignedBigInteger(column string) schema.ColumnDefinition {
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 @@ func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) {
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 @@ type ColumnDefinition struct {
length *int
name *string
nullable *bool
places *int
precision *int
total *int
ttype *string
unsigned *bool
}
Expand Down Expand Up @@ -58,6 +61,30 @@ func (r *ColumnDefinition) GetNullable() bool {
return false
}

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

return 2
}

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

return
}

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

return 8
}

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

0 comments on commit fb00d2f

Please sign in to comment.