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

Feature/support database schema #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

1saifj
Copy link

@1saifj 1saifj commented Oct 20, 2024

this fixes #74

@1saifj
Copy link
Author

1saifj commented Oct 20, 2024

@zhufuyi Could you please review this?
Additionally, where can I update the "tip information" displayed when clicking on the "!" icon?

@zhufuyi
Copy link
Owner

zhufuyi commented Oct 21, 2024

Thank you for your PR, yesterday I added the following code to the file cmd/sponge/server/handler.go, which supports specifying a schema in postgresql dsn and only lists tables under that schema. If no schema is specified, all tables under the schema will be listed.

type pgSchema struct {
       SchemaName string
}

type pgTable struct {
       TableName string
}

func getSchemas(db *ggorm.DB, dsn string) ([]pgSchema, error) {
       var schemas []pgSchema

       if strings.Contains(dsn, "search_path=") {
               ss := strings.Split(dsn, " ")
               for _, s := range ss {
                       if strings.Contains(s, "search_path=") {
                               schemaName := strings.Split(s, "=")[1]
                               if schemaName != "" {
                                       schemas = append(schemas, pgSchema{SchemaName: schemaName})
                               }
                       }
               }
       }

       if len(schemas) != 0 {
               return schemas, nil
       }

       err := db.Raw("SELECT schema_name FROM information_schema.schemata").Scan(&schemas).Error
       if err != nil {
               return nil, err
       }
       return schemas, nil
}

func getSchemaTables(db *ggorm.DB, schemas []pgSchema) ([]string, error) {
       var schemaTables []string
       for _, schema := range schemas {
               if schema.SchemaName == "information_schema" || schema.SchemaName == "pg_catalog" || schema.SchemaName == "pg_toast" {
                       continue
               }

               var tables []pgTable
               err := db.Raw("SELECT table_name FROM information_schema.tables WHERE table_schema = ?", schema.SchemaName).Scan(&tables).Error
               if err != nil {
                       return nil, err
               }

               for _, table := range tables {
                       schemaTables = append(schemaTables, table.TableName)
               }
       }
       return schemaTables, nil
}

@1saifj
Copy link
Author

1saifj commented Oct 21, 2024

@zhufuyi excellent, you can go forward to edit my code with your:

func getSchemaTables(db *ggorm.DB, schemas []pgSchema) ([]string, error) 

as it is more needed for this aspect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support selecting a PostgreSQL DB schema if found
2 participants