Class: Dalton::Connection
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#datomize, #set_up_datomizer
Constructor Details
Returns a new instance of Connection.
9
10
11
|
# File 'lib/dalton/connection.rb', line 9
def initialize(uri)
@uri = uri
end
|
Instance Attribute Details
#datomic_connection ⇒ Object
Returns the value of attribute datomic_connection.
20
21
22
|
# File 'lib/dalton/connection.rb', line 20
def datomic_connection
@datomic_connection
end
|
Returns the value of attribute db.
20
21
22
|
# File 'lib/dalton/connection.rb', line 20
def db
@db
end
|
Returns the value of attribute uri.
20
21
22
|
# File 'lib/dalton/connection.rb', line 20
def uri
@uri
end
|
Class Method Details
.connect(uri) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/dalton/connection.rb', line 13
def self.connect(uri)
Peer.createDatabase(uri)
database = new(uri)
database.connect
return database
end
|
.convert_datoms(datoms) ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/dalton/connection.rb', line 75
def self.convert_datoms(datoms)
case datoms
when Array
Translation.from_ruby(datoms)
when String
Utility.read_edn(datoms)
else
raise ArgumentError, 'datoms must be an Array or a String containing EDN.'
end
end
|
.tempid(partition = :'db.part/user', id = nil) ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/dalton/connection.rb', line 86
def self.tempid(partition=:'db.part/user', id=nil)
partition = Keyword.intern(partition.to_s.sub(/^:/, ''))
if id
Peer.tempid(partition, id)
else
Peer.tempid(partition)
end
end
|
.tempid?(id) ⇒ Boolean
95
96
97
98
99
100
101
102
|
# File 'lib/dalton/connection.rb', line 95
def self.tempid?(id)
0 > case id
when Numeric
id
when Java::DatomicDb::DbId
id.get(Utility::kw('idx'))
end
end
|
Instance Method Details
32
33
34
35
36
37
|
# File 'lib/dalton/connection.rb', line 32
def connect
@datomic_connection = Peer.connect(uri) or
raise DatomicError, "Unable to connect to database at \"#{uri}\"."
refresh
true
end
|
22
23
24
25
|
# File 'lib/dalton/connection.rb', line 22
def create
Peer.createDatabase(uri) or
raise DatomicError, "Unable to create database at \"#{uri}\"."
end
|
27
28
29
30
|
# File 'lib/dalton/connection.rb', line 27
def destroy
Peer.deleteDatabase(uri) or
raise DatomicError, "Unable to destroy database at \"#{uri}\"."
end
|
43
44
45
46
|
# File 'lib/dalton/connection.rb', line 43
def refresh
self.db = @datomic_connection.db
db
end
|
#retract(entity) ⇒ Object
70
71
72
73
|
# File 'lib/dalton/connection.rb', line 70
def retract(entity)
entity_id = entity.is_a?(Entity) ? entity.id : entity
transact([[:'db.fn/retractEntity', entity_id]])
end
|
#transact(datoms) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/dalton/connection.rb', line 48
def transact(datoms)
data = self.class.convert_datoms(datoms)
result = TransactionResult.new(@datomic_connection.transact(data).get)
self.db = result.db_after
Translation.from_clj(result)
rescue Java::JavaUtilConcurrent::ExecutionException => e
cause = e.getCause
if cause.respond_to?(:data)
err_data = Translation.from_clj(cause.data)
case err_data[:'db/error']
when :'db.error/unique-conflict'
raise UniqueConflict.parse(cause.getMessage)
when :'db.error/wrong-type-for-attribute'
raise TypeError.parse(cause.getMessage)
end
end
raise DatomicError, "Transaction failed: #{e.getMessage}"
end
|