Exemple #1
0
// GetEavValueTables returns a map of all custom and default EAV value tables for entity type codes.
// Despite value_table_prefix can have in Magento a different table name we treat it here
// as the table name itself. Not thread safe.
func GetEavValueTables(dbrConn *dbr.Connection, entityTypeCodes []string) (TypeCodeValueTable, error) {

	typeCodeTables := make(TypeCodeValueTable, len(entityTypeCodes))

	for _, typeCode := range entityTypeCodes {

		vtp, err := dbrConn.NewSession().
			Select("`value_table_prefix`").
			From(TablePrefix+TableEavEntityType).
			Where(dbr.ConditionRaw("`value_table_prefix` IS NOT NULL"), dbr.ConditionRaw("`entity_type_code` = ?", typeCode)).
			ReturnString()

		if err != nil && err != dbr.ErrNotFound {
			return nil, errgo.Mask(err)
		}
		if vtp == "" {
			vtp = typeCode + TableNameSeparator + TableEntityTypeSuffix + TableNameSeparator // e.g. catalog_product_entity_
		} else {
			vtp = vtp + TableNameSeparator
		}

		tableNames, err := GetTables(dbrConn.NewSession(), vtp+`%`)
		if err != nil {
			return nil, errgo.Mask(err)
		}

		if _, ok := typeCodeTables[typeCode]; !ok {
			typeCodeTables[typeCode] = make(map[string]string, len(tableNames))
		}
		for _, t := range tableNames {
			valueSuffix := t[len(vtp):]
			if TableEntityTypeValueSuffixes.contains(valueSuffix) {
				/*
				   other tables like catalog_product_entity_gallery, catalog_product_entity_group_price,
				   catalog_product_entity_tier_price, etc are the backend model tables for different storage systems.
				   they are not part of the default EAV model.
				*/
				typeCodeTables[typeCode][t] = valueSuffix
			}

		}

	}

	return typeCodeTables, nil
}
Exemple #2
0
func TestResurrectStmtRealDB(t *testing.T) {
	debugLogBuf.Reset()
	infoLogBuf.Reset()
	defer debugLogBuf.Reset()
	defer infoLogBuf.Reset()

	dbc := csdb.MustConnectTest()
	defer func() { assert.NoError(t, dbc.Close()) }()

	tw := newTypeWriterReal(dbc.DB)
	tw.Write.StartIdleChecker()

	assert.NoError(t, tw.Save("RSgopher1", 1))
	assert.False(t, tw.Write.IsIdle())

	assert.NoError(t, tw.Save("RSgopher2", 2))
	assert.False(t, tw.Write.IsIdle())

	time.Sleep(time.Millisecond * 60) // 1. close
	assert.True(t, tw.Write.IsIdle())
	assert.NoError(t, tw.Write.StopIdleChecker()) // 2.close

	assert.NoError(t, tw.Save("RSgopher3", 3))
	assert.NoError(t, tw.Save("RSgopher4", 4))

	//	println("\n", debugLogBuf.String(), "\n")
	//	println("\n", infoLogBuf.String(), "\n")

	// to be more precise you must check the order of the logged values
	assert.Exactly(t, 2, strings.Count(debugLogBuf.String(), `csdb.ResurrectStmt.stmt.Close SQL: "REPLACE INTO`))
	assert.Exactly(t, 2, strings.Count(debugLogBuf.String(), `csdb.ResurrectStmt.stmt.Prepare SQL: "REPLACE INTO`))

	res, err := dbc.NewSession().DeleteFrom("core_config_data").Where(dbr.ConditionRaw("path like \"RSgopher%\"")).Exec()
	assert.NoError(t, err)
	ar, err := res.RowsAffected()
	assert.NoError(t, err)
	assert.Exactly(t, int64(4), ar)
}
Exemple #3
0
// GetAttributeSelectSql generates the select query to retrieve full attribute configuration
// Implements the scope on a SQL query basis so that attribute functions does not need to deal with it.
// Tests see the tools package
// @see magento2/app/code/Magento/Eav/Model/Resource/Attribute/Collection.php::_initSelect()
func GetAttributeSelectSql(dbrSess dbr.SessionRunner, aat EntityTypeAdditionalAttributeTabler, entityTypeID, websiteID int64) (*dbr.SelectBuilder, error) {

	ta, err := TableCollection.Structure(TableIndexAttribute)
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableCollection.Structure", "err", err, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	taa, err := aat.TableAdditionalAttribute()
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableAdditionalAttribute", "err", err, "ta", ta, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	tew, err := aat.TableEavWebsite()
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableEavWebsite", "err", err, "ta", ta, "taa", taa, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	// tew table can now contains columns names which can occur in table eav_attribute and
	// or [catalog|customer|entity]_eav_attribute
	var (
		ifnull           []string
		tewAddedCols     []string
		taColumnsQuoted  = utils.StringSlice(ta.AllColumnAliasQuote(csdb.MainTable))
		taaColumnsQuoted = utils.StringSlice(taa.ColumnAliasQuote(csdb.AdditionalTable))
	)

	if tew != nil {
		ifnull = make([]string, len(tew.Columns))
		for i, tewC := range tew.Columns.ColumnsNoPK().FieldNames() {
			t := ""
			switch {
			case ta.In(tewC):
				t = csdb.MainTable
				break
			case taa.In(tewC):
				t = csdb.AdditionalTable
				break
			default:
				err := fmt.Errorf("Cannot find column name %s.%s neither in table %s nor in %s.", tew.Name, tewC, ta.Name, taa.Name)
				if PkgLog.IsDebug() {
					PkgLog.Debug("eav.GetAttributeSelectSql.Columns.FieldNames.default", "err", err, "ta", ta, "taa", taa, "entityTypeID", entityTypeID, "websiteID", websiteID)
				}
				return nil, err
			}
			ifnull[i] = dbr.IfNullAs(csdb.ScopeTable, tewC, t, tewC, tewC)
			tewAddedCols = append(tewAddedCols, tewC)
		}
		taColumnsQuoted.ReduceContains(tewAddedCols...)
		taaColumnsQuoted.ReduceContains(tewAddedCols...)
	}

	selectSql := dbrSess.
		Select(taColumnsQuoted...).
		From(ta.Name, csdb.MainTable).
		Join(
			dbr.JoinTable(taa.Name, csdb.AdditionalTable),
			taaColumnsQuoted,
			dbr.ConditionRaw(dbr.Quote+csdb.AdditionalTable+"`.`attribute_id` = `"+csdb.MainTable+"`.`attribute_id`"),
			dbr.ConditionRaw(dbr.Quote+csdb.MainTable+"`.`entity_type_id` = ?", entityTypeID),
		)

	if len(tewAddedCols) > 0 {
		selectSql.
			LeftJoin(
				dbr.JoinTable(tew.Name, csdb.ScopeTable),
				append(ifnull),
				dbr.ConditionRaw(dbr.Quote+csdb.ScopeTable+dbr.Quote+"."+dbr.Quote+"attribute_id"+dbr.Quote+" = "+dbr.Quote+csdb.MainTable+dbr.Quote+"."+dbr.Quote+"attribute_id"+dbr.Quote),
				dbr.ConditionRaw(dbr.Quote+csdb.ScopeTable+dbr.Quote+"."+dbr.Quote+"website_id"+dbr.Quote+" = ?", websiteID),
			)
	}
	return selectSql, nil
}
Exemple #4
0
func (et *TableEntityType) LoadByCode(dbrSess *dbr.Session, code string, cbs ...csdb.DbrSelectCb) error {
	s, err := TableCollection.Structure(TableIndexEntityType)
	if err != nil {
		return errgo.Mask(err)
	}
	sb := dbrSess.Select(s.AllColumnAliasQuote(csdb.MainTable)...).From(s.Name, csdb.MainTable).Where(dbr.ConditionRaw("entity_type_code = ?", code))
	for _, cb := range cbs {
		sb = cb(sb)
	}
	return errgo.Mask(sb.LoadStruct(et))
}