2014-02-15 15:29:53 +03:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fetchColumns(sql string) ([]string, error) {
|
|
|
|
if !strings.HasPrefix(sql, "CREATE TABLE ") {
|
|
|
|
return []string{}, fmt.Errorf("Sql input is not a DDL statement.")
|
|
|
|
}
|
|
|
|
|
|
|
|
parenIdx := strings.Index(sql, "(")
|
|
|
|
return strings.Split(sql[parenIdx+1:len(sql)-1], ","), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectName(columns []string) []string {
|
2014-02-15 18:17:22 +03:00
|
|
|
var results []string
|
2014-02-15 15:29:53 +03:00
|
|
|
for _, column := range columns {
|
|
|
|
col := strings.SplitN(strings.Trim(column, " \n\t"), " ", 2)
|
|
|
|
results = append(results, col[0])
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func setForUpdate(left []string, right []string) string {
|
2014-02-15 18:17:22 +03:00
|
|
|
var results []string
|
2014-02-15 15:29:53 +03:00
|
|
|
for k, str := range left {
|
|
|
|
results = append(results, fmt.Sprintf("%s = %s", str, right[k]))
|
|
|
|
}
|
|
|
|
return strings.Join(results, ", ")
|
|
|
|
}
|