Skip to content

Commit

Permalink
feat: [#280] Add integer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 18, 2024
1 parent 9f4f56e commit bbac347
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 22 deletions.
30 changes: 27 additions & 3 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Blueprint interface {
// BigIncrements Create a new auto-incrementing big integer (8-byte) column on the table.
BigIncrements(column string) ColumnDefinition
// BigInteger Create a new big integer (8-byte) column on the table.
BigInteger(column string) ColumnDefinition
BigInteger(column string, config ...IntegerConfig) ColumnDefinition
// Build Execute the blueprint to build / modify the table.
Build(query orm.Query, grammar Grammar) error
// Create Indicate that the table needs to be created.
Expand All @@ -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, config ...IntegerConfig) 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
Integer(column string, config ...IntegerConfig) 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, config ...IntegerConfig) 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, config ...IntegerConfig) 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
UnsignedBigInteger(column string, autoIncrement ...bool) ColumnDefinition
// UnsignedInteger Create a new unsigned integer (4-byte) column on the table.
UnsignedInteger(column string, autoIncrement ...bool) ColumnDefinition
// UnsignedMediumInteger Create a new unsigned medium integer (3-byte) column on the table.
UnsignedMediumInteger(column string, autoIncrement ...bool) ColumnDefinition
// UnsignedSmallInteger Create a new unsigned small integer (2-byte) column on the table.
UnsignedSmallInteger(column string, autoIncrement ...bool) ColumnDefinition
// UnsignedTinyInteger Create a new unsigned tiny integer (1-byte) column on the table.
UnsignedTinyInteger(column string, autoIncrement ...bool) ColumnDefinition
}

type IndexConfig struct {
Expand Down
11 changes: 11 additions & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ 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
Nullable() ColumnDefinition
// Unsigned set the column as unsigned
Unsigned() ColumnDefinition
}

type IntegerConfig struct {
AutoIncrement bool
Unsigned bool
}
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
}
105 changes: 87 additions & 18 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,8 @@ func (r *Blueprint) BigIncrements(column string) schema.ColumnDefinition {
return r.UnsignedBigInteger(column).AutoIncrement()
}

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

r.addColumn(columnImpl)

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

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) ColumnDefinition {

}

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

missing return

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / lint

missing return

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

missing return

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / codecov

missing return

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

missing return

Check failure on line 93 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

missing return

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) ColumnDefinition {

}

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

missing return

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / lint

missing return

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

missing return

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / codecov

missing return

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

missing return

Check failure on line 105 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

missing return

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"),
}
func (r *Blueprint) Integer(column string, config ...schema.IntegerConfig) schema.ColumnDefinition {
return r.addIntegerColumn("integer", column, config...)
}

r.addColumn(columnImpl)
func (r *Blueprint) IntegerIncrements(column string) schema.ColumnDefinition {

return columnImpl
}

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

missing return

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / lint

missing return

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

missing return

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / codecov

missing return

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

missing return

Check failure on line 119 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

missing return

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

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 {

}

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

missing return

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / lint

missing return

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

missing return

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / codecov

missing return

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

missing return

Check failure on line 135 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

missing return

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

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) ColumnDefinition {

}

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

missing return

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / lint / lint

missing return

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

missing return

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / codecov

missing return

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

missing return

Check failure on line 159 in database/schema/blueprint.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

missing return

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

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

Expand Down Expand Up @@ -172,8 +190,39 @@ func (r *Blueprint) ToSql(grammar schema.Grammar) []string {
return statements
}

