automodel-sqlserver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +81 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/.yardopts +1 -0
  8. data/Gemfile +6 -0
  9. data/Gemfile.lock +82 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +225 -0
  12. data/Rakefile +6 -0
  13. data/automodel-sqlserver.gemspec +39 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/docs/Automodel.html +161 -0
  17. data/docs/Automodel/AdapterAlreadyRegistered.html +144 -0
  18. data/docs/Automodel/AdapterAlreadyRegisteredError.html +144 -0
  19. data/docs/Automodel/CannotFindOnCompoundPrimaryKey.html +144 -0
  20. data/docs/Automodel/Connectors.html +117 -0
  21. data/docs/Automodel/Error.html +140 -0
  22. data/docs/Automodel/FindOnCompoundPrimaryKeyError.html +144 -0
  23. data/docs/Automodel/Helpers.html +722 -0
  24. data/docs/Automodel/NameCollisionError.html +143 -0
  25. data/docs/Automodel/SchemaInspector.html +1046 -0
  26. data/docs/Automodel/UnregisteredAdapter.html +144 -0
  27. data/docs/_index.html +206 -0
  28. data/docs/class_list.html +51 -0
  29. data/docs/css/common.css +1 -0
  30. data/docs/css/full_list.css +58 -0
  31. data/docs/css/style.css +496 -0
  32. data/docs/file.README.html +333 -0
  33. data/docs/file_list.html +56 -0
  34. data/docs/frames.html +17 -0
  35. data/docs/index.html +333 -0
  36. data/docs/js/app.js +292 -0
  37. data/docs/js/full_list.js +216 -0
  38. data/docs/js/jquery.js +4 -0
  39. data/docs/method_list.html +155 -0
  40. data/docs/top-level-namespace.html +478 -0
  41. data/lib/automodel.rb +132 -0
  42. data/lib/automodel/automodel.rb +4 -0
  43. data/lib/automodel/connectors.rb +8 -0
  44. data/lib/automodel/errors.rb +24 -0
  45. data/lib/automodel/helpers.rb +141 -0
  46. data/lib/automodel/schema_inspector.rb +218 -0
  47. data/lib/automodel/version.rb +10 -0
  48. data/samples/database.yml +38 -0
  49. metadata +259 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da6778caab75ce70c76ac47800481cb06e93e8b5
