github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/introspection/connection_table_sql.go (about) 1 package introspection 2 3 import ( 4 "fmt" 5 6 "github.com/turbot/steampipe/pkg/constants" 7 "github.com/turbot/steampipe/pkg/db/db_common" 8 "github.com/turbot/steampipe/pkg/steampipeconfig" 9 "github.com/turbot/steampipe/pkg/steampipeconfig/modconfig" 10 "golang.org/x/exp/maps" 11 ) 12 13 func GetConnectionStateTableDropSql() []db_common.QueryWithArgs { 14 queryFormat := `DROP TABLE IF EXISTS %s.%s;` 15 return getConnectionStateQueries(queryFormat, nil) 16 } 17 18 func GetConnectionStateTableCreateSql() []db_common.QueryWithArgs { 19 queryFormat := `CREATE TABLE IF NOT EXISTS %s.%s ( 20 name TEXT PRIMARY KEY, 21 state TEXT, 22 type TEXT NULL, 23 connections TEXT[] NULL, 24 import_schema TEXT, 25 error TEXT NULL, 26 plugin TEXT, 27 plugin_instance TEXT NULL, 28 schema_mode TEXT, 29 schema_hash TEXT NULL, 30 comments_set BOOL DEFAULT FALSE, 31 connection_mod_time TIMESTAMPTZ, 32 plugin_mod_time TIMESTAMPTZ, 33 file_name TEXT, 34 start_line_number INTEGER, 35 end_line_number INTEGER 36 );` 37 return getConnectionStateQueries(queryFormat, nil) 38 } 39 40 // GetConnectionStateTableGrantSql returns the sql to setup SELECT permission for the 'steampipe_users' role 41 func GetConnectionStateTableGrantSql() []db_common.QueryWithArgs { 42 queryFormat := fmt.Sprintf( 43 `GRANT SELECT ON TABLE %%s.%%s TO %s;`, 44 constants.DatabaseUsersRole, 45 ) 46 return getConnectionStateQueries(queryFormat, nil) 47 } 48 49 // GetConnectionStateErrorSql returns the sql to set a connection to 'error' 50 func GetConnectionStateErrorSql(connectionName string, err error) []db_common.QueryWithArgs { 51 queryFormat := fmt.Sprintf(`UPDATE %%s.%%s 52 SET state = '%s', 53 error = $1, 54 connection_mod_time = now() 55 WHERE 56 name = $2 57 `, constants.ConnectionStateError) 58 59 args := []any{err.Error(), connectionName} 60 return getConnectionStateQueries(queryFormat, args) 61 } 62 63 // GetIncompleteConnectionStateErrorSql returns the sql to set all incomplete connections to 'error' (unless they alre already in error) 64 func GetIncompleteConnectionStateErrorSql(err error) []db_common.QueryWithArgs { 65 queryFormat := fmt.Sprintf(`UPDATE %%s.%%s 66 SET state = '%s', 67 error = $1, 68 connection_mod_time = now() 69 WHERE 70 state <> 'ready' 71 AND state <> 'disabled' 72 AND state <> 'error' 73 `, 74 constants.ConnectionStateError) 75 args := []any{err.Error()} 76 return getConnectionStateQueries(queryFormat, args) 77 } 78 79 // GetUpsertConnectionStateSql returns the sql to update the connection state in the able with the current properties 80 func GetUpsertConnectionStateSql(c *steampipeconfig.ConnectionState) []db_common.QueryWithArgs { 81 // upsert 82 queryFormat := `INSERT INTO %s.%s (name, 83 state, 84 type, 85 connections, 86 import_schema, 87 error, 88 plugin, 89 plugin_instance, 90 schema_mode, 91 schema_hash, 92 comments_set, 93 connection_mod_time, 94 plugin_mod_time, 95 file_name, 96 start_line_number, 97 end_line_number) 98 VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,now(),$12,$13,$14,$15) 99 ON CONFLICT (name) 100 DO 101 UPDATE SET 102 state = $2, 103 type = $3, 104 connections = $4, 105 import_schema = $5, 106 error = $6, 107 plugin = $7, 108 plugin_instance = $8, 109 schema_mode = $9, 110 schema_hash = $10, 111 comments_set = $11, 112 connection_mod_time = now(), 113 plugin_mod_time = $12, 114 file_name = $13, 115 start_line_number = $14, 116 end_line_number = $15 117 118 ` 119 args := []any{ 120 c.ConnectionName, 121 c.State, 122 c.Type, 123 c.Connections, 124 c.ImportSchema, 125 c.ConnectionError, 126 c.Plugin, 127 c.PluginInstance, 128 c.SchemaMode, 129 c.SchemaHash, 130 c.CommentsSet, 131 c.PluginModTime, 132 c.FileName, 133 c.StartLineNumber, 134 c.EndLineNumber, 135 } 136 return getConnectionStateQueries(queryFormat, args) 137 } 138 139 func GetNewConnectionStateFromConnectionInsertSql(c *modconfig.Connection) []db_common.QueryWithArgs { 140 queryFormat := `INSERT INTO %s.%s (name, 141 state, 142 type, 143 connections, 144 import_schema, 145 error, 146 plugin, 147 plugin_instance, 148 schema_mode, 149 schema_hash, 150 comments_set, 151 connection_mod_time, 152 plugin_mod_time, 153 file_name, 154 start_line_number, 155 end_line_number) 156 VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,now(),now(),$12,$13,$14) 157 ` 158 schemaMode := "" 159 commentsSet := false 160 schemaHash := "" 161 162 args := []any{ 163 c.Name, 164 constants.ConnectionStatePendingIncomplete, 165 c.Type, 166 maps.Keys(c.Connections), 167 c.ImportSchema, 168 nil, 169 c.Plugin, 170 c.PluginInstance, 171 schemaMode, 172 schemaHash, 173 commentsSet, 174 c.DeclRange.Filename, 175 c.DeclRange.Start.Line, 176 c.DeclRange.End.Line, 177 } 178 179 return getConnectionStateQueries(queryFormat, args) 180 } 181 182 func GetSetConnectionStateSql(connectionName string, state string) []db_common.QueryWithArgs { 183 queryFormat := fmt.Sprintf(`UPDATE %%s.%%s 184 SET state = '%s', 185 connection_mod_time = now() 186 WHERE 187 name = $1 188 `, state) 189 190 args := []any{connectionName} 191 return getConnectionStateQueries(queryFormat, args) 192 } 193 194 func GetDeleteConnectionStateSql(connectionName string) []db_common.QueryWithArgs { 195 queryFormat := `DELETE FROM %s.%s WHERE NAME=$1` 196 args := []any{connectionName} 197 return getConnectionStateQueries(queryFormat, args) 198 } 199 200 func GetSetConnectionStateCommentLoadedSql(connectionName string, commentsLoaded bool) []db_common.QueryWithArgs { 201 queryFormat := `UPDATE %s.%s 202 SET comments_set = $1 203 WHERE NAME=$2` 204 args := []any{commentsLoaded, connectionName} 205 return getConnectionStateQueries(queryFormat, args) 206 } 207 208 func getConnectionStateQueries(queryFormat string, args []any) []db_common.QueryWithArgs { 209 query := fmt.Sprintf(queryFormat, constants.InternalSchema, constants.ConnectionTable) 210 legacyQuery := fmt.Sprintf(queryFormat, constants.InternalSchema, constants.LegacyConnectionStateTable) 211 return []db_common.QueryWithArgs{ 212 {Query: query, Args: args}, 213 {Query: legacyQuery, Args: args}, 214 } 215 }