mass_insert 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.rvmrc +53 -0
- data/.travis.yml +4 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +45 -0
- data/lib/mass_insert.rb +13 -0
- data/lib/mass_insert/adapters.rb +12 -0
- data/lib/mass_insert/adapters/abstract_query.rb +47 -0
- data/lib/mass_insert/adapters/adapter.rb +56 -0
- data/lib/mass_insert/adapters/column_value.rb +107 -0
- data/lib/mass_insert/adapters/helpers.rb +8 -0
- data/lib/mass_insert/adapters/helpers/sanitizer.rb +17 -0
- data/lib/mass_insert/adapters/helpers/timestamp.rb +38 -0
- data/lib/mass_insert/adapters/mysql2_adapter.rb +21 -0
- data/lib/mass_insert/adapters/postgresql_adapter.rb +15 -0
- data/lib/mass_insert/adapters/sqlite3_adapter.rb +37 -0
- data/lib/mass_insert/adapters/sqlserver_adapter.rb +23 -0
- data/lib/mass_insert/base.rb +43 -0
- data/lib/mass_insert/process_control.rb +24 -0
- data/lib/mass_insert/query_builder.rb +42 -0
- data/lib/mass_insert/query_execution.rb +29 -0
- data/lib/mass_insert/version.rb +3 -0
- data/mass_insert.gemspec +22 -0
- data/spec/active_record_dummy/.gitignore +15 -0
- data/spec/active_record_dummy/Gemfile +40 -0
- data/spec/active_record_dummy/README.rdoc +261 -0
- data/spec/active_record_dummy/Rakefile +7 -0
- data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
- data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
- data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
- data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
- data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/user.rb +3 -0
- data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/active_record_dummy/config.ru +4 -0
- data/spec/active_record_dummy/config/application.rb +68 -0
- data/spec/active_record_dummy/config/boot.rb +6 -0
- data/spec/active_record_dummy/config/database.yml +54 -0
- data/spec/active_record_dummy/config/environment.rb +5 -0
- data/spec/active_record_dummy/config/environments/development.rb +37 -0
- data/spec/active_record_dummy/config/environments/mysql2.rb +37 -0
- data/spec/active_record_dummy/config/environments/postgresql.rb +37 -0
- data/spec/active_record_dummy/config/environments/production.rb +67 -0
- data/spec/active_record_dummy/config/environments/sqlite3.rb +37 -0
- data/spec/active_record_dummy/config/environments/test.rb +37 -0
- data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
- data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
- data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
- data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
- data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/active_record_dummy/config/locales/en.yml +5 -0
- data/spec/active_record_dummy/config/routes.rb +58 -0
- data/spec/active_record_dummy/db/migrate/20130412154541_create_users.rb +14 -0
- data/spec/active_record_dummy/db/schema.rb +27 -0
- data/spec/active_record_dummy/db/seeds.rb +7 -0
- data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
- data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/active_record_dummy/log/.gitkeep +0 -0
- data/spec/active_record_dummy/public/404.html +26 -0
- data/spec/active_record_dummy/public/422.html +26 -0
- data/spec/active_record_dummy/public/500.html +25 -0
- data/spec/active_record_dummy/public/favicon.ico +0 -0
- data/spec/active_record_dummy/public/index.html +241 -0
- data/spec/active_record_dummy/public/robots.txt +5 -0
- data/spec/active_record_dummy/script/rails +6 -0
- data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/active_record_models/column_types/binary_spec.rb +60 -0
- data/spec/active_record_models/column_types/boolean_spec.rb +52 -0
- data/spec/active_record_models/column_types/decimal_spec.rb +49 -0
- data/spec/active_record_models/column_types/integer_spec.rb +49 -0
- data/spec/active_record_models/column_types/string_spec.rb +50 -0
- data/spec/active_record_models/model_spec.rb +50 -0
- data/spec/dummy_models/test.rb +5 -0
- data/spec/mass_insert/adapters/abstract_query_spec.rb +109 -0
- data/spec/mass_insert/adapters/adapter_spec.rb +117 -0
- data/spec/mass_insert/adapters/column_value_spec.rb +292 -0
- data/spec/mass_insert/adapters/helpers/sanitizer_spec.rb +39 -0
- data/spec/mass_insert/adapters/helpers/timestamp_spec.rb +79 -0
- data/spec/mass_insert/adapters/helpers_spec.rb +16 -0
- data/spec/mass_insert/adapters/mysql_adapter_spec.rb +39 -0
- data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +29 -0
- data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +90 -0
- data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +56 -0
- data/spec/mass_insert/adapters_spec.rb +40 -0
- data/spec/mass_insert/base_spec.rb +98 -0
- data/spec/mass_insert/process_control_spec.rb +56 -0
- data/spec/mass_insert/query_builder_spec.rb +88 -0
- data/spec/mass_insert/query_execution_spec.rb +53 -0
- data/spec/mass_insert_spec.rb +28 -0
- data/spec/spec_helper.rb +6 -0
- metadata +254 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p392@mass_insert"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.19.1 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
33
|
+
else
|
34
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
35
|
+
rvm --create "$environment_id" || {
|
36
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
37
|
+
return 1
|
38
|
+
}
|
39
|
+
fi
|
40
|
+
|
41
|
+
# If you use bundler, this might be useful to you:
|
42
|
+
# if [[ -s Gemfile ]] && {
|
43
|
+
# ! builtin command -v bundle >/dev/null ||
|
44
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
45
|
+
# }
|
46
|
+
# then
|
47
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
48
|
+
# gem install bundler
|
49
|
+
# fi
|
50
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
51
|
+
# then
|
52
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
53
|
+
# fi
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# MassInsert [![Build Status](https://travis-ci.org/alejandrogutierrez/mass_insert.png?branch=master)](https://travis-ci.org/alejandrogutierrez/mass_insert)
|
2
|
+
|
3
|
+
This gem aims to provide an easy and faster way to do single database insertions in Rails.
|
4
|
+
Support Mysql, PostgreSQL, SQLite3 and SQLServer adapters.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'mass_insert'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle install
|
15
|
+
|
16
|
+
Or install it yourself with:
|
17
|
+
|
18
|
+
$ gem install mass_insert
|
19
|
+
|
20
|
+
## Basic Usage
|
21
|
+
|
22
|
+
To use MassInsert gem you need to call mass_insert method from your ActiveRecord model and pass it an array with the values that you want to persist into the database.
|
23
|
+
|
24
|
+
The array of values:
|
25
|
+
|
26
|
+
values = [
|
27
|
+
{
|
28
|
+
:name => "Jay",
|
29
|
+
:email => "tremendous_gamer@gmail.com",
|
30
|
+
:age => 15
|
31
|
+
},
|
32
|
+
{
|
33
|
+
:name => "Beverly",
|
34
|
+
:email => "nippy_programmer@gmail.com",
|
35
|
+
:age => 24
|
36
|
+
},
|
37
|
+
{
|
38
|
+
:name => "Scottie",
|
39
|
+
:email => "angry_programmer@gmail.com",
|
40
|
+
:age => 32
|
41
|
+
}
|
42
|
+
]
|
43
|
+
|
44
|
+
And call mass_insert method:
|
45
|
+
|
46
|
+
User.mass_insert(values)
|
47
|
+
|
48
|
+
## Attention
|
49
|
+
|
50
|
+
Since this is a single database insertion your model validation will be ignored, then if you use this gem you need to be sure that information is OK to be persisted.
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
namespace :spec do
|
4
|
+
desc "Prepares the environment to use the gem"
|
5
|
+
task :prepare do
|
6
|
+
system("
|
7
|
+
bundle install
|
8
|
+
cd spec/active_record_dummy/
|
9
|
+
bundle install
|
10
|
+
rake db:drop db:create db:migrate RAILS_ENV=mysql2
|
11
|
+
rake db:drop db:create db:migrate RAILS_ENV=postgresql
|
12
|
+
rake db:drop db:create db:migrate RAILS_ENV=sqlite3
|
13
|
+
")
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Runs all unit tests"
|
17
|
+
task :all do
|
18
|
+
system("echo '\e[00;32m\033[1mRunning all unit tests...\e[00m'")
|
19
|
+
system("bundle exec rspec spec/mass_insert_spec.rb spec/mass_insert")
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Runs all the mysql2 specs"
|
23
|
+
task :mysql2 do
|
24
|
+
ENV["RAILS_ENV"] = "mysql2"
|
25
|
+
system("echo '\e[00;32m\033[1mRunning the Mysql2 adapter tests...\e[00m'")
|
26
|
+
system("bundle exec rspec spec/active_record_models")
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Runs all the Postgresql specs"
|
30
|
+
task :postgresql do
|
31
|
+
ENV["RAILS_ENV"] = "postgresql"
|
32
|
+
system("echo '\e[00;32m\033[1mRunning the Postgresql adapter tests...\e[00m'")
|
33
|
+
system("bundle exec rspec spec/active_record_models")
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Runs all the Sqlite3 specs"
|
37
|
+
task :sqlite3 do
|
38
|
+
ENV["RAILS_ENV"] = "sqlite3"
|
39
|
+
system("echo '\e[00;32m\033[1mRunning the Sqlite3 adapter tests...\e[00m'")
|
40
|
+
system("bundle exec rspec spec/active_record_models")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
task default: ['spec:mysql2', 'spec:postgresql', 'spec:sqlite3', 'spec:all']
|
data/lib/mass_insert.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "mass_insert/version"
|
2
|
+
|
3
|
+
module MassInsert
|
4
|
+
autoload :Adapters, 'mass_insert/adapters.rb'
|
5
|
+
autoload :Base, 'mass_insert/base.rb'
|
6
|
+
autoload :ProcessControl, 'mass_insert/process_control.rb'
|
7
|
+
autoload :QueryBuilder, 'mass_insert/query_builder.rb'
|
8
|
+
autoload :QueryExecution, 'mass_insert/query_execution.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
if defined?(ActiveRecord::Base)
|
12
|
+
ActiveRecord::Base.extend MassInsert::Base
|
13
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
autoload :Adapter, 'mass_insert/adapters/adapter.rb'
|
4
|
+
autoload :AbstractQuery, 'mass_insert/adapters/abstract_query.rb'
|
5
|
+
autoload :ColumnValue, 'mass_insert/adapters/column_value.rb'
|
6
|
+
autoload :Helpers, 'mass_insert/adapters/helpers.rb'
|
7
|
+
autoload :Mysql2Adapter, 'mass_insert/adapters/mysql2_adapter.rb'
|
8
|
+
autoload :PostgreSQLAdapter, 'mass_insert/adapters/postgresql_adapter.rb'
|
9
|
+
autoload :SQLite3Adapter, 'mass_insert/adapters/sqlite3_adapter.rb'
|
10
|
+
autoload :SQLServerAdapter, 'mass_insert/adapters/sqlserver_adapter.rb'
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
module AbstractQuery
|
4
|
+
|
5
|
+
# Returns a begin string to a basic mysql query insertion. Include
|
6
|
+
# the class table_name and it's included in the string.
|
7
|
+
def begin_string
|
8
|
+
"INSERT INTO #{table_name} "
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns a string with the column names to the class table name
|
12
|
+
# and divided by commmas.
|
13
|
+
def string_columns
|
14
|
+
"(#{column_names.join(", ")}) "
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the string with all the row values that will be included
|
18
|
+
# in the sql string.
|
19
|
+
def string_values
|
20
|
+
"VALUES (#{string_rows_values});"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Gives the correct format to the values string to all rows. This
|
24
|
+
# functions calls a function that will generate a single string row
|
25
|
+
# and at the end all the strings are concatenated.
|
26
|
+
def string_rows_values
|
27
|
+
values.map{ |row| string_single_row_values(row) }.join("), (")
|
28
|
+
end
|
29
|
+
|
30
|
+
def string_single_row_values row
|
31
|
+
# Prepare the single row to be included in the sql string.
|
32
|
+
row.merge!(timestamp_values) if timestamp?
|
33
|
+
|
34
|
+
# Generates the values to this row that will be included according
|
35
|
+
# to the type column and values.
|
36
|
+
column_names.map{ |col| string_single_value(row, col) }.join(", ")
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns a single column string value with the correct format and
|
40
|
+
# according to the database configuration, column type and presence.
|
41
|
+
def string_single_value row, column
|
42
|
+
ColumnValue.new(row, column, options).build
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
# This class provides some helper methods to build the sql string that
|
4
|
+
# be executed. The methods here provides a functionality that be required
|
5
|
+
# in all the adapters.
|
6
|
+
class Adapter
|
7
|
+
include AbstractQuery
|
8
|
+
include Helpers::Timestamp
|
9
|
+
include Helpers::Sanitizer
|
10
|
+
|
11
|
+
attr_accessor :values, :options, :column_names
|
12
|
+
|
13
|
+
def initialize values, options
|
14
|
+
@values = values
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns the class like a constant that invokes the mass insert.
|
19
|
+
# Should be a class that inherits from ActiveRecord::Base.
|
20
|
+
def class_name
|
21
|
+
options[:class_name]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns a string with the database table name where all the records
|
25
|
+
# will be saved.
|
26
|
+
def table_name
|
27
|
+
options[:table_name]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an array with the column names in the database table like
|
31
|
+
# a symbols.
|
32
|
+
def table_columns
|
33
|
+
class_name.column_names.map(&:to_sym)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the array with the column names valid to be included into the
|
37
|
+
# query string according to the options.
|
38
|
+
def column_names
|
39
|
+
@column_names ||= sanitized_columns
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the primary_key column that was configured by the user.
|
43
|
+
# Default primary_key it's id
|
44
|
+
def primary_key
|
45
|
+
options[:primary_key]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the primary key mode according to the user configuration.
|
49
|
+
# Default primary key mode it's automatic.
|
50
|
+
def primary_key_mode
|
51
|
+
options[:primary_key_mode]
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
class ColumnValue
|
4
|
+
|
5
|
+
attr_accessor :row, :column, :options
|
6
|
+
|
7
|
+
def initialize row, column, options
|
8
|
+
@row = row
|
9
|
+
@column = column
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns the class that invokes the mass insert process. The class
|
14
|
+
# is in the options hash.
|
15
|
+
def class_name
|
16
|
+
options[:class_name]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a symbol with the column type in the database. The column or
|
20
|
+
# attribute should belongs to the class that invokes the mass insert.
|
21
|
+
def column_type
|
22
|
+
class_name.columns_hash[@column.to_s].type
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the value to this column in the row hash. The value is
|
26
|
+
# finding by symbol or string key to be most flexible.
|
27
|
+
def column_value
|
28
|
+
row[column.to_sym]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the string with the database adapter name usually in the
|
32
|
+
# database.yml file in your Rails project.
|
33
|
+
def adapter
|
34
|
+
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns the default value string to be included in query string.
|
38
|
+
# This default value is added to the query if the row hash does not
|
39
|
+
# contains the database column value.
|
40
|
+
def default_value
|
41
|
+
default_db_value ? default_db_value.to_s : "null"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Return the database default value using methods that ActiveRecord
|
45
|
+
# provides to see database columns settings.
|
46
|
+
def default_db_value
|
47
|
+
class_name.columns_hash[@column.to_s].default
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a single column string value with the correct format and
|
51
|
+
# according to the database configuration, column type and presence.
|
52
|
+
def build
|
53
|
+
self.send "column_value_#{column_type}".to_sym
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the correct value when the column value is string, text,
|
57
|
+
# date, time, datetime, timestamp. There are alias method to the
|
58
|
+
# other column types that need a similar query value.
|
59
|
+
def column_value_string
|
60
|
+
column_value.nil? ? default_value : "'#{column_value}'"
|
61
|
+
end
|
62
|
+
alias :column_value_text :column_value_string
|
63
|
+
alias :column_value_date :column_value_string
|
64
|
+
alias :column_value_time :column_value_string
|
65
|
+
alias :column_value_datetime :column_value_string
|
66
|
+
alias :column_value_timestamp :column_value_string
|
67
|
+
alias :column_value_binary :column_value_string
|
68
|
+
|
69
|
+
# Returns the correct value to column value is integer. If the row
|
70
|
+
# hash does not include the value to this column return the default
|
71
|
+
# value according to database configuration.
|
72
|
+
def column_value_integer
|
73
|
+
column_value.nil? ? default_value : column_value.to_i.to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns the correct value to column value is decimal. There is an
|
77
|
+
# alias method to float type. If the row hash does not include the
|
78
|
+
# value to this column return the default value according to database
|
79
|
+
# configuration.
|
80
|
+
def column_value_decimal
|
81
|
+
column_value.nil? ? default_value : column_value.to_f.to_s
|
82
|
+
end
|
83
|
+
alias :column_value_float :column_value_decimal
|
84
|
+
|
85
|
+
# Returns the correct value to column value is boolean. If the row
|
86
|
+
# hash does not include the value to this column return the default
|
87
|
+
# value according to database configuration.
|
88
|
+
def column_value_boolean
|
89
|
+
case adapter
|
90
|
+
when "mysql2", "postgresql", "sqlserver"
|
91
|
+
if column_value.nil?
|
92
|
+
default_value
|
93
|
+
else
|
94
|
+
column_value ? "true" : "false"
|
95
|
+
end
|
96
|
+
when "sqlite3"
|
97
|
+
if column_value.nil?
|
98
|
+
default_value
|
99
|
+
else
|
100
|
+
column_value ? "1" : "0"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|