Class: Graphshaper::SqlAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/graphshaper/adapters/sql_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_file, vertex_file, edge_file) ⇒ SqlAdapter

Returns a new instance of SqlAdapter.


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphshaper/adapters/sql_adapter.rb', line 3

def initialize(schema_file, vertex_file, edge_file)
  @schema_file = schema_file
  @vertex_file = vertex_file
  @edge_file = edge_file

  File.readlines("templates/schema.sql").each do |schema_line|
    @schema_file << schema_line
  end

  @vertex_file << "LOCK TABLES `vertices` WRITE;\n"
  @vertex_file << "INSERT INTO `vertices` (`id`, `vertex_id`)\n"
  @vertex_file << "VALUES\n\t"

  @edge_file << "LOCK TABLES `edges` WRITE;\n"
  @edge_file << "INSERT INTO `edges` (`id`, `edge_id`, `from_id`, `to_id`)\n"
  @edge_file << "VALUES\n\t"

  @first_vertex = true
  @first_edge = true
end

Instance Method Details

#add_edge(edge_id, from_id, to_id) ⇒ Object


30
31
32
33
34
# File 'lib/graphshaper/adapters/sql_adapter.rb', line 30

def add_edge(edge_id, from_id, to_id)
  @edge_file << ",\n\t" unless @first_edge
  @edge_file << "(#{edge_id + 1},#{edge_id},#{from_id + 1},#{to_id + 1})"
  @first_edge = false
end

#add_vertex(vertex_id) ⇒ Object


24
25
26
27
28
# File 'lib/graphshaper/adapters/sql_adapter.rb', line 24

def add_vertex(vertex_id)
  @vertex_file << ",\n\t" unless @first_vertex
  @vertex_file << "(#{vertex_id + 1},#{vertex_id})"
  @first_vertex = false
end

#closeObject


36
37
38
39
# File 'lib/graphshaper/adapters/sql_adapter.rb', line 36

def close
  [@vertex_file, @edge_file].each { |file| file << ";\nUNLOCK TABLES;"}
  [@schema_file, @vertex_file, @edge_file].each { |file| file.close }
end