class DBConnection
Constants
- DB_FILE
- PRINT_QUERIES
- ROOT_FOLDER
- SQL_FILE
Public Class Methods
execute(*args)
click to toggle source
results hash of results [{id: 1, name: 'Bob'}…]
# File lib/railz_lite/models/db_connection.rb, line 44 def self.execute(*args) print_query(*args) instance.execute(*args) end
execute2(*args)
click to toggle source
returns result with header fields [['id', 'name'…], { id: 1, name: 'Bob' … }
# File lib/railz_lite/models/db_connection.rb, line 50 def self.execute2(*args) print_query(*args) instance.execute2(*args) end
insert(*args)
click to toggle source
used to insert data into tables
# File lib/railz_lite/models/db_connection.rb, line 56 def self.insert(*args) print_query(*args) instance.insert(*args) end
instance()
click to toggle source
# File lib/railz_lite/models/db_connection.rb, line 37 def self.instance start if @db.nil? @db end
open(db_name)
click to toggle source
# File lib/railz_lite/models/db_connection.rb, line 14 def self.open(db_name) # for sqlite3 we need file.db, for postgresql we need database name db_uri = ENV['DATABASE_URL'] @db = db_uri.nil? ? SQLite3Wrapper.new(db_name) : PGWrapper.new(db_uri) @db end
reset()
click to toggle source
# File lib/railz_lite/models/db_connection.rb, line 22 def self.reset db_uri = ENV['DATABASE_URL'] if db_uri.nil? # sqlite commands = ["rm '#{DB_FILE}'", "cat '#{SQL_FILE}' | sqlite3 '#{DB_FILE}'"] commands.each { |command| `#{command}` } DBConnection.open(DB_FILE) else # postgres DBConnection.open(db_uri) sql = File.read(SQL_FILE) instance.execute(sql) end end
start()
click to toggle source
# File lib/railz_lite/models/db_connection.rb, line 10 def self.start DBConnection.open(DB_FILE) end
Private Class Methods
print_query(query, *interpolation_args)
click to toggle source
# File lib/railz_lite/models/db_connection.rb, line 64 def self.print_query(query, *interpolation_args) return unless PRINT_QUERIES puts '--------------------' puts query unless interpolation_args.empty? puts "interpolate: #{interpolation_args.inspect}" end puts '--------------------' end