Class: SolidLitequeen::DatabasesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- SolidLitequeen::DatabasesController
- Defined in:
- app/controllers/solid_litequeen/databases_controller.rb
Instance Method Summary collapse
- #download ⇒ Object
- #get_command_palette_data ⇒ Object
- #get_foreign_key_data ⇒ Object
- #index ⇒ Object
- #set_column_order ⇒ Object
- #show ⇒ Object
- #table_rows ⇒ Object
Instance Method Details
#download ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 174 def download database_id = params[:database_id] database_location = Base64.urlsafe_decode64(database_id) # Ensure the database file exists unless File.exist?(database_location) flash[:error] = "Database file not found" redirect_to databases_path and return end # Create a temporary file for the backup backup_file = Tempfile.new([ "backup", ".sqlite3" ]) backup_file.close # Close the file so SQLite3 can write to it begin # Establish connection like we've been doing elsewhere DynamicDatabase.establish_connection( adapter: "sqlite3", database: database_location ) # Use VACUUM INTO for a more efficient backup DynamicDatabase.connection.execute("VACUUM INTO '#{backup_file.path}'") # Send the backup file as a download send_file backup_file.path, filename: File.basename(database_location), type: "application/x-sqlite3", disposition: "attachment" end end |
#get_command_palette_data ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 251 def get_command_palette_data palette_data = [] id_counter = 1 @available_databases ||= ActiveRecord::Base.configurations.configurations.select do |config| config.adapter == "sqlite3" && config.env_name == Rails.env && config.database.present? end @available_databases.each do |db| db_id = Base64.urlsafe_encode64(db.database) db_file_name = db.database # Add database entry palette_data << { id: id_counter, type: "database", name: db.name, database_file_name: db_file_name, rowCount: nil, path: database_path(db_id) } id_counter += 1 # Establish a connection to fetch tables for this database DynamicDatabase.establish_connection( adapter: "sqlite3", database: db.database ) DynamicDatabase.connection.tables.each do |table_name| row_count = DynamicDatabase.connection.select_value("SELECT COUNT(*) FROM #{table_name}").to_i # Add table entry palette_data << { id: id_counter, type: "table", name: table_name, database_name: db.name, database_file_name: db_file_name, rowCount: row_count, path: database_table_rows_path(db_id, table_name) } id_counter += 1 end end render json: palette_data end |
#get_foreign_key_data ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 228 def get_foreign_key_data @database_id = params.expect(:database_id) @table_name = params.expect(:table) @target_table = params.expect(:target_table) @target_field = params.expect(:target_field) @target_field_value = params.expect(:target_field_value) @database_location = Base64.urlsafe_decode64(@database_id) DynamicDatabase.establish_connection( adapter: "sqlite3", database: @database_location ) # Query the target table for the record matching the foreign key value query = "SELECT * FROM #{@target_table} WHERE #{@target_field} = ? LIMIT 1" @result = DynamicDatabase.connection.exec_query(query, "SQL", [ @target_field_value ]) render partial: "foreign-key-data" end |
#index ⇒ Object
7 8 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 7 def index end |
#set_column_order ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 207 def set_column_order table_name = params[:table] database_id = params[:database_id] column_order = params[:columnOrder] || [] database_location = Base64.urlsafe_decode64(database_id) DynamicDatabase.establish_connection( adapter: "sqlite3", database: database_location ) valid_columns = DynamicDatabase.connection.columns(table_name).map(&:name) sanitized_order = column_order & valid_columns session["#{database_id}_#{table_name}_column_order"] = sanitized_order head :ok end |
#show ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 10 def show @database_id = params.expect(:id) @database_location = Base64.urlsafe_decode64(@database_id) DynamicDatabase.establish_connection( adapter: "sqlite3", database: @database_location ) tables = DynamicDatabase.connection.tables foreign_keys = [] @tables = tables.map do |table| fk = DynamicDatabase.connection.foreign_keys(table) foreign_keys.concat(fk) unless fk.empty? row_count = DynamicDatabase.connection.select_value("SELECT COUNT(*) FROM #{table}").to_i { name: table, row_count: row_count } end tables = {} relations = [] foreign_keys.each do |rel| from_table = rel[:from_table] to_table = rel[:to_table] fk_field = rel.dig(:options).dig(:column) pk_field = rel.dig(:options).dig(:primary_key) # Initialize tables if not already present tables[from_table] ||= { name: from_table, fields: [] } tables[to_table] ||= { name: to_table, fields: [] } # Add fields if not already included tables[from_table][:fields] << fk_field unless tables[from_table][:fields].include?(fk_field) tables[to_table][:fields] << pk_field unless tables[to_table][:fields].include?(pk_field) # Build a simplified relation object relations << { from_table: from_table, from_field: fk_field, to_table: to_table, to_field: pk_field } end @table_relations = { tables: tables.values, relations: relations } end |
#table_rows ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/controllers/solid_litequeen/databases_controller.rb', line 63 def table_rows @database_id = params.expect(:database_id) @table_name = params.expect(:table) # Create a unique key for this table's sort preferences sort_key = "#{@database_id}_#{@table_name}_sort" # Update session if new sort params are provided if params[:sort_column].present? session[sort_key] = { sort_column: params[:sort_column].to_s, sort_direction: (params[:sort_direction]&.upcase == "DESC" ? "DESC" : "ASC") }.stringify_keys # Ensure all keys are strings in session end # Get sort preferences from session or set defaults with string keys sort_prefs = session[sort_key]&.with_indifferent_access || { "sort_column" => nil, "sort_direction" => nil } @sort_column = sort_prefs["sort_column"] @sort_direction = sort_prefs["sort_direction"] @database_location = Base64.urlsafe_decode64(@database_id) DynamicDatabase.establish_connection( adapter: "sqlite3", database: @database_location ) table_columns = DynamicDatabase.connection.columns(@table_name) foreign_keys = DynamicDatabase.connection.foreign_keys(@table_name) primary_key_column_name = DynamicDatabase.connection.primary_key(@table_name) # Build a mapping from column name to its foreign key details fk_info = {} foreign_keys.each do |fk| # Depending on your Rails version, you might access these properties as below: fk_info[fk.column] = { to_table: fk.to_table, primary_key: fk.primary_key, on_update: fk..dig(:on_update), on_delete: fk..dig(:on_delete) } end # Retrieve index info indexes = DynamicDatabase.connection.indexes(@table_name) index_data = {} indexes.each do |index| index.columns.each do |column_name| index_data[column_name] ||= [] index_data[column_name] << { name: index.name, unique: index.unique } end end enum_info = enum_mappings[@table_name] || {} @columns_info = table_columns.each_with_object({}) do |column, hash| info = { sql_type: column..sql_type, type: column..type, limit: column..limit, precision: column..precision, scale: column..scale, null: column.null, default: column.default, is_primary_key: primary_key_column_name == column.name } # Append foreign key info if available for this column if fk_info[column.name] info[:foreign_key] = fk_info[column.name] end # Append enum options if present info[:enum_options] = enum_info[column.name] if enum_info[column.name] # Append index info if available for this column info[:indexes] = index_data[column.name] if index_data[column.name] hash[column.name] = info end # Verify the sort column exists in the table to prevent SQL injection valid_columns = table_columns.map(&:name) stored_order = session["#{@database_id}_#{@table_name}_column_order"] || [] ordered_columns = stored_order & valid_columns ordered_columns += valid_columns - ordered_columns # Persist merged column order so future requests use the updated schema session["#{@database_id}_#{@table_name}_column_order"] = ordered_columns order_clause = if @sort_column.present? && valid_columns.include?(@sort_column) "#{DynamicDatabase.connection.quote_column_name(@sort_column)} #{@sort_direction}" end sql = [ "SELECT #{ordered_columns.join(', ')} FROM #{@table_name}" ] sql << "ORDER BY #{order_clause}" if order_clause sql << "LIMIT 50" @data = DynamicDatabase.connection.select_all(sql.join(" ")) @row_count = row_count = DynamicDatabase.connection.select_value("SELECT COUNT(*) FROM #{@table_name}").to_i end |