From b62ff79c2780ad5e255eb0f4746f4dfd0238ff9d Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Sat, 26 Nov 2022 21:10:33 +0800 Subject: [PATCH 1/6] support to get Index by column index --- schema/schema.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/schema/schema.go b/schema/schema.go index ea2ec5588..f9a683d17 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -183,10 +183,21 @@ func (ta *Table) FindColumn(name string) int { return -1 } +// Get TableColumn by Index index func (ta *Table) GetPKColumn(index int) *TableColumn { return &ta.Columns[ta.PKColumns[index]] } +// Get Index by column index. Return nil if the column is not a index. +func (ta *Table) GetIndex(colIndex int) *Index { + for i, _colIndex := range t.PKColumns { + if _colIndex == colIndex { + return t.Indexes[i] + } + } + return nil +} + func (ta *Table) AddIndex(name string) (index *Index) { index = NewIndex(name) ta.Indexes = append(ta.Indexes, index) From cd825ce0e6f44991e2e7299a11337523d6ead3eb Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Sat, 26 Nov 2022 21:16:02 +0800 Subject: [PATCH 2/6] fix typo --- schema/schema.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index f9a683d17..0c137dacc 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -190,9 +190,9 @@ func (ta *Table) GetPKColumn(index int) *TableColumn { // Get Index by column index. Return nil if the column is not a index. func (ta *Table) GetIndex(colIndex int) *Index { - for i, _colIndex := range t.PKColumns { + for i, _colIndex := range ta.PKColumns { if _colIndex == colIndex { - return t.Indexes[i] + return ta.Indexes[i] } } return nil From 647bbeebe087aa88f475864902796dbb423f07f2 Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Mon, 28 Nov 2022 10:43:47 +0800 Subject: [PATCH 3/6] rename to GetPKIndex --- schema/schema.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 0c137dacc..f5ca0ee47 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -188,8 +188,8 @@ func (ta *Table) GetPKColumn(index int) *TableColumn { return &ta.Columns[ta.PKColumns[index]] } -// Get Index by column index. Return nil if the column is not a index. -func (ta *Table) GetIndex(colIndex int) *Index { +// Get Primary Index by column index. Return nil if the column is not a primary index. +func (ta *Table) GetPKIndex(colIndex int) *Index { for i, _colIndex := range ta.PKColumns { if _colIndex == colIndex { return ta.Indexes[i] From 6d722ea39ab9ee1d9a5aa67fdc6617dc4f97775d Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Mon, 28 Nov 2022 21:03:38 +0800 Subject: [PATCH 4/6] add unit test --- schema/schema.go | 12 ++++++++---- schema/schema_test.go | 20 +++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index f5ca0ee47..edc04121d 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -183,16 +183,19 @@ func (ta *Table) FindColumn(name string) int { return -1 } -// Get TableColumn by Index index +// Get TableColumn by primary index index func (ta *Table) GetPKColumn(index int) *TableColumn { + if index >= len(ta.PKColumns) { + return nil + } return &ta.Columns[ta.PKColumns[index]] } -// Get Primary Index by column index. Return nil if the column is not a primary index. +// Get primary index by column index. Return nil if the column is not a primary index. func (ta *Table) GetPKIndex(colIndex int) *Index { - for i, _colIndex := range ta.PKColumns { + for _, _colIndex := range ta.PKColumns { if _colIndex == colIndex { - return ta.Indexes[i] + return ta.Indexes[0] } } return nil @@ -424,6 +427,7 @@ func (ta *Table) fetchPrimaryKeyColumns() error { return nil } + // Primary key must be the first index? pkIndex := ta.Indexes[0] if pkIndex.Name != "PRIMARY" { return nil diff --git a/schema/schema_test.go b/schema/schema_test.go index fb43c0e81..421b49237 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -14,6 +14,8 @@ import ( // use docker mysql for test var host = flag.String("host", "127.0.0.1", "MySQL host") +var schema = flag.String("schema", "test", "MySQL Database") +var pwd = flag.String("pwd", "", "MySQL password") func Test(t *testing.T) { TestingT(t) @@ -28,10 +30,10 @@ var _ = Suite(&schemaTestSuite{}) func (s *schemaTestSuite) SetUpSuite(c *C) { var err error - s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, 3306), "root", "", "test") + s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, 3306), "root", *pwd, *schema) c.Assert(err, IsNil) - s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:@%s:3306", *host)) + s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:%s@%s:3306", *pwd, *host)) c.Assert(err, IsNil) } @@ -75,12 +77,20 @@ func (s *schemaTestSuite) TestSchema(c *C) { _, err = s.conn.Execute(str) c.Assert(err, IsNil) - ta, err := NewTable(s.conn, "test", "schema_test") + ta, err := NewTable(s.conn, *schema, "schema_test") c.Assert(err, IsNil) c.Assert(ta.Columns, HasLen, 15) c.Assert(ta.Indexes, HasLen, 3) c.Assert(ta.PKColumns, DeepEquals, []int{2, 0}) + c.Assert(ta.GetPKIndex(0), Equals, ta.Indexes[0]) + c.Assert(ta.GetPKIndex(1), IsNil) + c.Assert(ta.GetPKIndex(2), Equals, ta.Indexes[0]) + c.Assert(ta.GetPKIndex(3), IsNil) + c.Assert(ta.GetPKColumn(0), Equals, &ta.Columns[2]) + c.Assert(ta.GetPKColumn(1), Equals, &ta.Columns[0]) + c.Assert(ta.GetPKColumn(2), IsNil) + c.Assert(ta.GetPKColumn(3), IsNil) c.Assert(ta.Indexes[0].Columns, HasLen, 2) c.Assert(ta.Indexes[0].Name, Equals, "PRIMARY") c.Assert(ta.Indexes[2].Name, Equals, "name_idx") @@ -107,7 +117,7 @@ func (s *schemaTestSuite) TestSchema(c *C) { c.Assert(ta.Columns[14].MaxSize, Equals, uint(12)) c.Assert(ta.Columns[14].FixedSize, Equals, uint(0)) - taSqlDb, err := NewTableFromSqlDB(s.sqlDB, "test", "schema_test") + taSqlDb, err := NewTableFromSqlDB(s.sqlDB, *schema, "schema_test") c.Assert(err, IsNil) c.Assert(taSqlDb, DeepEquals, ta) @@ -119,7 +129,7 @@ func (s *schemaTestSuite) TestQuoteSchema(c *C) { _, err := s.conn.Execute(str) c.Assert(err, IsNil) - ta, err := NewTable(s.conn, "test", "a-b_test") + ta, err := NewTable(s.conn, *schema, "a-b_test") c.Assert(err, IsNil) c.Assert(ta.Columns[0].Name, Equals, "a.b") From 36d88b9217d87b56f78cc8c17f667361f6afa20b Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Mon, 28 Nov 2022 21:12:53 +0800 Subject: [PATCH 5/6] IsPrimaryKey --- schema/schema.go | 7 +++---- schema/schema_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index edc04121d..a613eeaf6 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -191,14 +191,13 @@ func (ta *Table) GetPKColumn(index int) *TableColumn { return &ta.Columns[ta.PKColumns[index]] } -// Get primary index by column index. Return nil if the column is not a primary index. -func (ta *Table) GetPKIndex(colIndex int) *Index { +func (ta *Table) IsPrimaryKey(colIndex int) bool { for _, _colIndex := range ta.PKColumns { if _colIndex == colIndex { - return ta.Indexes[0] + return true } } - return nil + return false } func (ta *Table) AddIndex(name string) (index *Index) { diff --git a/schema/schema_test.go b/schema/schema_test.go index 421b49237..a38909334 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -83,10 +83,10 @@ func (s *schemaTestSuite) TestSchema(c *C) { c.Assert(ta.Columns, HasLen, 15) c.Assert(ta.Indexes, HasLen, 3) c.Assert(ta.PKColumns, DeepEquals, []int{2, 0}) - c.Assert(ta.GetPKIndex(0), Equals, ta.Indexes[0]) - c.Assert(ta.GetPKIndex(1), IsNil) - c.Assert(ta.GetPKIndex(2), Equals, ta.Indexes[0]) - c.Assert(ta.GetPKIndex(3), IsNil) + c.Assert(ta.IsPrimaryKey(0), IsTrue) + c.Assert(ta.IsPrimaryKey(1), IsFalse) + c.Assert(ta.IsPrimaryKey(2), IsTrue) + c.Assert(ta.IsPrimaryKey(3), IsFalse) c.Assert(ta.GetPKColumn(0), Equals, &ta.Columns[2]) c.Assert(ta.GetPKColumn(1), Equals, &ta.Columns[0]) c.Assert(ta.GetPKColumn(2), IsNil) From 1f54d98800ec173786091eb4bc3f537c05dc8422 Mon Sep 17 00:00:00 2001 From: "garen.fang" Date: Mon, 28 Nov 2022 21:38:57 +0800 Subject: [PATCH 6/6] code style --- schema/schema.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index a613eeaf6..61e653182 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -183,7 +183,7 @@ func (ta *Table) FindColumn(name string) int { return -1 } -// Get TableColumn by primary index index +// Get TableColumn by column index of primary key. func (ta *Table) GetPKColumn(index int) *TableColumn { if index >= len(ta.PKColumns) { return nil @@ -192,8 +192,8 @@ func (ta *Table) GetPKColumn(index int) *TableColumn { } func (ta *Table) IsPrimaryKey(colIndex int) bool { - for _, _colIndex := range ta.PKColumns { - if _colIndex == colIndex { + for _, i := range ta.PKColumns { + if i == colIndex { return true } }