| 35 | } |
| 36 | |
| 37 | func configNodeToSource(config *gdb.ConfigNode) (string, error) { |
| 38 | var source string |
| 39 | source = fmt.Sprintf( |
| 40 | "user=%s password='%s' host=%s sslmode=disable", |
| 41 | config.User, config.Pass, config.Host, |
| 42 | ) |
| 43 | if config.Port != "" { |
| 44 | source = fmt.Sprintf("%s port=%s", source, config.Port) |
| 45 | } |
| 46 | if config.Name != "" { |
| 47 | source = fmt.Sprintf("%s dbname=%s", source, config.Name) |
| 48 | } |
| 49 | if config.Namespace != "" { |
| 50 | source = fmt.Sprintf("%s search_path=%s", source, config.Namespace) |
| 51 | } |
| 52 | if config.Timezone != "" { |
| 53 | source = fmt.Sprintf("%s timezone=%s", source, config.Timezone) |
| 54 | } |
| 55 | if config.Extra != "" { |
| 56 | extraMap, err := gstr.Parse(config.Extra) |
| 57 | if err != nil { |
| 58 | return "", gerror.WrapCodef( |
| 59 | gcode.CodeInvalidParameter, |
| 60 | err, |
| 61 | `invalid extra configuration: %s`, config.Extra, |
| 62 | ) |
| 63 | } |
| 64 | for k, v := range extraMap { |
| 65 | source += fmt.Sprintf(` %s=%s`, k, v) |
| 66 | } |
| 67 | } |
| 68 | return source, nil |
| 69 | } |