Example #1
0
func connect() (base.IConnection, error) {
	conn := mongodb.NewConnection("localhost:27123", "", "", "ectest")
	e := conn.Connect()
	if e != nil {
		return nil, e
	}
	return conn, nil
}
Example #2
0
func prepareContext() (*DataContext, error) {
	dbLocation := beego.AppConfig.String("db_server")
	dbName := beego.AppConfig.String("db_name")

	conn := mongodb.NewConnection(dbLocation, "", "", dbName)
	if eConnect := conn.Connect(); eConnect != nil {
		return nil, eConnect
	}
	ctx := New(conn)
	return ctx, nil
}
Example #3
0
func DbConn() base.IConnection {
	if conn == nil {
		conn = mongodb.NewConnection("localhost:27123", "", "", "ecstora")
		e := conn.Connect()
		if e != nil {
			fmt.Println("Unable to connect database. " + e.Error())
			os.Exit(100)
		}
	}

	return conn
}
Example #4
0
func TestDb2(t *testing.T) {
	fmt.Println("Test 2b - Insert Data (Parallel)")
	numCount := pnumCount
	workerCount := pworkerCount
	idxs := make([]interface{}, numCount)
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i + 2000
	}

	conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
	//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
	e = conn.Connect()
	if e != nil {
		t.Error("Unable to connect to Database | " + e.Error())
		return
	}
	defer conn.Close()
	_, e = conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})
	if e != nil {
		fmt.Println("Unable to delete: " + e.Error())
	}

	fmt.Printf("Insert %d data within %d pools \n", numCount, workerCount)
	pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
		for jb := range js {
			j := jb.(int)
			user := toolkit.M{}
			userid := "User " + strconv.Itoa(j)
			user.Set("_id", userid)
			user.Set("fullname", "User "+strconv.Itoa(j))
			user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
			r := new(toolkit.Result)
			//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
			_, e := conn.Execute("appusers",
				toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
			if e == nil {
				r.Status = toolkit.Status_OK
				r.Data = user
			} else {
				r.Status = toolkit.Status_NOK
				r.Message = "Error ID " + strconv.Itoa(j) + " " + e.Error()
			}
			rs <- r
		}
	})

	if pr.Success == numCount {
		fmt.Printf("Test OK in %v \n\n", pr.Duration)
	} else {
		fmt.Printf("Test Fail has %d records. \nErrors\n%v\nIn %v \n\n", pr.Success, toolkit.JsonString(pr.Errors[0]), pr.Duration)
	}
}
Example #5
0
func NewConnection(connectionType string, host string, username string, password string, dbname string) (base.IConnection, error) {
	connectionType = strings.ToLower(connectionType)
	if connectionType == "mongodb" {
		c := mongodb.NewConnection(host, username, password, dbname)
		return c, nil
	} else if connectionType == "mysql" {
		c := mysql.NewConnection(host, username, password, dbname)
		return c, nil
	} else if connectionType == "mssql" {
		c := mssql.NewConnection(host, username, password, dbname)
		return c, nil
	}
	// else if connectionType == "oracle" {
	// 	c := oracle.NewConnection(host, username, password, dbname)
	// 	return c, nil
	// }

	e := err.Error(packageName, "", "NewConnection", fmt.Sprintf("Connection type %s is not yet supported", connectionType))
	return nil, e
}
Example #6
0
func TestDb1(t *testing.T) {
	//t.Skip()
	t0 := time.Now()
	fmt.Println("Test 2a - Insert Data (No Parallel)")
	numCount := pnumCount
	idxs := make([]interface{}, numCount)
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i
	}
	conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
	//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
	e = conn.Connect()
	if e != nil {
		t.Error("Unable to connect to Database | " + e.Error())
	}
	defer conn.Close()
	conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})

	success := 0
	fmt.Printf("Insert %d data with no pool \n", numCount)
	for _, jb := range idxs {
		j := jb.(int)
		user := toolkit.M{}
		userid := "User " + strconv.Itoa(j)
		user.Set("_id", userid)
		user.Set("fullname", "User "+strconv.Itoa(j))
		user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
		//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
		_, e := conn.Execute("appusers", toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
		if e == nil {
			success++
		}
	}

	if success == numCount {
		fmt.Printf("Test OK in %v \n\n", time.Since(t0))
	} else {
		fmt.Printf("Test Fail has %d records in %v \n\n", success, time.Since(t0))
	}
}
Example #7
0
func connect() error {
	if ctx == nil {
		ctx = mongodb.NewConnection("localhost:27123", "", "", "ecsec")
	}
	return ctx.Connect()
}