...

Source file src/github.com/doug-martin/goqu/v9/delete_dataset.go

Documentation: github.com/doug-martin/goqu/v9

     1  package goqu
     2  
     3  import (
     4  	"github.com/doug-martin/goqu/v9/exec"
     5  	"github.com/doug-martin/goqu/v9/exp"
     6  	"github.com/doug-martin/goqu/v9/internal/errors"
     7  	"github.com/doug-martin/goqu/v9/internal/sb"
     8  )
     9  
    10  var ErrBadFromArgument = errors.New("unsupported DeleteDataset#From argument, a string or identifier expression is required")
    11  
    12  type DeleteDataset struct {
    13  	dialect      SQLDialect
    14  	clauses      exp.DeleteClauses
    15  	isPrepared   prepared
    16  	queryFactory exec.QueryFactory
    17  	err          error
    18  }
    19  
    20  // used internally by database to create a database with a specific adapter
    21  func newDeleteDataset(d string, queryFactory exec.QueryFactory) *DeleteDataset {
    22  	return &DeleteDataset{
    23  		clauses:      exp.NewDeleteClauses(),
    24  		dialect:      GetDialect(d),
    25  		queryFactory: queryFactory,
    26  		isPrepared:   preparedNoPreference,
    27  		err:          nil,
    28  	}
    29  }
    30  
    31  func Delete(table interface{}) *DeleteDataset {
    32  	return newDeleteDataset("default", nil).From(table)
    33  }
    34  
    35  func (dd *DeleteDataset) Expression() exp.Expression {
    36  	return dd
    37  }
    38  
    39  // Clones the dataset
    40  func (dd *DeleteDataset) Clone() exp.Expression {
    41  	return dd.copy(dd.clauses)
    42  }
    43  
    44  // Set the parameter interpolation behavior. See examples
    45  //
    46  // prepared: If true the dataset WILL NOT interpolate the parameters.
    47  func (dd *DeleteDataset) Prepared(prepared bool) *DeleteDataset {
    48  	ret := dd.copy(dd.clauses)
    49  	ret.isPrepared = preparedFromBool(prepared)
    50  	return ret
    51  }
    52  
    53  // Returns true if Prepared(true) has been called on this dataset
    54  func (dd *DeleteDataset) IsPrepared() bool {
    55  	return dd.isPrepared.Bool()
    56  }
    57  
    58  // Sets the adapter used to serialize values and create the SQL statement
    59  func (dd *DeleteDataset) WithDialect(dl string) *DeleteDataset {
    60  	ds := dd.copy(dd.GetClauses())
    61  	ds.dialect = GetDialect(dl)
    62  	return ds
    63  }
    64  
    65  // Returns the current SQLDialect on the dataset
    66  func (dd *DeleteDataset) Dialect() SQLDialect {
    67  	return dd.dialect
    68  }
    69  
    70  // Set the dialect for this dataset.
    71  func (dd *DeleteDataset) SetDialect(dialect SQLDialect) *DeleteDataset {
    72  	cd := dd.copy(dd.GetClauses())
    73  	cd.dialect = dialect
    74  	return cd
    75  }
    76  
    77  // Returns the current clauses on the dataset.
    78  func (dd *DeleteDataset) GetClauses() exp.DeleteClauses {
    79  	return dd.clauses
    80  }
    81  
    82  // used interally to copy the dataset
    83  func (dd *DeleteDataset) copy(clauses exp.DeleteClauses) *DeleteDataset {
    84  	return &DeleteDataset{
    85  		dialect:      dd.dialect,
    86  		clauses:      clauses,
    87  		isPrepared:   dd.isPrepared,
    88  		queryFactory: dd.queryFactory,
    89  		err:          dd.err,
    90  	}
    91  }
    92  
    93  // Creates a WITH clause for a common table expression (CTE).
    94  //
    95  // The name will be available to SELECT from in the associated query; and can optionally
    96  // contain a list of column names "name(col1, col2, col3)".
    97  //
    98  // The name will refer to the results of the specified subquery.
    99  func (dd *DeleteDataset) With(name string, subquery exp.Expression) *DeleteDataset {
   100  	return dd.copy(dd.clauses.CommonTablesAppend(exp.NewCommonTableExpression(false, name, subquery)))
   101  }
   102  
   103  // Creates a WITH RECURSIVE clause for a common table expression (CTE)
   104  //
   105  // The name will be available to SELECT from in the associated query; and must
   106  // contain a list of column names "name(col1, col2, col3)" for a recursive clause.
   107  //
   108  // The name will refer to the results of the specified subquery. The subquery for
   109  // a recursive query will always end with a UNION or UNION ALL with a clause that
   110  // refers to the CTE by name.
   111  func (dd *DeleteDataset) WithRecursive(name string, subquery exp.Expression) *DeleteDataset {
   112  	return dd.copy(dd.clauses.CommonTablesAppend(exp.NewCommonTableExpression(true, name, subquery)))
   113  }
   114  
   115  // Adds a FROM clause. This return a new dataset with the original sources replaced. See examples.
   116  // You can pass in the following.
   117  //   string: Will automatically be turned into an identifier
   118  //   Dataset: Will be added as a sub select. If the Dataset is not aliased it will automatically be aliased
   119  //   LiteralExpression: (See Literal) Will use the literal SQL
   120  func (dd *DeleteDataset) From(table interface{}) *DeleteDataset {
   121  	switch t := table.(type) {
   122  	case exp.IdentifierExpression:
   123  		return dd.copy(dd.clauses.SetFrom(t))
   124  	case string:
   125  		return dd.copy(dd.clauses.SetFrom(exp.ParseIdentifier(t)))
   126  	default:
   127  		panic(ErrBadFromArgument)
   128  	}
   129  }
   130  
   131  // Adds a WHERE clause. See examples.
   132  func (dd *DeleteDataset) Where(expressions ...exp.Expression) *DeleteDataset {
   133  	return dd.copy(dd.clauses.WhereAppend(expressions...))
   134  }
   135  
   136  // Removes the WHERE clause. See examples.
   137  func (dd *DeleteDataset) ClearWhere() *DeleteDataset {
   138  	return dd.copy(dd.clauses.ClearWhere())
   139  }
   140  
   141  // Adds a ORDER clause. If the ORDER is currently set it replaces it. See examples.
   142  func (dd *DeleteDataset) Order(order ...exp.OrderedExpression) *DeleteDataset {
   143  	return dd.copy(dd.clauses.SetOrder(order...))
   144  }
   145  
   146  // Adds a more columns to the current ORDER BY clause. If no order has be previously specified it is the same as
   147  // calling Order. See examples.
   148  func (dd *DeleteDataset) OrderAppend(order ...exp.OrderedExpression) *DeleteDataset {
   149  	return dd.copy(dd.clauses.OrderAppend(order...))
   150  }
   151  
   152  // Adds a more columns to the beginning of the current ORDER BY clause. If no order has be previously specified it is the same as
   153  // calling Order. See examples.
   154  func (dd *DeleteDataset) OrderPrepend(order ...exp.OrderedExpression) *DeleteDataset {
   155  	return dd.copy(dd.clauses.OrderPrepend(order...))
   156  }
   157  
   158  // Removes the ORDER BY clause. See examples.
   159  func (dd *DeleteDataset) ClearOrder() *DeleteDataset {
   160  	return dd.copy(dd.clauses.ClearOrder())
   161  }
   162  
   163  // Adds a LIMIT clause. If the LIMIT is currently set it replaces it. See examples.
   164  func (dd *DeleteDataset) Limit(limit uint) *DeleteDataset {
   165  	if limit > 0 {
   166  		return dd.copy(dd.clauses.SetLimit(limit))
   167  	}
   168  	return dd.copy(dd.clauses.ClearLimit())
   169  }
   170  
   171  // Adds a LIMIT ALL clause. If the LIMIT is currently set it replaces it. See examples.
   172  func (dd *DeleteDataset) LimitAll() *DeleteDataset {
   173  	return dd.copy(dd.clauses.SetLimit(L("ALL")))
   174  }
   175  
   176  // Removes the LIMIT clause.
   177  func (dd *DeleteDataset) ClearLimit() *DeleteDataset {
   178  	return dd.copy(dd.clauses.ClearLimit())
   179  }
   180  
   181  // Adds a RETURNING clause to the dataset if the adapter supports it.
   182  func (dd *DeleteDataset) Returning(returning ...interface{}) *DeleteDataset {
   183  	return dd.copy(dd.clauses.SetReturning(exp.NewColumnListExpression(returning...)))
   184  }
   185  
   186  // Get any error that has been set or nil if no error has been set.
   187  func (dd *DeleteDataset) Error() error {
   188  	return dd.err
   189  }
   190  
   191  // Set an error on the dataset if one has not already been set. This error will be returned by a future call to Error
   192  // or as part of ToSQL. This can be used by end users to record errors while building up queries without having to
   193  // track those separately.
   194  func (dd *DeleteDataset) SetError(err error) *DeleteDataset {
   195  	if dd.err == nil {
   196  		dd.err = err
   197  	}
   198  
   199  	return dd
   200  }
   201  
   202  // Generates a DELETE sql statement, if Prepared has been called with true then the parameters will not be interpolated.
   203  // See examples.
   204  //
   205  // Errors:
   206  //  * There is an error generating the SQL
   207  func (dd *DeleteDataset) ToSQL() (sql string, params []interface{}, err error) {
   208  	return dd.deleteSQLBuilder().ToSQL()
   209  }
   210  
   211  // Appends this Dataset's DELETE statement to the SQLBuilder
   212  // This is used internally when using deletes in CTEs
   213  func (dd *DeleteDataset) AppendSQL(b sb.SQLBuilder) {
   214  	if dd.err != nil {
   215  		b.SetError(dd.err)
   216  		return
   217  	}
   218  	dd.dialect.ToDeleteSQL(b, dd.GetClauses())
   219  }
   220  
   221  func (dd *DeleteDataset) GetAs() exp.IdentifierExpression {
   222  	return nil
   223  }
   224  
   225  func (dd *DeleteDataset) ReturnsColumns() bool {
   226  	return dd.clauses.HasReturning()
   227  }
   228  
   229  // Creates an QueryExecutor to execute the query.
   230  //    db.Delete("test").Exec()
   231  //
   232  // See Dataset#ToUpdateSQL for arguments
   233  func (dd *DeleteDataset) Executor() exec.QueryExecutor {
   234  	return dd.queryFactory.FromSQLBuilder(dd.deleteSQLBuilder())
   235  }
   236  
   237  func (dd *DeleteDataset) deleteSQLBuilder() sb.SQLBuilder {
   238  	buf := sb.NewSQLBuilder(dd.isPrepared.Bool())
   239  	if dd.err != nil {
   240  		return buf.SetError(dd.err)
   241  	}
   242  	dd.dialect.ToDeleteSQL(buf, dd.clauses)
   243  	return buf
   244  }
   245  

View as plain text