github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/skiperrs/main.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // skiperrs connects to a postgres-compatible server with its URL specified as
    12  // the first argument. It then splits stdin into SQL statements and executes
    13  // them on the connection. Errors are printed but do not stop execution.
    14  package main
    15  
    16  import (
    17  	gosql "database/sql"
    18  	"fmt"
    19  	"io"
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/cockroachdb/cockroach/pkg/cmd/cr2pg/sqlstream"
    24  	"github.com/lib/pq"
    25  )
    26  
    27  func main() {
    28  	if len(os.Args) != 2 {
    29  		fmt.Printf("usage: %s <url>\n", os.Args[0])
    30  		os.Exit(1)
    31  	}
    32  	url := os.Args[1]
    33  
    34  	connector, err := pq.NewConnector(url)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	db := gosql.OpenDB(connector)
    39  	defer db.Close()
    40  
    41  	stream := sqlstream.NewStream(os.Stdin)
    42  	for {
    43  		stmt, err := stream.Next()
    44  		if err == io.EOF {
    45  			break
    46  		} else if err != nil {
    47  			log.Fatal(err)
    48  		}
    49  		if _, err := db.Exec(stmt.String()); err != nil {
    50  			fmt.Println(err)
    51  		}
    52  	}
    53  }