Exemplo n.º 1
0
func main() {

	fmt.Println("Program start...")
	//   	create connection to SQL DB voter_roll

	// log in to server
	db, err := sql.Open("mssql", dsn)
	if err != nil {
		message := "Cannot connect - 1 : "
		fmt.Println(message)
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}
	// ping to test connection
	err = db.Ping()
	if err != nil {
		message := "Ping failure - 2 : "
		fmt.Println(message)
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}
	defer db.Close()

	// 		Generate 1 million voters
	err = dummydata.LoadDummyData(db)
	if err != nil {
		fmt.Println("Load dummy data fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}

	candidates, err := prepVotingTable(db)
	if err != nil {
		fmt.Println("Prepping Voting table fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}

	//		Let them vote
	err = vote(db, candidates)
	if err != nil {
		fmt.Println("Vote process fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}

	fmt.Println("...The End!")

}
Exemplo n.º 2
0
// --Working
//******************************************************************************************
//
//  Load database login credentials from config file
//
//******************************************************************************************
func main() {

	fmt.Println("Go!")

	t := time.Now().Format(time.RFC850)
	fmt.Println("Run date start : ", t)

	cpus := runtime.NumCPU()
	x := runtime.GOMAXPROCS(cpus - 1)

	fmt.Println("maxcpu = ", x)
	fmt.Println("CPU used", runtime.GOMAXPROCS(-1))

	//-------------------------------------------------
	// log in to SQL server
	//-------------------------------------------------
	//   - sql.Open() : Get a connection to mssql (db)
	//	 - db.Ping()  : Test the connection created
	//   - db.Close() : Close connection when finished.
	//-------------------------------------------------

	var c credentials
	c.loadCredentials()

	dsn := "server=" + c.server + ";user id=" + c.userID + ";password="******";database=" + c.database

	// sql.Open()

	db, err := sql.Open("mssql", dsn)

	if err != nil {
		fmt.Println("Cannot connect - 1 : ")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	} else {
		fmt.Println("Opened")
	}

	// db.Ping()

	err = db.Ping()

	if err != nil {
		fmt.Println("Ping failure - 2 : ")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	} else {
		fmt.Println("Ping successfull!")
	}

	// db.Close()

	defer db.Close()

	//-------------------------------------------------
	// 	Generate 1 million voters
	//-------------------------------------------------
	err = dummydata.LoadDummyData(db)

	if err != nil {
		fmt.Println("Load dummy data fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}

	//-------------------------------------------------
	//  load candidates : demo db.Exec
	//-------------------------------------------------
	candidates, err := prepVotingTable(db)

	if err != nil {
		fmt.Println("Prepping Voting table fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	} else {
		fmt.Println("a: ", candidates)
	}

	//-------------------------------------------------
	//	Let them vote
	//-------------------------------------------------
	err = vote(db, candidates)

	if err != nil {
		fmt.Println("Vote process fail!!!!")
		errorString := err.Error()
		fmt.Println(errorString)
		return
	}

	//-------------------------------------------------
	//	Produce results from voting
	//-------------------------------------------------
	/*
		err = showResultsofVoting()

		if err != nil {
			fmt.Println("Print vote results fail!!!!")
			errorString := err.Error()
			fmt.Println(errorString)
			return
		}

	*/
	t1 := time.Now().Format(time.RFC850)

	fmt.Println("Run date start : ", t)
	fmt.Println("Run date end 	: ", t1)

	fmt.Println("The End!")
}