本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.Insert.Rows方法的典型用法代码示例。如果您正苦于以下问题:Golang Insert.Rows方法的具体用法?Golang Insert.Rows怎么用?Golang Insert.Rows使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cockroachdb/cockroach/sql/parser.Insert
的用法示例。
在下文中一共展示了Insert.Rows方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Insert
// Insert inserts rows into the database.
// Privileges: INSERT on table
// Notes: postgres requires INSERT. No "on duplicate key update" option.
// mysql requires INSERT. Also requires UPDATE on "ON DUPLICATE KEY UPDATE".
func (p *planner) Insert(n *parser.Insert, autoCommit bool) (planNode, *roachpb.Error) {
// TODO(marcb): We can't use the cached descriptor here because a recent
// update of the schema (e.g. the addition of an index) might not be
// reflected in the cached version (yet). Perhaps schema modification
// routines such as CREATE INDEX should not return until the schema change
// has been pushed everywhere.
tableDesc, pErr := p.getTableLease(n.Table)
if pErr != nil {
return nil, pErr
}
if err := p.checkPrivilege(&tableDesc, privilege.INSERT); err != nil {
return nil, roachpb.NewError(err)
}
var cols []ColumnDescriptor
// Determine which columns we're inserting into.
if n.DefaultValues() {
cols = tableDesc.Columns
} else {
var err error
if cols, err = p.processColumns(&tableDesc, n.Columns); err != nil {
return nil, roachpb.NewError(err)
}
}
// Number of columns expecting an input. This doesn't include the
// columns receiving a default value.
numInputColumns := len(cols)
// Construct a map from column ID to the index the value appears at within a
// row.
colIDtoRowIndex := map[ColumnID]int{}
for i, c := range cols {
colIDtoRowIndex[c.ID] = i
}
// Add the column if it has a DEFAULT expression.
addIfDefault := func(col ColumnDescriptor) {
if col.DefaultExpr != nil {
if _, ok := colIDtoRowIndex[col.ID]; !ok {
colIDtoRowIndex[col.ID] = len(cols)
cols = append(cols, col)
}
}
}
// Add any column that has a DEFAULT expression.
for _, col := range tableDesc.Columns {
addIfDefault(col)
}
// Also add any column in a mutation that is WRITE_ONLY and has
// a DEFAULT expression.
for _, m := range tableDesc.Mutations {
if m.State != DescriptorMutation_WRITE_ONLY {
continue
}
if col := m.GetColumn(); col != nil {
addIfDefault(*col)
}
}
// Verify we have at least the columns that are part of the primary key.
primaryKeyCols := map[ColumnID]struct{}{}
for i, id := range tableDesc.PrimaryIndex.ColumnIDs {
if _, ok := colIDtoRowIndex[id]; !ok {
return nil, roachpb.NewUErrorf("missing %q primary key column", tableDesc.PrimaryIndex.ColumnNames[i])
}
primaryKeyCols[id] = struct{}{}
}
// Construct the default expressions. The returned slice will be nil if no
// column in the table has a default expression.
defaultExprs, err := p.makeDefaultExprs(cols)
if err != nil {
return nil, roachpb.NewError(err)
}
// Replace any DEFAULT markers with the corresponding default expressions.
n.Rows = p.fillDefaults(defaultExprs, cols, n)
// Transform the values into a rows object. This expands SELECT statements or
// generates rows from the values contained within the query.
rows, pErr := p.makePlan(n.Rows, false)
if pErr != nil {
return nil, pErr
}
if expressions := len(rows.Columns()); expressions > numInputColumns {
return nil, roachpb.NewUErrorf("INSERT has more expressions than target columns: %d/%d", expressions, numInputColumns)
}
primaryIndex := tableDesc.PrimaryIndex
primaryIndexKeyPrefix := MakeIndexKeyPrefix(tableDesc.ID, primaryIndex.ID)
marshalled := make([]interface{}, len(cols))
//.........这里部分代码省略.........