MCPcopy Index your code
hub / github.com/ent/ent / insertLastID

Method insertLastID

dialect/sql/sqlgraph/graph.go:1904–1949  ·  view source on GitHub ↗

insertLastID invokes the insert query on the transaction and returns the LastInsertID.

(ctx context.Context, insert *sql.InsertBuilder)

Source from the content-addressed store, hash-verified

1902
1903// insertLastID invokes the insert query on the transaction and returns the LastInsertID.
1904func (c *creator) insertLastID(ctx context.Context, insert *sql.InsertBuilder) error {
1905 query, args, err := insert.QueryErr()
1906 if err != nil {
1907 return err
1908 }
1909 // MySQL does not support the "RETURNING" clause.
1910 if insert.Dialect() != dialect.MySQL {
1911 rows := &sql.Rows{}
1912 if err := c.tx.Query(ctx, query, args, rows); err != nil {
1913 return err
1914 }
1915 defer rows.Close()
1916 switch _, ok := c.ID.Value.(field.ValueScanner); {
1917 case ok:
1918 // If the ID implements the sql.Scanner
1919 // interface it should be a pointer type.
1920 return sql.ScanOne(rows, c.ID.Value)
1921 case c.ID.Type.Numeric():
1922 // Normalize the type to int64 to make it
1923 // looks like LastInsertId.
1924 id, err := sql.ScanInt64(rows)
1925 if err != nil {
1926 return err
1927 }
1928 c.ID.Value = id
1929 return nil
1930 default:
1931 return sql.ScanOne(rows, &c.ID.Value)
1932 }
1933 }
1934 // MySQL.
1935 var res sql.Result
1936 if err := c.tx.Exec(ctx, query, args, &res); err != nil {
1937 return err
1938 }
1939 // If the ID field is not numeric (e.g. string),
1940 // there is no way to scan the LAST_INSERT_ID.
1941 if c.ID.Type.Numeric() {
1942 id, err := res.LastInsertId()
1943 if err != nil {
1944 return err
1945 }
1946 c.ID.Value = id
1947 }
1948 return nil
1949}
1950
1951// insertLastIDs invokes the batch insert query on the transaction and returns the LastInsertID of all entities.
1952func (c *batchCreator) insertLastIDs(ctx context.Context, tx dialect.ExecQuerier, insert *sql.InsertBuilder) error {

Callers 1

insertMethod · 0.95

Calls 9

ScanOneFunction · 0.92
ScanInt64Function · 0.92
QueryErrMethod · 0.80
LastInsertIdMethod · 0.80
DialectMethod · 0.65
QueryMethod · 0.65
CloseMethod · 0.65
ExecMethod · 0.65
NumericMethod · 0.45

Tested by

no test coverage detected