Skip to content
Open

Sql #104

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions FMDB.podspec

This file was deleted.

16 changes: 6 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem "rake"
gem "bubble-wrap"
gem "motion-stump", '~>0.2'
gem 'rake'
gem 'motion-support'
gem 'motion-cocoapods'
gem 'bubble-wrap'
gem 'motion-stump'

gem "motion-support", ">= 0.2.2"

gem 'cocoapods', '~>0.19.1'
gem 'motion-cocoapods', '~>1.3.0'

gemspec
95 changes: 38 additions & 57 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,75 +1,56 @@
PATH
remote: .
specs:
motion_model (0.4.6)
bubble-wrap (= 1.3.0)
motion-support (>= 0.2.2)

GEM
remote: https://rubygems.org/
specs:
activesupport (3.2.13)
i18n (= 0.6.1)
activesupport (3.2.16)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.4)
bubble-wrap (1.3.0)
claide (0.2.0)
cocoapods (0.19.1)
activesupport (~> 3.2.13)
claide (~> 0.2.0)
cocoapods-core (= 0.19.1)
cocoapods-downloader (~> 0.1.0)
bubble-wrap (1.4.0)
claide (0.4.0)
cocoapods (0.29.0)
activesupport (>= 3.2.15, < 4)
claide (~> 0.4.0)
cocoapods-core (= 0.29.0)
cocoapods-downloader (~> 0.3.0)
cocoapods-try-release-fix (~> 0.1.1)
colored (~> 1.2)
escape (~> 0.0.4)
faraday (~> 0.8.1)
json (~> 1.7.3)
octokit (~> 1.7)
open4 (~> 1.3.0)
rake (~> 10.0.0)
xcodeproj (~> 0.5.5)
cocoapods-core (0.19.1)
activesupport (~> 3.2.13)
rake (~> 10.0.0)
cocoapods-downloader (0.1.0)
json_pure (~> 1.8)
nap (~> 0.5)
open4 (~> 1.3)
xcodeproj (~> 0.14.1)
cocoapods-core (0.29.0)
activesupport (>= 3.2.15, < 4)
fuzzy_match (~> 2.0.4)
json_pure (~> 1.8)
nap (~> 0.5)
cocoapods-downloader (0.3.0)
cocoapods-try-release-fix (0.1.1)
colored (1.2)
escape (0.0.4)
faraday (0.8.7)
multipart-post (~> 1.1)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
hashie (2.0.5)
i18n (0.6.1)
json (1.7.7)
motion-cocoapods (1.3.1)
cocoapods (>= 0.17.0)
motion-require (0.0.6)
motion-stump (0.2.1)
motion-support (0.2.2)
fuzzy_match (2.0.4)
i18n (0.6.9)
json_pure (1.8.1)
motion-cocoapods (1.4.0)
cocoapods (>= 0.26.2)
motion-require (0.1.0)
motion-stump (0.3.1)
motion-support (0.2.6)
motion-require (>= 0.0.6)
multi_json (1.7.3)
multipart-post (1.2.0)
netrc (0.7.7)
octokit (1.24.0)
addressable (~> 2.2)
faraday (~> 0.8)
faraday_middleware (~> 0.9)
hashie (~> 2.0)
multi_json (~> 1.3)
netrc (~> 0.7.7)
multi_json (1.8.4)
nap (0.6.0)
open4 (1.3.0)
rake (10.0.4)
xcodeproj (0.5.5)
activesupport (~> 3.2.13)
rake (10.1.1)
xcodeproj (0.14.1)
activesupport (~> 3.0)
colored (~> 1.2)
rake

PLATFORMS
ruby

DEPENDENCIES
bubble-wrap
cocoapods (~> 0.19.1)
motion-cocoapods (~> 1.3.0)
motion-stump (~> 0.2)
motion-support (>= 0.2.2)
motion_model!
motion-cocoapods
motion-stump
motion-support
rake
18 changes: 12 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# -*- coding: utf-8 -*-
require "bundler/gem_tasks"
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'
require 'bundler'
Bundler.require(:default)

begin
require 'bundler'
Bundler.require
rescue LoadError
end

require 'motion-cocoapods'

$: << File.expand_path('/lib', __FILE__)

require 'motion_model'
require 'motion_model/array'
require 'motion_model/sql'
require 'motion_model/fmdb'

Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'MotionModel'
app.delegate_class = 'FakeDelegate'

app.pods do
pod 'FMDB', '2.0'
end
end
7 changes: 2 additions & 5 deletions lib/motion_model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require 'motion-require'
require 'motion-support'

