Skip to content

Commit

Permalink
Refactor database interaction to use exec_query method and add schema…
Browse files Browse the repository at this point in the history
… structure dump functionality
  • Loading branch information
eliasjpr committed Dec 5, 2024
1 parent 51bc5a4 commit 94d6f77
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
9 changes: 9 additions & 0 deletions spec/schema_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe CQL::Schema do
it "dumps database structure" do
Example.dump_structure

File.exists?("db/structure.sql").should eq(true)
end
end
4 changes: 3 additions & 1 deletion src/delete.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ module CQL
# ```
def commit
query, params = to_sql
@schema.db.exec query, args: params
@schame.exec_query do |db|
db.exec query, args: params
end
end

# Generates the SQL query and parameters
Expand Down
8 changes: 6 additions & 2 deletions src/insert.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ module CQL
# Reset to ensure nothing else but the :id is returned
@back = Array(Expression::Column).new
query, params = back(:id).to_sql
@schema.db.query_one(query, args: params, as: type)
@schame.exec_query do |db|
db.query_one(query, args: params, as: type)
end
else
commit.last_insert_id
end
Expand All @@ -85,7 +87,9 @@ module CQL
# ```
def commit
query, params = to_sql
@schema.db.exec(query, args: params)
@schame.exec_query do |db|
db.exec(query, args: params)
end
rescue ex
Log.error { "Insert failed: #{ex.message}" }
raise ex
Expand Down
20 changes: 15 additions & 5 deletions src/query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ module CQL
# ```
def all(as as_kind)
query, params = to_sql
as_kind.from_rs @schema.db.query(query, args: params)
as_kind.from_rs

@schame.exec_query do |db|
db.query(query, args: params)
end
end

# - **@param** as [Type] The type to cast the results to
Expand Down Expand Up @@ -101,7 +105,9 @@ module CQL
def first(as as_kind)
query, params = to_sql
Log.debug { "Query: #{query}, Params: #{params}" }
@schema.db.query_one(query, args: params, as: as_kind)
@schame.exec_query do |db|
db.query_one(query, args: params, as: as_kind)
end
end

# - **@param** as [Type] The type to cast the result to
Expand Down Expand Up @@ -133,7 +139,9 @@ module CQL
# ```
def get(as as_kind)
query, params = to_sql
@schema.db.scalar(query, args: params, as: as_kind)
@schame.exec_query do |db|
db.scalar(query, args: params, as: as_kind)
end
end

# Iterates over each result and yields it to the provided block.
Expand All @@ -147,8 +155,10 @@ module CQL
# ```
def each(as as_kind, &)
query, params = to_sql
@schema.db.query_each(query, args: params) do |result|
yield as_kind.from_rs(result)
@schame.exec_query do |db|
db.query_each(query, args: params) do |result|
yield as_kind.from_rs(result)
end
end
end

Expand Down
30 changes: 24 additions & 6 deletions src/schema.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ module CQL
# schema = CQL::Schema.new(:northwind, "sqlite3://db.sqlite3")
# ```
def initialize(@name : Symbol, @uri : String, @adapter : Adapter = Adapter::SQLite, @version : String = "1.0")
@db = DB.connect(@uri)
@gen = Expression::Generator.new(@adapter)
end

Expand Down Expand Up @@ -114,10 +113,17 @@ module CQL
# schema.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
# ```
def exec(sql : String)
@db.transaction do |tx|
cnn = tx.connection
cnn.exec(sql)
end
db = DB.open(@uri)
db.exec(sql)
ensure
db.close
end

def exec_query(&)
db = DB.open(@uri)
yield db
ensure
db.close
end

# Creates a new query for the schema.
Expand Down Expand Up @@ -234,7 +240,19 @@ module CQL
# ```
# schema.dump_structure("db/structure.sql")
# ```
def dump_structure
def dump_structure(file = "db/structure.sql")
Dir.mkdir_p(File.dirname(file))

tables_structure = @tables.map do |_, table|
String.build do |str|
str << "-- Table: #{table.table_name}\n\n"
str << "-- Primary Key: #{table.primary.name} - Type: #{table.primary.type} \n\n"
str << table.create_sql
str << "\n\n"
end
end.join("\n")

File.write(file, tables_structure)
end

macro method_missing(call)
Expand Down
4 changes: 3 additions & 1 deletion src/update.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ module CQL
# ```
def commit
query, params = to_sql
@schema.db.exec query, args: params
@schame.exec_query do |db|
db.exec query, args: params
end
end

# Generates the SQL query and parameters.
Expand Down

0 comments on commit 94d6f77

Please sign in to comment.