github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/query/execute_options.go (about) 1 package query 2 3 import ( 4 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query" 5 "google.golang.org/grpc" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/params" 8 ) 9 10 type ( 11 Syntax Ydb_Query.Syntax 12 ExecMode Ydb_Query.ExecMode 13 StatsMode Ydb_Query.StatsMode 14 callOptions []grpc.CallOption 15 commonExecuteSettings struct { 16 syntax Syntax 17 params params.Parameters 18 execMode ExecMode 19 statsMode StatsMode 20 callOptions []grpc.CallOption 21 } 22 executeSettings struct { 23 commonExecuteSettings 24 25 txControl *TransactionControl 26 } 27 ExecuteOption interface { 28 applyExecuteOption(s *executeSettings) 29 } 30 txExecuteSettings struct { 31 ExecuteSettings *executeSettings 32 33 commitTx bool 34 } 35 TxExecuteOption interface { 36 applyTxExecuteOption(s *txExecuteSettings) 37 } 38 txCommitOption struct{} 39 parametersOption params.Parameters 40 ) 41 42 func (t txCommitOption) applyTxExecuteOption(s *txExecuteSettings) { 43 s.commitTx = true 44 } 45 46 func (syntax Syntax) applyTxExecuteOption(s *txExecuteSettings) { 47 syntax.applyExecuteOption(s.ExecuteSettings) 48 } 49 50 func (syntax Syntax) applyExecuteOption(s *executeSettings) { 51 s.syntax = syntax 52 } 53 54 const ( 55 SyntaxYQL = Syntax(Ydb_Query.Syntax_SYNTAX_YQL_V1) 56 SyntaxPostgreSQL = Syntax(Ydb_Query.Syntax_SYNTAX_PG) 57 ) 58 59 func (params parametersOption) applyTxExecuteOption(s *txExecuteSettings) { 60 params.applyExecuteOption(s.ExecuteSettings) 61 } 62 63 func (params parametersOption) applyExecuteOption(s *executeSettings) { 64 s.params = append(s.params, params...) 65 } 66 67 func (opts callOptions) applyExecuteOption(s *executeSettings) { 68 s.callOptions = append(s.callOptions, opts...) 69 } 70 71 func (opts callOptions) applyTxExecuteOption(s *txExecuteSettings) { 72 opts.applyExecuteOption(s.ExecuteSettings) 73 } 74 75 func (mode StatsMode) applyTxExecuteOption(s *txExecuteSettings) { 76 mode.applyExecuteOption(s.ExecuteSettings) 77 } 78 79 func (mode StatsMode) applyExecuteOption(s *executeSettings) { 80 s.statsMode = mode 81 } 82 83 func (mode ExecMode) applyTxExecuteOption(s *txExecuteSettings) { 84 mode.applyExecuteOption(s.ExecuteSettings) 85 } 86 87 func (mode ExecMode) applyExecuteOption(s *executeSettings) { 88 s.execMode = mode 89 } 90 91 const ( 92 ExecModeParse = ExecMode(Ydb_Query.ExecMode_EXEC_MODE_PARSE) 93 ExecModeValidate = ExecMode(Ydb_Query.ExecMode_EXEC_MODE_VALIDATE) 94 ExecModeExplain = ExecMode(Ydb_Query.ExecMode_EXEC_MODE_EXPLAIN) 95 ExecModeExecute = ExecMode(Ydb_Query.ExecMode_EXEC_MODE_EXECUTE) 96 ) 97 98 const ( 99 StatsModeBasic = StatsMode(Ydb_Query.StatsMode_STATS_MODE_BASIC) 100 StatsModeNone = StatsMode(Ydb_Query.StatsMode_STATS_MODE_NONE) 101 StatsModeFull = StatsMode(Ydb_Query.StatsMode_STATS_MODE_FULL) 102 StatsModeProfile = StatsMode(Ydb_Query.StatsMode_STATS_MODE_PROFILE) 103 ) 104 105 func defaultCommonExecuteSettings() commonExecuteSettings { 106 return commonExecuteSettings{ 107 syntax: SyntaxYQL, 108 execMode: ExecModeExecute, 109 statsMode: StatsModeNone, 110 } 111 } 112 113 func ExecuteSettings(opts ...ExecuteOption) (settings *executeSettings) { 114 settings = &executeSettings{ 115 commonExecuteSettings: defaultCommonExecuteSettings(), 116 } 117 settings.commonExecuteSettings = defaultCommonExecuteSettings() 118 settings.txControl = DefaultTxControl() 119 for _, opt := range opts { 120 opt.applyExecuteOption(settings) 121 } 122 123 return settings 124 } 125 126 func (s *executeSettings) TxControl() *TransactionControl { 127 return s.txControl 128 } 129 130 func (s *commonExecuteSettings) CallOptions() []grpc.CallOption { 131 return s.callOptions 132 } 133 134 func (s *commonExecuteSettings) Syntax() Syntax { 135 return s.syntax 136 } 137 138 func (s *commonExecuteSettings) ExecMode() ExecMode { 139 return s.execMode 140 } 141 142 func (s *commonExecuteSettings) StatsMode() StatsMode { 143 return s.statsMode 144 } 145 146 func (s *commonExecuteSettings) Params() *params.Parameters { 147 if len(s.params) == 0 { 148 return nil 149 } 150 151 return &s.params 152 } 153 154 func TxExecuteSettings(id string, opts ...TxExecuteOption) (settings *txExecuteSettings) { 155 settings = &txExecuteSettings{ 156 ExecuteSettings: ExecuteSettings(WithTxControl(TxControl(WithTxID(id)))), 157 } 158 for _, opt := range opts { 159 opt.applyTxExecuteOption(settings) 160 } 161 162 return settings 163 } 164 165 var _ ExecuteOption = (*parametersOption)(nil) 166 167 func WithParameters(parameters *params.Parameters) *parametersOption { 168 params := parametersOption(*parameters) 169 170 return ¶ms 171 } 172 173 var ( 174 _ ExecuteOption = ExecMode(0) 175 _ ExecuteOption = StatsMode(0) 176 _ TxExecuteOption = ExecMode(0) 177 _ TxExecuteOption = StatsMode(0) 178 _ TxExecuteOption = txCommitOption{} 179 ) 180 181 func WithCommit() txCommitOption { 182 return txCommitOption{} 183 } 184 185 func WithExecMode(mode ExecMode) ExecMode { 186 return mode 187 } 188 189 func WithSyntax(syntax Syntax) Syntax { 190 return syntax 191 } 192 193 func WithStatsMode(mode StatsMode) StatsMode { 194 return mode 195 } 196 197 func WithCallOptions(opts ...grpc.CallOption) callOptions { 198 return opts 199 } 200 201 func WithTxControl(txControl *TransactionControl) *transactionControlOption { 202 return &transactionControlOption{ 203 txControl: txControl, 204 } 205 }