Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
U
ulfssar-go
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Faizal Aziz
ulfssar-go
Commits
6d822213
Commit
6d822213
authored
Jan 30, 2024
by
Faizal Aziz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding foreign key and improve db gen
parent
cc05862e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
2 deletions
+37
-2
postgresql.go
dbcon/postgresql.go
+36
-1
constant.go
pkg/http-client/constant.go
+1
-1
No files found.
dbcon/postgresql.go
View file @
6d822213
...
...
@@ -3,6 +3,7 @@ package dbcon
import
(
"context"
"fmt"
"os"
"reflect"
"strings"
"time"
...
...
@@ -32,6 +33,8 @@ type (
MaxLifeTimeConnection
time
.
Duration
MaxIdleConnection
,
MaxOpenConnection
int
Logger
log
.
Logger
AutoGenerateDDL
bool
EntitiesPath
string
}
)
...
...
@@ -314,6 +317,22 @@ func NewPostgreSql(option *PostgreSqlOption) (ORM, error) {
sql
.
SetMaxOpenConns
(
option
.
MaxOpenConnection
)
sql
.
SetMaxIdleConns
(
option
.
MaxIdleConnection
)
dirs
,
err
:=
os
.
ReadDir
(
option
.
EntitiesPath
)
if
option
.
AutoGenerateDDL
&&
len
(
dirs
)
>
0
{
var
typeRegistry
=
make
(
map
[
string
]
reflect
.
Type
)
for
_
,
data
:=
range
dirs
{
instances
:=
reflect
.
New
(
typeRegistry
[
data
.
Name
()])
.
Elem
()
test
:=
instances
.
MethodByName
(
"TableName"
)
if
!
test
.
IsNil
()
{
i
,
_
:=
test
.
TryRecv
()
generateSQLFromStorage
(
i
.
String
(),
instances
)
}
}
}
return
&
postgresqldb
{
db
:
db
},
nil
}
...
...
@@ -359,7 +378,12 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin
argName
=
val
}
}
argName
=
fmt
.
Sprintf
(
"constraint %s_%s"
,
strings
.
Replace
(
keysName
,
" "
,
"_"
,
1
),
argName
)
if
argName
!=
""
&&
strings
.
Contains
(
argName
,
"pk"
)
||
strings
.
Contains
(
argName
,
"unique"
)
{
argName
=
getPKConstraint
(
tableName
,
columnName
,
argName
,
keysName
)
}
else
if
argName
!=
""
&&
strings
.
Contains
(
argName
,
"fk"
)
{
argName
,
keysName
=
getFKConstraint
(
tableName
,
columnName
,
argName
,
keysName
)
}
}
}
...
...
@@ -370,3 +394,14 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin
return
fmt
.
Sprintf
(
"CREATE TABLE IF NOT EXISTS %s (%s);"
,
tableName
,
columnNamesStr
)
}
func
getPKConstraint
(
tableName
,
columnName
,
argsName
,
keysName
string
)
string
{
return
fmt
.
Sprintf
(
"constraint %s_%s_%s_%s"
,
strings
.
Replace
(
keysName
,
" "
,
"_"
,
1
),
tableName
,
columnName
,
argsName
)
}
func
getFKConstraint
(
tableName
,
columnName
,
argsName
,
keysName
string
)
(
args
,
keys
string
)
{
args
=
fmt
.
Sprintf
(
"constraint %s_%s_%s_%s"
,
strings
.
Replace
(
keysName
,
" "
,
"_"
,
1
),
tableName
,
columnName
,
argsName
)
keysArgs
:=
strings
.
Split
(
keys
,
";"
)
keys
=
fmt
.
Sprintf
(
" references %s %s"
,
keysArgs
[
1
],
keysArgs
[
2
])
return
}
pkg/http-client/constant.go
View file @
6d822213
...
...
@@ -4,7 +4,7 @@ const (
ContentType
=
"Content-Type"
ApplicationJSON
=
"application/json"
UserAgent
=
"User-Agent"
UserAgentValue
=
"https://
digdayatech.id
"
UserAgentValue
=
"https://
ursabyte.com
"
)
const
(
...
...
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