MCPcopy
hub / github.com/ent/ent / node

Method node

dialect/sql/sqlgraph/graph.go:1106–1185  ·  view source on GitHub ↗
(ctx context.Context, tx dialect.ExecQuerier)

Source from the content-addressed store, hash-verified

1104}
1105
1106func (u *updater) node(ctx context.Context, tx dialect.ExecQuerier) error {
1107 var (
1108 id driver.Value
1109 idp *sql.Predicate
1110 addEdges = EdgeSpecs(u.Edges.Add).GroupRel()
1111 clearEdges = EdgeSpecs(u.Edges.Clear).GroupRel()
1112 )
1113 switch {
1114 // In case it is not an edge schema, the id holds the PK
1115 // of the node used for linking it with the other nodes.
1116 case u.Node.ID != nil:
1117 id = u.Node.ID.Value
1118 idp = sql.EQ(u.Node.ID.Column, id)
1119 case len(u.Node.CompositeID) == 2:
1120 idp = sql.And(
1121 sql.EQ(u.Node.CompositeID[0].Column, u.Node.CompositeID[0].Value),
1122 sql.EQ(u.Node.CompositeID[1].Column, u.Node.CompositeID[1].Value),
1123 )
1124 case len(u.Node.CompositeID) != 2:
1125 return fmt.Errorf("sql/sqlgraph: invalid composite id for update table %q", u.Node.Table)
1126 default:
1127 return fmt.Errorf("sql/sqlgraph: missing node id for update table %q", u.Node.Table)
1128 }
1129 update := u.builder.Update(u.Node.Table).Schema(u.Node.Schema).Where(idp)
1130 if pred := u.Predicate; pred != nil {
1131 selector := u.builder.Select().From(u.builder.Table(u.Node.Table).Schema(u.Node.Schema))
1132 pred(selector)
1133 update.FromSelect(selector)
1134 }
1135 if err := u.setTableColumns(update, addEdges, clearEdges); err != nil {
1136 return err
1137 }
1138 for _, m := range u.Modifiers {
1139 m(update)
1140 }
1141 if err := update.Err(); err != nil {
1142 return err
1143 }
1144 if !update.Empty() {
1145 var res sql.Result
1146 query, args := update.Query()
1147 if err := tx.Exec(ctx, query, args, &res); err != nil {
1148 return err
1149 }
1150 affected, err := res.RowsAffected()
1151 if err != nil {
1152 return err
1153 }
1154 // In case there are zero affected rows by this statement, we need to distinguish
1155 // between the case of "record was not found" and "record was not changed".
1156 if affected == 0 && u.Predicate != nil {
1157 if err := u.ensureExists(ctx); err != nil {
1158 return err
1159 }
1160 }
1161 }
1162 if id != nil {
1163 // Not an edge schema.

Callers 1

UpdateNodeFunction · 0.95

Calls 15

setTableColumnsMethod · 0.95
ensureExistsMethod · 0.95
setExternalEdgesMethod · 0.95
scanMethod · 0.95
EQFunction · 0.92
AndFunction · 0.92
EdgeSpecsTypeAlias · 0.85
GroupRelMethod · 0.80
EmptyMethod · 0.80
RowsAffectedMethod · 0.80
WhereMethod · 0.65
ErrMethod · 0.65

Tested by

no test coverage detected