Class: Rubiks::Mondrian::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rubiks/mondrian/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
21
22
# File 'lib/rubiks/mondrian/connection.rb', line 17

def initialize(params={})
  @params = params
  @driver = params[:driver]
  @connected = false
  @raw_connection = nil
end

Instance Attribute Details

#raw_catalogObject (readonly)

Returns the value of attribute raw_catalog.



15
16
17
# File 'lib/rubiks/mondrian/connection.rb', line 15

def raw_catalog
  @raw_catalog
end

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



15
16
17
# File 'lib/rubiks/mondrian/connection.rb', line 15

def raw_connection
  @raw_connection
end

#raw_schemaObject (readonly)

Returns the value of attribute raw_schema.



15
16
17
# File 'lib/rubiks/mondrian/connection.rb', line 15

def raw_schema
  @raw_schema
end

Class Method Details

.create(params) ⇒ Object



9
10
11
12
13
# File 'lib/rubiks/mondrian/connection.rb', line 9

def self.create(params)
  connection = new(params)
  connection.connect
  connection
end

Instance Method Details

#closeObject



72
73
74
75
76
77
# File 'lib/rubiks/mondrian/connection.rb', line 72

def close
  @raw_connection.close
  @connected = false
  @raw_connection = @raw_jdbc_connection = nil
  true
end

#connectObject



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/rubiks/mondrian/connection.rb', line 24

def connect
  ::Rubiks::MondrianError.wrap_native_exception do
    # hack to call private constructor of MondrianOlap4jDriver
    # to avoid using DriverManager which fails to load JDBC drivers
    # because of not seeing JRuby required jar files
    cons = Java::MondrianOlap4j::MondrianOlap4jDriver.java_class.declared_constructor
    cons.accessible = true
    driver = cons.new_instance.to_java

    props = java.util.Properties.new
    props.setProperty('JdbcUser', @params[:username]) if @params[:username]
    props.setProperty('JdbcPassword', @params[:password]) if @params[:password]

    conn_string = connection_string
    @raw_jdbc_connection = driver.connect(conn_string, props)

    @raw_connection = @raw_jdbc_connection.unwrap(Java::OrgOlap4j::OlapConnection.java_class)
    @raw_catalog = @raw_connection.getOlapCatalog
    # currently it is assumed that there is just one schema per connection catalog
    @raw_schema = @raw_catalog.getSchemas.first
    @connected = true
    true

    # latest Mondrian version added ClassResolver which uses current thread class loader to load some classes
    # therefore need to set it to JRuby class loader to ensure that Mondrian classes are found
    # (e.g. when running mondrian-olap inside OSGi container)
    current_thread = Java::JavaLang::Thread.currentThread
    class_loader = current_thread.getContextClassLoader
    begin
      current_thread.setContextClassLoader JRuby.runtime.jruby_class_loader
      @raw_jdbc_connection = driver.connect(conn_string, props)
    ensure
      current_thread.setContextClassLoader(class_loader)
    end

    @raw_connection = @raw_jdbc_connection.unwrap(Java::OrgOlap4j::OlapConnection.java_class)
    @raw_catalog = @raw_connection.getOlapCatalog
    # currently it is assumed that there is just one schema per connection catalog
    @raw_schema = @raw_catalog.getSchemas.first
    @connected = true
    true
  end
end

#connected?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rubiks/mondrian/connection.rb', line 68

def connected?
  @connected
end

#execute(query_string) ⇒ Object



79
80
81
82
83
84
# File 'lib/rubiks/mondrian/connection.rb', line 79

def execute(query_string)
  ::Rubiks::MondrianError.wrap_native_exception do
    statement = @raw_connection.prepareOlapStatement(query_string)
    ::Rubiks::Mondrian::CellSet.new(statement.executeQuery())
  end
end

#flush_schema_cacheObject

Will affect only the next created connection. If it is necessary to clear all schema cache then flush_schema_cache should be called, then close and then new connection should be created.



88
89
90
91
92
# File 'lib/rubiks/mondrian/connection.rb', line 88

def flush_schema_cache
  unwrapped_connection = @raw_connection.unwrap(Java::MondrianOlap::Connection.java_class)
  raw_cache_control = unwrapped_connection.getCacheControl(nil)
  raw_cache_control.flushSchemaCache
end