dm-metamapper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,7 +77,7 @@ module DataMapper
77
77
  raise NoTemplateError, "Template does not exist at path #{template.full_path}"
78
78
  end
79
79
 
80
- compiled = ERB.new(File.read(template.full_path)).result(binding)
80
+ compiled = ERB.new(File.read(template.full_path), nil, "%<>-").result(binding)
81
81
  path = respond_to?(:output_path) ? output_path(model, template) : template.output_name
82
82
  if !File.exists?(path) || File.read(path) != compiled
83
83
  File.open(path, 'w') {|f| f << compiled}
@@ -17,10 +17,10 @@ module DataMapper
17
17
  VALID_TYPES = [:global, :model]
18
18
 
19
19
  def initialize(name, opts={})
20
- @name = name.to_s
21
- @generator = parse_generator(opts.delete(:generator))
22
- @type = parse_type(opts.delete(:type))
23
- @template = (opts.delete(:template) || @name).to_s + ".erb"
20
+ @name = name.to_s
21
+ @generator = parse_generator(opts.delete(:generator))
22
+ @type = parse_type(opts.delete(:type))
23
+ @template_name = parse_template(opts.delete(:template))
24
24
  end
25
25
 
26
26
  attr_reader :name, :type
@@ -30,23 +30,25 @@ module DataMapper
30
30
  end
31
31
 
32
32
  def full_path
33
- File.join(@generator.config.template_dir, @template)
33
+ File.join(@generator.config.template_dir, @template_name)
34
34
  end
35
35
 
36
36
  private
37
37
 
38
38
  def parse_type(type)
39
- unless type && VALID_TYPES.include?(type.to_sym)
40
- raise InvalidTypeError, "type `#{type}' is not a recognized template " +
41
- "type. Valid types: #{VALID_TYPES.inspect}]"
42
- end
39
+ return type if type && VALID_TYPES.include?(type.to_sym)
43
40
 
44
- type
41
+ raise InvalidTypeError, "type `#{type}' is not a recognized template " +
42
+ "type. Valid types: #{VALID_TYPES.inspect}]"
45
43
  end
46
44
 
47
45
  def parse_generator(generator)
48
46
  generator || raise(NoGeneratorError, "opts did not contain :generator")
49
47
  end
48
+
49
+ def parse_template(template)
50
+ (template || @name).to_s + ".erb"
51
+ end
50
52
  end
51
53
  end
52
54
  end
@@ -1,6 +1,6 @@
1
1
  module DataMapper
2
2
  module MetaMapper
3
- VERSION = "0.1.0" unless defined?(::DataMapper::MetaMapper::VERSION)
3
+ VERSION = "0.1.1" unless defined?(::DataMapper::MetaMapper::VERSION)
4
4
  end
5
5
 
6
6
  end
@@ -1,5 +1,5 @@
1
- <% class_name = decolonize(model.name) %>
2
- <% all_caps_name = class_name.upcase %>
1
+ <%- class_name = decolonize(model.name) -%>
2
+ <%- all_caps_name = class_name.upcase -%>
3
3
  #ifndef T_<%= all_caps_name %>
4
4
  #define T_<%= all_caps_name %>
5
5
  #include "O_<%= class_name %>.hpp"
@@ -22,17 +22,18 @@ public:
22
22
  {
23
23
  _tables.push_back("<%= model.storage_name %>");
24
24
  }
25
- <% many_to_one.each do |r| %>
26
- <% parent = decolonize(r[1].parent_model_name) %>
25
+
26
+ <%- many_to_one.each do |r| -%>
27
+ <%- parent = decolonize(r[1].parent_model_name) -%>
27
28
  T_<%= class_name %>(const T_<%= parent %>& parent)
28
29
  {
29
30
  _tables.push_back("<%= model.storage_name %>");
30
31
  _tables.insert(_tables.end(),
31
- parent._tables.begin(), parent._tables.end());
32
+ parent._tables.begin(), parent._tables.end());
32
33
 
33
34
  _constraint._cond = "(<%= model.storage_name + "." + r[1].child_key.first.name.to_s %> = " + parent._tables[0] + ".<%= r[1].parent_key.first.name.to_s %>)";
34
35
  if (!parent._constraint.nil())
35
- _constraint._cond += " AND " + parent._constraint._cond;
36
+ _constraint._cond += " AND " + parent._constraint._cond;
36
37
  }
37
38
 
38
39
  T_<%= class_name %>(const I_<%= parent %>& parentId)
@@ -41,9 +42,9 @@ public:
41
42
 
42
43
  _constraint._cond = "(<%= model.storage_name + "." + r[1].child_key.first.name.to_s %> = " + parentId.to_s() + ")";
43
44
  }
44
- <% end %>
45
+ <%- end -%>
45
46
 
