RowIter implements the interface sql.ExecSourceRel.
(ctx *sql.Context, r sql.Row)
| 79 | |
| 80 | // RowIter implements the interface sql.ExecSourceRel. |
| 81 | func (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 | } |
nothing calls this directly
no test coverage detected