// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
Insert a single record using an executor.
See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
// Insert a single record using an executor . // See boil . Columns . InsertColumnSet documentation to understand column list inference for inserts .
func (o *MessagesLink) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
	if o == nil {
		return errors.New("models: no messages_links provided for insertion")
	}
	var err error
	if err := o.doBeforeInsertHooks(ctx, exec); err != nil {
		return err
	}
	nzDefaults := queries.NonZeroDefaultSet(messagesLinkColumnsWithDefault, o)
	key := makeCacheKey(columns, nzDefaults)
	messagesLinkInsertCacheMut.RLock()
	cache, cached := messagesLinkInsertCache[key]
	messagesLinkInsertCacheMut.RUnlock()
	if !cached {
		wl, returnColumns := columns.InsertColumnSet(
			messagesLinkColumns,
			messagesLinkColumnsWithDefault,
			messagesLinkColumnsWithoutDefault,
			nzDefaults,
		)
		cache.valueMapping, err = queries.BindMapping(messagesLinkType, messagesLinkMapping, wl)
		if err != nil {
			return err
		}
		cache.retMapping, err = queries.BindMapping(messagesLinkType, messagesLinkMapping, returnColumns)
		if err != nil {
			return err
		}
		if len(wl) != 0 {
			cache.query = fmt.Sprintf("INSERT INTO \"messages_links\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
		} else {
			cache.query = "INSERT INTO \"messages_links\" %sDEFAULT VALUES%s"
		}
		var queryOutput, queryReturning string
		if len(cache.retMapping) != 0 {
			queryReturning = fmt.Sprintf(" RETURNING \"%s\"", strings.Join(returnColumns, "\",\""))
		}
		cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
	}
	value := reflect.Indirect(reflect.ValueOf(o))
	vals := queries.ValuesFromMapping(value, cache.valueMapping)
	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, vals)
	}
	if len(cache.retMapping) != 0 {
		err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
	} else {
		_, err = exec.ExecContext(ctx, cache.query, vals...)
	}
	if err != nil {
		return errors.Wrap(err, "models: unable to insert into messages_links")
	}
	if !cached {
		messagesLinkInsertCacheMut.Lock()
		messagesLinkInsertCache[key] = cache
		messagesLinkInsertCacheMut.Unlock()
	}
	return o.doAfterInsertHooks(ctx, exec)
}
func ( o * MessagesLink ) Insert ( ctx context . Context , exec boil . ContextExecutor , columns boil . Columns ) error { if o == nil { return errors . New ( " " ) } var err error if err := o . doBeforeInsertHooks ( ctx , exec ) ; err != nil { return err } nzDefaults := queries . NonZeroDefaultSet ( messagesLinkColumnsWithDefault , o ) key := makeCacheKey ( columns , nzDefaults ) messagesLinkInsertCacheMut . RLock ( ) cache , cached := messagesLinkInsertCache [ key ] messagesLinkInsertCacheMut . RUnlock ( ) if ! cached { wl , returnColumns := columns . InsertColumnSet ( messagesLinkColumns , messagesLinkColumnsWithDefault , messagesLinkColumnsWithoutDefault , nzDefaults , ) cache . valueMapping , err = queries . BindMapping ( messagesLinkType , messagesLinkMapping , wl ) if err != nil { return err } cache . retMapping , err = queries . BindMapping ( messagesLinkType , messagesLinkMapping , returnColumns ) if err != nil { return err } if len ( wl ) != 0 { cache . query = fmt . Sprintf ( " \" \" \" \" " , strings . Join ( wl , " \" \" " ) , strmangle . Placeholders ( dialect . UseIndexPlaceholders , len ( wl ) , 1 , 1 ) ) } else { cache . query = " \" \" " } var queryOutput , queryReturning string if len ( cache . retMapping ) != 0 { queryReturning = fmt . Sprintf ( " \" \" " , strings . Join ( returnColumns , " \" \" " ) ) } cache . query = fmt . Sprintf ( cache . query , queryOutput , queryReturning ) } value := reflect . Indirect ( reflect . ValueOf ( o ) ) vals := queries . ValuesFromMapping ( value , cache . valueMapping ) if boil . DebugMode { fmt . Fprintln ( boil . DebugWriter , cache . query ) fmt . Fprintln ( boil . DebugWriter , vals ) } if len ( cache . retMapping ) != 0 { err = exec . QueryRowContext ( ctx , cache . query , vals ... ) . Scan ( queries . PtrsFromMapping ( value , cache . retMapping ) ... ) } else { _ , err = exec . ExecContext ( ctx , cache . query , vals ... ) } if err != nil { return errors . Wrap ( err , " " ) } if ! cached { messagesLinkInsertCacheMut . Lock ( ) messagesLinkInsertCache [ key ] = cache messagesLinkInsertCacheMut . Unlock ( ) } return o . doAfterInsertHooks ( ctx , exec ) }
Insert a single record using an executor.
Insert a single record using an executor .