MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / addFieldsToProjectTable

Function addFieldsToProjectTable

cmd/cql-proxy/api/project.go:977–1046  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

975}
976
977func addFieldsToProjectTable(c *gin.Context) {
978 r := struct {
979 DB proto.DatabaseID `json:"db" json:"project" form:"db" form:"project" uri:"db" uri:"project" binding:"required,len=64"`
980 Table string `json:"table" form:"table" uri:"table" binding:"required,max=128"`
981 ColumnName string `json:"name" form:"name" binding:"required,max=32"`
982 ColumnType string `json:"type" form:"type" binding:"required,max=16"`
983 }{}
984
985 _ = c.ShouldBindUri(&r)
986
987 if err := c.ShouldBind(&r); err != nil {
988 abortWithError(c, http.StatusBadRequest, err)
989 return
990 }
991
992 _, projectDB, err := getProjectDB(c, r.DB)
993 if err != nil {
994 _ = c.Error(err)
995 abortWithError(c, http.StatusForbidden, ErrLoadProjectDatabaseFailed)
996 return
997 }
998
999 pc, ptc, err := model.GetProjectTableConfig(projectDB, r.Table)
1000 if err != nil {
1001 _ = c.Error(err)
1002 abortWithError(c, http.StatusInternalServerError, ErrGetProjectTableConfigFailed)
1003 return
1004 }
1005
1006 // find column in current column list
1007 for _, col := range ptc.Columns {
1008 if strings.EqualFold(col, r.ColumnName) {
1009 _ = c.Error(err)
1010 abortWithError(c, http.StatusBadRequest, ErrColumnAlreadyExists)
1011 return
1012 }
1013 }
1014
1015 ptc.Columns = append(ptc.Columns, r.ColumnName)
1016 ptc.Types = append(ptc.Types, r.ColumnType)
1017
1018 // execute alter table add column to database
1019 _, err = projectDB.Exec(fmt.Sprintf(
1020 `ALTER TABLE "%s" ADD COLUMN "%s" %s;`, r.Table, r.ColumnName, r.ColumnType))
1021 if err != nil {
1022 _ = c.Error(err)
1023 abortWithError(c, http.StatusInternalServerError, ErrDDLExecuteFailed)
1024 return
1025 }
1026
1027 err = model.UpdateProjectConfig(projectDB, pc)
1028 if err != nil {
1029 _ = c.Error(err)
1030 abortWithError(c, http.StatusInternalServerError, ErrUpdateProjectConfigFailed)
1031 return
1032 }
1033
1034 responseWithData(c, http.StatusOK, gin.H{

Callers

nothing calls this directly

Calls 8

GetProjectTableConfigFunction · 0.92
UpdateProjectConfigFunction · 0.92
abortWithErrorFunction · 0.85
getProjectDBFunction · 0.85
responseWithDataFunction · 0.85
formatUnixTimeFunction · 0.85
ErrorMethod · 0.80
ExecMethod · 0.65

Tested by

no test coverage detected