(ctx context.Context, objectType string, objectName string)
| 163 | } |
| 164 | |
| 165 | func (d *Driver) showCreateDDL(ctx context.Context, objectType string, objectName string) (string, error) { |
| 166 | objectName = fmt.Sprintf("`%s`", objectName) |
| 167 | |
| 168 | // 'SHOW CREATE TABLE' can also be used for dumping views |
| 169 | stmt := fmt.Sprintf("SHOW CREATE %s %s", objectType, objectName) |
| 170 | if objectType == "VIEW" { |
| 171 | stmt = fmt.Sprintf("SHOW CREATE TABLE %s", objectName) |
| 172 | } |
| 173 | |
| 174 | rows, err := d.db.QueryContext(ctx, stmt) |
| 175 | if err != nil { |
| 176 | return "", errors.Wrapf(err, "failed to execute %s", stmt) |
| 177 | } |
| 178 | defer rows.Close() |
| 179 | |
| 180 | var schemaDDL strings.Builder |
| 181 | for rows.Next() { |
| 182 | var ddlLine string |
| 183 | if err := rows.Scan(&ddlLine); err != nil { |
| 184 | return "", errors.Wrap(err, "failed to scan DDL row") |
| 185 | } |
| 186 | schemaDDL.WriteString(ddlLine) |
| 187 | schemaDDL.WriteString("\n") |
| 188 | } |
| 189 | |
| 190 | if err := rows.Err(); err != nil { |
| 191 | return "", errors.Wrap(err, "error reading DDL rows") |
| 192 | } |
| 193 | |
| 194 | if schemaDDL.Len() == 0 { |
| 195 | return "", errors.New("empty DDL result") |
| 196 | } |
| 197 | |
| 198 | return fmt.Sprintf(schemaStmtFmt, objectType, objectName, schemaDDL.String()), nil |
| 199 | } |
| 200 | |
| 201 | func genIndexDDL(opts *IndexDDLOptions) (string, error) { |
| 202 | var builder strings.Builder |
no test coverage detected