Пример #1
0
// defines a new value for column returning if the column should provoque a new sql
func (this *DmlCore) defineParameter(col *Column, value Tokener) (interface{}, bool) {
	if col.GetTable().GetName() != this.table.GetName() {
		panic(col.String() + " does not belong to table " + this.table.String())
	}

	if this.vals == nil {
		this.vals = coll.NewLinkedHashMap()
	}

	old := this.vals.Put(col, value)
	// if it is a parameter remove it
	if old != nil {
		tok := old.(Tokener)
		if value.GetOperator() == TOKEN_PARAM && tok.GetOperator() == TOKEN_PARAM {
			/*
				Replace one param by another
			*/
			oldKey := tok.GetValue().(string)
			key := value.GetValue().(string)
			// change the new param name to the old param name
			value.SetValue(tok.GetValue())
			val := this.parameters[oldKey]
			// update the old value to the new one
			this.parameters[oldKey] = this.parameters[key]
			// remove the new token
			delete(this.parameters, key)
			// The replace of one param by another should not trigger a new SQL string
			return val, false
		} else if tok.GetOperator() == TOKEN_PARAM {
			// removes the previous token
			delete(this.parameters, tok.GetValue().(string))
		}
	}
	return nil, true
}
Пример #2
0
func NewAliasBag(prefix string) *AliasBag {
	ab := new(AliasBag)
	ab.prefix = prefix
	ab.counter = 0
	ab.bag = coll.NewLinkedHashMap()
	return ab
}
Пример #3
0
func NewInsert(db IDb, table *Table) *Insert {
	this := new(Insert)
	this.Super(db, table)
	this.vals = coll.NewLinkedHashMap()
	this.returnId = true

	discriminators := table.GetDiscriminators()
	// several discriminators, at maximum one for each column
	for _, discriminator := range discriminators {
		this.Set(discriminator.Column, discriminator.Value)
	}
	return this
}
Пример #4
0
func TABLE(name string) *Table {
	if name == "" {
		panic("Null for table name is not allowed.")
	}
	this := new(Table).As(dbx.ToCamelCase(name))
	this.columnsMap = coll.NewLinkedHashMap()
	this.columns = coll.NewLinkedHashSet()
	this.keys = coll.NewLinkedHashSet()
	this.name = name
	AddEntity(this)

	return this
}
Пример #5
0
func (this *Table) AddAssociationAs(name string, fk *Association) *Association {
	key := Str(name)

	if this.associationMap == nil {
		this.associationMap = coll.NewLinkedHashMap()
	} else {
		if value, ok := this.associationMap.Get(key); ok {
			panic(
				fmt.Sprintf("An association %s is already mapped to this table (%s) with the key %s",
					value, this.Alias, name))
		}
	}

	this.associationMap.Put(key, fk)
	return fk
}
Пример #6
0
package db

import (
	coll "github.com/quintans/toolkit/collection"
	. "github.com/quintans/toolkit/ext"

	"strings"
)

const FK_NAV_SEP = "."

// entity mapping
var Tables = coll.NewLinkedHashMap()

func AddEntity(table *Table) {
	Tables.Put(Str(table.Alias), table)
}

func GetLink(chain string) *LinkNav {
	idx := strings.Index(chain, FK_NAV_SEP)
	var link Str
	if idx > 0 {
		link = Str(chain[:idx])
	} else {
		link = Str(chain)
	}
	o, _ := Tables.Get(link)
	table, _ := o.(*Table)

	if idx < 0 {
		return NewLinkNav(nil, table)
Пример #7
0
func NewUpdate(db IDb, table *Table) *Update {
	this := new(Update)
	this.Super(db, table)
	this.vals = coll.NewLinkedHashMap()
	return this
}