46
- <% model.generated_properties.each do |property| %>
47
+ <%- model.generated_properties.each do |property| -%>
47
48
  struct E_<%= property.name %>{
48
49
  E_<%= property.name %>()
49
50
  {
@@ -57,14 +58,14 @@ public:
57
58
  E_<%= property.name %> _<%= property.name %>(){
58
59
  return E_<%= property.name %>();
59
60
  }
60
- <% end %>
61
+ <%- end -%>
61
62
 
62
63
  void getFields(std::vector<std::string>& rFields)
63
64
  {
64
65
  rFields.clear();
65
- <% model.generated_properties.each do |property| %>
66
+ <%- model.generated_properties.each do |property| -%>
66
67
  rFields.push_back("<%= property.name %>");
67
- <% end %>
68
+ <%- end -%>
68
69
  }
69
70
 
70
71
  void select(const Condition& c,
@@ -79,21 +80,21 @@ public:
79
80
  additional, res);
80
81
  r.resize(res.size());
81
82
  for(size_t i = 0; i < res.size(); ++i){
82
- <% model.generated_properties.each do |property| %>
83
+ <%- model.generated_properties.each do |property| -%>
83
84
  r[i]._f_<%= property.name %>._base =
84
- <% if DataMapper::Property::Enum === property %>
85
+ <%- if DataMapper::Property::Enum === property -%>
85
86
  (<%= property.cpp_name %>::Base)UTILS::fromString<size_t>(res[i]["<%= property.name %>"]);
86
- <% else %>
87
+ <%- else -%>
87
88
  UTILS::fromString<<%= property.cpp_name %>::Base>(res[i]["<%= property.name %>"]);
88
- <% end %>
89
- <% end %>
89
+ <%- end -%>
90
+ <%- end -%>
90
91
  }
91
92
  }
92
93
 
93
94
  size_t count(const Condition& c)
94
95
  {
95
96
  Condition c1 = _constraint.nil() ? c : _constraint && c;
96
- return DBFace::instance()->count(_tables, c1._cond);
97
+ return DBFace::instance()->count(_tables, c1._cond);
97
98
  }
98
99
 
99
100
  void erase(const Condition& c)
@@ -101,6 +102,7 @@ public:
101
102
  Condition c1 = _constraint.nil() ? c : _constraint && c;
102
103
  DBFace::instance()->erase(_tables, c1._cond);
103
104
  }
105
+
104
106
  void erase()
105
107
  {
106
108
  DBFace::instance()->erase(_tables, _constraint._cond);
@@ -113,6 +115,7 @@ public:
113
115
  Condition c1 = _constraint.nil() ? c : _constraint && c;
114
116
  select(c1, "", r);
115
117
  }
118
+
116
119
  void select(std::vector<O_<%= class_name %>>& r)
117
120
  {
118
121
  select(_constraint, "", r);
@@ -137,9 +140,29 @@ public:
137
140
  return first(_constraint);
138
141
  }
139
142
 
140
-
141
-
142
-
143
+ bool insertAllFields(std::vector<O_<%= class_name %>>::const_iterator begin,
144
+ std::vector<O_<%= class_name %>>::const_iterator end)
145
+ {
146
+ std::vector<std::string> fields;
147
+ <%- model.generated_properties.each do |property| -%>
148
+ <% if property.name != model.serial.name %>
149
+ fields.push_back(std::string("<%= property.name %>"));
150
+ <%- end -%>
151
+ <%- end -%>
152
+ std::vector<std::vector<std::string> > rows;
153
+ for (; begin != end; ++begin){
154
+ const O_<%= class_name %>& r = *begin;
155
+ std::vector<std::string> row;
156
+ <%- model.generated_properties.each do |property| -%>
157
+ <% if property.name != model.serial.name %>
158
+ row.push_back(UTILS::toString(r._<%= property.name %>()));
159
+ <%- end -%>
160
+ <%- end -%>
161
+ rows.push_back(row);
162
+ }
163
+ return DBFace::instance()->insert("<%= model.storage_name %>",
164
+ fields, rows);
165
+ }
143
166
 
144
167
  Condition _constraint;
145
168
  std::vector<std::string> _tables;
@@ -87,7 +87,8 @@ DBFace::select(const vector<string>& tables,
87
87
  for (size_t i = 0; i < mysqlRes.num_rows(); ++i){
88
88
  rRes.resize(rRes.size() + 1);
89
89
  for (size_t j = 0; j < columns.size(); ++j)
90
- rRes.back()[columns[j]] = UTILS::toString(mysqlRes[i][columns[j].c_str()]);
90
+ if (!mysqlRes[i][columns[j].c_str()].is_null())
91
+ rRes.back()[columns[j]] = UTILS::toString(mysqlRes[i][columns[j].c_str()]);
91
92
  }
92
93
  return true;
93
94
  }
