vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/command/query.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package command 18 19 import ( 20 "fmt" 21 22 "github.com/spf13/cobra" 23 24 "vitess.io/vitess/go/cmd/vtctldclient/cli" 25 "vitess.io/vitess/go/sqltypes" 26 "vitess.io/vitess/go/vt/topo/topoproto" 27 28 vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" 29 ) 30 31 var ( 32 // ExecuteFetchAsApp makes an ExecuteFetchAsApp gRPC call to a vtctld. 33 ExecuteFetchAsApp = &cobra.Command{ 34 Use: "ExecuteFetchAsApp [--max-rows <max-rows>] [--json|-j] [--use-pool] <tablet-alias> <query>", 35 Short: "Executes the given query as the App user on the remote tablet.", 36 DisableFlagsInUseLine: true, 37 Args: cobra.ExactArgs(2), 38 RunE: commandExecuteFetchAsApp, 39 } 40 // ExecuteFetchAsDBA makes an ExecuteFetchAsDBA gRPC call to a vtctld. 41 ExecuteFetchAsDBA = &cobra.Command{ 42 Use: "ExecuteFetchAsDBA [--max-rows <max-rows>] [--json|-j] [--disable-binlogs] [--reload-schema] <tablet alias> <query>", 43 Short: "Executes the given query as the DBA user on the remote tablet.", 44 DisableFlagsInUseLine: true, 45 Args: cobra.ExactArgs(2), 46 RunE: commandExecuteFetchAsDBA, 47 Aliases: []string{"ExecuteFetchAsDba"}, 48 } 49 ) 50 51 var executeFetchAsAppOptions = struct { 52 MaxRows int64 53 UsePool bool 54 JSON bool 55 }{ 56 MaxRows: 10_000, 57 } 58 59 func commandExecuteFetchAsApp(cmd *cobra.Command, args []string) error { 60 alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0)) 61 if err != nil { 62 return err 63 } 64 65 cli.FinishedParsing(cmd) 66 67 query := cmd.Flags().Arg(1) 68 69 resp, err := client.ExecuteFetchAsApp(commandCtx, &vtctldatapb.ExecuteFetchAsAppRequest{ 70 TabletAlias: alias, 71 Query: query, 72 MaxRows: executeFetchAsAppOptions.MaxRows, 73 UsePool: executeFetchAsAppOptions.UsePool, 74 }) 75 if err != nil { 76 return err 77 } 78 79 qr := sqltypes.Proto3ToResult(resp.Result) 80 switch executeFetchAsAppOptions.JSON { 81 case true: 82 data, err := cli.MarshalJSON(qr) 83 if err != nil { 84 return err 85 } 86 87 fmt.Printf("%s\n", data) 88 default: 89 cli.WriteQueryResultTable(cmd.OutOrStdout(), qr) 90 } 91 92 return nil 93 } 94 95 var executeFetchAsDBAOptions = struct { 96 MaxRows int64 97 DisableBinlogs bool 98 ReloadSchema bool 99 JSON bool 100 }{ 101 MaxRows: 10_000, 102 } 103 104 func commandExecuteFetchAsDBA(cmd *cobra.Command, args []string) error { 105 alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0)) 106 if err != nil { 107 return err 108 } 109 110 cli.FinishedParsing(cmd) 111 112 query := cmd.Flags().Arg(1) 113 114 resp, err := client.ExecuteFetchAsDBA(commandCtx, &vtctldatapb.ExecuteFetchAsDBARequest{ 115 TabletAlias: alias, 116 Query: query, 117 MaxRows: executeFetchAsDBAOptions.MaxRows, 118 DisableBinlogs: executeFetchAsDBAOptions.DisableBinlogs, 119 ReloadSchema: executeFetchAsDBAOptions.ReloadSchema, 120 }) 121 if err != nil { 122 return err 123 } 124 125 qr := sqltypes.Proto3ToResult(resp.Result) 126 switch executeFetchAsDBAOptions.JSON { 127 case true: 128 data, err := cli.MarshalJSON(qr) 129 if err != nil { 130 return err 131 } 132 133 fmt.Printf("%s\n", data) 134 default: 135 cli.WriteQueryResultTable(cmd.OutOrStdout(), qr) 136 } 137 138 return nil 139 } 140 141 func init() { 142 ExecuteFetchAsApp.Flags().Int64Var(&executeFetchAsAppOptions.MaxRows, "max-rows", 10_000, "The maximum number of rows to fetch from the remote tablet.") 143 ExecuteFetchAsApp.Flags().BoolVar(&executeFetchAsAppOptions.UsePool, "use-pool", false, "Use the tablet connection pool instead of creating a fresh connection.") 144 ExecuteFetchAsApp.Flags().BoolVarP(&executeFetchAsAppOptions.JSON, "json", "j", false, "Output the results in JSON instead of a human-readable table.") 145 Root.AddCommand(ExecuteFetchAsApp) 146 147 ExecuteFetchAsDBA.Flags().Int64Var(&executeFetchAsDBAOptions.MaxRows, "max-rows", 10_000, "The maximum number of rows to fetch from the remote tablet.") 148 ExecuteFetchAsDBA.Flags().BoolVar(&executeFetchAsDBAOptions.DisableBinlogs, "disable-binlogs", false, "Disables binary logging during the query.") 149 ExecuteFetchAsDBA.Flags().BoolVar(&executeFetchAsDBAOptions.ReloadSchema, "reload-schema", false, "Instructs the tablet to reload its schema after executing the query.") 150 ExecuteFetchAsDBA.Flags().BoolVarP(&executeFetchAsDBAOptions.JSON, "json", "j", false, "Output the results in JSON instead of a human-readable table.") 151 Root.AddCommand(ExecuteFetchAsDBA) 152 }