Skip to content

Commit

Permalink
feat: Add columns to table (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTekken authored Oct 11, 2022
1 parent b156224 commit 4bc0c63
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
15 changes: 15 additions & 0 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ func DropTable(tableName string) error {
func GetSQLQuery(tableName string) (string, error) {
return "", nil
}

/*
AddColumn adds a column to a specified table.
@param tableName - name of the table
@param columnName - name of column to be added
@param dataType - field type of the new column
*/
func AddColumn(tableName string, columnName string, dataType string) error {
_, err := DB.Exec(fmt.Sprintf("ALTER TABLE '%s' ADD '%s' '%s'", tableName, columnName, dataType))
if err != nil {
return err
}
return nil
}
22 changes: 22 additions & 0 deletions routes/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routes

import (
"log"
"fmt"

"github.com/gofiber/fiber/v2"
"github.com/shivansh-yadav13/sqlite-web/db"
Expand Down Expand Up @@ -81,3 +82,24 @@ func DropTable(c *fiber.Ctx) error {

return c.RedirectBack("/")
}

/*
AddColumn takes the table name from the post request
and calls the AddColumn of the db package.
*/
func AddColumn(c *fiber.Ctx) error {
table := TableStruct{}
column := ColumnStruct{}
if err := c.BodyParser(&table); err != nil {
return err
}
if err := c.BodyParser(&column); err != nil {
return err
}
if err := db.AddColumn(table.Name, column.Name, column.Type); err != nil {
log.Fatalln(err)
}

return c.RedirectBack(fmt.Sprintf("/%s", table.Name))
}
5 changes: 5 additions & 0 deletions routes/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ package routes
type TableStruct struct {
Name string `json:"table_name" form:"table_name"`
}
// Table struct helps to bind the post requests to the API.
type ColumnStruct struct {
Name string `json:"column_name" form:"column_name"`
Type string `json:"column_type" form:"column_type"`
}
2 changes: 1 addition & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func SetRoutes(app *fiber.App) {
app.Get("/get-tables", GetTables)
app.Post("/create-table", CreateTable)
app.Post("/drop-table", DropTable)
// app.Post("/add-col", AddCol)
app.Post("/add-column", AddColumn)
// app.Post("add")
}
14 changes: 14 additions & 0 deletions templates/table_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,19 @@
</head>
<body>
{{.table_name}}
<form action="/add-column" method="post">
<label for="table_name">&#43; Add Column</label>
<input type="hidden" id="table_name" name="table_name" placeholder="table-name" value="{{.table_name}}"/>
<input type="text" id="column_name" name="column_name" placeholder="column-name"/>
<label for="data_type"></label>
<select name="column_type">
<option value="CHARACTER(20)">CHARACTER(20)</option>
<option value="VARCHAR(255)">VARCHAR(255)</option>
<option value="INTEGER">INTEGER</option>
<option value="FLOAT">FLOAT</option>
<option value="BOOLEAN">BOOLEAN</option>
</select>
<button type="submit">Create</button>
</form>
</body>
</html>

0 comments on commit 4bc0c63

Please sign in to comment.