parseComplete parses the "command tag" from a CommandComplete message, and returns the number of rows affected (if applicable) and a string identifying only the command that was executed, e.g. "ALTER TABLE". Returns an error if the command can cannot be parsed.
(commandTag string)
| 1434 | // only the command that was executed, e.g. "ALTER TABLE". Returns an error if |
| 1435 | // the command can cannot be parsed. |
| 1436 | func (cn *conn) parseComplete(commandTag string) (driver.Result, string, error) { |
| 1437 | commandsWithAffectedRows := []string{ |
| 1438 | "SELECT ", |
| 1439 | // INSERT is handled below |
| 1440 | "UPDATE ", |
| 1441 | "DELETE ", |
| 1442 | "FETCH ", |
| 1443 | "MOVE ", |
| 1444 | "COPY ", |
| 1445 | } |
| 1446 | |
| 1447 | var affectedRows *string |
| 1448 | for _, tag := range commandsWithAffectedRows { |
| 1449 | if strings.HasPrefix(commandTag, tag) { |
| 1450 | t := commandTag[len(tag):] |
| 1451 | affectedRows = &t |
| 1452 | commandTag = tag[:len(tag)-1] |
| 1453 | break |
| 1454 | } |
| 1455 | } |
| 1456 | // INSERT also includes the oid of the inserted row in its command tag. Oids |
| 1457 | // in user tables are deprecated, and the oid is only returned when exactly |
| 1458 | // one row is inserted, so it's unlikely to be of value to any real-world |
| 1459 | // application and we can ignore it. |
| 1460 | if affectedRows == nil && strings.HasPrefix(commandTag, "INSERT ") { |
| 1461 | parts := strings.Split(commandTag, " ") |
| 1462 | if len(parts) != 3 { |
| 1463 | cn.err.set(driver.ErrBadConn) |
| 1464 | return nil, "", fmt.Errorf("pq: unexpected INSERT command tag %s", commandTag) |
| 1465 | } |
| 1466 | affectedRows = &parts[len(parts)-1] |
| 1467 | commandTag = "INSERT" |
| 1468 | } |
| 1469 | // There should be no affected rows attached to the tag, just return it |
| 1470 | if affectedRows == nil { |
| 1471 | return driver.RowsAffected(0), commandTag, nil |
| 1472 | } |
| 1473 | n, err := strconv.ParseInt(*affectedRows, 10, 64) |
| 1474 | if err != nil { |
| 1475 | cn.err.set(driver.ErrBadConn) |
| 1476 | return nil, "", fmt.Errorf("pq: could not parse commandTag: %w", err) |
| 1477 | } |
| 1478 | return driver.RowsAffected(n), commandTag, nil |
| 1479 | } |
| 1480 | |
| 1481 | func md5s(s string) string { |
| 1482 | h := md5.New() |