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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO reuse
|
|
||||||
func openConnection(dbConn DBConnection) (*sql.DB, error) {
|
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)
|
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)
|
db, err := sql.Open("mysql", connString)
|
||||||
|
|
|
||||||
|
|
@ -348,6 +348,31 @@ func (i *ClauseInsert) Serialize(statementType StatementType, out *SqlBuilder) e
|
||||||
return nil
|
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 {
|
type ClauseValues struct {
|
||||||
Rows [][]Serializer
|
Rows [][]Serializer
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ type InsertStatement interface {
|
||||||
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
|
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
|
||||||
newInsert := &insertStatementImpl{}
|
newInsert := &insertStatementImpl{}
|
||||||
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
||||||
&newInsert.Insert, &newInsert.Values, &newInsert.Select)
|
&newInsert.Insert, &newInsert.ValuesQuery)
|
||||||
|
|
||||||
newInsert.Insert.Table = table
|
newInsert.Insert.Table = table
|
||||||
newInsert.Insert.Columns = columns
|
newInsert.Insert.Columns = columns
|
||||||
|
|
@ -31,26 +31,25 @@ type insertStatementImpl struct {
|
||||||
jet.StatementImpl
|
jet.StatementImpl
|
||||||
|
|
||||||
Insert jet.ClauseInsert
|
Insert jet.ClauseInsert
|
||||||
Values jet.ClauseValues
|
ValuesQuery jet.ClauseValuesQuery
|
||||||
Select jet.ClauseQuery
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
||||||
i.Select.Query = selectStatement
|
i.ValuesQuery.Query = selectStatement
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//
|
func TestInvalidInsert(t *testing.T) {
|
||||||
//func TestInvalidInsert(t *testing.T) {
|
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||||
// 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")
|
||||||
// assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
func TestInsertNilValue(t *testing.T) {
|
func TestInsertNilValue(t *testing.T) {
|
||||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ type InsertStatement interface {
|
||||||
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
|
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
|
||||||
newInsert := &insertStatementImpl{}
|
newInsert := &insertStatementImpl{}
|
||||||
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
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.Table = table
|
||||||
newInsert.Insert.Columns = columns
|
newInsert.Insert.Columns = columns
|
||||||
|
|
@ -34,23 +34,22 @@ type insertStatementImpl struct {
|
||||||
jet.StatementImpl
|
jet.StatementImpl
|
||||||
|
|
||||||
Insert jet.ClauseInsert
|
Insert jet.ClauseInsert
|
||||||
Values jet.ClauseValues
|
ValuesQuery jet.ClauseValuesQuery
|
||||||
Select jet.ClauseQuery
|
|
||||||
Returning ClauseReturning
|
Returning ClauseReturning
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
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
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,6 +59,6 @@ func (i *insertStatementImpl) RETURNING(projections ...jet.Projection) InsertSta
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
||||||
i.Select.Query = selectStatement
|
i.ValuesQuery.Query = selectStatement
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//TODO:
|
func TestInvalidInsert(t *testing.T) {
|
||||||
//func TestInvalidInsert(t *testing.T) {
|
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
||||||
// assertStatementErr(t, table1.INSERT(table1Col1), "jet: no row values or query specified")
|
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||||
// assertStatementErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
func TestInsertNilValue(t *testing.T) {
|
func TestInsertNilValue(t *testing.T) {
|
||||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue