| 115 | /// Panics if the rows have different column sets from what've previously been cached in the query statement |
| 116 | #[allow(clippy::should_implement_trait)] |
| 117 | pub fn add<M>(mut self, m: M) -> Self |
| 118 | where |
| 119 | M: IntoActiveModel<A>, |
| 120 | { |
| 121 | let mut am: A = m.into_active_model(); |
| 122 | self.primary_key = |
| 123 | if !<<A::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::auto_increment() { |
| 124 | am.get_primary_key_value() |
| 125 | } else { |
| 126 | None |
| 127 | }; |
| 128 | let mut columns = Vec::new(); |
| 129 | let mut values = Vec::new(); |
| 130 | let columns_empty = self.columns.is_empty(); |
| 131 | for (idx, col) in <A::Entity as EntityTrait>::Column::iter().enumerate() { |
| 132 | let av = am.take(col); |
| 133 | let av_has_val = av.is_set() || av.is_unchanged(); |
| 134 | if columns_empty { |
| 135 | self.columns.push(av_has_val); |
| 136 | } else if self.columns[idx] != av_has_val { |
| 137 | panic!("columns mismatch"); |
| 138 | } |
| 139 | match av { |
| 140 | ActiveValue::Set(value) | ActiveValue::Unchanged(value) => { |
| 141 | columns.push(col); |
| 142 | values.push(col.save_as(Expr::val(value))); |
| 143 | } |
| 144 | ActiveValue::NotSet => {} |
| 145 | } |
| 146 | } |
| 147 | self.query.columns(columns); |
| 148 | self.query.values_panic(values); |
| 149 | self |
| 150 | } |
| 151 | |
| 152 | /// Add many Models to Self. This is the legacy implementation priori to `1.1.3`. |
| 153 | /// |