rb.templates.create_method.erb Maven / Gradle / Ivy
<%#
# Copyright 2011 Rapleaf
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
%>
<% if model_defn.should_make_created_at_methods && only_not_null == false %>
public <%= model_defn.model_name %> create(<%= model_defn.create_signature_full("created_at")%>) throws IOException {
return this.create(<%= model_defn.fields_with_default_created_at %>);
}
<% end %>
public <%= model_defn.model_name %> create(<%= signature %>) throws IOException {
StatementCreator statementCreator = new StatementCreator() {
private final List nonNullFields = new ArrayList<>();
private final List statementSetters = new ArrayList<>();
<% if !only_not_null || model_defn.fields.select{|f| !f.nullable?}.any? %>
{
int index = 1;
<% model_defn.fields.each do |field_defn| %>
<% if !field_defn.nullable? || !only_not_null %>
<% if field_defn.nullable? %>
if (<%=field_defn.name%> != null) {
nonNullFields.add("<%=field_defn.name%>");
int fieldIndex<%= field_defn.ordinal %> = index++;
statementSetters.add(stmt -> stmt.set<%=field_defn.prep_stmt_type%>(fieldIndex<%= field_defn.ordinal %>, <%=field_defn.prep_stmt_modifier(field_defn.name)%>));
}
<% else %>
nonNullFields.add("<%=field_defn.name%>");
int fieldIndex<%= field_defn.ordinal %> = index++;
statementSetters.add(stmt -> stmt.set<%=field_defn.prep_stmt_type%>(fieldIndex<%= field_defn.ordinal %>, <%=field_defn.prep_stmt_modifier(field_defn.name)%>));
<% end %>
<% end %>
<% end %>
}
<% end %>
@Override
public String getStatement() {
return getInsertStatement(nonNullFields);
}
@Override
public void setStatement(PreparedStatement statement) throws SQLException {
for (AttrSetter setter : statementSetters) {
setter.set(statement);
}
}
};
long __id = realCreate(statementCreator);
<% names_only = model_defn.fields.map{|field_defn| !field_defn.nullable? || !only_not_null ? field_defn.name : "null"}.join(", ") %>
<%= model_defn.model_name %> newInst = new <%= model_defn.model_name %>(__id<%= names_only.empty? ? "" : ", "%><%= names_only %>, databases);
newInst.setCreated(true);
cachedById.put(__id, newInst);
clearForeignKeyCache();
return newInst;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy