|
| 1 | +{% macro sqlserver__get_binding_char() %} |
| 2 | + {{ return('?') }} |
| 3 | +{% endmacro %} |
| 4 | + |
| 5 | +{% macro sqlserver__get_batch_size() %} |
| 6 | + {{ return(400) }} |
| 7 | +{% endmacro %} |
| 8 | + |
| 9 | +{% macro calc_batch_size(num_columns) %} |
| 10 | + {# |
| 11 | + SQL Server allows for a max of 2098 parameters in a single statement. |
| 12 | + Check if the max_batch_size fits with the number of columns, otherwise |
| 13 | + reduce the batch size so it fits. |
| 14 | + #} |
| 15 | + {% set max_batch_size = get_batch_size() %} |
| 16 | + {% set calculated_batch = (2098 / num_columns)|int %} |
| 17 | + {% set batch_size = [max_batch_size, calculated_batch] | min %} |
| 18 | + |
| 19 | + {{ return(batch_size) }} |
| 20 | +{% endmacro %} |
| 21 | + |
| 22 | +{% macro sqlserver__load_csv_rows(model, agate_table) %} |
| 23 | + {% set cols_sql = get_seed_column_quoted_csv(model, agate_table.column_names) %} |
| 24 | + {% set batch_size = calc_batch_size(agate_table.column_names|length) %} |
| 25 | + {% set bindings = [] %} |
| 26 | + {% set statements = [] %} |
| 27 | + |
| 28 | + {{ log("Inserting batches of " ~ batch_size ~ " records") }} |
| 29 | + |
| 30 | + {% for chunk in agate_table.rows | batch(batch_size) %} |
| 31 | + {% set bindings = [] %} |
| 32 | + |
| 33 | + {% for row in chunk %} |
| 34 | + {% do bindings.extend(row) %} |
| 35 | + {% endfor %} |
| 36 | + |
| 37 | + {% set sql %} |
| 38 | + insert into {{ this.render() }} ({{ cols_sql }}) values |
| 39 | + {% for row in chunk -%} |
| 40 | + ({%- for column in agate_table.column_names -%} |
| 41 | + {{ get_binding_char() }} |
| 42 | + {%- if not loop.last%},{%- endif %} |
| 43 | + {%- endfor -%}) |
| 44 | + {%- if not loop.last%},{%- endif %} |
| 45 | + {%- endfor %} |
| 46 | + {% endset %} |
| 47 | + |
| 48 | + {% do adapter.add_query(sql, bindings=bindings, abridge_sql_log=True) %} |
| 49 | + |
| 50 | + {% if loop.index0 == 0 %} |
| 51 | + {% do statements.append(sql) %} |
| 52 | + {% endif %} |
| 53 | + {% endfor %} |
| 54 | + |
| 55 | + {# Return SQL so we can render it out into the compiled files #} |
| 56 | + {{ return(statements[0]) }} |
| 57 | +{% endmacro %} |
0 commit comments