| Conditions | 7 |
| Total Lines | 57 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package database |
||
| 63 | func (s *SQLite) GetColumnsOfTable(table *Table) (err error) { |
||
| 64 | |||
| 65 | rows, err := s.Queryx(` |
||
| 66 | SELECT * |
||
| 67 | FROM PRAGMA_TABLE_INFO('` + table.Name + `') |
||
| 68 | `) |
||
| 69 | if err != nil { |
||
| 70 | if s.Verbose { |
||
| 71 | fmt.Printf("> Error at GetColumnsOfTable(%v)\r\n", table.Name) |
||
| 72 | fmt.Printf("> database: %q\r\n", s.DbName) |
||
| 73 | } |
||
| 74 | return err |
||
| 75 | } |
||
| 76 | |||
| 77 | type column struct { |
||
| 78 | CID int `db:"cid"` |
||
| 79 | Name string `db:"name"` |
||
| 80 | DataType string `db:"type"` |
||
| 81 | NotNull int `db:"notnull"` |
||
| 82 | DefaultValue sql.NullString `db:"dflt_value"` |
||
| 83 | PrimaryKey int `db:"pk"` |
||
| 84 | } |
||
| 85 | |||
| 86 | for rows.Next() { |
||
| 87 | var col column |
||
| 88 | err = rows.StructScan(&col) |
||
| 89 | if err != nil { |
||
| 90 | return err |
||
| 91 | } |
||
| 92 | |||
| 93 | isNullable := "YES" |
||
| 94 | if col.NotNull == 1 { |
||
| 95 | isNullable = "NO" |
||
| 96 | } |
||
| 97 | |||
| 98 | isPrimaryKey := "" |
||
| 99 | if col.PrimaryKey == 1 { |
||
| 100 | isPrimaryKey = "PK" |
||
| 101 | } |
||
| 102 | |||
| 103 | table.Columns = append(table.Columns, Column{ |
||
| 104 | OrdinalPosition: col.CID, |
||
| 105 | Name: col.Name, |
||
| 106 | DataType: col.DataType, |
||
| 107 | DefaultValue: col.DefaultValue, |
||
| 108 | IsNullable: isNullable, |
||
| 109 | CharacterMaximumLength: sql.NullInt64{}, |
||
| 110 | NumericPrecision: sql.NullInt64{}, |
||
| 111 | // reuse mysql column_key as primary key indicator |
||
| 112 | ColumnKey: isPrimaryKey, |
||
| 113 | Extra: "", |
||
| 114 | ConstraintName: sql.NullString{}, |
||
| 115 | ConstraintType: sql.NullString{}, |
||
| 116 | }) |
||
| 117 | } |
||
| 118 | |||
| 119 | return nil |
||
| 120 | } |
||
| 182 |