-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daz-3ux <[email protected]>
- Loading branch information
Showing
13 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
-- Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
-- Use of this source code is governed by a MIT style | ||
-- license that can be found in the LICENSE file. The original repo for | ||
-- this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
-- MariaDB dump 10.19-11.1.2-MariaDB, for Linux (x86_64) | ||
-- | ||
-- Host: 127.0.0.1 Database: dazBlog | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
// Package store defines the store layer of dazBlog | ||
package store // import "github.com/Daz-3ux/dBlog/internal/dazBlog/store" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package store | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package store | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package model | ||
|
||
import "time" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package model | ||
|
||
import "time" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package db | ||
|
||
import ( | ||
"fmt" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"time" | ||
) | ||
|
||
// MySQLOptions defines the options for connecting to MySQL | ||
type MySQLOptions struct { | ||
Host string | ||
Username string | ||
Password string | ||
Database string | ||
MaxIdleConnections int | ||
MaxOpenConnections int | ||
MaxConnectionLifeTime time.Duration | ||
LogLevel int | ||
} | ||
|
||
func (o *MySQLOptions) DSN() string { | ||
return fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=%t&loc=%s", | ||
o.Username, | ||
o.Password, | ||
o.Host, | ||
o.Database, | ||
true, | ||
"Local") | ||
} | ||
|
||
func NewMySql(opts *MySQLOptions) (*gorm.DB, error) { | ||
logLevel := logger.Silent | ||
if opts.LogLevel != 0 { | ||
logLevel = logger.LogLevel(opts.LogLevel) | ||
} | ||
db, err := gorm.Open(mysql.Open(opts.DSN()), &gorm.Config{ | ||
Logger: logger.Default.LogMode(logLevel), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// sqlDB is the low-level SQL connection object | ||
sqlDB, err := db.DB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sqlDB.SetMaxIdleConns(opts.MaxIdleConnections) | ||
sqlDB.SetMaxOpenConns(opts.MaxOpenConnections) | ||
sqlDB.SetConnMaxLifetime(opts.MaxConnectionLifeTime) | ||
|
||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
// Package db provides useful functions to create mysql instance | ||
package db // import "github.com/Daz-3ux/dBlog/pkg/db" |