Module: Simple::SQL
- Extended by:
- Forwardable, SQL
- Included in:
- SQL
- Defined in:
- lib/simple/sql.rb,
lib/simple/sql/version.rb
Overview
The Simple::SQL module
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Method Summary collapse
-
#all(sql, *args, &block) ⇒ Object
Runs a query, with optional arguments, and returns the result.
-
#ask(sql, *args) ⇒ Object
Runs a query and returns the first result row of a query.
- #connect!(url) ⇒ Object
- #db_config_from_url(url) ⇒ Object
-
#record(sql, *args, into: Hash) ⇒ Object
Runs a query and returns the first result row of a query as a Hash.
-
#records(sql, *args, into: Hash, &block) ⇒ Object
Runs a query, with optional arguments, and returns the result as an array of Hashes.
Instance Method Details
#all(sql, *args, &block) ⇒ Object
Runs a query, with optional arguments, and returns the result. If the SQL query returns rows with one column, this method returns an array of these values. Otherwise it returns an array of arrays.
Example:
-
Simple::SQL.all("SELECT id FROM users")returns an array of id values -
Simple::SQL.all("SELECT id, email FROM users")returns an array ofarrays `[ <id>, <email> ]`.
Simple::SQL.all “SELECT id, email FROM users” do |id, email|
# do something
end
30 31 32 33 34 35 |
# File 'lib/simple/sql.rb', line 30 def all(sql, *args, &block) result = connection.exec_params(sql, Encoder.encode_args(args)) decoder = Decoder.new(result) enumerate(result, decoder, block) end |
#ask(sql, *args) ⇒ Object
Runs a query and returns the first result row of a query.
Examples:
-
Simple::SQL.ask "SELECT id FROM users WHERE email=$?", "foo@local"returns a number (ornil) -
Simple::SQL.ask "SELECT id, email FROM users WHERE email=$?", "foo@local"returns an array[ <id>, <email> ](ornil)
45 46 47 48 49 50 |
# File 'lib/simple/sql.rb', line 45 def ask(sql, *args) catch(:ok) do all(sql, *args) { |row| throw :ok, row } nil end end |
#connect!(url) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/simple/sql.rb', line 127 def connect!(url) require "pg" config = db_config_from_url(url) connection = PG::Connection.new(config) self.connector = lambda { connection } end |
#db_config_from_url(url) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/simple/sql.rb', line 135 def db_config_from_url(url) require "uri" raise ArgumentError, "Invalid URL #{url.inspect}" unless url.is_a?(String) raise ArgumentError, "Invalid URL #{url.inspect}" unless url =~ /^postgres(ql)?s?:\/\// uri = URI.parse(url) raise ArgumentError, "Invalid URL #{url}" unless uri.hostname && uri.path config = { dbname: uri.path.sub(%r{^/}, ""), host: uri.hostname } config[:port] = uri.port if uri.port config[:user] = uri.user if uri.user config[:password] = uri.password if uri.password config[:sslmode] = uri.scheme == "postgress" || uri.scheme == "postgresqls" ? "require" : "prefer" config end |
#record(sql, *args, into: Hash) ⇒ Object
Runs a query and returns the first result row of a query as a Hash.
72 73 74 75 76 77 |
# File 'lib/simple/sql.rb', line 72 def record(sql, *args, into: Hash) catch(:ok) do records(sql, *args, into: into) { |row| throw :ok, row } nil end end |
#records(sql, *args, into: Hash, &block) ⇒ Object
Runs a query, with optional arguments, and returns the result as an array of Hashes.
Example:
-
Simple::SQL.records("SELECT id, email FROM users")returns an array ofhashes { id: id, email: email }
Simple::SQL.records “SELECT id, email FROM users” do |record|
# do something
end
64 65 66 67 68 69 |
# File 'lib/simple/sql.rb', line 64 def records(sql, *args, into: Hash, &block) result = connection.exec_params(sql, Encoder.encode_args(args)) decoder = Decoder.new(result, :record, into: into) enumerate(result, decoder, block) end |