func main() {

	var outputFileName string
	flag.StringVarP(&outputFileName, "output-file", "o", "", "Sends output to a file")

	dbInfo := pgutil.DbInfo{}
	verFlag, helpFlag := dbInfo.Populate()

	if verFlag {
		fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], version)
		fmt.Fprintln(os.Stderr, "Copyright (c) 2015 Jon Carlson.  All rights reserved.")
		fmt.Fprintln(os.Stderr, "Use of this source code is governed by the MIT license")
		fmt.Fprintln(os.Stderr, "that can be found here: http://opensource.org/licenses/MIT")
		os.Exit(1)
	}

	if helpFlag {
		usage()
	}

	// Remaining args:
	args := flag.Args()
	if len(args) < 2 {
		fmt.Fprintln(os.Stderr, "Missing some arguments")
		usage()
	}

	// genType
	genType := strings.ToUpper(args[0])
	if genType != "INSERT" && genType != "UPDATE" {
		fmt.Fprintf(os.Stderr, "Invalid generation type: %s.  Requires either INSERT or UPDATE\n", genType)
		usage()
	}

	if len(outputFileName) > 0 {
		var err error
		out, err = os.Create(outputFileName)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot open file: %s. \n", outputFileName)
			fmt.Fprintln(os.Stderr, err)
			os.Exit(2)
		}
	}

	// tableName
	tableName := args[1]

	// idColumn (UPDATE only) and whereClause
	whereClause := ""
	idCol := ""
	if genType == "INSERT" {
		if len(args) > 2 {
			whereClause = args[2]
		}
	} else {
		if len(args) < 3 {
			fmt.Fprintf(os.Stderr, "UPDATE requires an idColumn.")
			usage()
		}
		idCol = args[2]
		if len(args) > 3 {
			whereClause = args[3]
		}
	}

	if len(whereClause) == 0 {
		// Make sure user intended there to be no where clause
		if !misc.PromptYesNo("Did you intend to have no where clause?", true) {
			os.Exit(1)
		}
	}

	db, err := dbInfo.Open()
	check("opening database", err)

	query := "SELECT * FROM " + tableName + " " + whereClause
	printf("-- Creating %s(s) from query: %s\n", genType, query)
	rowChan, columnNames := querySqlValues(db, query)

	for row := range rowChan {
		if genType == "INSERT" {
			generateInsert(tableName, row, columnNames)
		} else {
			generateUpdate(tableName, row, idCol)
		}
	}
}
/*
 * Queries the given table name and copies the column values to either an INSERT statement or
 * an UPDATE statement.
 *
 * Example: pgcp -U myuser -d mydb INSERT t_user "WHERE user_id < 4"
 */
Queries the given table name and copies the column values to either an INSERT statement or
an UPDATE statement.

U myuser -d mydb INSERT t_user "WHERE user_id < 4"
/ * * Queries the given table name and copies the column values to either an INSERT statement or * an UPDATE statement . * * Example : pgcp - U myuser - d mydb INSERT t_user " WHERE user_id < 4 " * /
func main() {
	var outputFileName string
	flag.StringVarP(&outputFileName, "output-file", "o", "", "Sends output to a file")
	dbInfo := pgutil.DbInfo{}
	verFlag, helpFlag := dbInfo.Populate()
	if verFlag {
		fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], version)
		fmt.Fprintln(os.Stderr, "Copyright (c) 2015 Jon Carlson.  All rights reserved.")
		fmt.Fprintln(os.Stderr, "Use of this source code is governed by the MIT license")
		fmt.Fprintln(os.Stderr, "that can be found here: http://opensource.org/licenses/MIT")
		os.Exit(1)
	}
	if helpFlag {
		usage()
	}
	args := flag.Args()
	if len(args) < 2 {
		fmt.Fprintln(os.Stderr, "Missing some arguments")
		usage()
	}
	genType := strings.ToUpper(args[0])
	if genType != "INSERT" && genType != "UPDATE" {
		fmt.Fprintf(os.Stderr, "Invalid generation type: %s.  Requires either INSERT or UPDATE\n", genType)
		usage()
	}
	if len(outputFileName) > 0 {
		var err error
		out, err = os.Create(outputFileName)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot open file: %s. \n", outputFileName)
			fmt.Fprintln(os.Stderr, err)
			os.Exit(2)
		}
	}
	tableName := args[1]
	whereClause := ""
	idCol := ""
	if genType == "INSERT" {
		if len(args) > 2 {
			whereClause = args[2]
		}
	} else {
		if len(args) < 3 {
			fmt.Fprintf(os.Stderr, "UPDATE requires an idColumn.")
			usage()
		}
		idCol = args[2]
		if len(args) > 3 {
			whereClause = args[3]
		}
	}
	if len(whereClause) == 0 {
		if !misc.PromptYesNo("Did you intend to have no where clause?", true) {
			os.Exit(1)
		}
	}
	db, err := dbInfo.Open()
	check("opening database", err)
	query := "SELECT * FROM " + tableName + " " + whereClause
	printf("-- Creating %s(s) from query: %s\n", genType, query)
	rowChan, columnNames := querySqlValues(db, query)
	for row := range rowChan {
		if genType == "INSERT" {
			generateInsert(tableName, row, columnNames)
		} else {
			generateUpdate(tableName, row, idCol)
		}
	}
}
func main ( ) { var outputFileName string flag . StringVarP ( & outputFileName , " " , " " , " " , " " ) dbInfo := pgutil . DbInfo { } verFlag , helpFlag := dbInfo . Populate ( ) if verFlag { fmt . Fprintf ( os . Stderr , " \n " , os . Args [ 0 ] , version ) fmt . Fprintln ( os . Stderr , " " ) fmt . Fprintln ( os . Stderr , " " ) fmt . Fprintln ( os . Stderr , " " ) os . Exit ( 1 ) } if helpFlag { usage ( ) } args := flag . Args ( ) if len ( args ) < 2 { fmt . Fprintln ( os . Stderr , " " ) usage ( ) } genType := strings . ToUpper ( args [ 0 ] ) if genType != " " && genType != " " { fmt . Fprintf ( os . Stderr , " \n " , genType ) usage ( ) } if len ( outputFileName ) > 0 { var err error out , err = os . Create ( outputFileName ) if err != nil { fmt . Fprintf ( os . Stderr , " \n " , outputFileName ) fmt . Fprintln ( os . Stderr , err ) os . Exit ( 2 ) } } tableName := args [ 1 ] whereClause := " " idCol := " " if genType == " " { if len ( args ) > 2 { whereClause = args [ 2 ] } } else { if len ( args ) < 3 { fmt . Fprintf ( os . Stderr , " " ) usage ( ) } idCol = args [ 2 ] if len ( args ) > 3 { whereClause = args [ 3 ] } } if len ( whereClause ) == 0 { if ! misc . PromptYesNo ( " " , true ) { os . Exit ( 1 ) } } db , err := dbInfo . Open ( ) check ( " " , err ) query := " " + tableName + " " + whereClause printf ( " \n " , genType , query ) rowChan , columnNames := querySqlValues ( db , query ) for row := range rowChan { if genType == " " { generateInsert ( tableName , row , columnNames ) } else { generateUpdate ( tableName , row , idCol ) } } }
Queries the given table name and copies the column values to either an INSERT statement or
an UPDATE statement.
Queries the given table name and copies the column values to either an INSERT statement or an UPDATE statement .
// Remaining args: // genType // tableName // idColumn (UPDATE only) and whereClause // Make sure user intended there to be no where clause