Esempio n. 1
0
// Inserts rows into the currently active table.
func (t *MysqlTable) Append(items ...interface{}) ([]db.Id, error) {

	ids := []db.Id{}

	itop := len(items)

	for i := 0; i < itop; i++ {

		values := []string{}
		fields := []string{}

		item := items[i]

		for field, value := range item.(db.Item) {
			fields = append(fields, field)
			values = append(values, toInternal(value))
		}

		_, err := t.parent.myExec(
			"Exec",
			"INSERT INTO",
			myTable(t.name),
			myFields(fields),
			"VALUES",
			myValues(values),
		)

		res, _ := t.parent.myExec(
			"Query",
			"SELECT LAST_INSERT_ID()",
		)

		var lastId string

		res.Next()

		res.Scan(&lastId)

		ids = append(ids, db.Id(lastId))

		if err != nil {
			return ids, err
		}

	}

	return ids, nil
}
Esempio n. 2
0
// Inserts rows into the currently active table.
func (t *PostgresqlTable) Append(items ...interface{}) ([]db.Id, error) {

	ids := []db.Id{}

	itop := len(items)

	for i := 0; i < itop; i++ {

		values := []string{}
		fields := []string{}

		item := items[i]

		for field, value := range item.(db.Item) {
			fields = append(fields, field)
			values = append(values, toInternal(value))
		}

		_, err := t.parent.pgExec(
			"Exec",
			"INSERT INTO",
			pgTable(t.name),
			pgFields(fields),
			"VALUES",
			pgValues(values),
		)

		res, _ := t.parent.pgExec(
			"Query",
			fmt.Sprintf("SELECT CURRVAL(pg_get_serial_sequence('%s','id'))", t.name),
		)

		var lastId string

		res.Next()

		res.Scan(&lastId)

		ids = append(ids, db.Id(lastId))

		if err != nil {
			return ids, err
		}

	}

	return ids, nil
}
Esempio n. 3
0
// Transforms data from mgo format into db.Item format.
func toNative(val interface{}) interface{} {

	switch val.(type) {
	case bson.M:
		v2 := map[string]interface{}{}
		for k, v := range val.(bson.M) {
			v2[k] = toNative(v)
		}
		return v2
	case bson.ObjectId:
		return db.Id(val.(bson.ObjectId).Hex())
	}

	return val

}
Esempio n. 4
0
// Appends an item to the collection.
func (self *SourceCollection) Append(items ...interface{}) ([]db.Id, error) {

	var err error

	ids := []db.Id{}

	parent := reflect.TypeOf(self.collection)
	method, _ := parent.MethodByName("Insert")

	args := make([]reflect.Value, 1+len(items))
	args[0] = reflect.ValueOf(self.collection)

	itop := len(items)

	for i := 0; i < itop; i++ {
		id := db.Id(bson.NewObjectId().Hex())

		switch items[i].(type) {
		case map[string]interface{}:
			if items[i].(map[string]interface{})["_id"] == nil {
				items[i].(map[string]interface{})["_id"] = id
			}
		case db.Item:
			if items[i].(db.Item)["_id"] == nil {
				items[i].(db.Item)["_id"] = id
			}
		}

		args[i+1] = reflect.ValueOf(toInternal(items[i]))
		ids = append(ids, id)
	}

	exec := method.Func.Call(args)

	if exec[0].Interface() != nil {
		err = exec[0].Interface().(error)
	}

	return ids, err
}
Esempio n. 5
0
func TestDataTypes(t *testing.T) {

	sess := Session(db.DataSource{Host: pgHost, Database: pgDatabase, User: pgUser, Password: pgPassword})

	err := sess.Open()

	if err == nil {
		defer sess.Close()
	}

	col := sess.Collection("data_types")

	col.Truncate()

	data := getTestData()

	ids, err := col.Append(data)

	if err != nil {
		t.Errorf("Could not append test data.")
	}

	found, _ := col.Count(db.Cond{"id": db.Id(ids[0])})

	if found == 0 {
		t.Errorf("Cannot find recently inserted item (by ID).")
	}

	// Getting and reinserting.
	item := col.Find()

	_, err = col.Append(item)

	if err == nil {
		t.Errorf("Expecting duplicated-key error.")
	}

	delete(item, "id")

	_, err = col.Append(item)

	if err != nil {
		t.Errorf("Could not append second element.")
	}

	// Testing rows
	items := col.FindAll()

	for i := 0; i < len(items); i++ {

		item := items[i]

		for key, _ := range item {

			switch key {

			// Signed integers.
			case
				"_int",
				"_int8",
				"_int16",
				"_int32",
				"_int64":
				if item.GetInt(key) != int64(data["_int"].(int)) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Unsigned integers.
			case
				"_uint",
				"_uintptr",
				"_uint8",
				"_uint16",
				"_uint32",
				"_uint64",
				"_byte",
				"_rune":
				if item.GetInt(key) != int64(data["_uint"].(uint)) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Floating point.
			case "_float32":
			case "_float64":
				if item.GetFloat(key) != data["_float64"].(float64) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Boolean
			case "_bool":
				if item.GetBool(key) != data["_bool"].(bool) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// String
			case "_string":
				if item.GetString(key) != data["_string"].(string) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Map
			case "_map":
				if item.GetTuple(key)["a"] != data["_map"].(sugar.Tuple)["a"] {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Array
			case "_list":
				if item.GetList(key)[0] != data["_list"].(sugar.List)[0] {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Time
			case "_time":
				if item.GetDuration(key).String() != data["_time"].(time.Duration).String() {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Date
			case "_date":
				if item.GetDate(key).Equal(data["_date"].(time.Time)) == false {
					t.Errorf("Wrong datatype %v.", key)
				}
			}
		}
	}

}
Esempio n. 6
0
func TestDataTypes(t *testing.T) {

	sess, err := db.Open("sqlite", db.DataSource{Database: dbpath})

	if err != nil {
		t.Errorf(err.Error())
		return
	}

	defer sess.Close()

	dataTypes, _ := sess.Collection("data_types")

	dataTypes.Truncate()

	testData := testItem()

	ids, err := dataTypes.Append(testData)

	if err != nil {
		t.Errorf("Could not append test data: %s.", err.Error())
	}

	found, _ := dataTypes.Count(db.Cond{"id": db.Id(ids[0])})

	if found == 0 {
		t.Errorf("Cannot find recently inserted item (by ID).")
	}

	// Getting and reinserting.

	item := dataTypes.Find()

	_, err = dataTypes.Append(item)

	if err == nil {
		t.Errorf("Expecting duplicated-key error.")
	}

	delete(item, "id")

	_, err = dataTypes.Append(item)

	if err != nil {
		t.Errorf("Could not append second element: %s.", err.Error())
	}

	// Testing rows

	results := dataTypes.FindAll()

	for _, item := range results {

		for key, _ := range item {

			switch key {

			// Signed integers.
			case
				"_int",
				"_int8",
				"_int16",
				"_int32",
				"_int64":
				if item.GetInt(key) != int64(testData["_int"].(int)) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Unsigned integers.
			case
				"_uint",
				"_uintptr",
				"_uint8",
				"_uint16",
				"_uint32",
				"_uint64",
				"_byte",
				"_rune":
				if item.GetInt(key) != int64(testData["_uint"].(uint)) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Floating point.
			case "_float32":
			case "_float64":
				if item.GetFloat(key) != testData["_float64"].(float64) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// Boolean
			case "_bool":
				if item.GetBool(key) != testData["_bool"].(bool) {
					t.Errorf("Wrong datatype %v.", key)
				}

			// String
			case "_string":
				if item.GetString(key) != testData["_string"].(string) {
					t.Errorf("Wrong datatype %v.", key)
				}

			/*
				// Map
				case "_map":
					if item.GetTuple(key)["a"] != testData["_map"].(sugar.Tuple)["a"] {
						t.Errorf("Wrong datatype %v.", key)
					}

				// Array
				case "_list":
					if item.GetList(key)[0] != testData["_list"].(sugar.List)[0] {
						t.Errorf("Wrong datatype %v.", key)
					}
			*/

			// Date
			case "_date":
				if item.GetDate(key).Equal(testData["_date"].(time.Time)) == false {
					t.Errorf("Wrong datatype %v.", key)
				}
			}
		}
	}

}