MCPcopy Create free account
hub / github.com/dolthub/doltgresql / RowIter

Method RowIter

server/node/create_extension.go:81–171  ·  view source on GitHub ↗

RowIter implements the interface sql.ExecSourceRel.

(ctx *sql.Context, r sql.Row)

Source from the content-addressed store, hash-verified

79
80// RowIter implements the interface sql.ExecSourceRel.
81func (c *CreateExtension) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) {
82 extCollection, err := core.GetExtensionsCollectionFromContext(ctx, "")
83 if err != nil {
84 return nil, err
85 }
86 if extCollection.HasLoadedExtension(ctx, id.NewExtension(c.Name)) {
87 if c.IfNotExists {
88 return sql.RowsToRowIter(), nil
89 }
90 return nil, errors.Errorf(`extension "%s" already exists`, c.Name)
91 }
92 ext, err := extensions.GetExtension(c.Name)
93 if err != nil {
94 return nil, err
95 }
96 // The returned files are in their proper order of execution, so we can iterate and execute
97 sqlFiles, err := ext.LoadSQLFiles()
98 if err != nil {
99 return nil, err
100 }
101 // save the current search_path
102 originalSchema, err := ctx.GetSessionVariable(ctx, "search_path")
103 if err != nil {
104 return nil, err
105 }
106
107 if c.SchemaName != "" {
108 defer func() {
109 _ = ctx.SetSessionVariable(ctx, "search_path", originalSchema)
110 }()
111
112 spErr := ctx.SetSessionVariable(ctx, "search_path", c.SchemaName)
113 if spErr != nil {
114 return nil, spErr
115 }
116 }
117
118 for _, sqlFile := range sqlFiles {
119 // Remove echo PSQL control statements
120 for {
121 echoStartIdx := strings.Index(sqlFile, `\echo`)
122 if echoStartIdx == -1 {
123 break
124 }
125 echoEndIdx := strings.Index(sqlFile[echoStartIdx:], "\n")
126 if echoEndIdx != -1 {
127 // Set the correct absolute position if there is a newline
128 echoEndIdx += echoStartIdx
129 } else {
130 // Set the position at the end of the file if there's no newline (comment appears before EOF)
131 echoEndIdx = len(sqlFile)
132 }
133 sqlFile = strings.Replace(sqlFile, sqlFile[echoStartIdx:echoEndIdx], "", 1)
134 }
135 statements, err := parser.Parse(sqlFile)
136 if err != nil {
137 return nil, err
138 }

Callers

nothing calls this directly

Calls 11

NewExtensionFunction · 0.92
GetExtensionFunction · 0.92
ParseFunction · 0.92
NewNamespaceFunction · 0.92
CreateLibraryIdentifierFunction · 0.92
HasLoadedExtensionMethod · 0.80
LoadSQLFilesMethod · 0.80
QueryWithBindingsMethod · 0.80
AddLoadedExtensionMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected