()
| 130 | } |
| 131 | |
| 132 | func (m *Upsert) updateValues() (int64, error) { |
| 133 | |
| 134 | select { |
| 135 | case <-m.SigChan(): |
| 136 | return 0, nil |
| 137 | default: |
| 138 | // fall through |
| 139 | } |
| 140 | |
| 141 | valmap := make(map[string]driver.Value, len(m.update.Values)) |
| 142 | for key, valcol := range m.update.Values { |
| 143 | |
| 144 | // TODO: qlbridge#13 Need a way of expressing which layer (here, db) this expr should run in? |
| 145 | // - ie, run in backend datasource? or here? translate the expr to native language |
| 146 | if valcol.Expr != nil { |
| 147 | exprVal, ok := vm.Eval(nil, valcol.Expr) |
| 148 | if !ok { |
| 149 | u.Errorf("Could not evaluate: %s", valcol.Expr) |
| 150 | return 0, fmt.Errorf("Could not evaluate expression: %v", valcol.Expr) |
| 151 | } |
| 152 | valmap[key] = exprVal.Value() |
| 153 | } else { |
| 154 | u.Debugf("%T %v", valcol.Value.Value(), valcol.Value.Value()) |
| 155 | valmap[key] = valcol.Value.Value() |
| 156 | } |
| 157 | //u.Debugf("key:%v col: %v vals:%v", key, valcol, valmap[key]) |
| 158 | } |
| 159 | |
| 160 | // if our backend source supports Where-Patches, ie update multiple |
| 161 | dbpatch, ok := m.db.(schema.ConnPatchWhere) |
| 162 | if ok { |
| 163 | updated, err := dbpatch.PatchWhere(m.Ctx, m.update.Where.Expr, valmap) |
| 164 | u.Infof("patch: %v %v", updated, err) |
| 165 | if err != nil { |
| 166 | return updated, err |
| 167 | } |
| 168 | return updated, nil |
| 169 | } |
| 170 | |
| 171 | // TODO: If it does not implement Where Patch then we need to do a poly fill |
| 172 | // Do we have to recognize if the Where is on a primary key? |
| 173 | // - for sources/queries that can't do partial updates we need to do a read first |
| 174 | |
| 175 | // Create a key from Where |
| 176 | key := datasource.KeyFromWhere(m.update.Where) |
| 177 | if _, err := m.db.Put(m.Ctx, key, valmap); err != nil { |
| 178 | u.Errorf("Could not put values: %v", err) |
| 179 | return 0, err |
| 180 | } |
| 181 | return 1, nil |
| 182 | } |
| 183 | |
| 184 | func (m *Upsert) insertRows(rows [][]*rel.ValueColumn) (int64, error) { |
| 185 | for i, row := range rows { |
no test coverage detected