Class: SyntaxTree::Database::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_connection) ⇒ Connection

Returns a new instance of Connection.



279
280
281
# File 'lib/syntax_tree/database.rb', line 279

def initialize(raw_connection)
  @raw_connection = raw_connection
end

Instance Attribute Details

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



277
278
279
# File 'lib/syntax_tree/database.rb', line 277

def raw_connection
  @raw_connection
end

Instance Method Details

#execute(query, binds = []) ⇒ Object



283
284
285
# File 'lib/syntax_tree/database.rb', line 283

def execute(query, binds = [])
  raw_connection.execute(query, binds)
end

#index_file(filepath) ⇒ Object



287
288
289
290
# File 'lib/syntax_tree/database.rb', line 287

def index_file(filepath)
  program = SyntaxTree.parse(SyntaxTree.read(filepath))
  program.accept(IndexingVisitor.new(self, filepath))
end

#last_insert_row_idObject



292
293
294
# File 'lib/syntax_tree/database.rb', line 292

def last_insert_row_id
  raw_connection.last_insert_row_id
end

#prepareObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/syntax_tree/database.rb', line 296

def prepare
  raw_connection.execute(<<~SQL)
    CREATE TABLE nodes (
      id integer primary key,
      type varchar(20),
      path varchar(200),
      line integer,
      column integer
    );
  SQL

  raw_connection.execute(<<~SQL)
    CREATE INDEX nodes_type ON nodes (type);
  SQL

  raw_connection.execute(<<~SQL)
    CREATE TABLE edges (
      id integer primary key,
      from_id integer,
      to_id integer,
      name varchar(20),
      list_index integer
    );
  SQL

  raw_connection.execute(<<~SQL)
    CREATE INDEX edges_name ON edges (name);
  SQL
end

#search(query) ⇒ Object



326
327
328
# File 'lib/syntax_tree/database.rb', line 326

def search(query)
  QueryResult.new(self, Pattern.new(query).compile)
end