%w(*.rb model/*.rb adapters/*.rb).each do |path|
%w[*.rb model/*.rb adapters/*.rb adapters/array/*.rb adapters/sql/sql_db_adapter.rb adapters/sql/sqlite3_adapter.rb adapters/sql/*.rb].each do |path|
Motion::Require.all(Dir.glob(File.expand_path("../../motion/#{path}", __FILE__)))
end
end
2 changes: 1 addition & 1 deletion motion/adapters/sql/fmdb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def execute_sql(sql)
if rset
result = []
while rset.next do
result << rset.resultDictionary
result << rset.resultDict
end
end
else
Expand Down
2 changes: 2 additions & 0 deletions motion/adapters/sql/sql_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def to_sql_str
values = @value.map { |v| v.is_a?(Numeric) ? v : %Q["#{v}"] }
elsif @value.is_a?(Range)
values = [@value.min, @value.max]
elsif @value.is_a?(TrueClass) || @value.is_a?(FalseClass)
value = @value.to_i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is TrueClass#to_i an RM extension?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is. Before this fix "where(current: true)" was being translated to WHERE ("users"."current" = "true") and it should be WHERE ("users"."current" = 1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does value = @value work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is working here. Maibe caling @value.dup it would be more appropriate. I`m not sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For true/false I assume the boolean should be passed through, if FMDB handles booleans directly which I would assume it does. No dup necessary: a = true; b = a; b = false; a # => true

else
value = %Q["#{@value}"]
end
Expand Down
9 changes: 9 additions & 0 deletions motion/adapters/sql/sql_db_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ def to_delete_sql(scope)
SQL
end

def to_update_all_sql(scope, attrs)
typed_attrs = _quoted_db_typed_attributes(attrs)
column_values_str = typed_attrs.map { |k, v| %Q["#{k.to_s}" = #{v}] }.join(', ')

<<-SQL.strip << ';'
UPDATE "#{scope.table_name}" SET #{column_values_str} #{scope.options_str}
SQL
end

private

def _quoted_db_typed_attributes(attrs)
Expand Down
9 changes: 9 additions & 0 deletions motion/adapters/sql/sql_model_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ def get_loaded_attr(col)
_col.type == :belongs_to? ? get_attr(name) : relation(_col).loaded
end

def reload
_new_record = self.class.find(self.id)
if _new_record.present?
self.attributes = self.class.find(self.id).attributes
else
nil
end
end

private

def before_initialize(options)
Expand Down
31 changes: 30 additions & 1 deletion motion/adapters/sql/sql_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(model_class, db_adapter, type = :select)
@orders = nil
@group = nil
@limit = nil
@offset = nil
end

def method_missing(id, *args)
Expand All @@ -34,6 +35,7 @@ def deep_clone
_orders = @orders.try(:dup)
_group = @group.try(:dup)
_limit = @limit
_offset = @offset
_conditions = @conditions.try(:dup)
_type = @type
self.class.new(@model_class, @db_adapter).instance_eval do
Expand All @@ -43,6 +45,7 @@ def deep_clone
@orders = _orders
@group = _group
@limit = _limit
@offset = _offset
@conditions = _conditions
@type = _type
self
Expand Down Expand Up @@ -190,6 +193,14 @@ def limit(limit)
end
end

def offset(offset)
return self if null_scope?
deep_clone.instance_eval do
@offset = offset
self
end
end

def all
deep_clone
end
Expand Down Expand Up @@ -235,6 +246,20 @@ def delete
@model_class.do_delete(self)
end

def delete_all_sql
@db_adapter.to_delete_sql(default_scope)
end

def delete_all
_delete_all_sql = @db_adapter.to_delete_sql(self)
@db_adapter.build_sql_context(:delete, _delete_all_sql).execute
end

def update_all(_attributes)
_update_all_sql = @db_adapter.to_update_all_sql(self, _attributes)
@db_adapter.build_sql_context(:update, _update_all_sql).execute
end

def to_sql
@db_adapter.send("to_#{type.to_s}_sql", self)
end
Expand Down Expand Up @@ -286,8 +311,12 @@ def limit_str
@limit ? %Q[LIMIT #{@limit}] : nil
end

def offset_str
@offset ? %Q[OFFSET #{@offset}] : nil
end

def options_str
arr = [SQLCondition.to_sql_str(@conditions), group_str, order_str, limit_str].compact
arr = [SQLCondition.to_sql_str(@conditions), group_str, order_str, limit_str, offset_str].compact
arr.empty? ? nil : arr.join(' ')
end

Expand Down
8 changes: 6 additions & 2 deletions motion/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,12 @@ def save_without_transaction(options = {})
end

# Set created_at and updated_at fields

def set_auto_date_field(field_name)
method = "#{field_name}="
self.send(method, Time.now) if self.respond_to?(method)
if self.respond_to?(method)
self.send(method, Time.now) if self.send(field_name).blank?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this break setting #updated_at on existing records?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I missed this bug because I`m manually setting updated_at on my updater.

end
end

def hooks(name)
Expand Down Expand Up @@ -817,7 +820,8 @@ def unload_relation(col)
end

def initialize_data_columns(column, value) #nodoc
self.attributes = {column => value || self.class.default(column)}
_value = value.nil? ? self.class.default(column) : value
self.attributes = {column => _value}
end

def column_as(col) #nodoc
Expand Down
2 changes: 1 addition & 1 deletion motion_model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "motion_model"
gem.require_paths = ["lib"]
gem.add_dependency 'bubble-wrap', '1.3.0'
gem.add_dependency 'bubble-wrap', '>= 1.3.0'
gem.add_dependency 'motion-support', '>= 0.2.2'
gem.version = MotionModel::VERSION
end