Class: Neo4j::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/session.rb

Direct Known Subclasses

Embedded::EmbeddedSession, Neo4j::Server::CypherSession

Defined Under Namespace

Classes: CypherError

Constant Summary

@@current_session =
nil
@@all_sessions =
{}
@@factories =
{}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Neo4j::Session) current

Returns the current session

Returns:



115
116
117
# File 'lib/neo4j/session.rb', line 115

def current
  @@current_session
end

+ (Object) current!

Returns the current session or raise an exception if no session is available



120
121
122
123
# File 'lib/neo4j/session.rb', line 120

def current!
  fail 'No session, please create a session first with Neo4j::Session.open(:server_db) or :embedded_db' unless current
  current
end

+ (Object) inspect



194
195
196
# File 'lib/neo4j/session.rb', line 194

def inspect
  "Neo4j::Session available: #{@@factories && @@factories.keys}"
end

+ (Object) named(name)

Returns a session with given name or else raise an exception



131
132
133
# File 'lib/neo4j/session.rb', line 131

def named(name)
  @@all_sessions[name] || fail("No session named #{name}.")
end

+ (Object) on_session_available(&callback)

Registers a callback which will be called immediately if session is already available, or called when it later becomes available.



143
144
145
146
147
148
149
# File 'lib/neo4j/session.rb', line 143

def on_session_available(&callback)
  callback.call(Neo4j::Session.current) if Neo4j::Session.current

  add_listener do |event, data|
    callback.call(data) if event == :session_available
  end
end

+ (Object) open(db_type = :server_db, *params)

Creates a new session to Neo4j. This will be the default session to be used unless there is already a session created (see #current and #set_current)

Examples:

A Neo4j Server session

Neo4j::Session.open(:server_db, 'http://localhost:7474', {basic_auth: {username: 'foo', password: 'bar'}})

Using a user defined Faraday HTTP connection

connection = Faraday.new do |b|
  # faraday config
end
Neo4j::Session.open(:server_db, 'http://localhost:7474', connection: connection)

A embedded Neo4j session

Neo4j::Session.open(:embedded_db, 'path/to/db')

Parameters:

  • db_type (defaults to: :server_db)

    the type of database, e.g. :embedded_db, or :server_db

See Also:

  • Neo4j::Server::CypherSession#open for :server_db params


97
98
99
# File 'lib/neo4j/session.rb', line 97

def open(db_type = :server_db, *params)
  register(create_session(db_type, params))
end

+ (Object) open_named(db_type, name, default = nil, *params)



101
102
103
104
# File 'lib/neo4j/session.rb', line 101

def open_named(db_type, name, default = nil, *params)
  fail 'Multiple sessions is currently only supported for Neo4j Server connections.' unless db_type == :server_db
  register(create_session(db_type, params), name, default)
end

+ (Object) query(*args)

See Also:



126
127
128
# File 'lib/neo4j/session.rb', line 126

def query(*args)
  current!.query(*args)
end

+ (Object) set_current(session)

Sets the session to be used as default

Parameters:



137
138
139
# File 'lib/neo4j/session.rb', line 137

def set_current(session)
  @@current_session = session
end

+ (Object) user_agent_string



151
152
153
154
155
156
157
158
159
160
# File 'lib/neo4j/session.rb', line 151

def user_agent_string
  gem, version = if defined?(::Neo4j::ActiveNode)
                   ['neo4j', ::Neo4j::VERSION]
                 else
                   ['neo4j-core', ::Neo4j::Core::VERSION]
                 end


  "#{gem} gem/#{version} (https://github.com/neo4jrb/#{gem})"
end

Instance Method Details

- (Object) _query(*params)

This method is abstract.

Same as #query but does not accept an DSL and returns the raw result from the database. Notice, it might return different values depending on which database is used, embedded or server.



75
76
77
# File 'lib/neo4j/session.rb', line 75

def _query(*params)
  fail 'not implemented'
end

- (Boolean) auto_commit?

Returns:

  • (Boolean)


35
36
37
# File 'lib/neo4j/session.rb', line 35

def auto_commit?
  true # TODO
end

- (Object) begin_tx

This method is abstract.


40
41
42
# File 'lib/neo4j/session.rb', line 40

def begin_tx
  fail 'not impl.'
end

- (Object) close

This method is abstract.


8
9
10
# File 'lib/neo4j/session.rb', line 8

def close
  self.class.unregister(self)
end

- (:embedded_db | :server_db) db_type

Returns:

  • (:embedded_db | :server_db)


31
32
33
# File 'lib/neo4j/session.rb', line 31

def db_type
  fail 'not impl.'
end

- (Neo4j::Core::Query, Enumerable) query(options = {})

Performs a cypher query. See Core::Query for more details, but basic usage looks like:

Examples:

Using cypher DSL

Neo4j::Session.query.match("(c:person)-[:friends]->(p:person)").where(c: {name: 'andreas'}).pluck(:p).first[:name]

Show the generated Cypher

Neo4j::Session.query..match("(c:person)-[:friends]->(p:person)").where(c: {name: 'andreas'}).return(:p).to_cypher

Use Cypher string instead of the cypher DSL

Neo4j::Session.query("MATCH (c:person)-[:friends]->(p:person) WHERE c.name = \"andreas\" RETURN p").first[:p][:name]

Returns:

  • (Neo4j::Core::Query, Enumerable)

    return a Query object for DSL or a Enumerable if using raw cypher strings

See Also:



68
69
70
# File 'lib/neo4j/session.rb', line 68

def query(options = {})
  fail 'not implemented, abstract'
end

- (Object) running

This method is abstract.

Only for embedded database



26
27
28
# File 'lib/neo4j/session.rb', line 26

def running
  fail 'not impl.'
end

- (Object) shutdown

This method is abstract.

Only for embedded database



20
21
22
# File 'lib/neo4j/session.rb', line 20

def shutdown
  fail 'not impl.'
end

- (Object) start

This method is abstract.

Only for embedded database



14
15
16
# File 'lib/neo4j/session.rb', line 14

def start
  fail 'not impl.'
end