Handle todos.
This commit is contained in:
parent
d3d00910aa
commit
9a34dc9fd7
6 changed files with 48 additions and 28 deletions
|
|
@ -48,7 +48,6 @@ func Generate(destDir string, dbConn DBConnection) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// TODO reuse
|
||||
func openConnection(dbConn DBConnection) (*sql.DB, error) {
|
||||
var connString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConn.User, dbConn.Password, dbConn.Host, dbConn.Port, dbConn.DBName)
|
||||
db, err := sql.Open("mysql", connString)
|
||||
|
|
|
|||
|
|
@ -348,6 +348,31 @@ func (i *ClauseInsert) Serialize(statementType StatementType, out *SqlBuilder) e
|
|||
return nil
|
||||
}
|
||||
|
||||
type ClauseValuesQuery struct {
|
||||
ClauseValues
|
||||
ClauseQuery
|
||||
}
|
||||
|
||||
func (v *ClauseValuesQuery) Serialize(statementType StatementType, out *SqlBuilder) error {
|
||||
if len(v.Rows) == 0 && v.Query == nil {
|
||||
return errors.New("jet: no row values or query specified")
|
||||
}
|
||||
|
||||
if len(v.Rows) > 0 && v.Query != nil {
|
||||
return errors.New("jet: only row values or query has to be specified")
|
||||
}
|
||||
|
||||
if err := v.ClauseValues.Serialize(statementType, out); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := v.ClauseQuery.Serialize(statementType, out); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ClauseValues struct {
|
||||
Rows [][]Serializer
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ type InsertStatement interface {
|
|||
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
|
||||
newInsert := &insertStatementImpl{}
|
||||
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
||||
&newInsert.Insert, &newInsert.Values, &newInsert.Select)
|
||||
&newInsert.Insert, &newInsert.ValuesQuery)
|
||||
|
||||
newInsert.Insert.Table = table
|
||||
newInsert.Insert.Columns = columns
|
||||
|
|
@ -30,27 +30,26 @@ func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
|
|||
type insertStatementImpl struct {
|
||||
jet.StatementImpl
|
||||
|
||||
Insert jet.ClauseInsert
|
||||
Values jet.ClauseValues
|
||||
Select jet.ClauseQuery
|
||||
Insert jet.ClauseInsert
|
||||
ValuesQuery jet.ClauseValuesQuery
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromValues(value, values))
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromValues(value, values))
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
||||
i.Select.Query = selectStatement
|
||||
i.ValuesQuery.Query = selectStatement
|
||||
return i
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,10 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
//
|
||||
//func TestInvalidInsert(t *testing.T) {
|
||||
// assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||
// assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||
//}
|
||||
func TestInvalidInsert(t *testing.T) {
|
||||
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||
}
|
||||
|
||||
func TestInsertNilValue(t *testing.T) {
|
||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ type InsertStatement interface {
|
|||
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
|
||||
newInsert := &insertStatementImpl{}
|
||||
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
||||
&newInsert.Insert, &newInsert.Values, &newInsert.Select, &newInsert.Returning)
|
||||
&newInsert.Insert, &newInsert.ValuesQuery, &newInsert.Returning)
|
||||
|
||||
newInsert.Insert.Table = table
|
||||
newInsert.Insert.Columns = columns
|
||||
|
|
@ -33,24 +33,23 @@ func newInsertStatement(table WritableTable, columns []jet.Column) InsertStateme
|
|||
type insertStatementImpl struct {
|
||||
jet.StatementImpl
|
||||
|
||||
Insert jet.ClauseInsert
|
||||
Values jet.ClauseValues
|
||||
Select jet.ClauseQuery
|
||||
Returning ClauseReturning
|
||||
Insert jet.ClauseInsert
|
||||
ValuesQuery jet.ClauseValuesQuery
|
||||
Returning ClauseReturning
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromValues(value, values))
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromValues(value, values))
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
||||
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
|
||||
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
|
||||
return i
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +59,6 @@ func (i *insertStatementImpl) RETURNING(projections ...jet.Projection) InsertSta
|
|||
}
|
||||
|
||||
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
||||
i.Select.Query = selectStatement
|
||||
i.ValuesQuery.Query = selectStatement
|
||||
return i
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,10 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
//TODO:
|
||||
//func TestInvalidInsert(t *testing.T) {
|
||||
// assertStatementErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||
// assertStatementErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||
//}
|
||||
func TestInvalidInsert(t *testing.T) {
|
||||
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||
}
|
||||
|
||||
func TestInsertNilValue(t *testing.T) {
|
||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue