Class: ChDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/chdb/database.rb

Overview

Represents a database connection and provides methods to interact with the database.

Constant Summary collapse

@@instance =

rubocop:disable Style/ClassVars

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Database

rubocop:disable Metrics/MethodLength

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chdb/database.rb', line 40

def initialize(file, options = {}) # rubocop:disable Metrics/MethodLength
  raise InternalException, 'Existing database instance is not closed' if @@instance

  file = file.to_path if file.respond_to? :to_path

  @data_path = DataPath.new(file, options)
  @results_as_hash = @data_path.query_params['results_as_hash'] || false
  @readonly = @data_path.mode & Constants::Open::READONLY != 0

  argv = @data_path.generate_arguments
  @conn = ChDB::Connection.new(argv.size, argv)
  @closed = false

  @@instance = self # rubocop:disable Style/ClassVars

  return unless block_given?

  begin
    yield self
  ensure
    close
  end
end

Instance Attribute Details

#connObject

A boolean that indicates whether rows in result sets should be returned as hashes or not. By default, rows are returned as arrays.



38
39
40
# File 'lib/chdb/database.rb', line 38

def conn
  @conn
end

#results_as_hashObject

A boolean that indicates whether rows in result sets should be returned as hashes or not. By default, rows are returned as arrays.



38
39
40
# File 'lib/chdb/database.rb', line 38

def results_as_hash
  @results_as_hash
end

Class Method Details

.open(*args) ⇒ Object

Without block works exactly as new. With block, like new closes the database at the end, but unlike new returns the result of the block instead of the database instance.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chdb/database.rb', line 21

def open(*args)
  database = new(*args)

  if block_given?
    begin
      yield database
    ensure
      database.close
    end
  else
    database
  end
end

Instance Method Details

#build_result_set(stmt) ⇒ Object

Given a statement, return a result set. This is not intended for general consumption :nodoc:



153
154
155
156
157
158
159
# File 'lib/chdb/database.rb', line 153

def build_result_set(stmt)
  if results_as_hash
    HashResultSet.new(self, stmt)
  else
    ResultSet.new(self, stmt)
  end
end

#closeObject



64
65
66
67
68
69
70
71
72
# File 'lib/chdb/database.rb', line 64

def close
  return if defined?(@closed) && @closed

  @data_path.close if @data_path.respond_to?(:close)
  @conn.close if @conn.respond_to?(:close)
  @closed = true

  @@instance = nil # rubocop:disable Style/ClassVars
end

#closed?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/chdb/database.rb', line 74

def closed?
  defined?(@closed) && @closed
end

#execute(sql, bind_vars = [], &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chdb/database.rb', line 85

def execute(sql, bind_vars = [], &block)
  prepare(sql) do |stmt|
    result = stmt.execute(bind_vars)

    if block
      result.each(&block)
    else
      result.to_a.freeze
    end
  end
end

#execute2(sql, *bind_vars) ⇒ Object

rubocop:disable Metrics/MethodLength



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chdb/database.rb', line 97

def execute2(sql, *bind_vars, &) # rubocop:disable Metrics/MethodLength
  prepare(sql) do |stmt|
    result = stmt.execute(*bind_vars)
    stmt.parse

    if block_given?
      yield stmt.columns
      result.each(&)
    else
      return result.each_with_object([stmt.columns]) do |row, arr|
               arr << row
             end
    end
  end
end

#get_first_row(sql, *bind_vars) ⇒ Object



131
132
133
# File 'lib/chdb/database.rb', line 131

def get_first_row(sql, *bind_vars)
  execute(sql, *bind_vars).first
end

#get_first_value(sql, *bind_vars) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/chdb/database.rb', line 135

def get_first_value(sql, *bind_vars)
  query(sql, bind_vars) do |rs|
    if (row = rs.next)
      return @results_as_hash ? row[rs.columns[0]] : row[0]
    end
  end
  nil
end

#prepare(sql) {|stmt| ... } ⇒ Object

Yields:

  • (stmt)


78
79
80
81
82
83
# File 'lib/chdb/database.rb', line 78

def prepare(sql)
  stmt = ChDB::Statement.new(self, sql)
  return stmt unless block_given?

  yield stmt
end

#query(sql, bind_vars = []) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/chdb/database.rb', line 113

def query(sql, bind_vars = [])
  result = prepare(sql).execute(bind_vars)
  if block_given?
    yield result
  else
    result
  end
end

#query_with_format(sql, format = 'CSV', bind_vars = []) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/chdb/database.rb', line 122

def query_with_format(sql, format = 'CSV', bind_vars = [])
  result = prepare(sql).execute_with_format(bind_vars, format)
  if block_given?
    yield result
  else
    result
  end
end

#readonly?Boolean

Returns true if the database has been open in readonly mode A helper to check before performing any operation

Returns:

  • (Boolean)


146
147
148
# File 'lib/chdb/database.rb', line 146

def readonly?
  @readonly
end