go-on-rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2839e5d56004e5a3974cb77427e5c3438ef3c5df
4
- data.tar.gz: 24a489fcc1fac898832b6d1269a287d0c88d7a89
3
+ metadata.gz: cd067bbca3b46eb5e2a99202de5051c0999cc9b1
4
+ data.tar.gz: c512f1d35f70162b32ff9b072e8097e30558471f
5
5
  SHA512:
6
- metadata.gz: 6107aa404f72156d23e5191b0414c5d5c6d391eaca61e68cf48031f17d55c627fe4c30e8f46a2aeb6939c850835e2c83504354155ad369ffd03f7a64f443a7c5
7
- data.tar.gz: 3a3a53bc94bbc3d8ce3b9d48b8d6140b8e1e2b19699843d13ed97206cdc9345b809330092b350cbf66c5afa97de2bba5ee99b5b582cd2cfde5c456f3edbe9032
6
+ metadata.gz: b0e8e5466e9b097c88c7c7507a28d95e80f120897ca7796858e2ddfbad4b7eac258cbcc9849d3d31e1bc0e750773bd4dba89f77ec55771b124e7bf0784265951
7
+ data.tar.gz: '0946cdda0badf2dce152b99a933ccf46ce20d6f7f3e9db55e97b62c61b7e621af3cb10a0e5392374de7a7cb42618a8b554e3936f066f7157e6ca64a6f00c8315'
data/README.md CHANGED
@@ -88,3 +88,6 @@ When I had the idea to convert Rails app or build Golang app with Rails tools, I
88
88
  - Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
89
89
  - Send me a pull request. Bonus points for topic branches.
90
90
 
