engines.*oracleConn.ErrorHandler   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nop 1
dl 0
loc 26
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like engines.*oracleConn.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 engines
2
3
import (
4
	"database/sql"
5
	"database/sql/driver"
6
	"fmt"
7
	"time"
8
9
	adapter "github.com/cdleo/go-sql-adapter"
10
11
	"github.com/cdleo/go-commons/sqlcommons"
12
	"github.com/godror/godror"
13
	"github.com/godror/godror/dsn"
14
)
15
16
type oracleConn struct {
17
	connString string
18
	user       string
19
	password   string
20
}
21
22
const oracle_DriverName = "godror-proxy"
23
24
func NewOracleSqlConnector(host string, port int, user string, password string, database string) adapter.SQLEngineConnector {
25
26
	return &oracleConn{
27
		connString: fmt.Sprintf("(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%d))(CONNECT_DATA=(%s)))", host, port, database),
28
		user:       user,
29
		password:   password,
30
	}
31
}
32
33
func NewOracleTNSSqlConnector(tnsName string, user string, password string) adapter.SQLEngineConnector {
34
35
	return &oracleConn{
36
		connString: fmt.Sprintf("connectString=%s", tnsName),
37
		user:       user,
38
		password:   password,
39
	}
40
}
41
42
func (s *oracleConn) Open() (*sql.DB, error) {
43
44
	return sql.Open(oracle_DriverName, godror.ConnectionParams{
45
		CommonParams: godror.CommonParams{
46
			ConnectString: s.connString,
47
			Username:      s.user,
48
			Password:      godror.NewPassword(s.password),
49
			Timezone:      time.Local,
50
		},
51
		StandaloneConnection: true,
52
	}.StringWithPassword())
53
}
54
55
func (s *oracleConn) DriverName() string {
56
	return oracle_DriverName
57
}
58
59
func (s *oracleConn) Driver() driver.Driver {
60
	return godror.NewConnector(dsn.ConnectionParams{}).Driver()
61
}
62
63
func (s *oracleConn) ErrorHandler(err error) error {
64
	if err == nil {
65
		return nil
66
	}
67
68
	if oraError, ok := godror.AsOraErr(err); ok {
69
		switch oraError.Code() {
70
		case 1: //ORA-00001"
71
			return sqlcommons.UniqueConstraintViolation
72
		case 2291, 2292: //ORA-02291 (PKNotFound) AND ORA-02292 (ChildFound)
73
			return sqlcommons.IntegrityConstraintViolation
74
		case 12899: //ORA-12899
75
			return sqlcommons.ValueTooLargeForColumn
76
		case 1438: //ORA-01438
77
			return sqlcommons.ValueLargerThanPrecision
78
		case 1400, 1407: //ORA-01400 (cannot insert) AND ORA-01407 (cannot change value to)
79
			return sqlcommons.CannotSetNullColumn
80
		case 1722: //ORA-01722
81
			return sqlcommons.InvalidNumericValue
82
		case 1427: //ORA-01427
83
			return sqlcommons.SubqueryReturnsMoreThanOneRow
84
		default:
85
			return fmt.Errorf("Unhandled Oracle error. Code:[%d] Desc:[%s]", oraError.Code(), oraError.Message())
86
		}
87
	} else {
88
		return err
89
	}
90
}
91