@@ -167,6 +168,39 @@ DBFace::getLastInsertId(Query& rQuery, size_t& rId)
167
168
  return true;
168
169
  }
169
170
 
171
+ bool
172
+ DBFace::insert(const string& table,
173
+ const vector<string>& fields,
174
+ const vector<vector<string> >& rows)
175
+ {
176
+ if (fields.size() == 0 || rows.size() == 0)
177
+ return true;
178
+
179
+ Query q = _connection.query();
180
+ q << "INSERT INTO " << table << " (";
181
+ for (size_t i = 0; i < fields.size(); ++i){
182
+ if (i != 0)
183
+ q << ",";
184
+ q << fields[i];
185
+ }
186
+ q << ")VALUES";
187
+ for (size_t i = 0; i < rows.size(); ++i){
188
+ if (i != 0)
189
+ q << ",";
190
+ q << "(";
191
+ for(size_t j = 0; j < rows[i].size(); ++j){
192
+ if (j != 0)
193
+ q << ",";
194
+ q << quote << rows[i][j];
195
+ }
196
+ q << ")";
197
+ }
198
+ log(q.str());
199
+ bool ok = executeQuery(q);
200
+ return ok;
201
+ }
202
+
203
+
170
204
  bool
171
205
  DBFace::insert(const string& table,
172
206
  const map<string, string>& field2Val,
@@ -23,6 +23,11 @@ public:
23
23
  bool insert(const std::string& table,
24
24
  const std::map<std::string, std::string>& field2Val,
25
25
  size_t& rInsertId);
26
+ bool insert(const std::string& table,
27
+ const std::vector<std::string>& fields,
28
+ const std::vector<std::vector<std::string> >& rows);
29
+
30
+
26
31
  bool update(const std::string& table,
27
32
  const std::map<std::string, std::string>& field2Val,
28
33
  const std::string& where);
@@ -5,12 +5,11 @@
5
5
 
6
6
  namespace DMMM {
7
7
 
8
- <% models.each do |model| %>
9
- <% model_name = decolonize(model.name) %>
8
+ <%- models.each do |model| -%>
9
+ <%- model_name = decolonize(model.name) -%>
10
10
  class DummyO_<%= model_name %>;
11
11
  typedef ID::Id<DummyO_<%= model_name %>> I_<%= model_name %>;
12
- <% end %>
13
-
12
+ <%- end -%>
14
13
 
15
14
  } //namespace DMMM
16
15
  #endif //DMMM_IDENTIFIERS_HPP
@@ -1,5 +1,5 @@
1
- <% class_name = decolonize(model.name) %>
2
- <% all_caps_name = class_name.upcase %>
1
+ <%- class_name = decolonize(model.name) -%>
2
+ <%- all_caps_name = class_name.upcase -%>
3
3
  #ifndef O_<%= all_caps_name %>
4
4
  #define O_<%= all_caps_name %>
5
5
 
@@ -10,16 +10,13 @@
10
10
  #include "dmmm_identifiers.hpp"
11
11
  #include "dmmm_fields.hpp"
12
12
  #include "dmmm_comparators.hpp"
13
- <% many_to_one.each do |relative| %>
13
+ <%- many_to_one.each do |relative| -%>
14
14
  #include "T_<%= decolonize(relative[1].parent_model.to_s) %>.hpp"
15
- <% end %>
15
+ <%- end -%>
16
16
 
17
- <%# self.relationships.select{|r,m| m.class.name == 'DataMapper::Associations::OneToMany::Relationship'}.each do |relative| %>
18
- <%# end %>
19
-
20
- <% model.enums.each do |name, property| %>
17
+ <%- model.enums.each do |name, property| -%>
21
18
  enum Enum<%= class_name %><%= name %> { <%= property.flag_map.map{|v, k| class_name.upcase + "_" + property.name.to_s.upcase + "_" + k.to_s.sub(".","_").upcase + " = " + v.to_s}.join(", ") %> };
22
- <% end %>
19
+ <%- end -%>
23
20
 
24
21
  namespace DMMM {
25
22
 
@@ -27,24 +24,24 @@ class O_<%= class_name %>{
27
24
  public:
28
25
 
29
26
  O_<%= class_name %>() {}
30
- <% many_to_one.each do |r| %>
31
- <% parent = decolonize(r[1].parent_model_name) %>
27
+ <%- many_to_one.each do |r| -%>
28
+ <%- parent = decolonize(r[1].parent_model_name) -%>
32
29
  O_<%= class_name %>(const O_<%= parent %>& parent)
33
30
  : _f_<%= r[1].child_key.first.name.to_s %>(parent._<%= r[1].parent_key.first.name.to_s %>())
34
31
  {}
35
32
  O_<%= class_name %>(const I_<%= parent %>& parent_id)
36
33
  : _f_<%= r[1].child_key.first.name.to_s %>(parent_id)
37
34
  {}
38
- <% end %>
35
+ <%- end -%>
39
36
 
40
- <% if model.serial %>
37
+ <%- if model.serial -%>
41
38
  O_<%= class_name %>(const I_<%= class_name %>& id)
42
39
  : _f_<%= model.serial.name %>(id)
43
40
  {}
44
- <% end %>
41
+ <%- end -%>
45
42
 
46
43
 
47
- <% model.generated_properties.each do |property| %>
44
+ <%- model.generated_properties.each do |property| -%>
48
45
  const <%= property.cpp_name %>::Base& _<%= property.name %>() const {
49
46
  return _f_<%= property.name %>._base;
50
47
  }
@@ -52,71 +49,71 @@ public:
52
49
  _f_<%= property.name %>._dirty = true;
53
50
  return _f_<%= property.name %>._base;
54
51
  }
55
- <% end %>
52
+ <%- end -%>
56
53
 
57
- <% if model.serial %>
54
+ <%- if model.serial -%>
58
55
  bool update(){
59
56
  std::map<std::string, std::string> field2Val;
60
- <% model.generated_properties.each do |property| %>
57
+ <%- model.generated_properties.each do |property| -%>
61
58
  if (_f_<%= property.name %>._dirty)
62
59
  field2Val["<%= property.name %>"] =
63
60
  UTILS::toString(_f_<%= property.name %>._base);
64
- <% end %>
61
+ <%- end -%>
65
62
  std::string where =
66
- "<%= model.serial.name %>=" + UTILS::toString(_f_<%= model.serial.name %>._base);
63
+ "<%= model.serial.name %>=" + UTILS::toString(_f_<%= model.serial.name %>._base);
67
64
  if (DBFace::instance()->update("<%= model.storage_name %>",
68
65
  field2Val, where))
69
66
  {
70
- <% model.generated_properties.each do |property| %>
67
+ <%- model.generated_properties.each do |property| -%>
71
68
  _f_<%= property.name %>._dirty = false;
72
- <% end %>
73
- return true;
74
- }
75
- else
76
- return false;
69
+ <%- end -%>
70
+ return true;
71
+ }
72
+ else
73
+ return false;
77
74
  }
78
- <% end %>
75
+ <%- end -%>
79
76
 
80
77
  bool insert(){
81
78
  std::map<std::string, std::string> field2Val;
82
- <% model.generated_properties.each do |property| %>
79
+ <%- model.generated_properties.each do |property| -%>
83
80
  if (_f_<%= property.name %>._dirty)
84
81
  field2Val["<%= property.name %>"] =
85
82
  UTILS::toString(_f_<%= property.name %>._base);
86
- <% end %>
83
+ <%- end -%>
87
84
  <% if model.serial %>
88
85
  if (DBFace::instance()->
89
86
  insert("<%= model.storage_name %>", field2Val,
90
87
  _f_<%= model.serial.name %>._base.serialization()))
91
- <% else %>
88
+ <%- else -%>
92
89
  size_t id;
93
90
  if (DBFace::instance()->
94
91
  insert("<%= model.storage_name %>", field2Val,
95
- id))
96
- <% end %>
92
+ id))
93
+ <%- end -%>
97
94
  {
98
- <% model.generated_properties.each do |property| %>
95
+ <%- model.generated_properties.each do |property| -%>
99
96
  _f_<%= property.name %>._dirty = false;
100
- <% end %>
97
+ <%- end -%>
101
98
  return true;
102
99
  }
103
- else
104
- return false;
105
-
100
+ else
101
+ return false;
102
+
106
103
  }
107
104
 
108
- <% many_to_one.each do |r| %>
109
- <% parent = decolonize(r[1].parent_model_name) %>
105
+ <%- many_to_one.each do |r| -%>
106
+ <%- parent = decolonize(r[1].parent_model_name) -%>
110
107
  std::pair<O_<%= parent %>, bool> <%= r[0] %>(){
111
108
  T_<%= parent %> T(T_<%= parent %>::E_<%= r[1].parent_key.first.name.to_s %>() == _<%= r[1].child_key.first.name.to_s %>());
112
- return T.first();
109
+ return T.first();
113
110
  }
114
- <% end %>
111
+ <%- end -%>
115
112
 
116
113
  private:
117
- <% model.generated_properties.each do |property| %>
114
+ <%- model.generated_properties.each do |property| -%>
118
115
  <%= property.cpp_name %> _f_<%= property.name %>;
119
- <% end %>
116
+ <%- end -%>
120
117
 
121
118
  friend class T_<%= class_name %>;
122
119
  };
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-metamapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jonah Honeyman
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-19 00:00:00 +02:00
19
+ date: 2011-02-03 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies: []
22
22