Class: DBConnection
- Inherits:
-
Object
- Object
- DBConnection
- Defined in:
- lib/railz_lite/models/db_connection.rb
Constant Summary collapse
- PRINT_QUERIES =
ENV['PRINT_QUERIES'] == 'true'
- ROOT_FOLDER =
Dir.pwd
- SQL_FILE =
File.join(ROOT_FOLDER, 'db', 'app.sql')
- DB_FILE =
File.join(ROOT_FOLDER, 'db', 'app.db')
Class Method Summary collapse
-
.execute(*args) ⇒ Object
results hash of results [1, name: ‘Bob’…].
-
.execute2(*args) ⇒ Object
returns result with header fields [[‘id’, ‘name’…], { id: 1, name: ‘Bob’ … }.
-
.insert(*args) ⇒ Object
used to insert data into tables.
- .instance ⇒ Object
-
.open(db_name) ⇒ Object
for sqlite3 we need file.db, for postgresql we need database name.
- .reset ⇒ Object
- .start ⇒ Object
Class Method Details
.execute(*args) ⇒ Object
results hash of results [1, name: ‘Bob’…]
44 45 46 47 |
# File 'lib/railz_lite/models/db_connection.rb', line 44 def self.execute(*args) print_query(*args) instance.execute(*args) end |
.execute2(*args) ⇒ Object
returns result with header fields [[‘id’, ‘name’…], { id: 1, name: ‘Bob’ … }
50 51 52 53 |
# File 'lib/railz_lite/models/db_connection.rb', line 50 def self.execute2(*args) print_query(*args) instance.execute2(*args) end |
.insert(*args) ⇒ Object
used to insert data into tables
56 57 58 59 |
# File 'lib/railz_lite/models/db_connection.rb', line 56 def self.insert(*args) print_query(*args) instance.insert(*args) end |
.instance ⇒ Object
37 38 39 40 41 |
# File 'lib/railz_lite/models/db_connection.rb', line 37 def self.instance start if @db.nil? @db end |
.open(db_name) ⇒ Object
for sqlite3 we need file.db, for postgresql we need database name
14 15 16 17 18 19 20 |
# 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 ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 ⇒ Object
10 11 12 |
# File 'lib/railz_lite/models/db_connection.rb', line 10 def self.start DBConnection.open(DB_FILE) end |