MCPcopy
hub / github.com/github/gh-ost / NewDMLInsertQueryBuilder

Function NewDMLInsertQueryBuilder

go/sql/builder.go:859–892  ·  view source on GitHub ↗

NewDMLInsertQueryBuilder creates a new DMLInsertQueryBuilder. It prepares the INSERT query statement. Returns an error if no shared columns are given, the shared columns are not a subset of the table columns, or the prepared statement cannot be built.

(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns *ColumnList)

Source from the content-addressed store, hash-verified

857// Returns an error if no shared columns are given, the shared columns are not a subset of the table columns,
858// or the prepared statement cannot be built.
859func NewDMLInsertQueryBuilder(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns *ColumnList) (*DMLInsertQueryBuilder, error) {
860 if !sharedColumns.IsSubsetOf(tableColumns) {
861 return nil, fmt.Errorf("shared columns is not a subset of table columns in NewDMLInsertQueryBuilder")
862 }
863 if sharedColumns.Len() == 0 {
864 return nil, fmt.Errorf("no shared columns found in NewDMLInsertQueryBuilder")
865 }
866 databaseName = EscapeName(databaseName)
867 tableName = EscapeName(tableName)
868 mappedSharedColumnNames := duplicateNames(mappedSharedColumns.Names())
869 for i := range mappedSharedColumnNames {
870 mappedSharedColumnNames[i] = EscapeName(mappedSharedColumnNames[i])
871 }
872 preparedValues := buildColumnsPreparedValues(mappedSharedColumns)
873
874 stmt := fmt.Sprintf(`
875 insert /* gh-ost %s.%s */ ignore
876 into
877 %s.%s
878 (%s)
879 values
880 (%s)`,
881 databaseName, tableName,
882 databaseName, tableName,
883 strings.Join(mappedSharedColumnNames, ", "),
884 strings.Join(preparedValues, ", "),
885 )
886
887 return &DMLInsertQueryBuilder{
888 tableColumns: tableColumns,
889 sharedColumns: sharedColumns,
890 preparedStatement: stmt,
891 }, nil
892}
893
894// BuildQuery builds the arguments array for a DML event INSERT query.
895// It returns the query string and the shared arguments array.

Callers 3

prepareQueriesMethod · 0.92
TestBuildDMLInsertQueryFunction · 0.85

Calls 7

EscapeNameFunction · 0.85
duplicateNamesFunction · 0.85
IsSubsetOfMethod · 0.80
NamesMethod · 0.80
ErrorfMethod · 0.65
LenMethod · 0.45

Tested by 2

TestBuildDMLInsertQueryFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…