labilerecord 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,22 @@
1
+ == 0.0.9 2009-05-07
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+ * 2 :
6
+ * Bumped version for github
7
+ * 3 :
8
+ * Changed Query.data to Query.rows
9
+ * 4 :
10
+ * Access Model[x] instea of Model.rows[x]
11
+ * 5 :
12
+ * Fixed bug introduced in 0.0.4 always referencing row[0]
13
+ * 6 :
14
+ * Fix incorrect field.type_id
15
+ * 7 :
16
+ * Increment revision; minor bug fix
17
+ * 8 :
18
+ * Added Query.to_insert_sql to transform dataset into
19
+ an insert statement ... handy for data copies.
20
+ * 9 :
21
+ * Attempt to load alternate postgresql adapter
22
+ * Fix readme example typo
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/labilerecord.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_labilerecord.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,2 @@
1
+
2
+ For more information on labilerecord, see http://github.com/adacosta/labilerecord
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ = labilerecord
2
+
3
+ * http://github.com/adacosta/labilerecord
4
+
5
+ == DESCRIPTION:
6
+
7
+ * Simple data access for dynamic data sets through postgres with ruby
8
+
9
+ == FEATURES:
10
+
11
+ * Access data by row[x].column
12
+ * Access field types
13
+ * Access types which ActiveRecord won't (e.g. _int4)
14
+ * Easy to use on tables, views, and functions
15
+ * Fast - no ruby type conversion on values from connection adapter
16
+ * Simple sql generation for data copy using Query.to_insert_sql
17
+
18
+ == SYNOPSIS:
19
+
20
+ require 'rubygems'
21
+ require 'labilerecord'
22
+
23
+ LabileRecord::Base.connection = { :dbname => 'postgres',
24
+ :user => user,
25
+ :pass => pass,
26
+ :host => host,
27
+ :port => port}
28
+
29
+ databases = LabileRecord::Query.new("SELECT * FROM pg_database")
30
+ # execute the query
31
+ databases.exec!
32
+ # inspect rows
33
+ puts databases.inspect
34
+ # inspect fields (columns)
35
+ puts databases.fields.inspect
36
+ # inspect specific row and column of data set
37
+ # datname - being a column returned by the query
38
+ puts databases[0].datname
39
+
40
+ == REQUIREMENTS:
41
+
42
+ * pg gem (gem install pg)
43
+
44
+ == INSTALL:
45
+
46
+ sudo gem sources -a http://gems.github.com
47
+ sudo gem install adacosta-labilerecord
48
+
49
+ == LICENSE:
50
+
51
+ (The MIT License)
52
+
53
+ Copyright (c) 2009 Alan Da Costa
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining
56
+ a copy of this software and associated documentation files (the
57
+ 'Software'), to deal in the Software without restriction, including
58
+ without limitation the rights to use, copy, modify, merge, publish,
59
+ distribute, sublicense, and/or sell copies of the Software, and to
60
+ permit persons to whom the Software is furnished to do so, subject to
61
+ the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be
64
+ included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
67
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
70
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
71
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
72
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/labilerecord'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('labilerecord', LabileRecord::VERSION) do |p|
7
+ p.developer('Alan Da Costa', 'alandacosta@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,144 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module LabileRecord
5
+ VERSION = '0.0.10'
6
+
7
+ begin
8
+ require 'pg'
9
+ rescue LoadError
10
+ begin
11
+ require 'postgres'
12
+ rescue LoadError
13
+ raise LoadError, 'no postgres adapters available to load; ensure rubygems or a postgres connection adapter path is required'
14
+ end
15
+ end
16
+
17
+ class Base
18
+ class << self
19
+ def connection
20
+ @connection
21
+ end
22
+
23
+ def connection=(*args)
24
+ @connection = PGconn.open(*args)
25
+ end
26
+ end
27
+ end
28
+
29
+ class Query < Array
30
+ attr_reader :result
31
+ attr_reader :fields
32
+ attr_reader :string
33
+
34
+ def initialize(query_string)
35
+ @string = query_string
36
+ end
37
+
38
+ def exec!
39
+ @result = connection.exec(@string)
40
+ parse_fields
41
+ parse_result_data
42
+ self
43
+ end
44
+
45
+ def parse_result_data
46
+ columns = @result.fields
47
+ row_count = @result.num_tuples
48
+ field_names = @fields.map {|field| field.name}
49
+ # iterate rows
50
+ (0..(row_count-1)).each do |row_index|
51
+ row = Row.new(field_names)
52
+ columns.each do |column_name|
53
+ row << @result[row_index][column_name]
54
+ end
55
+ send "<<", row
56
+ end
57
+ end
58
+
59
+ def parse_fields
60
+ @fields = @field_names = []
61
+ @result.fields.each_with_index do |field_name, i|
62
+ pg_field_type_id = @result.ftype(i)
63
+ type = connection.exec("SELECT typname FROM
64
+ pg_type WHERE oid = #{pg_field_type_id}")
65
+ field_type_name = type[0][type.fields[0]].to_s
66
+ @fields << Field.new( field_name, field_type_name, pg_field_type_id)
67
+ end
68
+ end
69
+
70
+ def connection
71
+ LabileRecord::Base.connection
72
+ end
73
+
74
+ def to_insert_sql(table_name=nil, quote="'")
75
+ # return: [INSERT INTO table_name] (column_list) VALUES(value_list);
76
+ sql = ""
77
+ each do |row|
78
+ non_nil_column_names = []
79
+ non_nil_values = []
80
+ row.each_with_index do |column, i|
81
+ non_nil_column_names << fields[i].name if !column.nil?
82
+ non_nil_values << column if !column.nil?
83
+ end
84
+ sql += %Q[
85
+ #{"INSERT INTO " + table_name.to_s if table_name} (#{ non_nil_column_names.map { |c| quote_object(c) } * "," }) VALUES (#{ non_nil_values.map { |c| quote_value(c, quote) } * "," });
86
+ ].strip + "\n"
87
+ end
88
+ sql
89
+ end
90
+
91
+ def to_static_set_sql(quote="'")
92
+ rows_sql = ""
93
+ self.each_with_index do |row, row_index|
94
+ row_sql = "SELECT "
95
+ self.fields.each_with_index do |field, field_index|
96
+ row_field_value = row.send(field.name.to_sym)
97
+ # builds: value + cast as column + [',' if not last row]
98
+ row_sql << (row_field_value ? quote_value(row_field_value, quote) : "NULL")
99
+ row_sql << "::" + field.type + %Q[ AS "#{field.name}"]
100
+ row_sql << ", " if field_index < self.fields.length - 1
101
+ end
102
+ row_sql << " UNION\n" if row_index < self.length - 1
103
+ rows_sql << row_sql
104
+ end
105
+ rows_sql
106
+ end
107
+
108
+ private
109
+
110
+ def quote_value(string, quote="'")
111
+ quote + string.to_s + quote
112
+ end
113
+
114
+ def quote_object(string)
115
+ '"' + string.to_s + '"'
116
+ end
117
+ end
118
+
119
+ class Field
120
+ attr_reader :name
121
+ attr_reader :type
122
+ attr_reader :type_id
123
+
124
+ def initialize(name, type, type_id)
125
+ @name = name
126
+ @type = type
127
+ @type_id = type_id
128
+ end
129
+ end
130
+
131
+ class Row < Array
132
+ def initialize(fields)
133
+ @fields = fields
134
+ end
135
+
136
+ def method_missing(meth, *args)
137
+ if ( field_index = @fields.index(meth.id2name) )
138
+ at field_index
139
+ else
140
+ super(meth, *args)
141
+ end
142
+ end
143
+ end
144
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/labilerecord.rb'}"
9
+ puts "Loading labilerecord gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/labilerecord'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestLabilerecord < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: labilerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - Alan Da Costa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: pg
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.8.0
44
+ version:
45
+ description: "* Simple data access for dynamic data sets through postgres with ruby"
46
+ email:
47
+ - alandacosta@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - PostInstall.txt
56
+ - README.rdoc
57
+ files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - PostInstall.txt
61
+ - README.rdoc
62
+ - Rakefile
63
+ - lib/labilerecord.rb
64
+ - script/console
65
+ - script/destroy
66
+ - script/generate
67
+ - test/test_helper.rb
68
+ - test/test_labilerecord.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/adacosta/labilerecord
71
+ licenses: []
72
+
73
+ post_install_message: PostInstall.txt
74
+ rdoc_options:
75
+ - --main
76
+ - README.rdoc
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: labilerecord
94
+ rubygems_version: 1.3.5
95
+ signing_key:
96
+ specification_version: 2
97
+ summary: "* Simple data access for dynamic data sets through postgres with ruby"
98
+ test_files:
99
+ - test/test_helper.rb
100
+ - test/test_labilerecord.rb