activerecord-runivedo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +21 -0
- data/Rakefile +9 -0
- data/activerecord-runivedo.gemspec +25 -0
- data/lib/active_record/connection_adapters/runivedo_adapter.rb +181 -0
- data/lib/activerecord-runivedo.rb +1 -0
- data/test/setup_test.rb +19 -0
- data/test/test_helper.rb +9 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e25a056930f94bb82573e4e8a4023f765a6bc2e3
|
4
|
+
data.tar.gz: ebe085f4b67152f3747dfff6cc408e674c0370b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cee41775147a1a6095ed14a3c6418481f07ea8243f3a4339bb1ae91ea1fca70bd4a427099be5cc8c5816fa0a417f05caf606f4b23d462623e7894d62c64845b7
|
7
|
+
data.tar.gz: 3cacb86954963ce9a00e5f1637473704e354ffedb500cd67cf26278dac8ee6456a89f90cb2dfcaba6b19b13f20ab0bbb86f228a7a3d1aa6dbc81407988a5ceef
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Lucas Clemente
|
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,21 @@
|
|
1
|
+
# Activerecord Univedo Adapter
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
In your `Gemfile`:
|
6
|
+
|
7
|
+
gem 'activerecord-runivedo'
|
8
|
+
|
9
|
+
Create your app's data perspective in [USpec](https://spec.univedo.com) and download it as an XML file to a file called `perspective.xml` in your Rails folder.
|
10
|
+
|
11
|
+
In your `config/database.yml`:
|
12
|
+
|
13
|
+
development:
|
14
|
+
adapter: runivedo
|
15
|
+
server: ws://localhost:9000/f8018f09-fb75-4d3d-8e11-44b2dc796130
|
16
|
+
app: <your app uuid>
|
17
|
+
uts: perspective.xml # The file you downloaded before
|
18
|
+
|
19
|
+
Since we're using a perspective from USpec we don't need Rails migrations. Create a file under `config/initializers/disable_migrations.rb` with the contents
|
20
|
+
|
21
|
+
Rails.configuration.middleware.delete ::ActiveRecord::Migration::CheckPending
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "activerecord-runivedo"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Lucas Clemente"]
|
9
|
+
spec.email = ["lucas@univedo.com"]
|
10
|
+
spec.summary = "ActiveRecord adapter for Univedo"
|
11
|
+
spec.homepage = "https://github.com/univedo/activerecord-runivedo"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "runivedo", "~> 0.1.0"
|
20
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
21
|
+
spec.add_development_dependency 'minitest'
|
22
|
+
spec.add_development_dependency 'minitest-emoji'
|
23
|
+
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
2
|
+
require 'active_record/connection_adapters/statement_pool'
|
3
|
+
require 'arel/visitors/bind_visitor'
|
4
|
+
|
5
|
+
require 'runivedo'
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
module ConnectionHandling # :nodoc:
|
9
|
+
def runivedo_connection(config)
|
10
|
+
raise ArgumentError, "No univedo url specified. Missing argument: server" unless config[:server]
|
11
|
+
raise ArgumentError, "No univedo app specified. Missing argument: app" unless config[:app]
|
12
|
+
|
13
|
+
url = config[:server]
|
14
|
+
app = config[:app]
|
15
|
+
uts = config[:uts] ? IO.read(config[:uts]) : nil
|
16
|
+
ConnectionAdapters::RunivedoAdapter.new(url, app, uts, logger, config)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ConnectionAdapters #:nodoc:
|
21
|
+
class RunivedoAdapter < AbstractAdapter
|
22
|
+
attr_reader :session, :perspective, :url
|
23
|
+
|
24
|
+
class Version
|
25
|
+
include Comparable
|
26
|
+
|
27
|
+
def initialize(version_string)
|
28
|
+
@version = version_string.split('.').map { |v| v.to_i }
|
29
|
+
end
|
30
|
+
|
31
|
+
def <=>(version_string)
|
32
|
+
@version <=> version_string.split('.').map { |v| v.to_i }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class BindSubstitution < Arel::Visitors::SQLite # :nodoc:
|
37
|
+
include Arel::Visitors::BindVisitor
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(url, app, uts, logger, config)
|
41
|
+
super(nil, logger)
|
42
|
+
|
43
|
+
@url = url
|
44
|
+
@app = app
|
45
|
+
@uts = uts
|
46
|
+
@result = nil
|
47
|
+
|
48
|
+
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
|
49
|
+
@visitor = Arel::Visitors::SQLite.new self
|
50
|
+
else
|
51
|
+
@visitor = unprepared_visitor
|
52
|
+
end
|
53
|
+
|
54
|
+
connect
|
55
|
+
end
|
56
|
+
|
57
|
+
def adapter_name #:nodoc:
|
58
|
+
'Runivedo'
|
59
|
+
end
|
60
|
+
|
61
|
+
def active?
|
62
|
+
!@session.closed?
|
63
|
+
end
|
64
|
+
|
65
|
+
def connect
|
66
|
+
@session = Runivedo::Session.new(@url, username: "marvin")
|
67
|
+
@session.apply_uts(@uts) if @uts
|
68
|
+
@perspective = session.get_perspective(@app)
|
69
|
+
@connection = @perspective.query
|
70
|
+
end
|
71
|
+
|
72
|
+
# Disconnects from the database if already connected. Otherwise, this
|
73
|
+
# method does nothing.
|
74
|
+
def disconnect!
|
75
|
+
super
|
76
|
+
@session.close rescue nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def reconnect!
|
80
|
+
super
|
81
|
+
disconnect! rescue nil
|
82
|
+
connect
|
83
|
+
end
|
84
|
+
|
85
|
+
def native_database_types #:nodoc:
|
86
|
+
{
|
87
|
+
:primary_key => default_primary_key_type,
|
88
|
+
:string => { :name => "varchar", :limit => 255 },
|
89
|
+
:text => { :name => "text" },
|
90
|
+
:integer => { :name => "integer" },
|
91
|
+
:float => { :name => "float" },
|
92
|
+
:decimal => { :name => "decimal" },
|
93
|
+
:datetime => { :name => "datetime" },
|
94
|
+
:timestamp => { :name => "datetime" },
|
95
|
+
:time => { :name => "time" },
|
96
|
+
:date => { :name => "date" },
|
97
|
+
:binary => { :name => "blob" },
|
98
|
+
:boolean => { :name => "boolean" }
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
# DATABASE STATEMENTS ======================================
|
103
|
+
|
104
|
+
def exec_query(sql, name = nil, binds = [])
|
105
|
+
log(sql, name, binds) do
|
106
|
+
stmt = @connection.prepare(sql)
|
107
|
+
cols = stmt.column_names
|
108
|
+
i = -1
|
109
|
+
binds_hash = Hash[binds.map { |col, val|
|
110
|
+
[i += 1, val]
|
111
|
+
}]
|
112
|
+
@result.close if @result
|
113
|
+
@result = stmt.execute(binds_hash)
|
114
|
+
records = @result.to_a
|
115
|
+
stmt.close
|
116
|
+
ActiveRecord::Result.new(cols, records)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def exec_delete(sql, name = 'SQL', binds = [])
|
121
|
+
exec_query(sql, name, binds)
|
122
|
+
@result.num_affected_rows
|
123
|
+
end
|
124
|
+
alias :exec_update :exec_delete
|
125
|
+
|
126
|
+
def last_inserted_id(result)
|
127
|
+
raise "didn't insert anything" unless @result
|
128
|
+
@result.last_inserted_id
|
129
|
+
end
|
130
|
+
|
131
|
+
def execute(sql, name = nil) #:nodoc:
|
132
|
+
log(sql, name) do
|
133
|
+
@connection.prepare(sql).execute.to_a
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def update_sql(sql, name = nil) #:nodoc:
|
138
|
+
super
|
139
|
+
@result.num_affected_rows
|
140
|
+
end
|
141
|
+
|
142
|
+
def delete_sql(sql, name = nil) #:nodoc:
|
143
|
+
sql += " WHERE 1=1" unless sql =~ /WHERE/i
|
144
|
+
super sql, name
|
145
|
+
end
|
146
|
+
|
147
|
+
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
|
148
|
+
super
|
149
|
+
id_value || @connection.last_insert_row_id
|
150
|
+
end
|
151
|
+
alias :create :insert_sql
|
152
|
+
|
153
|
+
def select_rows(sql, name = nil)
|
154
|
+
exec_query(sql, name).rows
|
155
|
+
end
|
156
|
+
|
157
|
+
# SCHEMA STATEMENTS ========================================
|
158
|
+
|
159
|
+
def tables(name = nil, table_name = nil) #:nodoc:
|
160
|
+
@perspective.get_tables
|
161
|
+
end
|
162
|
+
|
163
|
+
def primary_key(table_name)
|
164
|
+
"id"
|
165
|
+
end
|
166
|
+
|
167
|
+
def columns(table_name)
|
168
|
+
@perspective.get_fields_for_table(table_name).map do |name, datatype|
|
169
|
+
datatype = "integer" if datatype == "pk"
|
170
|
+
Column.new(name, nil, datatype)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
protected
|
175
|
+
|
176
|
+
def select(sql, name = nil, binds = []) #:nodoc:
|
177
|
+
exec_query(sql, name, binds)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/connection_adapters/runivedo_adapter"
|
data/test/setup_test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
adapter: "runivedo",
|
5
|
+
server: TEST_URL,
|
6
|
+
app: "6e5a3a08-9bb0-4d92-ad04-7c6fed3874fa",
|
7
|
+
)
|
8
|
+
|
9
|
+
class Table < ActiveRecord::Base; end
|
10
|
+
|
11
|
+
class SetupTest < MiniTest::Test
|
12
|
+
def test_count
|
13
|
+
assert Table.count > 10
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_queries
|
17
|
+
assert_equal "tables", Table.where(id: 1).first.name
|
18
|
+
end
|
19
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-runivedo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Clemente
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: runivedo
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-emoji
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- lucas@univedo.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- activerecord-runivedo.gemspec
|
110
|
+
- lib/active_record/connection_adapters/runivedo_adapter.rb
|
111
|
+
- lib/activerecord-runivedo.rb
|
112
|
+
- test/setup_test.rb
|
113
|
+
- test/test_helper.rb
|
114
|
+
homepage: https://github.com/univedo/activerecord-runivedo
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.2.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: ActiveRecord adapter for Univedo
|
138
|
+
test_files:
|
139
|
+
- test/setup_test.rb
|
140
|
+
- test/test_helper.rb
|