Insert

SQL INSERT statements can be executed using the genorm.Insert function.

It must start with a genorm.Insert function and end with a Do or DoCtx, but the rest of the order can be swapped. However, if you execute Values or Field twice in the same method chain, the Do or DoCtx will not execute the query at runtime and return an error.

// INSERT INTO `users` (`id`, `name`, `created_at`) VALUES ({{uuid.New()}}, "name", {{time.Now()}})
affectedRows, err := genorm.
    Insert(orm.User()).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name"),
        CreatedAt: genorm.Wrap(time.Now()),
    }).
    Do(db)

// INSERT INTO `users` (`id`, `name`) VALUES ({{uuid.New()}}, "name")
affectedRows, err := genorm.
    Insert(orm.User()).
    Fields(user.ID, user.Name).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name"),
    }).
    Do(db)

// INSERT INTO `users` (`id`, `name`, `created_at`) VALUES ({{uuid.New()}}, "name", {{time.Now()}})
affectedRows, err := genorm.
    Insert(orm.User()).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name"),
        CreatedAt: genorm.Wrap(time.Now()),
    }).
    DoCtx(context.Background(), db)

Insert

Function to start a chain of methods to issue an INSERT statement. It takes as its first argument a table generated by the CLI. No joined table is given, since INSERT statements cannot be executed on joined tables.

Fields(optional)

Specifies the target column for INSERT. By default, all columns of the table are the target of INSERT.

Values(required)

Specify the value to insert. Must be called exactly once before the method chain ends with Do or DoCtx. values in columns not targeted for INSERT in Fields are ignored.

Do

Executes the query and exits the method chain. The argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query.

DoCtx

Executes the query and exits the method chain. The second argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query. It also receives context.Context as its first argument. When context is canceled, the connection to the database is released, preventing unnecessary use of connections.

results matching ""

    No results matching ""