adapter/oracle.go   A
last analyzed

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C adapter.*oracleAdapter.ErrorHandler 0 26 11
A adapter.NewOracleAdapter 0 2 1
A adapter.*oracleAdapter.Translate 0 2 1
1
package adapter
2
3
import (
4
	"fmt"
5
6
	"github.com/cdleo/go-commons/sqlcommons"
7
	"github.com/godror/godror"
8
)
9
10
type oracleAdapter struct{}
11
12
func NewOracleAdapter() sqlcommons.SQLAdapter {
13
	return &oracleAdapter{}
14
}
15
16
func (t *oracleAdapter) Translate(query string) string {
17
	return query
18
}
19
20
func (s *oracleAdapter) ErrorHandler(err error) error {
21
	if err == nil {
22
		return nil
23
	}
24
25
	if oraError, ok := godror.AsOraErr(err); ok {
26
		switch oraError.Code() {
27
		case 1: //ORA-00001"
28
			return sqlcommons.UniqueConstraintViolation
29
		case 2291, 2292: //ORA-02291 (PKNotFound) AND ORA-02292 (ChildFound)
30
			return sqlcommons.IntegrityConstraintViolation
31
		case 12899: //ORA-12899
32
			return sqlcommons.ValueTooLargeForColumn
33
		case 1438: //ORA-01438
34
			return sqlcommons.ValueLargerThanPrecision
35
		case 1400, 1407: //ORA-01400 (cannot insert) AND ORA-01407 (cannot change value to)
36
			return sqlcommons.CannotSetNullColumn
37
		case 1722: //ORA-01722
38
			return sqlcommons.InvalidNumericValue
39
		case 1427: //ORA-01427
40
			return sqlcommons.SubqueryReturnsMoreThanOneRow
41
		default:
42
			return fmt.Errorf("Unhandled Oracle error. Code:[%d] Desc:[%s]", oraError.Code(), oraError.Message())
43
		}
44
	} else {
45
		return err
46
	}
47
}
48