Database abstraction / adapters for ruby

What are the abstractions / database adapters you use in Ruby? I'm mainly interested in data-oriented functions, not those that have object mappings (like active recording or data mapping).

I am currently using Sequel. Are there any other options?

I'm most interested in:

  • simple, clean and unambiguous API
  • data selection (obviously), filtering and aggregation
  • selection of the initial value without matching fields: SELECT col1, col2, col3 => [val1, val2, val3] does not haveh from {: col1 => val1 ...}
  • be able to pass a list of columns / values ​​to select: select (array_of_columns) (not: dataset.select (: col1 ,: col2 ,: col3), which requires the columns to be known)
  • The API takes into account the "some_schema.some_table" table schemas in a sequential (and operational) way; also a reflection for this (get the diagram from the table)
  • database reflection: get a list of table columns, types of database storages, and possibly adapted abstract types.
  • table creation, deletion
  • be able to work with other tables (insert, update) in a loop, listing the selection from another table, without requiring all records from the table to be listed.

The goal is to manipulate data with an unknown structure while writing code, which is the opposite of mapping objects where the structure or most of the structure is usually well known. I do not need overhead to map objects.

What are the options, including the back-end for object mapping libraries?

+3
1

Sequel, , Ruby- , , .

, Sequel, :

  • : SELECT col1, col2, col3 = > [val1, val2, val3] hash {: col1 = > val1...}

Try:

DB[:table].filter([:col1, :col2, :col3].zip([1, 2, 3]))
# SELECT * FROM table WHERE ((col1 = 1) AND (col2 = 2) AND (col3 = 3))

, API :

DB[:table].bfilter([:col1, :col2, :col3], [1, 2, 3])
  • /, : select (array_of_columns) (not: dataset.select(: col1,: col2,: col3), , )

Try:

array_of_columns = [:col1, :col2, :col3]
DB[:table].select(*array_of_columns)
# SELECT col1, col2, col3 FROM table
  • API "some_schema.some_table" ( ) ; ( )

:

DB[:schema__table]
DB[:table.qualify(:schema)]
# SELECT * FROM schema.table

, , . , . , , , .

  • : , , , .

:

DB[:table].columns
# => [:col1, :col2, :col3]

:

DB.schema(:table)
# [[:col1=>{:type=>:integer, :db_type=>'int(11)', :default=>nil, ...}], ...]

: type - ruby: db_type - .

  • (, ) , , , .

, - :

DB[:table].each do |row|
  DB[:other_table].insert(:blah=>row[:blah])
end

Sequel - : other_table, : select on table. , :

DB = Sequel.connect(..., :servers=>{:read_only=>{}})
DB[:table].each do |row|
  DB[:other_table].insert(:blah=>row[:blah])
end

: read_only shard select : : other_table. :

DB[:table].server(:read_only).each do |row|
  DB[:other_table].server(:default).insert(:blah=>row[:blah])
end

, , , , Sequel . , , , Sequel , , Ruby- .

+5

Source: https://habr.com/ru/post/1747818/


All Articles