Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
4c061fed
Commit
4c061fed
authored
Aug 22, 2013
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
orm support custom multi unique / index
parent
02d29905
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
1 deletion
+90
-1
cmd_utils.go
orm/cmd_utils.go
+45
-0
models_info_m.go
orm/models_info_m.go
+1
-1
models_test.go
orm/models_test.go
+12
-0
models_utils.go
orm/models_utils.go
+32
-0
No files found.
orm/cmd_utils.go
View file @
4c061fed
...
...
@@ -39,6 +39,7 @@ func getDbCreateSql(al *alias) (sqls []string) {
Q
:=
al
.
DbBaser
.
TableQuote
()
T
:=
al
.
DbBaser
.
DbTypes
()
sep
:=
fmt
.
Sprintf
(
"%s, %s"
,
Q
,
Q
)
for
_
,
mi
:=
range
modelCache
.
allOrdered
()
{
sql
:=
fmt
.
Sprintf
(
"-- %s
\n
"
,
strings
.
Repeat
(
"-"
,
50
))
...
...
@@ -49,6 +50,8 @@ func getDbCreateSql(al *alias) (sqls []string) {
columns
:=
make
([]
string
,
0
,
len
(
mi
.
fields
.
fieldsDB
))
sqlIndexes
:=
[][]
string
{}
for
_
,
fi
:=
range
mi
.
fields
.
fieldsDB
{
fieldType
:=
fi
.
fieldType
...
...
@@ -120,6 +123,10 @@ func getDbCreateSql(al *alias) (sqls []string) {
if
fi
.
unique
{
column
+=
" "
+
"UNIQUE"
}
if
fi
.
index
{
sqlIndexes
=
append
(
sqlIndexes
,
[]
string
{
column
})
}
}
if
strings
.
Index
(
column
,
"%COL%"
)
!=
-
1
{
...
...
@@ -129,6 +136,21 @@ func getDbCreateSql(al *alias) (sqls []string) {
columns
=
append
(
columns
,
column
)
}
if
mi
.
model
!=
nil
{
for
_
,
names
:=
range
getTableUnique
(
mi
.
addrField
)
{
cols
:=
make
([]
string
,
0
,
len
(
names
))
for
_
,
name
:=
range
names
{
if
fi
,
ok
:=
mi
.
fields
.
GetByAny
(
name
);
ok
&&
fi
.
dbcol
{
cols
=
append
(
cols
,
fi
.
column
)
}
else
{
panic
(
fmt
.
Errorf
(
"cannot found column `%s` when parse UNIQUE in `%s.TableUnique`"
,
name
,
mi
.
fullName
))
}
}
column
:=
fmt
.
Sprintf
(
" UNIQUE (%s%s%s)"
,
Q
,
strings
.
Join
(
cols
,
sep
),
Q
)
columns
=
append
(
columns
,
column
)
}
}
sql
+=
strings
.
Join
(
columns
,
",
\n
"
)
sql
+=
"
\n
)"
...
...
@@ -136,7 +158,30 @@ func getDbCreateSql(al *alias) (sqls []string) {
sql
+=
" ENGINE=INNODB"
}
sql
+=
";"
sqls
=
append
(
sqls
,
sql
)
if
mi
.
model
!=
nil
{
for
_
,
names
:=
range
getTableIndex
(
mi
.
addrField
)
{
cols
:=
make
([]
string
,
0
,
len
(
names
))
for
_
,
name
:=
range
names
{
if
fi
,
ok
:=
mi
.
fields
.
GetByAny
(
name
);
ok
&&
fi
.
dbcol
{
cols
=
append
(
cols
,
fi
.
column
)
}
else
{
panic
(
fmt
.
Errorf
(
"cannot found column `%s` when parse INDEX in `%s.TableIndex`"
,
name
,
mi
.
fullName
))
}
}
sqlIndexes
=
append
(
sqlIndexes
,
cols
)
}
}
for
_
,
names
:=
range
sqlIndexes
{
name
:=
strings
.
Join
(
names
,
"_"
)
cols
:=
strings
.
Join
(
names
,
sep
)
sql
:=
fmt
.
Sprintf
(
"CREATE INDEX %s%s%s ON %s%s%s (%s%s%s);"
,
Q
,
name
,
Q
,
Q
,
mi
.
table
,
Q
,
Q
,
cols
,
Q
)
sqls
=
append
(
sqls
,
sql
)
}
}
return
sqls
...
...
orm/models_info_m.go
View file @
4c061fed
...
...
@@ -31,7 +31,7 @@ func newModelInfo(val reflect.Value) (info *modelInfo) {
ind
:=
reflect
.
Indirect
(
val
)
typ
:=
ind
.
Type
()
info
.
addrField
=
ind
.
Addr
()
info
.
addrField
=
val
info
.
name
=
typ
.
Name
()
info
.
fullName
=
getFullName
(
typ
)
...
...
orm/models_test.go
View file @
4c061fed
...
...
@@ -78,6 +78,18 @@ type User struct {
ShouldSkip
string
`orm:"-"`
}
func
(
u
*
User
)
TableIndex
()
[][]
string
{
return
[][]
string
{
[]
string
{
"Id"
,
"UserName"
},
}
}
func
(
u
*
User
)
TableUnique
()
[][]
string
{
return
[][]
string
{
[]
string
{
"UserName"
,
"Email"
},
}
}
func
NewUser
()
*
User
{
obj
:=
new
(
User
)
return
obj
...
...
orm/models_utils.go
View file @
4c061fed
...
...
@@ -26,6 +26,38 @@ func getTableName(val reflect.Value) string {
return
snakeString
(
ind
.
Type
()
.
Name
())
}
func
getTableIndex
(
val
reflect
.
Value
)
[][]
string
{
fun
:=
val
.
MethodByName
(
"TableIndex"
)
if
fun
.
IsValid
()
{
vals
:=
fun
.
Call
([]
reflect
.
Value
{})
if
len
(
vals
)
>
0
{
val
:=
vals
[
0
]
if
val
.
CanInterface
()
{
if
d
,
ok
:=
val
.
Interface
()
.
([][]
string
);
ok
{
return
d
}
}
}
}
return
nil
}
func
getTableUnique
(
val
reflect
.
Value
)
[][]
string
{
fun
:=
val
.
MethodByName
(
"TableUnique"
)
if
fun
.
IsValid
()
{
vals
:=
fun
.
Call
([]
reflect
.
Value
{})
if
len
(
vals
)
>
0
{
val
:=
vals
[
0
]
if
val
.
CanInterface
()
{
if
d
,
ok
:=
val
.
Interface
()
.
([][]
string
);
ok
{
return
d
}
}
}
}
return
nil
}
func
getColumnName
(
ft
int
,
addrField
reflect
.
Value
,
sf
reflect
.
StructField
,
col
string
)
string
{
column
:=
strings
.
ToLower
(
col
)
if
column
==
""
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment