github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/db/transaction/transaction.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package transaction 17 18 import ( 19 "github.com/cloudwan/gohan/db/pagination" 20 "github.com/cloudwan/gohan/schema" 21 "github.com/jmoiron/sqlx" 22 ) 23 24 //Type represents transaction types 25 type Type string 26 27 //Filter represents db filter 28 type Filter map[string]interface{} 29 30 const ( 31 //ReadUncommited is transaction type for READ UNCOMMITTED 32 //You don't need to use this for most case 33 ReadUncommited Type = "READ UNCOMMITTED" 34 //ReadCommited is transaction type for READ COMMITTED 35 //You don't need to use this for most case 36 ReadCommited Type = "READ COMMITTED" 37 //RepeatableRead is transaction type for REPEATABLE READ 38 //This is default value for read request 39 RepeatableRead Type = "REPEATABLE READ" 40 //Serializable is transaction type for Serializable 41 Serializable Type = "Serializable" 42 ) 43 44 //ResourceState represents the state of a resource 45 type ResourceState struct { 46 ConfigVersion int64 47 StateVersion int64 48 Error string 49 State string 50 Monitoring string 51 } 52 53 //Transaction is common interface for handing transaction 54 type Transaction interface { 55 Create(*schema.Resource) error 56 Update(*schema.Resource) error 57 SetIsolationLevel(Type) error 58 StateUpdate(*schema.Resource, *ResourceState) error 59 Delete(*schema.Schema, interface{}) error 60 Fetch(*schema.Schema, Filter) (*schema.Resource, error) 61 StateFetch(*schema.Schema, Filter) (ResourceState, error) 62 List(*schema.Schema, Filter, *pagination.Paginator) ([]*schema.Resource, uint64, error) 63 RawTransaction() *sqlx.Tx 64 Query(*schema.Schema, string, []interface{}) (list []*schema.Resource, err error) 65 Commit() error 66 Exec(query string, args ...interface{}) error 67 Close() error 68 Closed() bool 69 } 70 71 // GetIsolationLevel returns isolation level for an action 72 func GetIsolationLevel(s *schema.Schema, action string) Type { 73 level, ok := s.IsolationLevel[action] 74 if !ok { 75 switch action { 76 case "read": 77 return RepeatableRead 78 default: 79 return Serializable 80 } 81 } 82 return level.(Type) 83 } 84 85 //IDFilter create filter for specific ID 86 func IDFilter(ID interface{}) Filter { 87 return Filter{"id": ID} 88 }