good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
740c3475
Commit
740c3475
authored
7 years ago
by
José Nieto
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #387 from upper/issue-383
Add test case and fix for #383
parents
a350d862
53df71e1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mysql/adapter_test.go
+53
-0
53 additions, 0 deletions
mysql/adapter_test.go
mysql/collection.go
+11
-6
11 additions, 6 deletions
mysql/collection.go
with
64 additions
and
6 deletions
mysql/adapter_test.go
+
53
−
0
View file @
740c3475
...
@@ -26,8 +26,10 @@ import (
...
@@ -26,8 +26,10 @@ import (
"database/sql"
"database/sql"
"fmt"
"fmt"
"os"
"os"
"testing"
"time"
"time"
"github.com/stretchr/testify/assert"
"upper.io/db.v3/internal/sqladapter"
"upper.io/db.v3/internal/sqladapter"
"upper.io/db.v3/lib/sqlbuilder"
"upper.io/db.v3/lib/sqlbuilder"
)
)
...
@@ -124,6 +126,14 @@ func tearUp() error {
...
@@ -124,6 +126,14 @@ func tearUp() error {
some_val VARCHAR(255) default '',
some_val VARCHAR(255) default '',
primary key (code, user_id)
primary key (code, user_id)
)`
,
)`
,
`CREATE TABLE admin (
ID int(11) NOT NULL AUTO_INCREMENT,
Accounts varchar(255) DEFAULT '',
LoginPassWord varchar(255) DEFAULT '',
Date TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (ID,Date)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8`
,
}
}
for
_
,
s
:=
range
batch
{
for
_
,
s
:=
range
batch
{
...
@@ -156,6 +166,49 @@ func getStats(sess sqlbuilder.Database) (map[string]int, error) {
...
@@ -156,6 +166,49 @@ func getStats(sess sqlbuilder.Database) (map[string]int, error) {
return
stats
,
nil
return
stats
,
nil
}
}
func
TestInsertReturningCompositeKey_Issue383
(
t
*
testing
.
T
)
{
sess
:=
mustOpen
()
defer
sess
.
Close
()
type
Admin
struct
{
ID
int
`db:"ID,omitempty"`
Accounts
string
`db:"Accounts"`
LoginPassWord
string
`db:"LoginPassWord"`
Date
time
.
Time
`db:"Date"`
}
dateNow
:=
time
.
Now
()
a
:=
Admin
{
Accounts
:
"admin"
,
LoginPassWord
:
"E10ADC3949BA59ABBE56E057F20F883E"
,
Date
:
dateNow
,
}
adminCollection
:=
sess
.
Collection
(
"admin"
)
err
:=
adminCollection
.
InsertReturning
(
&
a
)
assert
.
NoError
(
t
,
err
)
assert
.
NotZero
(
t
,
a
.
ID
)
assert
.
NotZero
(
t
,
a
.
Date
)
assert
.
Equal
(
t
,
"admin"
,
a
.
Accounts
)
assert
.
Equal
(
t
,
"E10ADC3949BA59ABBE56E057F20F883E"
,
a
.
LoginPassWord
)
b
:=
Admin
{
Accounts
:
"admin2"
,
LoginPassWord
:
"E10ADC3949BA59ABBE56E057F20F883E"
,
Date
:
dateNow
,
}
err
=
adminCollection
.
InsertReturning
(
&
b
)
assert
.
NoError
(
t
,
err
)
assert
.
NotZero
(
t
,
b
.
ID
)
assert
.
NotZero
(
t
,
b
.
Date
)
assert
.
Equal
(
t
,
"admin2"
,
b
.
Accounts
)
assert
.
Equal
(
t
,
"E10ADC3949BA59ABBE56E057F20F883E"
,
a
.
LoginPassWord
)
}
func
cleanUpCheck
(
sess
sqlbuilder
.
Database
)
(
err
error
)
{
func
cleanUpCheck
(
sess
sqlbuilder
.
Database
)
(
err
error
)
{
var
stats
map
[
string
]
int
var
stats
map
[
string
]
int
...
...
This diff is collapsed.
Click to expand it.
mysql/collection.go
+
11
−
6
View file @
740c3475
...
@@ -78,16 +78,12 @@ func (t *table) Insert(item interface{}) (interface{}, error) {
...
@@ -78,16 +78,12 @@ func (t *table) Insert(item interface{}) (interface{}, error) {
return
nil
,
err
return
nil
,
err
}
}
if
len
(
pKey
)
<=
1
{
lastID
,
err
:=
res
.
LastInsertId
()
// Attempt to use LastInsertId() (probably won't work, but the Exec()
if
err
==
nil
&&
len
(
pKey
)
<=
1
{
// succeeded, so we can safely ignore the error from LastInsertId()).
lastID
,
_
:=
res
.
LastInsertId
()
return
lastID
,
nil
return
lastID
,
nil
}
}
keyMap
:=
db
.
Cond
{}
keyMap
:=
db
.
Cond
{}
for
i
:=
range
columnNames
{
for
i
:=
range
columnNames
{
for
j
:=
0
;
j
<
len
(
pKey
);
j
++
{
for
j
:=
0
;
j
<
len
(
pKey
);
j
++
{
if
pKey
[
j
]
==
columnNames
[
i
]
{
if
pKey
[
j
]
==
columnNames
[
i
]
{
...
@@ -96,5 +92,14 @@ func (t *table) Insert(item interface{}) (interface{}, error) {
...
@@ -96,5 +92,14 @@ func (t *table) Insert(item interface{}) (interface{}, error) {
}
}
}
}
// There was an auto column among primary keys, let's search for it.
if
lastID
>
0
{
for
j
:=
0
;
j
<
len
(
pKey
);
j
++
{
if
keyMap
[
pKey
[
j
]]
==
nil
{
keyMap
[
pKey
[
j
]]
=
lastID
}
}
}
return
keyMap
,
nil
return
keyMap
,
nil
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment