Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
Updates Notifier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Picasoft
Technique
Updates Notifier
Commits
2df4367c
Verified
Commit
2df4367c
authored
3 years ago
by
Gaëtan Blond
Browse files
Options
Downloads
Patches
Plain Diff
Added database
parent
60bd55ee
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1
Refactor and clean old code
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
updates_notifier/__init__.py
+1
-0
1 addition, 0 deletions
updates_notifier/__init__.py
updates_notifier/database.py
+84
-0
84 additions, 0 deletions
updates_notifier/database.py
with
85 additions
and
0 deletions
updates_notifier/__init__.py
+
1
−
0
View file @
2df4367c
from
.database
import
EntriesDatabase
from
.entry
import
Entry
This diff is collapsed.
Click to expand it.
updates_notifier/database.py
0 → 100644
+
84
−
0
View file @
2df4367c
import
sqlite3
from
typing
import
Any
,
Iterable
,
Optional
from
.entry
import
Entry
CREATE_ENTRIES_TABLE_SQL
=
"""
\
CREATE TABLE IF NOT EXISTS {table_name} (
feed_id integer,
tag_id text,
name text,
version text,
url text
)
"""
SEARCH_FOR_ENTRY_SQL
=
"""
\
SELECT
tag_id
FROM {table_name}
WHERE feed_id=?
AND tag_id=?
"""
INSERT_ENTRY_SQL
=
"""
\
INSERT INTO {table_name}
(feed_id, tag_id, name, version, url)
VALUES (?, ?, ?, ?, ?)
"""
class
EntriesDatabase
:
def
__init__
(
self
,
db_path
:
str
,
table_name
:
str
=
"
entries
"
):
self
.
table_name
=
table_name
self
.
_conn
=
sqlite3
.
connect
(
db_path
)
self
.
_cursor
:
Optional
[
sqlite3
.
Cursor
]
=
None
self
.
_execute
(
CREATE_ENTRIES_TABLE_SQL
)
self
.
_conn
.
commit
()
def
_execute
(
self
,
query
:
str
,
params
:
Iterable
[
Any
]
=
None
,
no_cursor
=
False
)
->
sqlite3
.
Cursor
:
if
params
is
None
:
params
=
[]
if
no_cursor
or
self
.
_cursor
is
None
:
execute_func
=
self
.
_conn
.
execute
else
:
execute_func
=
self
.
_cursor
.
execute
return
execute_func
(
query
.
format
(
table_name
=
self
.
table_name
),
params
)
def
contains
(
self
,
entry
:
Entry
)
->
bool
:
if
not
isinstance
(
entry
,
Entry
):
raise
TypeError
(
"
Element must be of type Entry
"
)
cursor
=
self
.
_execute
(
SEARCH_FOR_ENTRY_SQL
,
(
entry
.
feed_id
,
entry
.
tag_id
),
True
)
return
cursor
.
fetchone
()
is
not
None
def
insert
(
self
,
entry
:
Entry
)
->
None
:
self
.
_execute
(
INSERT_ENTRY_SQL
,
(
entry
.
feed_id
,
entry
.
tag_id
,
entry
.
name
,
entry
.
version
,
entry
.
url
),
)
def
__contains__
(
self
,
entry
:
Entry
)
->
bool
:
return
self
.
contains
(
entry
)
def
__enter__
(
self
)
->
None
:
if
self
.
_cursor
is
not
None
:
raise
RuntimeError
(
"
Global cursor already activated.
"
)
self
.
_cursor
=
self
.
_conn
.
cursor
()
def
__exit__
(
self
,
*
_
)
->
None
:
# TODO decide wether all transactions are committed when an exception is raised
if
self
.
_cursor
is
None
:
raise
RuntimeError
(
"
Global cursor not activated
"
)
self
.
_conn
.
commit
()
self
.
_cursor
=
None
def
__iadd__
(
self
,
entry
:
Entry
)
->
None
:
self
.
insert
(
entry
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment