View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.insert;
17  
18  import java.util.Collection;
19  
20  import org.mybatis.dynamic.sql.insert.render.MultiRowInsertRenderer;
21  import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
22  import org.mybatis.dynamic.sql.render.RenderingStrategy;
23  import org.mybatis.dynamic.sql.util.Validator;
24  
25  public class MultiRowInsertModel<T> extends AbstractMultiRowInsertModel<T> {
26  
27      private MultiRowInsertModel(Builder<T> builder) {
28          super(builder);
29          Validator.assertNotEmpty(records(), "ERROR.20"); //$NON-NLS-1$
30          Validator.assertNotEmpty(columnMappings, "ERROR.8"); //$NON-NLS-1$
31      }
32  
33      public MultiRowInsertStatementProvider<T> render(RenderingStrategy renderingStrategy) {
34          return MultiRowInsertRenderer.withMultiRowInsertModel(this)
35                  .withRenderingStrategy(renderingStrategy)
36                  .build()
37                  .render();
38      }
39  
40      public static <T> Builder<T> withRecords(Collection<T> records) {
41          return new Builder<T>().withRecords(records);
42      }
43  
44      public static class Builder<T> extends AbstractBuilder<T, Builder<T>> {
45          @Override
46          protected Builder<T> getThis() {
47              return this;
48          }
49  
50          public MultiRowInsertModel<T> build() {
51              return new MultiRowInsertModel<>(this);
52          }
53      }
54  }