4
+ data.tar.gz: 5b16c56f2a195569b818fe353a489baeafc862da
5
+ SHA512:
6
+ metadata.gz: 0f5533c643c01671712102512daa355947cfbfd8616baa48c836241c290a9609e4e1154ec50956e0052ba157045a22ccda08824e880a8071f25099f2a6201a7a
7
+ data.tar.gz: 68db03e12bc8c2cbfea6fdf8095d8213415ba65cf1df68dcffbe69f621f3220eae98957a0295e27631f86f53e4cfa9f35210af9599b24275d98fc930dcb4e831
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /pkg/
6
+ /spec/reports/
7
+ /tmp/
8
+ /true/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,81 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Include:
4
+ - "**/*.rake"
5
+ - "**/Gemfile"
6
+ - "**/Rakefile"
7
+ - "**/Capfile"
8
+ - "**/Berksfile"
9
+ - "**/Cheffile"
10
+ Exclude:
11
+ - "vendor/**/*"
12
+ - "db/**/*"
13
+ - "tmp/**/*"
14
+ - "true/**/*"
15
+ Metrics/ClassLength:
16
+ Description: Avoid classes longer than 100 lines of code.
17
+ Enabled: false
18
+ CountComments: false
19
+ Max: 100
20
+ Metrics/LineLength:
21
+ Description: Limit lines to 100 characters.
22
+ Enabled: false
23
+ Max: 100
24
+ Metrics/BlockLength:
25
+ Exclude:
26
+ - 'spec/**/*.rb'
27
+ Metrics/MethodLength:
28
+ Description: Avoid methods longer than 10 lines of code.
29
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
30
+ Enabled: false
31
+ CountComments: false
32
+ Max: 10
33
+ Metrics/AbcSize:
34
+ Description: A calculated magnitude based on number of assignments, branches, and conditions.
35
+ Enabled: false
36
+ Max: 15
37
+ Metrics/CyclomaticComplexity:
38
+ Description: A complexity metric that is strongly correlated to the number of test cases needed to validate a method.
39
+ Enabled: false
40
+ Max: 6
41
+ Lint/Debugger:
42
+ Description: Warn in debugger entries
43
+ Enabled: false
44
+ Style/SymbolArray:
45
+ Description: Use %i or %I for arrays of symbols.
46
+ Enabled: false
47
+ Style/RegexpLiteral:
48
+ Description: Enforces using / or %r around regular expressions.
49
+ EnforcedStyle: percent_r
50
+ Style/AsciiComments:
51
+ # Disabling this so we can use non-breaking spaces (' ') in documentation comments, preventing browsers from collapsing multiple spaces in code blocks.
52
+ Description: This cop checks for non-ascii (non-English) characters in comments.
53
+ Enabled: false
54
+ Style/NumericLiterals:
55
+ Description: This cop checks for big numeric literals without _ between groups of digits in them.
56
+ Enabled: false
57
+ Style/Documentation:
58
+ Description: Document classes and non-namespace modules.
59
+ Enabled: false
60
+ Style/ClassAndModuleChildren:
61
+ Description: Use nested modules/class definitions instead of compact style.
62
+ Enabled: false
63
+ Style/FrozenStringLiteralComment:
64
+ Enabled: false
65
+ Style/EmptyMethod:
66
+ Enabled: false
67
+ Style/StderrPuts:
68
+ Enabled: true
69
+ Exclude:
70
+ - 'bin/**/*'
71
+ Style/BlockDelimiters:
72
+ Description: Check for uses of braces or do/end around single line or multi-line blocks.
73
+ Enabled: true
74
+ Exclude:
75
+ - 'spec/**/*.rb'
76
+ Style/RescueModifier:
77
+ Description: This cop checks for uses of rescue in its modifier form.
78
+ Enabled: false
79
+ Naming/UncommunicativeMethodParamName:
80
+ Description: This cop checks method parameter names for how descriptive they are.
81
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.4.4
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1 @@
1
+ --output-dir=./docs --no-private --markup=markdown --format=html
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in automodel.gemspec
6
+ gemspec
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ automodel-sqlserver (0.1.0)
5
+ activerecord (~> 4.2)
6
+ activerecord-sqlserver-adapter (~> 4.2.0)
7
+ tiny_tds (~> 2.1.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (4.2.10)
13
+ activesupport (= 4.2.10)
14
+ builder (~> 3.1)
15
+ activerecord (4.2.10)
16
+ activemodel (= 4.2.10)
17
+ activesupport (= 4.2.10)
18
+ arel (~> 6.0)
19
+ activerecord-sqlserver-adapter (4.2.18)
20
+ activerecord (~> 4.2.1)
21
+ activesupport (4.2.10)
22
+ i18n (~> 0.7)
23
+ minitest (~> 5.1)
24
+ thread_safe (~> 0.3, >= 0.3.4)
25
+ tzinfo (~> 1.1)
26
+ arel (6.0.4)
27
+ builder (3.2.3)
28
+ byebug (10.0.2)
29
+ coderay (1.1.2)
30
+ concurrent-ruby (1.0.5)
31
+ diff-lcs (1.3)
32
+ i18n (0.9.5)
33
+ concurrent-ruby (~> 1.0)
34
+ method_source (0.9.0)
35
+ minitest (5.11.3)
36
+ mysql2 (0.5.1)
37
+ pg (0.21.0)
38
+ pry (0.11.3)
39
+ coderay (~> 1.1.0)
40
+ method_source (~> 0.9.0)
41
+ pry-byebug (3.6.0)
42
+ byebug (~> 10.0)
43
+ pry (~> 0.10)
44
+ rake (10.5.0)
45
+ rspec (3.7.0)
46
+ rspec-core (~> 3.7.0)
47
+ rspec-expectations (~> 3.7.0)
48
+ rspec-mocks (~> 3.7.0)
49
+ rspec-core (3.7.1)
50
+ rspec-support (~> 3.7.0)
51
+ rspec-expectations (3.7.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.7.0)
54
+ rspec-mocks (3.7.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.7.0)
57
+ rspec-support (3.7.1)
58
+ rspec_junit_formatter (0.4.1)
59
+ rspec-core (>= 2, < 4, != 2.12.0)
60
+ sqlite3 (1.3.13)
61
+ thread_safe (0.3.6)
62
+ tiny_tds (2.1.2)
63
+ tzinfo (1.2.5)
64
+ thread_safe (~> 0.1)
65
+
66
+ PLATFORMS
67
+ ruby
68
+
69
+ DEPENDENCIES
70
+ automodel-sqlserver!
71
+ bundler (~> 1.16)
72
+ mysql2 (~> 0.5.1)
73
+ pg (~> 0.21)
74
+ pry (~> 0.11)
75
+ pry-byebug (~> 3.6)
76
+ rake (~> 10.0)
77
+ rspec (~> 3.0)
78
+ rspec_junit_formatter (~> 0.3)
79
+ sqlite3 (~> 1.3.13)
80
+
81
+ BUNDLED WITH
82
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nestor Custodio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,225 @@
1
+ # automodel-sqlserver [![Gem Version](https://badge.fury.io/rb/automodel-sqlserver.svg)](https://badge.fury.io/rb/automodel-sqlserver)
2
+
3
+ Connecting your Rails application to a database created outside of the Rails environment usually means either spending hours writing up class files for every table, or giving up on using the ActiveRecord query DSL and resigning yourself to building SQL strings and making `execute`/`exec_query` calls.
4
+
5
+ Are those SQL strings you're building even injection-safe? Hmm... 😟
6
+
7
+ *With a single command*, **automodel-sqlserver** lets you connect to any database and access all of its tables via the ActiveRecord DSL you've grown to love!
8
+
9
+ It does this by analyzing the table structures and:
10
+ - automatically defining all of the corresponding model classes
11
+ - declaring column aliases so you can use Railsy names an idioms
12
+ - constructing model relations based on foreign key definitions
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'automodel-sqlserver'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install automodel-sqlserver
30
+
31
+
32
+ #### Additional Dependencies
33
+
34
+ If you are running on Windows and **not** using RubyInstaller, the above steps are all that is needed.
35
+
36
+ On all other platforms, this gem (and its dependencies) alone are not sufficient to connect to a SQL Server instance: **you will also need to [install FreeTDS on your system](https://github.com/rails-sqlserver/tiny_tds#install) if you haven't already**.
37
+
38
+
39
+ ## Using Automodel
40
+
41
+ The following examples all assume a Postgres database with the following tables:
42
+ ```sql
43
+ -- Create Table: Authors
44
+ CREATE TABLE public."Authors" (
45
+ "Author ID" serial NOT NULL ,
46
+ "Name" varchar NOT NULL DEFAULT '',
47
+ "Birthday" date NULL ,
48
+ "Address" varchar NOT NULL DEFAULT '',
49
+
50
+ CONSTRAINT authors__pk PRIMARY KEY ("Author ID")
51
+ ) WITH ( OIDS=FALSE );
52
+
53
+
54
+ -- Create Table: Publishers
55
+ CREATE TABLE public."Publishers" (
56
+ "Publisher ID" serial NOT NULL ,
57
+ "Name" varchar NOT NULL DEFAULT '',
58
+ "Address" varchar NOT NULL DEFAULT '',
59
+ "Website" varchar NOT NULL DEFAULT '',
60
+
61
+ CONSTRAINT publishers__pk PRIMARY KEY ("Publisher ID")
62
+ ) WITH ( OIDS=FALSE );
63
+
64
+
65
+ -- Create Table: Books
66
+ CREATE TABLE public."Books" (
67
+ "Book ID" serial NOT NULL ,
68
+ "Title" varchar NOT NULL DEFAULT '' ,
69
+ "Edition" int NOT NULL DEFAULT 1 ,
70
+ "ISBN Number" varchar NOT NULL DEFAULT '' ,
71
+ "Published On" date NOT NULL ,
72
+ "Is Out Of Print" bool NOT NULL DEFAULT FALSE,
73
+ "Author ID" bigint NOT NULL ,
74
+ "Publisher ID" bigint NOT NULL ,
75
+
76
+ CONSTRAINT books__pk PRIMARY KEY ("Book ID"),
77
+
78
+ CONSTRAINT books_authors_fk FOREIGN KEY ("Author ID")
79
+ REFERENCES public."Authors"("Author ID"),
80
+
81
+ CONSTRAINT books_publishers_fk FOREIGN KEY ("Publisher ID")
82
+ REFERENCES public."Publishers"("Publisher ID")
83
+ ) WITH ( OIDS=FALSE );
84
+ ```
85
+
86
+ ---
87
+
88
+ #### Connecting To The External Database
89
+
90
+ You can provide the connection spec inline ...
91
+
92
+ ```ruby
93
+ automodel adapter: 'postgresql' ,
94
+ encoding: 'unicode' ,
95
+ host: hostname ,
96
+ port: port_number ,
97
+ username: username ,
98
+ password: password ,
99
+ database: database_name
100
+ ```
101
+
102
+ ... or you can use a connection spec defined in "config/database.yml" ...
103
+
104
+ ```yml
105
+ ## In "database.yml" ...
106
+
107
+ ## ... (your application's own db connection stuff) ...
108
+
109
+
110
+ external_db:
111
+ adapter: postgresql
112
+ pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
113
+ timeouts: 5000
114
+ encoding: unicode
115
+ host: name_or_ip
116
+ port: port_number
117
+ username: username
118
+ password: password
119
+ database: sample_db
120
+ ```
121
+
122
+ ```ruby
123
+ ## In "config/puma.rb" or "config/unicorn.rb" ...
124
+
125
+ automodel :external_db
126
+ ```
127
+
128
+ ---
129
+
130
+ #### Using The Automodel'ed Objects
131
+
132
+ Connecting via either method above will allow you to issue all of the following expressions, just as
133
+ if these were your own models:
134
+
135
+ ```ruby
136
+ ## ISBNs for all non-first-edition books.
137
+ isbn_list = Book.where.not(edition: 1).pluck(:isbn_number)
138
+
139
+ ## Take any book and look up some values.
140
+ book = Book.take
141
+ book.title
142
+ book.out_of_print?
143
+ book.publisher.name
144
+
145
+
146
+ ## Note that some ActiveRecord constructs surface real table names,
147
+ ## which can look awkward in code when working with tables with non-Railsy names:
148
+ ## (the uppercase "P" in "Publishers" below makes it look like a class reference)
149
+ Book.joins(:Publishers).where(Publishers: { name: test_value }).all
150
+ ```
151
+
152
+ ---
153
+
154
+ #### Automodel With Namespacing
155
+
156
+ If you're worried about model name collisions (or just want to keep the global namespace tidy), Automodel can define all of the new model classes under a module.
157
+
158
+ ```ruby
159
+ automodel adapter: 'postgresql' ,
160
+ encoding: 'unicode' ,
161
+ host: hostname ,
162
+ port: port_number ,
163
+ username: username ,
164
+ password: password ,
165
+ database: database_name,
166
+ namespace: 'ExternalDB'
167
+
168
+ ## Now you can do everything you'd expect, but the models are namespaced under ExternalDB.
169
+ ExternalDB::Book.find(5) ## => Book #5
170
+ ExternalDB::Book.take.author.class ## => ExternalDB::Author
171
+
172
+ ```
173
+
174
+ ---
175
+
176
+ [Consult the repo docs for the full **automodel-sqlserver** documentation.](http://nestor-custodio.github.io/automodel-sqlserver/top-level-namespace.html#automodel-instance_method)
177
+
178
+
179
+ ## FAQs
180
+
181
+ - ##### Do I have to add anything to my Gemfile besides `'automodel-sqlserver'`?
182
+ Due to the nature of this version of the gem, it is assumed you will want to connect to a SQL Server database, so both **[activerecord-sqlserver-adapter](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter)** and the **[tiny_tds](https://github.com/rails-sqlserver/tiny_tds)** gems are included as dependencies (meaning you don't have to worry about them). Note that these gems alone are not sufficient to connect to a SQL Server instance, however: **you still need to [install FreeTDS on your system](https://github.com/rails-sqlserver/tiny_tds#install) if you haven't already**.
183
+
184
+ SQL Server aside, you will need to add the corresponding gems if you want to use connection adapters that are not yet part of your gemset. (e.g. Don't expect to be able to connect to a MySQL database without having added `'mysql2'` to your Gemfile.)
185
+
186
+ - ##### But what about my application's own models?
187
+ You can call `automodel` **and** continue to use your application's own models without changing a single line of code.
188
+
189
+ - ##### Can I Automodel more than one database?
190
+ Yes! You can Automodel as many databases with as many different adapters as you like. **automodel-sqlserver** takes care of connecting to the various databases and managing their connection pools for you.
191
+
192
+ - ##### What about model name collisions?
193
+ If an `automodel` call will result in a class name collision, an Automodel::NameCollisionError is raised *before* any classes are clobbered.
194
+
195
+ - ##### What if I want custom methods for certain models?
196
+ You can either monkey-patch your methods onto the applicable Automodel-generated classes once they've been defined, or you can monkey-patch the method onto the connection handler class returned by the `automodel` call itself, which will make it available for all models generated *by that call*.
197
+
198
+ - ##### What if I'm using ActiveRecord but not Rails?
199
+ That's no problem at all! The **automodel-sqlserver** gem depends on ActiveRecord -- not Rails. Adding `'automodel-sqlserver'` to your Gemfile (along with any relevant connection adapters, of course) is all you need to make use of the tool in your vanilla-Ruby project. Just be mindful that -- since "config/database.yml" isn't available (as you're not using Rails) -- you'll always need to pass in a full connection spec to your `automodel` calls (as in the very first example, under *"Connecting To The External Database"* above).
200
+
201
+
202
+ ## Feature Roadmap / Future Development
203
+
204
+ Additional features/options coming in the future:
205
+
206
+ - **Naming**: Better generation of Railsy names for `:date`/`:datetime` column types.
207
+ - **Reads**: Support for `#find` on tables with composite primary keys.
208
+ - **Writes**: Better handling of missing `created_at`/`updated_at` columns on record creation/updates.
209
+ - **Traversal**: Support for `has_many` relations (only `belongs_to` is currently supported).
210
+ - **Traversal**: Support for self-referential foreign keys.
211
+ - **Traversal**: Support for multiple relations to the same target model.
212
+
213
+
214
+ ## Contribution / Development
215
+
216
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nestor-custodio/automodel-sqlserver.
217
+
218
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
219
+
220
+ Linting is courtesy of [Rubocop](https://github.com/bbatsov/rubocop) and documentation is built using [Yard](https://yardoc.org/). Neither is included in the Gemspec; you'll need to install these locally (`gem install rubocop yard`) to take advantage.
221
+
222
+
223
+ ## License
224
+
225
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).