91
+ ## License
92
+
93
+ See the [LICENSE](https://github.com/goonr/go-on-rails/blob/master/MIT-LICENSE) file.
@@ -27,7 +27,7 @@ func Find<%= @model_name %>(id int64) (*<%= @model_name %>, error) {
27
27
  return nil, errors.New("Invalid id: it can't be zero")
28
28
  }
29
29
  var_<%= model_name_underscore %> := <%= @model_name %>{}
30
- err := db.Get(&var_<%= model_name_underscore %>, "SELECT * FROM <%= table_name %> WHERE id = $1 LIMIT 1", id)
30
+ err := db.Get(&var_<%= model_name_underscore %>, `SELECT * FROM <%= table_name %> WHERE id = ? LIMIT 1`, id)
31
31
  if err != nil {
32
32
  log.Printf("Error: %v\n", err)
33
33
  return nil, err
@@ -60,7 +60,7 @@ func Find<%= @model_name.pluralize %>(ids ...int64) ([]<%= @model_name %>, error
60
60
  // Find<%= @model_name %>By find a single <%= model_name_underscore %> by a field name and a value
61
61
  func Find<%= @model_name %>By(field string, val interface{}) (*<%= @model_name %>, error) {
62
62
  var_<%= model_name_underscore %> := <%= @model_name %>{}
63
- sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = $1 LIMIT 1`
63
+ sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = ? LIMIT 1`
64
64
  sqlStr := fmt.Sprintf(sqlFmt, field)
65
65
  err := db.Get(&var_<%= model_name_underscore %>, sqlStr, val)
66
66
  if err != nil {
@@ -72,7 +72,7 @@ func Find<%= @model_name %>By(field string, val interface{}) (*<%= @model_name %
72
72
 
73
73
  // Find<%= @model_name.pluralize %>By find all <%= param_name_plural %> by a field name and a value
74
74
  func Find<%= @model_name.pluralize %>By(field string, val interface{}) (var_<%= param_name_plural %> []<%= @model_name %>, err error) {
75
- sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = $1`
75
+ sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = ?`
76
76
  sqlStr := fmt.Sprintf(sqlFmt, field)
77
77
  err = db.Select(&var_<%= param_name_plural %>, sqlStr, val)
78
78
  if err != nil {
@@ -86,17 +86,99 @@ func Find<%= @model_name.pluralize %>By(field string, val interface{}) (var_<%=
86
86
  func All<%= @model_name.pluralize %>() (<%= param_name_plural %> []<%= @model_name %>, err error) {
87
87
  err = db.Select(&<%= table_name %>, "SELECT * FROM <%= table_name %>")
88
88
  if err != nil {
89
- log.Print(err)
89
+ log.Println(err)
90
90
  return nil, err
91
91
  }
92
92
  return <%= param_name_plural %>, nil
93
93
  }
94
94
 
95
+ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interface{}) (var_<%= param_name_plural %> []<%= @model_name %>, err error) {
96
+ var_<%= param_name_plural %>, err = Find<%= @model_name.pluralize %>Where(sql, args...)
97
+ if err != nil {
98
+ log.Println(err)
99
+ return nil, err
100
+ }
101
+ if len(assocs) == 0 {
102
+ log.Println("No associated fields ard specified")
103
+ return var_<%= param_name_plural %>, err
104
+ }
105
+ ids := make([]interface{}, len(var_<%= param_name_plural %>))
106
+ for _, v := range var_<%= param_name_plural %> {
107
+ ids = append(ids, interface{}(v.Id))
108
+ }
109
+ <%- unless @struct_info[:assoc_info][:has_many].empty? && @struct_info[:assoc_info][:has_one].empty? -%>
110
+ idsHolder := strings.Repeat(",?", len(ids)-1)
111
+ for _, assoc := range assocs {
112
+ switch assoc {
113
+ <%- unless @struct_info[:assoc_info][:has_many].empty? -%>
114
+ <%- has_many = @struct_info[:assoc_info][:has_many] -%>
115
+ <%- has_many.each do |k, v| -%>
116
+ case "<%= k.underscore.pluralize %>":
117
+ <%- if v[:foreign_key] -%>
118
+ where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
119
+ <%- else -%>
120
+ where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
121
+ <%- end -%>
122
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
123
+ if err != nil {
124
+ log.Printf("Error when query associated objects: %v\n", assoc)
125
+ continue
126
+ }
127
+ for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
128
+ for _, vvv := range var_<%= param_name_plural %> {
129
+ <%- if v[:foreign_key] -%>
130
+ if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
131
+ vvv.<%= k %> = append(vvv.<%= k %>, vv)
132
+ }
133
+ <%- else -%>
134
+ if vv.<%= @model_name.camelize %>Id == vvv.Id {
135
+ vvv.<%= k %> = append(vvv.<%= k %>, vv)
136
+ }
137
+ <%- end -%>
138
+ }
139
+ }
140
+ <%- end -%>
141
+ <%- end -%>
142
+ <%- unless @struct_info[:assoc_info][:has_one].empty? -%>
143
+ <%- has_one = @struct_info[:assoc_info][:has_one] -%>
144
+ <%- has_one.each do |k, v| -%>
145
+ case "<%= k %>":
146
+ <%- if v[:foreign_key] -%>
147
+ where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
148
+ <%- else -%>
149
+ where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
150
+ <%- end -%>
151
+ var_<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
152
+ if err != nil {
153
+ log.Printf("Error when query associated objects: %v\n", assoc)
154
+ continue
155
+ }
156
+ for _, vv := range var_<%= v[:class_name].underscore.pluralize %> {
157
+ for _, vvv := range var_<%= param_name_plural %> {
158
+ <%- if v[:foreign_key] -%>
159
+ if vv.<%= v[:foreign_key].camelize %> == vvv.Id {
160
+ vvv.<%= k %> = vv
161
+ }
162
+ <%- else -%>
163
+ if vv.<%= @model_name.camelize %>Id == vvv.Id {
164
+ vvv.<%= k %> = vv
165
+ }
166
+ <%- end -%>
167
+ }
168
+ }
169
+ <%- end -%>
170
+ <%- end -%>
171
+ }
172
+ }
173
+ <%- end -%>
174
+ return var_<%= param_name_plural %>, nil
175
+ }
176
+
95
177
  // <%= @model_name %>Ids get all the Ids of <%= @model_name %> records
96
178
  func <%= @model_name %>Ids() (ids []int64, err error) {
97
179
  err = db.Select(&ids, "SELECT id FROM <%= table_name %>")
98
180
  if err != nil {
99
- log.Print(err)
181
+ log.Println(err)
100
182
  return nil, err
101
183
  }
102
184
  return ids, nil
@@ -116,12 +198,12 @@ func <%= @model_name %>IntCol(col, where string, args ...interface{}) (intColRec
116
198
  }
117
199
  stmt, err := db.Preparex(sql)
118
200
  if err != nil {
119
- log.Print(err)
201
+ log.Println(err)
120
202
  return nil, err
121
203
  }
122
204
  err = stmt.Select(&intColRecs, args...)
123
205
  if err != nil {
124
- log.Print(err)
206
+ log.Println(err)
125
207
  return nil, err
126
208
  }
127
209
  return intColRecs, nil
@@ -135,12 +217,12 @@ func <%= @model_name %>StrCol(col, where string, args ...interface{}) (strColRec
135
217
  }
136
218
  stmt, err := db.Preparex(sql)
137
219
  if err != nil {
138
- log.Print(err)
220
+ log.Println(err)
139
221
  return nil, err
140
222
  }
141
223
  err = stmt.Select(&strColRecs, args...)
142
224
  if err != nil {
143
- log.Print(err)
225
+ log.Println(err)
144
226
  return nil, err
145
227
  }
146
228
  return strColRecs, nil
@@ -156,12 +238,12 @@ func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<
156
238
  }
157
239
  stmt, err := db.Preparex(sql)
158
240
  if err != nil {
159
- log.Print(err)
241
+ log.Println(err)
160
242
  return nil, err
161
243
  }
162
244
  err = stmt.Select(&<%= param_name_plural %>, args...)
163
245
  if err != nil {
164
- log.Print(err)
246
+ log.Println(err)
165
247
  return nil, err
166
248
  }
167
249
  return <%= param_name_plural %>, nil
@@ -173,12 +255,12 @@ func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<
173
255
  func Find<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (<%= param_name_plural %> []<%= @model_name %>, err error) {
174
256
  stmt, err := db.Preparex(sql)
175
257
  if err != nil {
176
- log.Print(err)
258
+ log.Println(err)
177
259
  return nil, err
178
260
  }
179
261
  err = stmt.Select(&<%= param_name_plural %>, args...)
180
262
  if err != nil {
181
- log.Print(err)
263
+ log.Println(err)
182
264
  return nil, err
183
265
  }
184
266
  return <%= param_name_plural %>, nil
@@ -206,12 +288,12 @@ func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
206
288
  sqlStr := fmt.Sprintf(sqlFmt, strings.Join(keys, ","), ":"+strings.Join(keys, ",:"))
207
289
  result, err := db.NamedExec(sqlStr, am)
208
290
  if err != nil {
209
- log.Print(err)
291
+ log.Println(err)
210
292
  return 0, err
211
293
  }
212
294
  lastId, err := result.LastInsertId()
213
295
  if err != nil {
214
- log.Print(err)
296
+ log.Println(err)
215
297
  return 0, err
216
298
  }
217
299
  return lastId, nil
@@ -324,18 +406,23 @@ func Destroy<%= @model_name %>(id int64) error {
324
406
  <%- ass_cols.each_value do |opts| -%>
325
407
  <%- if opts.key? :dependent -%>
326
408
  <%- if opts[:through] -%>
327
- <%= opts[:class_name].underscore %>_ids, err := <%= opts[:through].to_s.singularize.camelize %>IntCol("<%= opts[:class_name].underscore %>_id", "<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
328
- if err != nil {
329
- return err
330
- }
331
- _, err = Destroy<%= opts[:class_name].pluralize %>(<%= opts[:class_name].underscore %>_ids...)
332
- if err != nil {
333
- return err
334
- }
335
- _, err = Destroy<%= opts[:through].to_s.pluralize.camelize %>Where("<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
336
- if err != nil {
337
- return err
338
- }
409
+ <%- case opts[:through] -%>
410
+ <%- when :destroy, :delete_all -%>
411
+ <%= opts[:class_name].underscore %>_ids, err := <%= opts[:through].to_s.singularize.camelize %>IntCol("<%= opts[:class_name].underscore %>_id", "<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
412
+ if err != nil {
413
+ return err
414
+ }
415
+ _, err = Destroy<%= opts[:class_name].pluralize %>(<%= opts[:class_name].underscore %>_ids...)
416
+ if err != nil {
417
+ return err
418
+ }
419
+ _, err = Destroy<%= opts[:through].to_s.pluralize.camelize %>Where("<%= model_name_underscore %>_id = ?", var_<%= model_name_underscore %>.Id)
420
+ if err != nil {
421
+ return err
422
+ }
423
+ <%- when :nullify -%>
424
+ <%#- FIXME: Need a DeleteAssociation func -%>
425
+ <%- end -%>
339
426
  <%- else -%>
340
427
  <%- if opts.key? :foreign_key -%>
341
428
  Destroy<%= opts[:class_name].pluralize %>Where("<%= opts[:foreign_key] %> = ?", id)
@@ -448,7 +535,7 @@ func Update<%= @model_name %>(id int64, am map[string]interface{}) error {
448
535
  sqlStr := fmt.Sprintf(sqlFmt, strings.Join(setKeysArr, ", "), id)
449
536
  _, err := db.NamedExec(sqlStr, am)
450
537
  if err != nil {
451
- log.Print(err)
538
+ log.Println(err)
452
539
  return err
453
540
  }
454
541
  return nil
metadata CHANGED
@@ -1,18 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - B1nj0y
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-14 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Modeling, developing and testing your Golang app with your familiar Rails
14
- tools like rails generate, db migration, console etc. It also help integrating some
15
- api written in Golang to existed Rails app for high performance.
14
+ tools like rails generate, db migration, console etc. It is more meant to help integrating
15
+ some APIs written in Golang to existed Rails app for high performance.
16
16
  email: idegorepl@gmail.com
17
17
  executables: []
18
18
  extensions: []