func (r *Blueprint) UnsignedBigInteger(column string) schema.ColumnDefinition {
return r.BigInteger(column).Unsigned()
func (r *Blueprint) UnsignedBigInteger(column string, autoIncrement ...bool) schema.ColumnDefinition {
columnDefinition := r.BigInteger(column).Unsigned()
if len(autoIncrement) > 0 && autoIncrement[0] {
columnDefinition.AutoIncrement()
}

return columnDefinition
}

func (r *Blueprint) UnsignedInteger(column string, autoIncrement ...bool) schema.ColumnDefinition {
columnDefinition := r.Integer(column).Unsigned()
if len(autoIncrement) > 0 && autoIncrement[0] {
columnDefinition.AutoIncrement()
}

return columnDefinition
}

func (r *Blueprint) UnsignedMediumInteger(column string, autoIncrement ...bool) schema.ColumnDefinition {
columnDefinition := r.Integer(column).Unsigned()
if len(autoIncrement) > 0 && autoIncrement[0] {
columnDefinition.AutoIncrement()
}

return columnDefinition
}

func (r *Blueprint) UnsignedSmallInteger(column string, autoIncrement ...bool) schema.ColumnDefinition {

}

func (r *Blueprint) UnsignedTinyInteger(column string, autoIncrement ...bool) schema.ColumnDefinition {

}

func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) {
Expand Down Expand Up @@ -209,6 +258,26 @@ func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) {
r.addAttributeCommands(grammar)
}

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

if len(config) > 0 {
if config[0].AutoIncrement {
columnImpl.AutoIncrement()
}
if config[0].Unsigned {
columnImpl.Unsigned()
}
}

r.addColumn(columnImpl)

return columnImpl
}

func (r *Blueprint) createIndexName(ttype string, columns []string) string {
var table string
if strings.Contains(r.table, ".") {
Expand Down
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
29 changes: 29 additions & 0 deletions database/schema/grammars/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,39 @@ func (r *Mysql) TypeBigInteger(column schema.ColumnDefinition) string {
return "bigint"
}

func (r *Mysql) TypeDecimal(column schema.ColumnDefinition) string {
return fmt.Sprintf("decimal(%d, %d)", column.GetTotal(), column.GetPlaces())
}

func (r *Mysql) TypeDouble() string {
return "double"
}

func (r *Mysql) TypeFloat(column schema.ColumnDefinition) string {
precision := column.GetPrecision()
if precision > 0 {
return fmt.Sprintf("float(%d)", precision)
}

return "float"
}

func (r *Mysql) TypeInteger(column schema.ColumnDefinition) string {
return "int"
}

func (r *Mysql) TypeMediumInteger(column schema.ColumnDefinition) string {
return "mediumint"
}

func (r *Mysql) TypeTinyInteger(column schema.ColumnDefinition) string {
return "tinyint"
}

func (r *Mysql) TypeSmallInteger(column schema.ColumnDefinition) string {
return "smallint"
}

func (r *Mysql) TypeString(column schema.ColumnDefinition) string {
length := column.GetLength()
if length > 0 {
Expand Down
33 changes: 33 additions & 0 deletions database/schema/grammars/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ func (r *Postgres) TypeBigInteger(column schema.ColumnDefinition) string {
return "bigint"
}

func (r *Postgres) TypeDecimal(column schema.ColumnDefinition) string {
return fmt.Sprintf("decimal(%d, %d)", column.GetTotal(), column.GetPlaces())
}

func (r *Postgres) TypeDouble() string {
return "double precision"
}

func (r *Postgres) TypeFloat(column schema.ColumnDefinition) string {
precision := column.GetPrecision()
if precision > 0 {
return fmt.Sprintf("float(%d)", precision)
}

return "float"
}

func (r *Postgres) TypeInteger(column schema.ColumnDefinition) string {
if column.GetAutoIncrement() {
return "serial"
Expand All @@ -196,6 +213,22 @@ func (r *Postgres) TypeInteger(column schema.ColumnDefinition) string {
return "integer"
}

func (r *Postgres) TypeMediumInteger(column schema.ColumnDefinition) string {
return r.TypeInteger(column)
}

func (r *Postgres) TypeTinyInteger(column schema.ColumnDefinition) string {
return r.TypeSmallInteger(column)
}

func (r *Postgres) TypeSmallInteger(column schema.ColumnDefinition) string {
if column.GetAutoIncrement() {
return "smallserial"
}

return "smallint"
}

func (r *Postgres) TypeString(column schema.ColumnDefinition) string {
length := column.GetLength()
if length > 0 {
Expand Down
Loading

0 comments on commit bbac347

Please sign in to comment.