netshade-sql_valued_columns 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/lib/sql_valued_columns.rb +80 -0
- data/rails/bootstrap.rb +1 -0
- data/sql_valued_columns.gemspec +100 -0
- data/test/sql_valued_columns_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +74 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 netshade
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= sql_valued_columns
|
2
|
+
|
3
|
+
SqlValuedColumns is an ActiveRecord plugin that will let you have specific
|
4
|
+
SQL statements executed on INSERT / UPDATE. It will call the SQL function
|
5
|
+
you provide, passing the arguments specified in the call to sql_column.
|
6
|
+
|
7
|
+
See the documentation for SqlValuedColumns::ClassMethods#sql_column for more
|
8
|
+
information regarding usage, including passing Strings and Proc objects as
|
9
|
+
arguments to your SQL function.
|
10
|
+
|
11
|
+
Example: You have a model with two columns, one named "another_column" and
|
12
|
+
the other named "size_of_another_column". Whenever you insert data into
|
13
|
+
"another_column", you want to have size_of_another_column have the result
|
14
|
+
of the SQL function LENGTH inserted into it.
|
15
|
+
|
16
|
+
class MyModel < ActiveRecord::Base
|
17
|
+
sql_column :size_of_another_column, "LENGTH", :another_column
|
18
|
+
end
|
19
|
+
|
20
|
+
Example 2: You have a model with three columns, position, latitude and longitude.
|
21
|
+
Latitude and longitude are values expressed as angles, and position is a special
|
22
|
+
datatype for your database that represents the X/Y/Z projection of that particular
|
23
|
+
latitude and longitude (example: http://www.postgresql.org/docs/8.3/static/earthdistance.html )
|
24
|
+
|
25
|
+
When you insert data with latitude and longitude, you want to automatically call a function
|
26
|
+
in your database to transform the latitude and longitude into the appropriate represenation.
|
27
|
+
|
28
|
+
class MyModel < ActiveRecord::Base
|
29
|
+
sql_column :position, "ll_to_earth", :latitude, :longitude
|
30
|
+
end
|
31
|
+
|
32
|
+
Example 3: You are an insane criminal who has somehow learned SQL. You would like to
|
33
|
+
make anyone who runs your code to suffer database punishing queries and odd security and
|
34
|
+
data formatting issues that will make them rue the day they ever learned of computers.
|
35
|
+
|
36
|
+
class MyModel < ActiveRecord::Base
|
37
|
+
sql_column :a_column, "(SELECT count(id) FROM large_list_of_things)", :raw => true
|
38
|
+
sql_column :another_column, '(SELECT count(other_id) FROM other_large_list_of_things WHERE some_column = \'#{some_model_method}\')', :raw => true
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
== Notes
|
43
|
+
|
44
|
+
No tests yet, am lazy.
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2009 Chris Zelenak. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sql_valued_columns"
|
8
|
+
gem.summary = %Q{Specify values to be inserted into the database as the result of specific SQL functions}
|
9
|
+
gem.email = "netshade@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/netshade/sql_valued_columns"
|
11
|
+
gem.authors = ["netshade"]
|
12
|
+
gem.add_dependency("activerecord", ">= 2.3.2")
|
13
|
+
gem.files.include "rails/**"
|
14
|
+
gem.description = File.read("README.rdoc")
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION.yml')
|
48
|
+
config = YAML.load(File.read('VERSION.yml'))
|
49
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
50
|
+
else
|
51
|
+
version = ""
|
52
|
+
end
|
53
|
+
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "sql_valued_columns #{version}"
|
56
|
+
rdoc.rdoc_files.include('README*')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
59
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module SqlValuedColumns
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(SqlValuedColumns::ClassMethods)
|
4
|
+
base.class_eval { include SqlValuedColumns::InstanceMethods }
|
5
|
+
base.alias_method_chain(:attributes_with_quotes, :sqlcolumns)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Includes the SqlValuedColumns module to ActiveRecord, overriding existing value insertion functionality.
|
9
|
+
def self.activate!
|
10
|
+
ActiveRecord::Base.class_eval { include ::SqlValuedColumns }
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
# Indicates the given column will have its value filled by the result of an SQL function. Pass a hash as the last
|
16
|
+
# argument with any optional args.
|
17
|
+
#
|
18
|
+
# ==== Arguments
|
19
|
+
# * <tt>column</tt> - +Symbol+, the name of the column to fill
|
20
|
+
# * <tt>function</tt> - +String+, the name of the function to execute, or the literal SQL to call if options[:raw] is true
|
21
|
+
# * <tt>args</tt> - +Array+ of arguments to pass to the SQL function specified. If an argument is a +Symbol+, the name it
|
22
|
+
# represents will be called on the model at save time. If it is a +Proc+, it will be called with the
|
23
|
+
# model passed as the only argument to the +Proc+. If it is a +String+, the +String+ will evaluated if
|
24
|
+
# the pattern #{some_code} appears within it. Once all these conditions have been exhausted, the
|
25
|
+
# resultant values will be quoted as appropriate by your database adapter and populate the arguments
|
26
|
+
# list of the SQL function you specified.
|
27
|
+
# * <tt>options</tt> (optional) - Hash of optional arguments ( :raw ) .
|
28
|
+
def sql_column(column, function, *args)
|
29
|
+
options = args.extract_options!
|
30
|
+
raw = options[:raw] == true
|
31
|
+
@sql_columns ||= { }
|
32
|
+
@sql_columns[column] = [function, args, raw]
|
33
|
+
end
|
34
|
+
|
35
|
+
def sql_columns
|
36
|
+
@sql_columns ||= { }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
|
43
|
+
def attributes_with_quotes_with_sqlcolumns(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
|
44
|
+
h = self.attributes_with_quotes_without_sqlcolumns(include_primary_key, include_readonly_attributes, attribute_names)
|
45
|
+
names = h.keys
|
46
|
+
names.each do |name|
|
47
|
+
if self.class.sql_columns[name]
|
48
|
+
function, args, raw = self.class.sql_columns[name]
|
49
|
+
unless raw
|
50
|
+
fmt_args = args.collect do |a|
|
51
|
+
case a
|
52
|
+
when Symbol
|
53
|
+
connection.quote(read_attribute(a.to_s))
|
54
|
+
when Proc
|
55
|
+
connection.quote(a.call(self))
|
56
|
+
when String
|
57
|
+
str = a
|
58
|
+
if a =~ /\#\{[^\}]+\}/
|
59
|
+
str = val('"' + str + '"')
|
60
|
+
end
|
61
|
+
connection.quote(str)
|
62
|
+
else
|
63
|
+
connection.quote(a)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
h[name] = "#{function}(#{fmt_args.join(",")})"
|
67
|
+
else
|
68
|
+
h[name] = function
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
h
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
end
|
80
|
+
|
data/rails/bootstrap.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SqlValuedColumns.activate!
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{sql_valued_columns}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["netshade"]
|
9
|
+
s.date = %q{2009-05-18}
|
10
|
+
s.description = %q{= sql_valued_columns
|
11
|
+
|
12
|
+
SqlValuedColumns is an ActiveRecord plugin that will let you have specific
|
13
|
+
SQL statements executed on INSERT / UPDATE. It will call the SQL function
|
14
|
+
you provide, passing the arguments specified in the call to sql_column.
|
15
|
+
|
16
|
+
See the documentation for SqlValuedColumns::ClassMethods#sql_column for more
|
17
|
+
information regarding usage, including passing Strings and Proc objects as
|
18
|
+
arguments to your SQL function.
|
19
|
+
|
20
|
+
Example: You have a model with two columns, one named "another_column" and
|
21
|
+
the other named "size_of_another_column". Whenever you insert data into
|
22
|
+
"another_column", you want to have size_of_another_column have the result
|
23
|
+
of the SQL function LENGTH inserted into it.
|
24
|
+
|
25
|
+
class MyModel < ActiveRecord::Base
|
26
|
+
sql_column :size_of_another_column, "LENGTH", :another_column
|
27
|
+
end
|
28
|
+
|
29
|
+
Example 2: You have a model with three columns, position, latitude and longitude.
|
30
|
+
Latitude and longitude are values expressed as angles, and position is a special
|
31
|
+
datatype for your database that represents the X/Y/Z projection of that particular
|
32
|
+
latitude and longitude (example: http://www.postgresql.org/docs/8.3/static/earthdistance.html )
|
33
|
+
|
34
|
+
When you insert data with latitude and longitude, you want to automatically call a function
|
35
|
+
in your database to transform the latitude and longitude into the appropriate represenation.
|
36
|
+
|
37
|
+
class MyModel < ActiveRecord::Base
|
38
|
+
sql_column :position, "ll_to_earth", :latitude, :longitude
|
39
|
+
end
|
40
|
+
|
41
|
+
Example 3: You are an insane criminal who has somehow learned SQL. You would like to
|
42
|
+
make anyone who runs your code to suffer database punishing queries and odd security and
|
43
|
+
data formatting issues that will make them rue the day they ever learned of computers.
|
44
|
+
|
45
|
+
class MyModel < ActiveRecord::Base
|
46
|
+
sql_column :a_column, "(SELECT count(id) FROM large_list_of_things)", :raw => true
|
47
|
+
sql_column :another_column, '(SELECT count(other_id) FROM other_large_list_of_things WHERE some_column = \'#{some_model_method}\')', :raw => true
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
== Notes
|
52
|
+
|
53
|
+
No tests yet, am lazy.
|
54
|
+
|
55
|
+
== Copyright
|
56
|
+
|
57
|
+
Copyright (c) 2009 Chris Zelenak. See LICENSE for details.
|
58
|
+
}
|
59
|
+
s.email = %q{netshade@gmail.com}
|
60
|
+
s.extra_rdoc_files = [
|
61
|
+
"LICENSE",
|
62
|
+
"README.rdoc"
|
63
|
+
]
|
64
|
+
s.files = [
|
65
|
+
".document",
|
66
|
+
".gitignore",
|
67
|
+
"LICENSE",
|
68
|
+
"README.rdoc",
|
69
|
+
"Rakefile",
|
70
|
+
"VERSION",
|
71
|
+
"lib/sql_valued_columns.rb",
|
72
|
+
"rails/bootstrap.rb",
|
73
|
+
"rails/bootstrap.rb",
|
74
|
+
"sql_valued_columns.gemspec",
|
75
|
+
"test/sql_valued_columns_test.rb",
|
76
|
+
"test/test_helper.rb"
|
77
|
+
]
|
78
|
+
s.homepage = %q{http://github.com/netshade/sql_valued_columns}
|
79
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
80
|
+
s.require_paths = ["lib"]
|
81
|
+
s.rubygems_version = %q{1.3.3}
|
82
|
+
s.summary = %q{Specify values to be inserted into the database as the result of specific SQL functions}
|
83
|
+
s.test_files = [
|
84
|
+
"test/sql_valued_columns_test.rb",
|
85
|
+
"test/test_helper.rb"
|
86
|
+
]
|
87
|
+
|
88
|
+
if s.respond_to? :specification_version then
|
89
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
90
|
+
s.specification_version = 3
|
91
|
+
|
92
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
93
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.2"])
|
94
|
+
else
|
95
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.2"])
|
96
|
+
end
|
97
|
+
else
|
98
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.2"])
|
99
|
+
end
|
100
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: netshade-sql_valued_columns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- netshade
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-18 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: "= sql_valued_columns SqlValuedColumns is an ActiveRecord plugin that will let you have specific SQL statements executed on INSERT / UPDATE. It will call the SQL function you provide, passing the arguments specified in the call to sql_column. See the documentation for SqlValuedColumns::ClassMethods#sql_column for more information regarding usage, including passing Strings and Proc objects as arguments to your SQL function. Example: You have a model with two columns, one named \"another_column\" and the other named \"size_of_another_column\". Whenever you insert data into \"another_column\", you want to have size_of_another_column have the result of the SQL function LENGTH inserted into it. class MyModel < ActiveRecord::Base sql_column :size_of_another_column, \"LENGTH\", :another_column end Example 2: You have a model with three columns, position, latitude and longitude. Latitude and longitude are values expressed as angles, and position is a special datatype for your database that represents the X/Y/Z projection of that particular latitude and longitude (example: http://www.postgresql.org/docs/8.3/static/earthdistance.html ) When you insert data with latitude and longitude, you want to automatically call a function in your database to transform the latitude and longitude into the appropriate represenation. class MyModel < ActiveRecord::Base sql_column :position, \"ll_to_earth\", :latitude, :longitude end Example 3: You are an insane criminal who has somehow learned SQL. You would like to make anyone who runs your code to suffer database punishing queries and odd security and data formatting issues that will make them rue the day they ever learned of computers. class MyModel < ActiveRecord::Base sql_column :a_column, \"(SELECT count(id) FROM large_list_of_things)\", :raw => true sql_column :another_column, '(SELECT count(other_id) FROM other_large_list_of_things WHERE some_column = \\'#{some_model_method}\\')', :raw => true end == Notes No tests yet, am lazy. == Copyright Copyright (c) 2009 Chris Zelenak. See LICENSE for details."
|
26
|
+
email: netshade@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/sql_valued_columns.rb
|
42
|
+
- rails/bootstrap.rb
|
43
|
+
- sql_valued_columns.gemspec
|
44
|
+
- test/sql_valued_columns_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
has_rdoc: false
|
47
|
+
homepage: http://github.com/netshade/sql_valued_columns
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Specify values to be inserted into the database as the result of specific SQL functions
|
72
|
+
test_files:
|
73
|
+
- test/sql_valued_columns_test.rb
|
74
|
+
- test/test_helper.rb
|