Module: Kernel
- Defined in:
- lib/arql/ext/kernel.rb,
lib/arql/commands/table.rb,
lib/arql/commands/models.rb
Constant Summary collapse
- CSV_BOM =
"\xef\xbb\xbf"
Instance Method Summary collapse
- #generate_csv(filename, **options, &block) ⇒ Object
- #generate_excel(filename) ⇒ Object
- #models ⇒ Object
- #parse_csv(filename, **options) ⇒ Object
- #parse_excel(filename) ⇒ Object
- #print_tables(format = :md) ⇒ Object
- #sql(sql) ⇒ Object
- #table(name) ⇒ Object
- #tables ⇒ Object
Instance Method Details
#generate_csv(filename, **options, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/arql/ext/kernel.rb', line 68 def generate_csv(filename, **, &block) opts = { col_sep: "\t", row_sep: "\r\n" } opts.merge!(.except(:encoding)) encoding = [:encoding] || 'UTF-16LE' File.open(File.(filename), "w:#{encoding}") do |file| file.write(CSV_BOM) file.write CSV.generate(**opts, &block) end end |
#generate_excel(filename) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/arql/ext/kernel.rb', line 92 def generate_excel(filename) Axlsx::Package.new do |package| yield(package.workbook) package.serialize(filename) end end |
#models ⇒ Object
37 38 39 |
# File 'lib/arql/commands/models.rb', line 37 def models Arql::Commands::Models::models end |
#parse_csv(filename, **options) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/arql/ext/kernel.rb', line 81 def parse_csv(filename, **) encoding = [:encoding] || 'UTF-16' opts = { headers: false, col_sep: "\t", row_sep: "\r\n" } opts.merge!(.except(:encoding)) CSV.parse(IO.read(File.(filename), encoding: encoding, binmode: true).encode('UTF-8'), **opts).to_a end |
#parse_excel(filename) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/arql/ext/kernel.rb', line 99 def parse_excel(filename) xlsx = Roo::Excelx.new(File.(filename)) xlsx.sheets.each_with_object({}) do |sheet_name, result| begin result[sheet_name] = xlsx.sheet(sheet_name).to_a rescue end end end |
#print_tables(format = :md) ⇒ Object
8 9 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 62 63 64 65 66 |
# File 'lib/arql/ext/kernel.rb', line 8 def print_tables(format = :md) require 'terminal-table' tables = ActiveRecord::Base.connection.tables.map do |table_name| { table: table_name, table_comment: ActiveRecord::Base.connection.table_comment(table_name) || '', columns: ::ActiveRecord::Base.connection.columns(table_name) } end outputs = tables.map do |table| table_name = table[:table] table_comment = table[:table_comment] case format when :md "# #{table_name} #{table_comment}\n\n" + Terminal::Table.new { |t| t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment'] t.rows = table[:columns].map { |column| pk = if column.name == ::ActiveRecord::Base.connection.primary_key(table_name) 'Y' else '' end [pk, "`#{column.name}`", column.sql_type, column..limit || '', column..precision || '', column..scale || '', column.default || '', column.null, column.comment || ''] } t.style = { border_top: false, border_bottom: false, border_i: '|' } }.to_s.lines.map { |l| ' ' + l }.join when :org "* #{table_name} #{table_comment}\n\n" + Terminal::Table.new { |t| t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment'] t.rows = table[:columns].map { |column| pk = if column.name == ::ActiveRecord::Base.connection.primary_key(table_name) 'Y' else '' end [pk, "=#{column.name}=", column.sql_type, column..limit || '', column..precision || '', column..scale || '', column.default || '', column.null, column.comment || ''] } t.style = { border_top: false, border_bottom: false, } }.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join when :sql "-- Table: #{table_name}\n\n" + ActiveRecord::Base.connection.exec_query("show create table `#{table_name}`").rows.last.last + ';' end end outputs.each { |out| puts out; puts } end |
#sql(sql) ⇒ Object
4 5 6 |
# File 'lib/arql/ext/kernel.rb', line 4 def sql(sql) ActiveRecord::Base.connection.exec_query(sql) end |
#table(name) ⇒ Object
52 53 54 |
# File 'lib/arql/commands/table.rb', line 52 def table(name) Arql::Commands::Table::table_info(Arql::Commands::Table::get_table_name(name)) end |
#tables ⇒ Object
41 42 43 |
# File 'lib/arql/commands/models.rb', line 41 def tables models end |