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
c697b980
Commit
c697b980
authored
Sep 01, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enhancement
parent
56aa224a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
35 deletions
+28
-35
db_alias.go
orm/db_alias.go
+1
-1
models_boot.go
orm/models_boot.go
+15
-23
models_info_f.go
orm/models_info_f.go
+9
-8
models_info_m.go
orm/models_info_m.go
+3
-3
No files found.
orm/db_alias.go
View file @
c697b980
...
...
@@ -80,7 +80,7 @@ type _dbCache struct {
func
(
ac
*
_dbCache
)
add
(
name
string
,
al
*
alias
)
(
added
bool
)
{
ac
.
mux
.
Lock
()
defer
ac
.
mux
.
Unlock
()
if
_
,
ok
:=
ac
.
cache
[
name
];
ok
==
false
{
if
_
,
ok
:=
ac
.
cache
[
name
];
!
ok
{
ac
.
cache
[
name
]
=
al
added
=
true
}
...
...
orm/models_boot.go
View file @
c697b980
...
...
@@ -15,7 +15,6 @@
package
orm
import
(
"errors"
"fmt"
"os"
"reflect"
...
...
@@ -55,34 +54,34 @@ func registerModel(prefix string, model interface{}) {
os
.
Exit
(
2
)
}
info
:=
newModelInfo
(
val
)
if
info
.
fields
.
pk
==
nil
{
mi
:=
newModelInfo
(
val
)
if
mi
.
fields
.
pk
==
nil
{
outFor
:
for
_
,
fi
:=
range
info
.
fields
.
fieldsDB
{
for
_
,
fi
:=
range
mi
.
fields
.
fieldsDB
{
if
strings
.
ToLower
(
fi
.
name
)
==
"id"
{
switch
fi
.
addrValue
.
Elem
()
.
Kind
()
{
case
reflect
.
Int
,
reflect
.
Int32
,
reflect
.
Int64
,
reflect
.
Uint
,
reflect
.
Uint32
,
reflect
.
Uint64
:
fi
.
auto
=
true
fi
.
pk
=
true
info
.
fields
.
pk
=
fi
mi
.
fields
.
pk
=
fi
break
outFor
}
}
}
if
info
.
fields
.
pk
==
nil
{
if
mi
.
fields
.
pk
==
nil
{
fmt
.
Printf
(
"<orm.RegisterModel> `%s` need a primary key field, default use 'id' if not set
\n
"
,
name
)
os
.
Exit
(
2
)
}
}
info
.
table
=
table
info
.
pkg
=
typ
.
PkgPath
()
info
.
model
=
model
info
.
manual
=
true
mi
.
table
=
table
mi
.
pkg
=
typ
.
PkgPath
()
mi
.
model
=
model
mi
.
manual
=
true
modelCache
.
set
(
table
,
info
)
modelCache
.
set
(
table
,
mi
)
}
// boostrap models
...
...
@@ -90,12 +89,10 @@ func bootStrap() {
if
modelCache
.
done
{
return
}
var
(
err
error
models
map
[
string
]
*
modelInfo
)
if
dataBaseCache
.
getDefault
()
==
nil
{
err
=
fmt
.
Errorf
(
"must have one register DataBase alias named `default`"
)
goto
end
...
...
@@ -106,14 +103,13 @@ func bootStrap() {
for
_
,
fi
:=
range
mi
.
fields
.
columns
{
if
fi
.
rel
||
fi
.
reverse
{
elm
:=
fi
.
addrValue
.
Type
()
.
Elem
()
switch
fi
.
fieldType
{
case
RelReverseMany
,
RelManyToMany
:
if
fi
.
fieldType
==
RelReverseMany
||
fi
.
fieldType
==
RelManyToMany
{
elm
=
elm
.
Elem
()
}
// check the rel or reverse model already register
name
:=
getFullName
(
elm
)
mii
,
ok
:=
modelCache
.
getByFullName
(
name
)
if
ok
==
false
||
mii
.
pkg
!=
elm
.
PkgPath
()
{
if
!
ok
||
mii
.
pkg
!=
elm
.
PkgPath
()
{
err
=
fmt
.
Errorf
(
"can not found rel in field `%s`, `%s` may be miss register"
,
fi
.
fullName
,
elm
.
String
())
goto
end
}
...
...
@@ -122,20 +118,17 @@ func bootStrap() {
switch
fi
.
fieldType
{
case
RelManyToMany
:
if
fi
.
relThrough
!=
""
{
msg
:=
fmt
.
Sprintf
(
"field `%s` wrong rel_through value `%s`"
,
fi
.
fullName
,
fi
.
relThrough
)
if
i
:=
strings
.
LastIndex
(
fi
.
relThrough
,
"."
);
i
!=
-
1
&&
len
(
fi
.
relThrough
)
>
(
i
+
1
)
{
pn
:=
fi
.
relThrough
[
:
i
]
rmi
,
ok
:=
modelCache
.
getByFullName
(
fi
.
relThrough
)
if
ok
==
false
||
pn
!=
rmi
.
pkg
{
err
=
errors
.
New
(
msg
+
" cannot find table"
)
err
=
fmt
.
Errorf
(
"field `%s` wrong rel_through value `%s` cannot find table"
,
fi
.
fullName
,
fi
.
relThrough
)
goto
end
}
fi
.
relThroughModelInfo
=
rmi
fi
.
relTable
=
rmi
.
table
}
else
{
err
=
errors
.
New
(
msg
)
err
=
fmt
.
Errorf
(
"field `%s` wrong rel_through value `%s`"
,
fi
.
fullName
,
fi
.
relThrough
)
goto
end
}
}
else
{
...
...
@@ -143,7 +136,6 @@ func bootStrap() {
if
fi
.
relTable
!=
""
{
i
.
table
=
fi
.
relTable
}
if
v
:=
modelCache
.
set
(
i
.
table
,
i
);
v
!=
nil
{
err
=
fmt
.
Errorf
(
"the rel table name `%s` already registered, cannot be use, please change one"
,
fi
.
relTable
)
goto
end
...
...
orm/models_info_f.go
View file @
c697b980
...
...
@@ -104,7 +104,7 @@ type fieldInfo struct {
mi
*
modelInfo
fieldIndex
[]
int
fieldType
int
dbcol
bool
dbcol
bool
// table column fk and onetoone
inModel
bool
name
string
fullName
string
...
...
@@ -116,13 +116,13 @@ type fieldInfo struct {
null
bool
index
bool
unique
bool
colDefault
bool
initial
StrTo
colDefault
bool
// whether has default tag
initial
StrTo
// store the default value
size
int
toText
bool
autoNow
bool
autoNowAdd
bool
rel
bool
rel
bool
// if type equal to RelForeignKey, RelOneToOne, RelManyToMany then true
reverse
bool
reverseField
string
reverseFieldInfo
*
fieldInfo
...
...
@@ -134,7 +134,7 @@ type fieldInfo struct {
relModelInfo
*
modelInfo
digits
int
decimals
int
isFielder
bool
isFielder
bool
// implement Fielder interface
onDelete
string
}
...
...
@@ -265,6 +265,9 @@ checkType:
}
}
// check the rel and reverse type
// rel should Ptr
// reverse should slice []*struct
switch
fieldType
{
case
RelForeignKey
,
RelOneToOne
,
RelReverseOne
:
if
field
.
Kind
()
!=
reflect
.
Ptr
{
...
...
@@ -403,14 +406,12 @@ checkType:
if
fi
.
auto
||
fi
.
pk
{
if
fi
.
auto
{
switch
addrField
.
Elem
()
.
Kind
()
{
case
reflect
.
Int
,
reflect
.
Int32
,
reflect
.
Int64
,
reflect
.
Uint
,
reflect
.
Uint32
,
reflect
.
Uint64
:
default
:
err
=
fmt
.
Errorf
(
"auto primary key only support int, int32, int64, uint, uint32, uint64 but found `%s`"
,
addrField
.
Elem
()
.
Kind
())
goto
end
}
fi
.
pk
=
true
}
fi
.
null
=
false
...
...
@@ -422,8 +423,8 @@ checkType:
fi
.
index
=
false
}
// can not set default for these type
if
fi
.
auto
||
fi
.
pk
||
fi
.
unique
||
fieldType
==
TypeTimeField
||
fieldType
==
TypeDateField
||
fieldType
==
TypeDateTimeField
{
// can not set default
initial
.
Clear
()
}
...
...
orm/models_info_m.go
View file @
c697b980
...
...
@@ -107,9 +107,9 @@ func newM2MModelInfo(m1, m2 *modelInfo) (mi *modelInfo) {
mi
.
name
=
camelString
(
mi
.
table
)
mi
.
fullName
=
m1
.
pkg
+
"."
+
mi
.
name
fa
:=
new
(
fieldInfo
)
f1
:=
new
(
fieldInfo
)
f2
:=
new
(
fieldInfo
)
fa
:=
new
(
fieldInfo
)
// pk
f1
:=
new
(
fieldInfo
)
// m1 table RelForeignKey
f2
:=
new
(
fieldInfo
)
// m2 table RelForeignKey
fa
.
fieldType
=
TypeBigIntegerField
fa
.
auto
=
true
fa
.
pk
=
true
...
...
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