Passed
Push — main ( 9e2022...6ef1c9 )
by Christian
02:04 queued 14s
created

adapter.*sqlite3Adapter.ErrorHandler   C

Complexity

Conditions 10

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
nop 1
dl 0
loc 28
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like adapter.*sqlite3Adapter.ErrorHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package adapter
2
3
import (
4
	"fmt"
5
6
	"github.com/cdleo/go-commons/sqlcommons"
7
	"github.com/mattn/go-sqlite3"
8
)
9
10
type sqlite3Adapter struct{}
11
12
func NewSQLite3Adapter() sqlcommons.SQLAdapter {
13
	return &sqlite3Adapter{}
14
}
15
16
func (t *sqlite3Adapter) Translate(query string) string {
17
	return query
18
}
19
20
func (s *sqlite3Adapter) ErrorHandler(err error) error {
21
22
	if sqliteError, ok := err.(sqlite3.Error); ok {
23
24
		if sqliteError.Code == 18 { //SQLITE_TOOBIG
25
			return sqlcommons.ValueTooLargeForColumn
26
27
		} else if sqliteError.Code == 19 { //SQLITE_CONSTRAINT
28
			if sqliteError.ExtendedCode == 787 || /*SQLITE_CONSTRAINT_FOREIGNKEY*/
29
				sqliteError.ExtendedCode == 1555 || /*SQLITE_CONSTRAINT_PRIMARYKEY*/
30
				sqliteError.ExtendedCode == 1811 { /*SQLITE_CONSTRAINT_TRIGGER*/
31
				return sqlcommons.IntegrityConstraintViolation
32
33
			} else if sqliteError.ExtendedCode == 1299 { //SQLITE_CONSTRAINT_NOTNULL
34
				return sqlcommons.CannotSetNullColumn
35
36
			} else if sqliteError.ExtendedCode == 2067 { //SQLITE_CONSTRAINT_UNIQUE
37
				return sqlcommons.UniqueConstraintViolation
38
39
			}
40
		} else if sqliteError.Code == 25 { //SQLITE_RANGE
41
			return sqlcommons.InvalidNumericValue
42
		}
43
44
		return fmt.Errorf("Unhandled SQLite3 error. Code:[%s] Extended:[%s] Desc:[%s]", sqliteError.Code, sqliteError.ExtendedCode, sqliteError.Error())
45
46
	} else {
47
		return err
48
	}
49
}
50