Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
1c58b6e6
Commit
1c58b6e6
authored
Nov 03, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
08aaa17a
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
17 deletions
+70
-17
application/config/config.yaml
application/config/config.yaml
+4
-2
application/extensions/init_sqlalchemy.py
application/extensions/init_sqlalchemy.py
+14
-2
application/utils/__init__.py
application/utils/__init__.py
+1
-1
application/utils/dsn/__init__.py
application/utils/dsn/__init__.py
+1
-1
application/utils/dsn/dsn.py
application/utils/dsn/dsn.py
+50
-11
No files found.
application/config/config.yaml
View file @
1c58b6e6
System
:
Env
:
public
MySQL
:
User
:
root
Database
:
Type
:
mysql
Driver
:
pymysql
Username
:
root
Password
:
localhost123
Host
:
localhost
Port
:
3306
...
...
application/extensions/init_sqlalchemy.py
View file @
1c58b6e6
...
...
@@ -13,7 +13,7 @@
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
application.utils
import
dsn
from
application.utils
import
DatabaseURI
db
=
SQLAlchemy
()
...
...
@@ -25,5 +25,17 @@ def init_database(app: Flask) -> None:
:param app: flask.Flask application instance
:return: None
"""
app
.
config
.
setdefault
(
'SQLALCHEMY_DATABASE_URI'
,
dsn
(
app
))
cfg
=
app
.
config
.
get
(
'Database'
)
if
cfg
is
None
:
raise
KeyError
(
'Key Database error'
)
uri
=
DatabaseURI
(
db_type
=
cfg
.
Type
,
username
=
cfg
.
Username
,
password
=
cfg
.
Password
,
host
=
cfg
.
Host
,
port
=
cfg
.
Port
,
db
=
cfg
.
DB
,
driver
=
cfg
.
Driver
,
)
app
.
config
.
setdefault
(
'SQLALCHEMY_DATABASE_URI'
,
uri
.
create
())
db
.
init_app
(
app
)
application/utils/__init__.py
View file @
1c58b6e6
...
...
@@ -10,5 +10,5 @@
# @Description :
"""
from
.dsn
import
dsn
from
.dsn
import
dsn
,
DatabaseURI
from
.elasticsearch
import
ElasticsearchUtils
application/utils/dsn/__init__.py
View file @
1c58b6e6
...
...
@@ -10,4 +10,4 @@
# @Description :
"""
from
.dsn
import
dsn
from
.dsn
import
DatabaseURI
application/utils/dsn/dsn.py
View file @
1c58b6e6
...
...
@@ -10,18 +10,57 @@
# @Description :
"""
from
flask
import
Flask
class
DatabaseURI
:
"""
The DatabaseURI class is a utility for generating SQLAlchemy database
connection strings for various types of databases.
:param db_type: The type of the database. It could be "mysql", "postgresql", "oracle", "sqlite".
:param username: The username of the database. Not required for "sqlite".
:param password: The password of the database. Not required for "sqlite".
:param host: The host of the database. Not required for "sqlite".
:param port: The port of the database. Not required for "sqlite".
:param db: The database name. For "sqlite", it is the path to the database file.
:param driver: The python library to connect to the database. Not required but could be provided.
"""
def
dsn
(
app
:
Flask
)
->
str
:
def
__init__
(
self
,
db_type
:
str
,
username
:
str
=
None
,
password
:
str
=
None
,
host
:
str
=
None
,
port
:
str
=
None
,
db
:
str
=
None
,
driver
:
str
=
None
)
->
None
:
self
.
db_type
=
db_type
self
.
username
=
username
self
.
password
=
password
self
.
host
=
host
self
.
port
=
port
self
.
db
=
db
self
.
driver
=
driver
def
create
(
self
)
->
str
:
"""
Initialize the MySQL dsn extension
Creates a SQLAlchemy engine string for the specified database.
:param app: flask.Flask application instance
:return: dsn
:return: A SQLAlchemy engine string.
"""
mysql_cfg
=
app
.
config
.
get
(
'MySQL'
)
if
mysql_cfg
is
None
:
raise
KeyError
(
'Key MySQL error'
)
return
(
f
'mysql+pymysql://
{
app
.
config
.
MySQL
.
User
}
:'
f
'
{
app
.
config
.
MySQL
.
Password
}
@
{
app
.
config
.
MySQL
.
Host
}
:
{
app
.
config
.
MySQL
.
Port
}
/
{
app
.
config
.
MySQL
.
DB
}
'
)
# SQLite database only require a path to the database file
if
self
.
db_type
==
'sqlite'
:
return
f
'
{
self
.
db_type
}
:///
{
self
.
db
}
'
else
:
# Other databases require a full connection string with optional driver
if
self
.
driver
:
return
(
f
'
{
self
.
db_type
}
+
{
self
.
driver
}
://'
f
'
{
self
.
username
}
:
{
self
.
password
}
@
{
self
.
host
}
:
{
self
.
port
}
/
{
self
.
db
}
'
)
else
:
return
f
'
{
self
.
db_type
}
://
{
self
.
username
}
:
{
self
.
password
}
@
{
self
.
host
}
:
{
self
.
port
}
/
{
self
.
db
}
'
def
__repr__
(
self
):
return
f
"<DatabaseURI(
{
self
.
db_type
}
)>"
def
__str__
(
self
):
return
self
.
create
()
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