diff --git a/.ameba.yml b/.ameba.yml new file mode 100644 index 0000000..2245b95 --- /dev/null +++ b/.ameba.yml @@ -0,0 +1,5 @@ +Documentation/DocumentationAdmonition: + Enabled: false + +Lint/NotNil: + Enabled: false diff --git a/.github/workflows/crystal.yml b/.github/workflows/crystal.yml index f078078..4180b9c 100644 --- a/.github/workflows/crystal.yml +++ b/.github/workflows/crystal.yml @@ -56,6 +56,8 @@ jobs: run: shards install - name: Check code style run: crystal tool format --check + - name: Check code linting with Ameba + run: bin/ameba - name: Run tests env: DATABASE_URL: postgres://example:example@postgres:5432/example diff --git a/shard.yml b/shard.yml index 6eed505..1c1dfb4 100644 --- a/shard.yml +++ b/shard.yml @@ -12,6 +12,8 @@ dependencies: tallboy: github: epoch/tallboy development_dependencies: + ameba: + github: crystal-ameba/ameba sqlite3: github: crystal-lang/crystal-sqlite3 mysql: diff --git a/spec/migrations/alter_users_migration.cr b/spec/migrations/alter_users_migration_spec.cr similarity index 100% rename from spec/migrations/alter_users_migration.cr rename to spec/migrations/alter_users_migration_spec.cr diff --git a/spec/migrations/create_users_migration.cr b/spec/migrations/create_users_migration_spec.cr similarity index 100% rename from spec/migrations/create_users_migration.cr rename to spec/migrations/create_users_migration_spec.cr diff --git a/spec/models/customer_model.cr b/spec/models/customer_model_spec.cr similarity index 100% rename from spec/models/customer_model.cr rename to spec/models/customer_model_spec.cr diff --git a/spec/models/user_model.cr b/spec/models/user_model_spec.cr similarity index 100% rename from spec/models/user_model.cr rename to spec/models/user_model_spec.cr diff --git a/spec/schemas/billing.cr b/spec/schemas/billing_spec.cr similarity index 100% rename from spec/schemas/billing.cr rename to spec/schemas/billing_spec.cr diff --git a/spec/schemas/data.cr b/spec/schemas/data_spec.cr similarity index 100% rename from spec/schemas/data.cr rename to spec/schemas/data_spec.cr diff --git a/spec/schemas/example.cr b/spec/schemas/example_spec.cr similarity index 100% rename from spec/schemas/example.cr rename to spec/schemas/example_spec.cr diff --git a/spec/schemas/northwind.cr b/spec/schemas/northwind_spec.cr similarity index 100% rename from spec/schemas/northwind.cr rename to spec/schemas/northwind_spec.cr diff --git a/spec/schemas/tabledb.cr b/spec/schemas/tabledb_spec.cr similarity index 100% rename from spec/schemas/tabledb.cr rename to spec/schemas/tabledb_spec.cr diff --git a/spec/table_spec.cr b/spec/table_spec.cr index 0f5b0a9..0768c2a 100644 --- a/spec/table_spec.cr +++ b/spec/table_spec.cr @@ -7,7 +7,7 @@ describe Cql::Table do customer = CustomerModel.new(1, "John", "New York", 100) - insert_query = TableDB.insert.into(:customers).values( + TableDB.insert.into(:customers).values( id: customer.id, name: customer.name, city: customer.city, @@ -40,7 +40,7 @@ describe Cql::Table do count_query = TableDB.query.from(:customers).count count_query.first!(as: Int32).should eq 2 - result = TableDB.delete.from(:customers).commit + TableDB.delete.from(:customers).commit count_query.first!(as: Int32).should eq 0 end @@ -50,7 +50,7 @@ describe Cql::Table do check_query = "SELECT name FROM sqlite_master WHERE type='table' AND name='#{table}'" expect_raises(DB::NoResultsError) do - name = TableDB.db.query_one(check_query, as: String) + TableDB.db.query_one(check_query, as: String) end end end diff --git a/src/alter_table.cr b/src/alter_table.cr index 189e6ec..760c081 100644 --- a/src/alter_table.cr +++ b/src/alter_table.cr @@ -199,15 +199,15 @@ module Cql # sql = to_sql(visitor) # ``` def to_sql(visitor : Expression::Visitor) - String::Builder.build do |sb| + String.build do |string| @actions.each do |action| case action when Expression::CreateIndex, Expression::DropIndex, Expression::RenameTable - sb << action.accept(visitor) + string << action.accept(visitor) else - sb << Expression::AlterTable.new(@table, action).accept(visitor) + string << Expression::AlterTable.new(@table, action).accept(visitor) end - sb << ";\n" + string << ";\n" end end end diff --git a/src/dialects/mysql_dialect.cr b/src/dialects/mysql_dialect.cr index bfa9d00..97f9c29 100644 --- a/src/dialects/mysql_dialect.cr +++ b/src/dialects/mysql_dialect.cr @@ -10,12 +10,12 @@ module Expression end def auto_increment_primary_key(column : Cql::BaseColumn, col_type : String) : String - String::Builder.build do |sb| - sb << column.name - sb << " " - sb << col_type - sb << " PRIMARY KEY" - sb << " AUTO_INCREMENT" if column.auto_increment? + String.build do |string| + string << column.name + string << " " + string << col_type + string << " PRIMARY KEY" + string << " AUTO_INCREMENT" if column.auto_increment? end end diff --git a/src/dialects/postgres_dialect.cr b/src/dialects/postgres_dialect.cr index 88c0c46..f5f10fa 100644 --- a/src/dialects/postgres_dialect.cr +++ b/src/dialects/postgres_dialect.cr @@ -11,13 +11,13 @@ module Expression end def auto_increment_primary_key(column : Cql::BaseColumn, col_type : String) : String - String::Builder.build do |sb| - sb << column.name - sb << " " - sb << col_type - sb << " GENERATED" - sb << " ALWAYS" if column.auto_increment? - sb << " AS IDENTITY PRIMARY KEY" + String.build do |string| + string << column.name + string << " " + string << col_type + string << " GENERATED" + string << " ALWAYS" if column.auto_increment? + string << " AS IDENTITY PRIMARY KEY" end end diff --git a/src/dialects/sqlite_dialect.cr b/src/dialects/sqlite_dialect.cr index 8b26738..7848d89 100644 --- a/src/dialects/sqlite_dialect.cr +++ b/src/dialects/sqlite_dialect.cr @@ -33,7 +33,7 @@ module Expression end def drop_foreign_key(table_name : String, constraint_name : String) : String - message = <<-MSG + <<-MSG SQLite does not support dropping foreign keys directly via the ALTER TABLE statement. You need to recreate the table without the foreign key constraint. diff --git a/src/expression/aggregator_builder.cr b/src/expression/aggregator_builder.cr index 05b83e0..f87b987 100644 --- a/src/expression/aggregator_builder.cr +++ b/src/expression/aggregator_builder.cr @@ -5,28 +5,28 @@ module Expression def initialize(@aggregate_function : Condition) end - def >(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, ">", value)) + def >(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, ">", other)) end - def <(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, "<", value)) + def <(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, "<", other)) end - def >=(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, ">=", value)) + def >=(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, ">=", other)) end - def <=(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, "<=", value)) + def <=(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, "<=", other)) end - def ==(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, "=", value)) + def ==(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, "=", other)) end - def !=(value : Column | DB::Any) - ConditionBuilder.new(CompareCondition.new(@aggregate_function, "!=", value)) + def !=(other : Column | DB::Any) + ConditionBuilder.new(CompareCondition.new(@aggregate_function, "!=", other)) end end end diff --git a/src/expression/generator.cr b/src/expression/generator.cr index e8457f7..0eb3667 100644 --- a/src/expression/generator.cr +++ b/src/expression/generator.cr @@ -30,28 +30,28 @@ module Expression # Template Method for common visit methods def visit(node : Query) : String @params.clear - @query = String::Builder.build do |sb| - sb << "SELECT " - sb << "DISTINCT " if node.distinct? - sb << node.columns.map(&.accept(self)).join(", ") - sb << ", " if !node.aggr_columns.empty? && !node.columns.empty? - sb << node.aggr_columns.map(&.accept(self)).join(", ") - sb << node.from.accept(self) - node.joins.each { |join| sb << join.accept(self) } - sb << node.where.try &.accept(self) if node.where - sb << node.group_by.try &.accept(self) if node.group_by - sb << node.having.try &.accept(self) if node.having - sb << node.order_by.not_nil!.accept(self) if node.order_by - sb << node.limit.try &.accept(self) if node.limit + @query = String.build do |string| + string << "SELECT " + string << "DISTINCT " if node.distinct? + string << node.columns.map(&.accept(self)).join(", ") + string << ", " if !node.aggr_columns.empty? && !node.columns.empty? + string << node.aggr_columns.map(&.accept(self)).join(", ") + string << node.from.accept(self) + node.joins.each { |join| string << join.accept(self) } + string << node.where.try &.accept(self) if node.where + string << node.group_by.try &.accept(self) if node.group_by + string << node.having.try &.accept(self) if node.having + string << node.order_by.not_nil!.accept(self) if node.order_by + string << node.limit.try &.accept(self) if node.limit end end def visit(node : Join) : String - String::Builder.build do |sb| - sb << " #{node.join_type.to_s.upcase} JOIN " - sb << node.table.accept(self) - sb << " ON " - sb << node.condition.accept(self) + String.build do |string| + string << " #{node.join_type.to_s.upcase} JOIN " + string << node.table.accept(self) + string << " ON " + string << node.condition.accept(self) end end @@ -62,132 +62,132 @@ module Expression node.query.not_nil!.columns end - @query = String::Builder.build do |sb| - sb << "INSERT INTO " - sb << node.table.accept(self) - sb << " (" + @query = String.build do |string| + string << "INSERT INTO " + string << node.table.accept(self) + string << " (" columns.each_with_index do |col, i| - sb << col.column.name - sb << ", " if i < columns.size - 1 + string << col.column.name + string << ", " if i < columns.size - 1 end - sb << ")" + string << ")" if q = node.query - sb << " " << q.accept(self) + string << " " << q.accept(self) else - sb << " VALUES " + string << " VALUES " node.values.each_with_index do |row, i| - sb << "(" + string << "(" row.each_with_index do |val, j| @params << val - sb << "#{placeholder}" - sb << ", " if j < row.size - 1 + string << "#{placeholder}" + string << ", " if j < row.size - 1 end if i < node.values.size - 1 - sb << "), " + string << "), " else - sb << ")" + string << ")" end end - if node.back.any? - sb << " RETURNING (" + if !node.back.empty? + string << " RETURNING (" node.back.each_with_index do |column, i| - sb << column.accept(self) - sb << ", " if i < node.back.size - 1 + string << column.accept(self) + string << ", " if i < node.back.size - 1 end - sb << ")" + string << ")" end end end end def visit(node : Delete) : String - @query = String::Builder.build do |sb| - sb << "DELETE FROM " - sb << node.table.accept(self) + @query = String.build do |string| + string << "DELETE FROM " + string << node.table.accept(self) if using = node.using - sb << " USING " << using.accept(self) + string << " USING " << using.accept(self) end - sb << node.where.not_nil!.accept(self) if node.where - if node.back.any? - sb << " RETURNING (" + string << node.where.not_nil!.accept(self) if node.where + if !node.back.empty? + string << " RETURNING (" node.back.each_with_index do |column, i| - sb << column.accept(self) - sb << ", " if i < node.back.size - 1 + string << column.accept(self) + string << ", " if i < node.back.size - 1 end - sb << ")" + string << ")" end end end def visit(node : Where) : String - String::Builder.build do |sb| - sb << " WHERE (" - sb << node.condition.accept(self) - sb << ")" + String.build do |string| + string << " WHERE (" + string << node.condition.accept(self) + string << ")" end end def visit(node : Column) : String - String::Builder.build do |sb| + String.build do |string| unless node.column.name == :* - sb << node.column.table.not_nil!.table_name - sb << "." + string << node.column.table.not_nil!.table_name + string << "." end - sb << node.column.name + string << node.column.name end end def visit(node : And) : String - String::Builder.build do |sb| - sb << node.left.accept(self) - sb << " AND " - sb << node.right.accept(self) + String.build do |string| + string << node.left.accept(self) + string << " AND " + string << node.right.accept(self) end end def visit(node : Or) : String - String::Builder.build do |sb| - sb << node.left.accept(self) - sb << " OR " - sb << node.right.accept(self) + String.build do |string| + string << node.left.accept(self) + string << " OR " + string << node.right.accept(self) end end def visit(node : Not) : String - String::Builder.build do |sb| - sb << "NOT " - sb << node.condition.accept(self) + String.build do |string| + string << "NOT " + string << node.condition.accept(self) end end def visit(node : Compare) : String @params << node.right - String::Builder.build do |sb| - sb << node.left.accept(self) - sb << " " - sb << node.operator - sb << " " - sb << placeholder + String.build do |string| + string << node.left.accept(self) + string << " " + string << node.operator + string << " " + string << placeholder end end def visit(node : CompareCondition) : String - String::Builder.build do |sb| + String.build do |string| if node.left.is_a?(Column) || node.left.is_a?(Condition) - sb << node.left.as(Column | Condition).accept(self) + string << node.left.as(Column | Condition).accept(self) else @params << node.left.as(DB::Any) - sb << placeholder + string << placeholder end - sb << " " - sb << node.operator - sb << " " + string << " " + string << node.operator + string << " " if node.right.is_a?(Column) || node.right.is_a?(Condition) - sb << node.right.as(Column | Condition).accept(self) + string << node.right.as(Column | Condition).accept(self) else @params << node.right.as(DB::Any) - sb << placeholder + string << placeholder end end end @@ -195,12 +195,12 @@ module Expression def visit(node : Between) : String @params << node.low @params << node.high - String::Builder.build do |sb| - sb << node.column.accept(self) - sb << " BETWEEN " - sb << placeholder - sb << " AND " - sb << placeholder + String.build do |string| + string << node.column.accept(self) + string << " BETWEEN " + string << placeholder + string << " AND " + string << placeholder end end @@ -215,27 +215,27 @@ module Expression end def visit(node : InCondition) : String - String::Builder.build do |sb| - sb << node.column.accept(self) - sb << " IN (" + String.build do |string| + string << node.column.accept(self) + string << " IN (" node.values.each_with_index do |value, i| @params << value - sb << placeholder - sb << ", " if i < node.values.size - 1 + string << placeholder + string << ", " if i < node.values.size - 1 end - sb << ")" + string << ")" end end def visit(node : OrderBy) : String return "" if node.orders.empty? - String::Builder.build do |sb| - sb << " ORDER BY " + String.build do |string| + string << " ORDER BY " node.orders.each_with_index do |(column, direction), i| - sb << column.accept(self) - sb << " " - sb << direction.to_s.upcase - sb << ", " if i < node.orders.size - 1 + string << column.accept(self) + string << " " + string << direction.to_s.upcase + string << ", " if i < node.orders.size - 1 end end end @@ -243,64 +243,64 @@ module Expression def visit(node : GroupBy) : String return "" if node.columns.empty? - String::Builder.build do |sb| - sb << " GROUP BY " + String.build do |string| + string << " GROUP BY " node.columns.each_with_index do |column, i| - sb << column.accept(self) - sb << ", " if i < node.columns.size - 1 + string << column.accept(self) + string << ", " if i < node.columns.size - 1 end end end def visit(node : InSelect) : String - String::Builder.build do |sb| - sb << node.column.accept(self) - sb << " IN (" - sb << node.query.accept(self) - sb << ")" + String.build do |string| + string << node.column.accept(self) + string << " IN (" + string << node.query.accept(self) + string << ")" end end def visit(node : Exists) : String - String::Builder.build do |sb| - sb << "EXISTS (" - sb << node.sub_query.accept(self) - sb << ")" + String.build do |string| + string << "EXISTS (" + string << node.sub_query.accept(self) + string << ")" end end def visit(node : Having) : String - String::Builder.build do |sb| - sb << " HAVING " - sb << node.condition.accept(self) + String.build do |string| + string << " HAVING " + string << node.condition.accept(self) end end def visit(node : Limit) : String - String::Builder.build do |sb| + String.build do |string| @params << node.limit - sb << " LIMIT #{placeholder}" + string << " LIMIT #{placeholder}" if node.offset @params << node.offset - sb << " OFFSET #{placeholder}" + string << " OFFSET #{placeholder}" end end end def visit(node : Top) : String - String::Builder.build do |sb| - sb << "TOP " - sb << node.count.to_s + String.build do |string| + string << "TOP " + string << node.count.to_s end end def visit(node : From) : String - String::Builder.build do |sb| - sb << " FROM " + String.build do |string| + string << " FROM " node.tables.each_with_index do |table, i| - sb << table.table_name - sb << " AS " << table.as_name if table.as_name - sb << ", " if i < node.tables.size - 1 + string << table.table_name + string << " AS " << table.as_name if table.as_name + string << ", " if i < node.tables.size - 1 end end end @@ -310,10 +310,10 @@ module Expression end def visit(node : Null) : String - String::Builder.build do |sb| - sb << "NULL" + String.build do |string| + string << "NULL" if column = node.column - sb << column.accept(self) + string << column.accept(self) end end end @@ -366,64 +366,64 @@ module Expression end def visit(node : Update) : String - @query = String::Builder.build do |sb| - sb << "UPDATE " - sb << node.table.accept(self) - sb << " SET " + @query = String.build do |string| + string << "UPDATE " + string << node.table.accept(self) + string << " SET " node.setters.each_with_index do |setter, i| - sb << setter.accept(self) - sb << ", " if i < node.setters.size - 1 + string << setter.accept(self) + string << ", " if i < node.setters.size - 1 end if where = node.where - sb << where.accept(self) + string << where.accept(self) end - if node.back.any? - sb << " RETURNING " + if !node.back.empty? + string << " RETURNING " node.back.each_with_index do |column, i| - sb << column.accept(self) - sb << ", " if i < node.back.size - 1 + string << column.accept(self) + string << ", " if i < node.back.size - 1 end end end end def visit(node : CreateIndex) : String - @query = String::Builder.build do |sb| - sb << "CREATE " - sb << "UNIQUE " if node.index.unique? - sb << "INDEX " - sb << node.index.index_name - sb << " ON " - sb << node.index.table.table_name - sb << " (" - sb << node.index.columns.map { |c| c.to_s }.join(", ") - sb << ")" + @query = String.build do |string| + string << "CREATE " + string << "UNIQUE " if node.index.unique? + string << "INDEX " + string << node.index.index_name + string << " ON " + string << node.index.table.table_name + string << " (" + string << node.index.columns.map(&.to_s).join(", ") + string << ")" end end def visit(node : CreateTable) : String - @query = String::Builder.build do |sb| - sb << "CREATE TABLE IF NOT EXISTS " - sb << node.table.table_name - sb << " (" + @query = String.build do |string| + string << "CREATE TABLE IF NOT EXISTS " + string << node.table.table_name + string << " (" node.table.columns.each_with_index do |(name, column), i| if column.is_a?(Cql::PrimaryKey) - sb << @dialect.auto_increment_primary_key(column, @adapter.sql_type(column.type)) + string << @dialect.auto_increment_primary_key(column, @adapter.sql_type(column.type)) else - sb << column.name - sb << " " << @adapter.sql_type(column.type) + string << column.name + string << " " << @adapter.sql_type(column.type) if [:created_at, :updated_at].includes?(column.name) - sb << " DEFAULT " << column.default + string << " DEFAULT " << column.default elsif column.default - sb << " DEFAULT " << column.default + string << " DEFAULT " << column.default end - sb << " NOT NULL" unless column.null? - sb << " UNIQUE" if column.unique? + string << " NOT NULL" unless column.null? + string << " UNIQUE" if column.unique? end - sb << ", " if i < node.table.columns.size - 1 + string << ", " if i < node.table.columns.size - 1 end - sb << ")" + string << ")" end end @@ -441,13 +441,13 @@ module Expression end def visit(node : AddColumn) : String - String::Builder.build do |sb| - sb << "ADD COLUMN " - sb << node.column.name - sb << " " << @adapter.sql_type(node.column.type) - sb << " PRIMARY KEY" if node.column.is_a?(Cql::PrimaryKey) - sb << " NOT NULL" unless node.column.null? - sb << " UNIQUE" if node.column.unique? + String.build do |string| + string << "ADD COLUMN " + string << node.column.name + string << " " << @adapter.sql_type(node.column.type) + string << " PRIMARY KEY" if node.column.is_a?(Cql::PrimaryKey) + string << " NOT NULL" unless node.column.null? + string << " UNIQUE" if node.column.unique? end end @@ -496,17 +496,17 @@ module Expression raise DB::Error.new message end - String::Builder.build do |sb| - sb << "ADD CONSTRAINT " - sb << node.fk.name - sb << " FOREIGN KEY (" - sb << node.fk.columns.map(&.to_s).join(", ") - sb << ") REFERENCES " - sb << node.fk.table - sb << " (" - sb << node.fk.references.map(&.to_s).join(", ") - sb << ") ON DELETE " << node.fk.on_delete - sb << " ON UPDATE " << node.fk.on_update + String.build do |string| + string << "ADD CONSTRAINT " + string << node.fk.name + string << " FOREIGN KEY (" + string << node.fk.columns.map(&.to_s).join(", ") + string << ") REFERENCES " + string << node.fk.table + string << " (" + string << node.fk.references.map(&.to_s).join(", ") + string << ") ON DELETE " << node.fk.on_delete + string << " ON UPDATE " << node.fk.on_update end end diff --git a/src/index.cr b/src/index.cr index 2e3fc16..725b677 100644 --- a/src/index.cr +++ b/src/index.cr @@ -48,7 +48,7 @@ module Cql # index_name = index.index_name # ``` def index_name - @name || "idx_#{columns.map { |c| c.to_s[0..3] }.join("_")}" + @name || "idx_#{columns.map { |column| column.to_s[0..3] }.join("_")}" end end end diff --git a/src/migrations.cr b/src/migrations.cr index 4234d2c..c46dc3a 100644 --- a/src/migrations.cr +++ b/src/migrations.cr @@ -217,7 +217,7 @@ module Cql # ``` # @return [Migration.class | Nil] def last : BaseMigration.class | Nil - Migrator.migrations.find { |m| m.version == repo.last.version } + Migrator.migrations.find { |migration| migration.version == repo.last.version } rescue DB::NoResultsError nil end @@ -229,7 +229,7 @@ module Cql # migrator.down_to(1_i64) # ``` def down_to(version : Int64) - index = sorted_migrations.index { |m| m.version == version } + index = sorted_migrations.index { |migration| migration.version == version } down(index ? index + 1 : 0) if index end @@ -240,7 +240,7 @@ module Cql # migrator.up_to(1_i64) # ``` def up_to(version : Int64) - index = sorted_migrations.index { |m| m.version == version } + index = sorted_migrations.index { |migration| migration.version == version } up(index ? index + 1 : 0) if index end @@ -270,7 +270,7 @@ module Cql # migrator.print_pending_migrations # ``` def print_pending_migrations - print_table(pending_migrations.map { |m| build_migration_record(m) }, "⏱".colorize.yellow.to_s) + print_table(pending_migrations.map { |migration| build_migration_record(migration) }, "⏱".colorize.yellow.to_s) end # Returns the pending migrations. @@ -280,7 +280,7 @@ module Cql # migrator.pending_migrations # ``` def pending_migrations : Array(BaseMigration.class) - sorted_migrations.reject { |m| migration_applied?(m.version) } + sorted_migrations.reject { |migration| migration_applied?(migration.version) } end # Returns the applied migrations. diff --git a/src/query.cr b/src/query.cr index 2e4efbe..876adc4 100644 --- a/src/query.cr +++ b/src/query.cr @@ -145,10 +145,10 @@ module Cql # # => John # ``` - def each(as as_kind, &block) + def each(as as_kind, &) query, params = to_sql - @schema.db.query_each(query, args: params) do |rs| - yield as_kind.from_rs(rs) + @schema.db.query_each(query, args: params) do |result| + yield as_kind.from_rs(result) end end @@ -260,14 +260,14 @@ module Cql # => "SELECT users.name, users.age, address.city, address.state FROM users, address" # ``` def select(**fields) - fields.each do |k, v| - if [:count, :sum, :avg, :min, :max].includes?(k) && v.is_a?(Symbol) - @aggr_columns << build_aggr_expression(k, v) + fields.each do |key, value| + if [:count, :sum, :avg, :min, :max].includes?(key) && value.is_a?(Symbol) + @aggr_columns << build_aggr_expression(key, value) else - if v.is_a?(Array(Symbol)) - v.map { |f| @columns << find_column(f, k) } + if value.is_a?(Array(Symbol)) + value.map { |name| @columns << find_column(name, key) } else - @columns << find_column(v) + @columns << find_column(value) end end end @@ -679,7 +679,7 @@ module Cql private def build_select if @columns.empty? && @aggr_columns.empty? - @tables.each do |tbl_name, table| + @tables.each do |_tbl_name, table| @columns.concat(table.columns.values) end end @@ -715,8 +715,8 @@ module Cql return column if column raise "Column #{name} not found in table #{table_name}" else - @tables.each do |_tbl_name, table| - column = table.columns[name]? + @tables.each do |_tbl_name, tbl| + column = tbl.columns[name]? return column if column end raise "Column #{name} not found in any of #{@tables.keys} tables" diff --git a/src/relations/collection.cr b/src/relations/collection.cr index d7ae90a..0bb12be 100644 --- a/src/relations/collection.cr +++ b/src/relations/collection.cr @@ -232,7 +232,7 @@ module Cql::Relations clear Target.insert .into(@target_table) - .values(ids.map { |id| {@key => @id} }).commit.rows_affected + .values(ids.map { |_id| {@key => @id} }).commit.rows_affected @records = reload end diff --git a/src/schema.cr b/src/schema.cr index 748bc54..9e4853c 100644 --- a/src/schema.cr +++ b/src/schema.cr @@ -98,7 +98,7 @@ module Cql # Schema.define # ``` def build - @tables.each do |name, table| + @tables.each do |_tbl_name, table| sql = table.create_sql Log.debug { sql } exec(sql)