Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
judell committed Sep 10, 2023
1 parent 61c3595 commit 0728d3a
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions odbc/table_code_odbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"

)
func constructColumnsFromNames(columnNames []string) []*plugin.Column {
cols := make([]*plugin.Column, len(columnNames))
for i, columnName := range columnNames {
cols[i] = &plugin.Column{
Name: columnName,
Type: proto.ColumnType_STRING, // Assuming string type for simplicity; a more robust approach would inspect column types
Description: columnName,
Transform: transform.FromField(helpers.EscapePropertyName(columnName)),
}
}
return cols
}

func getSchema(ctx context.Context) ([]*plugin.Column, error) {
plugin.Logger(ctx).Debug("odbc.getSchema")
// Check if schema file exists
if _, err := os.Stat("/tmp/schema_cache"); err == nil {
// Read the schema from the file
Expand All @@ -30,18 +42,8 @@ func getSchema(ctx context.Context) ([]*plugin.Column, error) {
return nil, err
}

// Reconstruct the columns
cols := make([]*plugin.Column, len(columnNames))
for i, columnName := range columnNames {
cols[i] = &plugin.Column{
Name: columnName,
Type: proto.ColumnType_STRING, // Assuming string type for simplicity; a more robust approach would inspect column types
Description: columnName,
Transform: transform.FromField(helpers.EscapePropertyName(columnName)),
}
}
plugin.Logger(ctx).Debug("odbc.getSchema return existing", "cols", cols)
return cols, nil
// Use the shared function to construct columns
return constructColumnsFromNames(columnNames), nil
}

// If the file doesn't exist, fetch the schema from the database
Expand All @@ -62,27 +64,19 @@ func getSchema(ctx context.Context) ([]*plugin.Column, error) {
return nil, err
}

cols := make([]*plugin.Column, len(columnNames))
for i, columnName := range columnNames {
cols[i] = &plugin.Column{
Name: columnName,
Type: proto.ColumnType_STRING, // Assuming string type for simplicity; a more robust approach would inspect column types
Description: columnName,
Transform: transform.FromField(helpers.EscapePropertyName(columnName)),
}
}

// Now, serialize columnNames and save to /tmp/schema_cache
jsonData, err := json.Marshal(columnNames)
if err != nil {
return nil, err
}
err = os.WriteFile("/tmp/schema_cache", jsonData, 0644)
plugin.Logger(ctx).Debug("odbc.getSchema return new cols", "cols", cols)
return cols, err

// Use the shared function to construct columns
return constructColumnsFromNames(columnNames), err
}



func tableODBC(ctx context.Context, connection *plugin.Connection) (*plugin.Table, error) {
plugin.Logger(ctx).Debug("tableODBC")

Expand Down

0 comments on commit 0728d3a

Please sign in to comment.