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
837bd946
Commit
837bd946
authored
Jan 05, 2024
by
Faizal Aziz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dev adding create table
parent
dfe0840b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
postgresql.go
dbcon/postgresql.go
+51
-0
No files found.
dbcon/postgresql.go
View file @
837bd946
...
...
@@ -2,6 +2,9 @@ package dbcon
import
(
"context"
"fmt"
"reflect"
"strings"
"time"
"gorm.io/driver/postgres"
...
...
@@ -12,6 +15,12 @@ import (
log
"gitlab.ursabyte.com/faizal.aziz/ulfssar-go/logger"
)
var
Keys
=
map
[
string
]
string
{
"Primary Key"
:
"pk"
,
"Foregin Key"
:
"fk"
,
"Unique"
:
"unique"
,
}
type
(
postgresqldb
struct
{
db
*
gorm
.
DB
...
...
@@ -167,6 +176,10 @@ func (d *postgresqldb) Create(args interface{}) error {
return
d
.
db
.
Create
(
args
)
.
Error
}
func
(
d
*
postgresqldb
)
CreateTable
(
tableName
string
,
structs
interface
{})
error
{
return
d
.
db
.
Create
(
generateSQLFromStorage
(
tableName
,
structs
))
.
Error
}
func
(
d
*
postgresqldb
)
Update
(
args
interface
{})
error
{
return
d
.
db
.
Updates
(
args
)
.
Error
}
...
...
@@ -303,3 +316,41 @@ func NewPostgreSql(option *PostgreSqlOption) (ORM, error) {
return
&
postgresqldb
{
db
:
db
},
nil
}
func
generateSQLFromStorage
(
tableName
string
,
columnAndValues
interface
{})
string
{
st
:=
reflect
.
TypeOf
(
columnAndValues
)
var
columnNames
strings
.
Builder
for
i
:=
0
;
i
<
st
.
NumField
();
i
++
{
field
:=
st
.
Field
(
i
)
columnName
:=
field
.
Tag
.
Get
(
"sql-column"
)
if
columnName
==
""
{
columnName
=
field
.
Name
}
typeName
:=
field
.
Tag
.
Get
(
"sql-type"
)
if
typeName
==
""
{
typeName
=
"text"
}
argName
:=
field
.
Tag
.
Get
(
"sql-constraint"
)
keysName
:=
field
.
Tag
.
Get
(
"sql-keys"
)
if
keysName
==
""
{
keysName
=
""
}
else
{
if
keysName
!=
""
&&
argName
==
""
{
for
key
,
val
:=
range
Keys
{
if
strings
.
Contains
(
keysName
,
key
)
||
strings
.
Contains
(
keysName
,
strings
.
ToUpper
(
key
))
||
strings
.
Contains
(
keysName
,
strings
.
ToLower
(
key
))
{
argName
=
val
}
}
argName
=
fmt
.
Sprintf
(
"%s_%s"
,
keysName
,
argName
)
}
}
columnNames
.
WriteString
(
fmt
.
Sprintf
(
"%s %s %s %s ,"
,
columnNames
,
typeName
,
argName
,
keysName
))
}
columnNamesStr
:=
strings
.
TrimRight
(
columnNames
.
String
(),
","
)
return
fmt
.
Sprintf
(
"CREATE TABLE %s '(%s)'"
,
tableName
,
columnNamesStr
)
}
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