Class: TimeBuffer::DatabaseConnector
- Inherits:
-
Object
- Object
- TimeBuffer::DatabaseConnector
- Defined in:
- lib/time_buffer/database_connector.rb
Constant Summary collapse
- FILE_LOCATION =
"usage_data.db"
Instance Method Summary collapse
- #connect ⇒ Object
- #connection ⇒ Object
- #execute(sql, *params) ⇒ Object
-
#initialize ⇒ DatabaseConnector
constructor
A new instance of DatabaseConnector.
Constructor Details
#initialize ⇒ DatabaseConnector
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/time_buffer/database_connector.rb', line 7 def initialize return if File.exist?(FILE_LOCATION) # 1. Applications Table connection.execute " CREATE TABLE IF NOT EXISTS applications (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n bundle_id TEXT UNIQUE NOT NULL,\n app_name TEXT NOT NULL\n );\n SQL\n\n # 2. Time Sessions Table\n connection.execute <<-SQL\n CREATE TABLE IF NOT EXISTS time_sessions (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n start_time DATETIME NOT NULL,\n end_time DATETIME,\n metadata JSONB,\n FOREIGN KEY (application_id) REFERENCES applications(id)\n );\n SQL\n\n # 3. Daily Summaries Table\n connection.execute <<-SQL\n CREATE TABLE IF NOT EXISTS daily_summaries (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n date DATE NOT NULL,\n total_duration INTEGER NOT NULL,\n FOREIGN KEY (application_id) REFERENCES applications(id),\n UNIQUE(application_id, date)\n );\n SQL\n\n puts \"Database initialized successfully.\"\nend\n" |
Instance Method Details
#connect ⇒ Object
46 47 48 |
# File 'lib/time_buffer/database_connector.rb', line 46 def connect @db = SQLite3::Database.new(FILE_LOCATION) end |
#connection ⇒ Object
50 51 52 |
# File 'lib/time_buffer/database_connector.rb', line 50 def connection @db || connect end |
#execute(sql, *params) ⇒ Object
54 55 56 57 |
# File 'lib/time_buffer/database_connector.rb', line 54 def execute(sql, *params) # puts "Writing to db: #{params}" connection.execute(sql, *params) end |