github.com/aacfactory/fns-contrib/databases/sql@v1.2.84/dac/insert.go (about)

     1  package dac
     2  
     3  import (
     4  	"github.com/aacfactory/errors"
     5  	"github.com/aacfactory/fns-contrib/databases/sql"
     6  	"github.com/aacfactory/fns-contrib/databases/sql/dac/conditions"
     7  	"github.com/aacfactory/fns-contrib/databases/sql/dac/specifications"
     8  	"github.com/aacfactory/fns/context"
     9  )
    10  
    11  func Insert[T Table](ctx context.Context, entry T) (v T, ok bool, err error) {
    12  	entries := []T{entry}
    13  	method, query, arguments, returning, buildErr := specifications.BuildInsert[T](ctx, entries)
    14  	if buildErr != nil {
    15  		err = errors.Warning("sql: insert failed").WithCause(buildErr)
    16  		return
    17  	}
    18  	if method == specifications.QueryMethod {
    19  		rows, queryErr := sql.Query(ctx, query, arguments...)
    20  		if queryErr != nil {
    21  			err = errors.Warning("sql: insert failed").WithCause(queryErr)
    22  			return
    23  		}
    24  		affected, wErr := specifications.WriteInsertReturning[T](ctx, rows, returning, entries)
    25  		_ = rows.Close()
    26  		if wErr != nil {
    27  			err = errors.Warning("sql: insert failed").WithCause(wErr)
    28  			return
    29  		}
    30  		ok = affected > 0
    31  		if ok {
    32  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
    33  			if verErr != nil {
    34  				err = errors.Warning("sql: insert failed").WithCause(verErr)
    35  				return
    36  			}
    37  			v = entries[0]
    38  		}
    39  	} else {
    40  		result, execErr := sql.Execute(ctx, query, arguments...)
    41  		if execErr != nil {
    42  			err = errors.Warning("sql: insert failed").WithCause(execErr)
    43  			return
    44  		}
    45  		ok = result.RowsAffected > 0
    46  		if ok {
    47  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
    48  			if verErr != nil {
    49  				err = errors.Warning("sql: insert failed").WithCause(verErr)
    50  				return
    51  			}
    52  			if result.LastInsertId > 0 {
    53  				e, idErr := specifications.TrySetupLastInsertId[T](ctx, entries[0], result.LastInsertId)
    54  				if idErr != nil {
    55  					err = errors.Warning("sql: insert failed").WithCause(idErr)
    56  					return
    57  				}
    58  				entries[0] = e
    59  			}
    60  			v = entries[0]
    61  		}
    62  	}
    63  	return
    64  }
    65  
    66  func InsertMulti[T Table](ctx context.Context, entries []T) (affected int64, err error) {
    67  	if len(entries) == 0 {
    68  		return
    69  	}
    70  	method, query, arguments, returning, buildErr := specifications.BuildInsert[T](ctx, entries)
    71  	if buildErr != nil {
    72  		err = errors.Warning("sql: insert multi failed").WithCause(buildErr)
    73  		return
    74  	}
    75  	if method == specifications.QueryMethod {
    76  		rows, queryErr := sql.Query(ctx, query, arguments...)
    77  		if queryErr != nil {
    78  			err = errors.Warning("sql: insert multi failed").WithCause(queryErr)
    79  			return
    80  		}
    81  		affected, err = specifications.WriteInsertReturning[T](ctx, rows, returning, entries)
    82  		_ = rows.Close()
    83  		if err != nil {
    84  			err = errors.Warning("sql: insert multi failed").WithCause(err)
    85  			return
    86  		}
    87  		if affected > 0 {
    88  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
    89  			if verErr != nil {
    90  				err = errors.Warning("sql: insert multi failed").WithCause(verErr)
    91  				return
    92  			}
    93  		}
    94  	} else {
    95  		result, execErr := sql.Execute(ctx, query, arguments...)
    96  		if execErr != nil {
    97  			err = errors.Warning("sql: insert multi failed").WithCause(execErr)
    98  			return
    99  		}
   100  		affected = result.RowsAffected
   101  		if affected > 0 {
   102  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   103  			if verErr != nil {
   104  				err = errors.Warning("sql: insert multi failed").WithCause(verErr)
   105  				return
   106  			}
   107  		}
   108  	}
   109  	return
   110  }
   111  
   112  func InsertOrUpdate[T Table](ctx context.Context, entry T) (v T, ok bool, err error) {
   113  	entries := []T{entry}
   114  	method, query, arguments, returning, buildErr := specifications.BuildInsertOrUpdate[T](ctx, entries)
   115  	if buildErr != nil {
   116  		err = errors.Warning("sql: insert or update failed").WithCause(buildErr)
   117  		return
   118  	}
   119  	if method == specifications.QueryMethod {
   120  		rows, queryErr := sql.Query(ctx, query, arguments...)
   121  		if queryErr != nil {
   122  			err = errors.Warning("sql: insert or update failed").WithCause(queryErr)
   123  			return
   124  		}
   125  
   126  		affected, wErr := specifications.WriteInsertReturning[T](ctx, rows, returning, entries)
   127  		_ = rows.Close()
   128  		if wErr != nil {
   129  			err = errors.Warning("sql: insert or update failed").WithCause(wErr)
   130  			return
   131  		}
   132  		ok = affected == 1
   133  		if ok {
   134  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   135  			if verErr != nil {
   136  				err = errors.Warning("sql: insert or update failed").WithCause(verErr)
   137  				return
   138  			}
   139  			v = entries[0]
   140  		}
   141  	} else {
   142  		result, execErr := sql.Execute(ctx, query, arguments...)
   143  		if execErr != nil {
   144  			err = errors.Warning("sql: insert or update failed").WithCause(execErr)
   145  			return
   146  		}
   147  		ok = result.RowsAffected == 1
   148  		if ok {
   149  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   150  			if verErr != nil {
   151  				err = errors.Warning("sql: insert or update failed").WithCause(verErr)
   152  				return
   153  			}
   154  			if result.LastInsertId > 0 {
   155  				e, idErr := specifications.TrySetupLastInsertId[T](ctx, entries[0], result.LastInsertId)
   156  				if idErr != nil {
   157  					err = errors.Warning("sql: insert or update failed").WithCause(idErr)
   158  					return
   159  				}
   160  				entries[0] = e
   161  			}
   162  			v = entries[0]
   163  		}
   164  	}
   165  	return
   166  }
   167  
   168  func InsertWhenNotExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error) {
   169  	entries := []T{entry}
   170  	method, query, arguments, returning, buildErr := specifications.BuildInsertWhenNotExist[T](ctx, entries, specifications.QueryExpr{QueryExpr: source})
   171  	if buildErr != nil {
   172  		err = errors.Warning("sql: insert when exist failed").WithCause(buildErr)
   173  		return
   174  	}
   175  	if method == specifications.QueryMethod {
   176  		rows, queryErr := sql.Query(ctx, query, arguments...)
   177  		if queryErr != nil {
   178  			err = errors.Warning("sql: insert when exist failed").WithCause(queryErr)
   179  			return
   180  		}
   181  		affected, wErr := specifications.WriteInsertReturning[T](ctx, rows, returning, entries)
   182  		_ = rows.Close()
   183  		if wErr != nil {
   184  			err = errors.Warning("sql: insert when exist failed").WithCause(wErr)
   185  			return
   186  		}
   187  		ok = affected == 1
   188  		if ok {
   189  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   190  			if verErr != nil {
   191  				err = errors.Warning("sql: insert when exist failed").WithCause(verErr)
   192  				return
   193  			}
   194  			v = entries[0]
   195  		}
   196  	} else {
   197  		result, execErr := sql.Execute(ctx, query, arguments...)
   198  		if execErr != nil {
   199  			err = errors.Warning("sql: insert when exist failed").WithCause(execErr)
   200  			return
   201  		}
   202  		ok = result.RowsAffected == 1
   203  		if ok {
   204  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   205  			if verErr != nil {
   206  				err = errors.Warning("sql: insert when exist failed").WithCause(verErr)
   207  				return
   208  			}
   209  			if result.LastInsertId > 0 {
   210  				e, idErr := specifications.TrySetupLastInsertId[T](ctx, entries[0], result.LastInsertId)
   211  				if idErr != nil {
   212  					err = errors.Warning("sql: insert when exist failed").WithCause(idErr)
   213  					return
   214  				}
   215  				entries[0] = e
   216  			}
   217  			v = entries[0]
   218  		}
   219  	}
   220  	return
   221  }
   222  
   223  func InsertWhenExist[T Table](ctx context.Context, entry T, source conditions.QueryExpr) (v T, ok bool, err error) {
   224  	entries := []T{entry}
   225  	method, query, arguments, returning, buildErr := specifications.BuildInsertWhenExist[T](ctx, entries, specifications.QueryExpr{QueryExpr: source})
   226  	if buildErr != nil {
   227  		err = errors.Warning("sql: insert when not exist failed").WithCause(buildErr)
   228  		return
   229  	}
   230  	if method == specifications.QueryMethod {
   231  		rows, queryErr := sql.Query(ctx, query, arguments...)
   232  		if queryErr != nil {
   233  			err = errors.Warning("sql: insert when not exist failed").WithCause(queryErr)
   234  			return
   235  		}
   236  		affected, wErr := specifications.WriteInsertReturning[T](ctx, rows, returning, entries)
   237  		_ = rows.Close()
   238  		if wErr != nil {
   239  			err = errors.Warning("sql: insert when not exist failed").WithCause(wErr)
   240  			return
   241  		}
   242  		ok = affected == 1
   243  		if ok {
   244  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   245  			if verErr != nil {
   246  				err = errors.Warning("sql: insert when not exist failed").WithCause(verErr)
   247  				return
   248  			}
   249  			v = entries[0]
   250  		}
   251  	} else {
   252  		result, execErr := sql.Execute(ctx, query, arguments...)
   253  		if execErr != nil {
   254  			err = errors.Warning("sql: insert when not exist failed").WithCause(execErr)
   255  			return
   256  		}
   257  		ok = result.RowsAffected == 1
   258  		if ok {
   259  			verErr := specifications.TrySetupAuditVersion[T](ctx, entries)
   260  			if verErr != nil {
   261  				err = errors.Warning("sql: insert when not exist failed").WithCause(verErr)
   262  				return
   263  			}
   264  			if result.LastInsertId > 0 {
   265  				e, idErr := specifications.TrySetupLastInsertId[T](ctx, entries[0], result.LastInsertId)
   266  				if idErr != nil {
   267  					err = errors.Warning("sql: insert when not exist failed").WithCause(idErr)
   268  					return
   269  				}
   270  				entries[0] = e
   271  			}
   272  			v = entries[0]
   273  		}
   274  	}
   275  	return
   276  }