Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
modls
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Shane O'Neill
modls
Commits
5e054d03
Commit
5e054d03
authored
Feb 02, 2014
by
Travis Cline
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Export Keys and Columns on TableMap
Fixes #2, #2
parent
95fc957f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
29 deletions
+29
-29
dbmap.go
dbmap.go
+8
-8
modl.go
modl.go
+1
-1
tablemap.go
tablemap.go
+20
-20
No files found.
dbmap.go
View file @
5e054d03
...
...
@@ -91,7 +91,7 @@ func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
tmap
.
setupHooks
(
i
)
n
:=
t
.
NumField
()
tmap
.
c
olumns
=
make
([]
*
ColumnMap
,
0
,
n
)
tmap
.
C
olumns
=
make
([]
*
ColumnMap
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
f
:=
t
.
Field
(
i
)
columnName
:=
f
.
Tag
.
Get
(
"db"
)
...
...
@@ -105,9 +105,9 @@ func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
fieldName
:
f
.
Name
,
gotype
:
f
.
Type
,
}
tmap
.
columns
=
append
(
tmap
.
c
olumns
,
cm
)
tmap
.
Columns
=
append
(
tmap
.
C
olumns
,
cm
)
if
cm
.
fieldName
==
"Version"
{
tmap
.
version
=
tmap
.
columns
[
len
(
tmap
.
c
olumns
)
-
1
]
tmap
.
version
=
tmap
.
Columns
[
len
(
tmap
.
C
olumns
)
-
1
]
}
}
m
.
tables
=
append
(
m
.
tables
,
tmap
)
...
...
@@ -153,7 +153,7 @@ func writeColumnSql(sql *bytes.Buffer, table *TableMap, col *ColumnMap) {
sql
.
WriteString
(
fmt
.
Sprintf
(
"%s %s"
,
table
.
dbmap
.
Dialect
.
QuoteField
(
col
.
ColumnName
),
sqltype
))
if
col
.
isPK
{
sql
.
WriteString
(
" not null"
)
if
len
(
table
.
k
eys
)
==
1
{
if
len
(
table
.
K
eys
)
==
1
{
sql
.
WriteString
(
" primary key"
)
}
}
...
...
@@ -190,7 +190,7 @@ func (m *DbMap) createTables(ifNotExists, exec bool) (map[string]string, error)
s
.
WriteString
(
"
\n
"
)
}
x
:=
0
for
_
,
col
:=
range
table
.
c
olumns
{
for
_
,
col
:=
range
table
.
C
olumns
{
if
!
col
.
Transient
{
if
x
>
0
{
s
.
WriteString
(
sep
)
...
...
@@ -200,13 +200,13 @@ func (m *DbMap) createTables(ifNotExists, exec bool) (map[string]string, error)
x
++
}
}
if
len
(
table
.
k
eys
)
>
1
{
if
len
(
table
.
K
eys
)
>
1
{
s
.
WriteString
(
", primary key ("
)
for
x
:=
range
table
.
k
eys
{
for
x
:=
range
table
.
K
eys
{
if
x
>
0
{
s
.
WriteString
(
", "
)
}
s
.
WriteString
(
m
.
Dialect
.
QuoteField
(
table
.
k
eys
[
x
]
.
ColumnName
))
s
.
WriteString
(
m
.
Dialect
.
QuoteField
(
table
.
K
eys
[
x
]
.
ColumnName
))
}
s
.
WriteString
(
")"
)
}
...
...
modl.go
View file @
5e054d03
...
...
@@ -248,7 +248,7 @@ func get(m *DbMap, exec SqlExecutor, dest interface{}, keys ...interface{}) erro
if
table
==
nil
{
return
fmt
.
Errorf
(
"Could not find table for %v"
,
dest
)
}
if
len
(
table
.
k
eys
)
<
1
{
if
len
(
table
.
K
eys
)
<
1
{
return
&
NoKeysErr
{
table
}
}
...
...
tablemap.go
View file @
5e054d03
...
...
@@ -12,9 +12,9 @@ import (
type
TableMap
struct
{
// Name of database table.
TableName
string
Keys
[]
*
ColumnMap
Columns
[]
*
ColumnMap
gotype
reflect
.
Type
columns
[]
*
ColumnMap
keys
[]
*
ColumnMap
version
*
ColumnMap
insertPlan
bindPlan
updatePlan
bindPlan
...
...
@@ -47,12 +47,12 @@ func (t *TableMap) ResetSql() {
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
func
(
t
*
TableMap
)
SetKeys
(
isAutoIncr
bool
,
fieldNames
...
string
)
*
TableMap
{
t
.
k
eys
=
make
([]
*
ColumnMap
,
0
)
t
.
K
eys
=
make
([]
*
ColumnMap
,
0
)
for
_
,
name
:=
range
fieldNames
{
colmap
:=
t
.
ColMap
(
sqlx
.
NameMapper
(
name
))
colmap
.
isPK
=
true
colmap
.
isAutoIncr
=
isAutoIncr
t
.
keys
=
append
(
t
.
k
eys
,
colmap
)
t
.
Keys
=
append
(
t
.
K
eys
,
colmap
)
}
t
.
ResetSql
()
...
...
@@ -75,7 +75,7 @@ func (t *TableMap) ColMap(field string) *ColumnMap {
// Return the column map for this field, or nil if it can't be found.
func
colMapOrNil
(
t
*
TableMap
,
field
string
)
*
ColumnMap
{
for
_
,
col
:=
range
t
.
c
olumns
{
for
_
,
col
:=
range
t
.
C
olumns
{
if
col
.
fieldName
==
field
||
col
.
ColumnName
==
field
{
return
col
}
...
...
@@ -103,7 +103,7 @@ func (t *TableMap) bindGet() bindPlan {
s
.
WriteString
(
"select "
)
x
:=
0
for
_
,
col
:=
range
t
.
c
olumns
{
for
_
,
col
:=
range
t
.
C
olumns
{
if
!
col
.
Transient
{
if
x
>
0
{
s
.
WriteString
(
","
)
...
...
@@ -116,8 +116,8 @@ func (t *TableMap) bindGet() bindPlan {
s
.
WriteString
(
" from "
)
s
.
WriteString
(
t
.
dbmap
.
Dialect
.
QuoteField
(
t
.
TableName
))
s
.
WriteString
(
" where "
)
for
x
:=
range
t
.
k
eys
{
col
:=
t
.
k
eys
[
x
]
for
x
:=
range
t
.
K
eys
{
col
:=
t
.
K
eys
[
x
]
if
x
>
0
{
s
.
WriteString
(
" and "
)
}
...
...
@@ -143,8 +143,8 @@ func (t *TableMap) bindDelete(elem reflect.Value) bindInstance {
s
:=
bytes
.
Buffer
{}
s
.
WriteString
(
fmt
.
Sprintf
(
"delete from %s"
,
t
.
dbmap
.
Dialect
.
QuoteField
(
t
.
TableName
)))
for
y
:=
range
t
.
c
olumns
{
col
:=
t
.
c
olumns
[
y
]
for
y
:=
range
t
.
C
olumns
{
col
:=
t
.
C
olumns
[
y
]
if
!
col
.
Transient
{
if
col
==
t
.
version
{
plan
.
versField
=
col
.
fieldName
...
...
@@ -153,8 +153,8 @@ func (t *TableMap) bindDelete(elem reflect.Value) bindInstance {
}
s
.
WriteString
(
" where "
)
for
x
:=
range
t
.
k
eys
{
k
:=
t
.
k
eys
[
x
]
for
x
:=
range
t
.
K
eys
{
k
:=
t
.
K
eys
[
x
]
if
x
>
0
{
s
.
WriteString
(
" and "
)
}
...
...
@@ -190,8 +190,8 @@ func (t *TableMap) bindUpdate(elem reflect.Value) bindInstance {
s
.
WriteString
(
fmt
.
Sprintf
(
"update %s set "
,
t
.
dbmap
.
Dialect
.
QuoteField
(
t
.
TableName
)))
x
:=
0
for
y
:=
range
t
.
c
olumns
{
col
:=
t
.
c
olumns
[
y
]
for
y
:=
range
t
.
C
olumns
{
col
:=
t
.
C
olumns
[
y
]
if
!
col
.
isPK
&&
!
col
.
Transient
{
if
x
>
0
{
s
.
WriteString
(
", "
)
...
...
@@ -211,8 +211,8 @@ func (t *TableMap) bindUpdate(elem reflect.Value) bindInstance {
}
s
.
WriteString
(
" where "
)
for
y
:=
range
t
.
k
eys
{
col
:=
t
.
k
eys
[
y
]
for
y
:=
range
t
.
K
eys
{
col
:=
t
.
K
eys
[
y
]
if
y
>
0
{
s
.
WriteString
(
" and "
)
}
...
...
@@ -251,8 +251,8 @@ func (t *TableMap) bindInsert(elem reflect.Value) bindInstance {
x
:=
0
first
:=
true
for
y
:=
range
t
.
c
olumns
{
col
:=
t
.
c
olumns
[
y
]
for
y
:=
range
t
.
C
olumns
{
col
:=
t
.
C
olumns
[
y
]
if
!
col
.
Transient
{
if
!
first
{
...
...
@@ -283,7 +283,7 @@ func (t *TableMap) bindInsert(elem reflect.Value) bindInstance {
s
.
WriteString
(
s2
.
String
())
s
.
WriteString
(
")"
)
if
plan
.
autoIncrIdx
>
-
1
{
s
.
WriteString
(
t
.
dbmap
.
Dialect
.
AutoIncrInsertSuffix
(
t
.
c
olumns
[
plan
.
autoIncrIdx
]))
s
.
WriteString
(
t
.
dbmap
.
Dialect
.
AutoIncrInsertSuffix
(
t
.
C
olumns
[
plan
.
autoIncrIdx
]))
}
s
.
WriteString
(
";"
)
...
...
@@ -361,7 +361,7 @@ func tableForPointer(m *DbMap, i interface{}, checkPk bool) (*TableMap, reflect.
if
t
==
nil
{
return
nil
,
v
,
fmt
.
Errorf
(
"Could not find table for %v"
,
t
)
}
if
checkPk
&&
len
(
t
.
k
eys
)
<
1
{
if
checkPk
&&
len
(
t
.
K
eys
)
<
1
{
return
t
,
v
,
&
NoKeysErr
{
t
}
}
return
t
,
v
,
nil
...
...
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