class DuckDB::Connection
The DuckDB::Connection
encapsulates connection with DuckDB
database.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query(sql)
Public Instance Methods
Source
# File lib/duckdb/connection.rb, line 130 def appender(table) appender = create_appender(table) return appender unless block_given? yield appender appender.flush appender.close end
returns Appender
object. The first argument is table name
Source
# File lib/duckdb/connection.rb, line 61 def async_query(sql, *args, **kwargs) prepare(sql) do |stmt| stmt.bind_args(*args, **kwargs) stmt.pending_prepared end end
executes sql with args asynchronously. The first argument sql must be SQL string. The rest arguments are parameters of SQL string. This method returns DuckDB::PendingResult
object.
require 'duckdb' db = DuckDB::Database.open('duckdb_file') con = db.connect sql = 'SELECT * FROM users WHERE name = $name AND email = $email' pending_result = con.async_query(sql, name: 'Dave', email: 'dave@example.com') pending_result.execute_task while pending_result.state == :not_ready result = pending_result.execute_pending result.each.first
Source
# File lib/duckdb/connection.rb, line 83 def async_query_stream(sql, *args, **kwargs) prepare(sql) do |stmt| stmt.bind_args(*args, **kwargs) stmt.pending_prepared_stream end end
executes sql with args asynchronously and provides streaming result. The first argument sql must be SQL string. The rest arguments are parameters of SQL string. This method returns DuckDB::PendingResult
object.
require 'duckdb' db = DuckDB::Database.open('duckdb_file') con = db.connect sql = 'SELECT * FROM users WHERE name = $name AND email = $email' pending_result = con.async_query_stream(sql, name: 'Dave', email: 'dave@example.com') pending_result.execute_task while pending_result.state == :not_ready result = pending_result.execute_pending result.each.first
Source
# File lib/duckdb/connection.rb, line 92 def connect(db) conn = _connect(db) return conn unless block_given? begin yield conn ensure conn.disconnect end end
connects DuckDB
database The first argument is DuckDB::Database
object
Source
static VALUE duckdb_connection_disconnect(VALUE self) { rubyDuckDBConnection *ctx; TypedData_Get_Struct(self, rubyDuckDBConnection, &connection_data_type, ctx); duckdb_disconnect(&(ctx->con)); return self; }
Source
static VALUE duckdb_connection_interrupt(VALUE self) { rubyDuckDBConnection *ctx; TypedData_Get_Struct(self, rubyDuckDBConnection, &connection_data_type, ctx); duckdb_interrupt(ctx->con); return Qnil; }
Interrupts the currently running query.
db = DuckDB::Database.open conn = db.connect con.query('SET ENABLE_PROGRESS_BAR=true') con.query('SET ENABLE_PROGRESS_BAR_PRINT=false') pending_result = con.async_query('slow query') pending_result.execute_task con.interrupt # => nil
Source
# File lib/duckdb/connection.rb, line 122 def prepared_statement(str, &) return PreparedStatement.new(self, str) unless block_given? PreparedStatement.prepare(self, str, &) end
returns PreparedStatement
object. The first argument is SQL string. If block is given, the block is executed with PreparedStatement
object and the object is cleaned up immediately.
require 'duckdb' db = DuckDB::Database.open('duckdb_file') con = db.connect sql = 'SELECT * FROM users WHERE name = $name AND email = $email' stmt = con.prepared_statement(sql) stmt.bind_args(name: 'Dave', email: 'dave@example.com') result = stmt.execute # or result = con.prepared_statement(sql) do |stmt| stmt.bind_args(name: 'Dave', email: 'dave@example.com') stmt.execute end
Source
# File lib/duckdb/connection.rb, line 26 def query(sql, *args, **kwargs) return query_multi_sql(sql) if args.empty? && kwargs.empty? prepare(sql) do |stmt| stmt.bind_args(*args, **kwargs) stmt.execute end end
executes sql with args. The first argument sql must be SQL string. The rest arguments are parameters of SQL string.
require 'duckdb' db = DuckDB::Database.open('duckdb_file') con = db.connect users = con.query('SELECT * FROM users') sql = 'SELECT * FROM users WHERE name = ? AND email = ?' dave = con.query(sql, 'Dave', 'dave@example.com') # or You can use named parameter. sql = 'SELECT * FROM users WHERE name = $name AND email = $email' dave = con.query(sql, name: 'Dave', email: 'dave@example.com')
Source
# File lib/duckdb/connection.rb, line 35 def query_multi_sql(sql) stmts = ExtractedStatements.new(self, sql) result = nil stmts.each do |stmt| result = stmt.execute stmt.destroy end result ensure stmts&.destroy end
Source
static VALUE duckdb_connection_query_progress(VALUE self) { rubyDuckDBConnection *ctx; duckdb_query_progress_type progress; TypedData_Get_Struct(self, rubyDuckDBConnection, &connection_data_type, ctx); progress = duckdb_query_progress(ctx->con); return rb_funcall(mDuckDBConverter, rb_intern("_to_query_progress"), 3, DBL2NUM(progress.percentage), ULL2NUM(progress.rows_processed), ULL2NUM(progress.total_rows_to_process)); }
Returns the progress of the currently running query.
require 'duckdb' db = DuckDB::Database.open conn = db.connect con.query('SET ENABLE_PROGRESS_BAR=true') con.query('SET ENABLE_PROGRESS_BAR_PRINT=false') con.query_progress # => -1.0 pending_result = con.async_query('slow query') con.query_progress # => 0.0 pending_result.execute_task con.query_progress # => Float