-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
75 lines (61 loc) · 1.95 KB
/
Copy pathdatabase.py
File metadata and controls
75 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sqlite3
def get_db_connection():
conn = sqlite3.connect('git_analyzer.db')
conn.row_factory = sqlite3.Row
return conn
def create_database():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS owners (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS repositories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
owner_id INTEGER,
url TEXT,
description TEXT,
stars INTEGER,
forks INTEGER,
language TEXT,
FOREIGN KEY (owner_id) REFERENCES owners(id)
)
''')
conn.commit()
return conn, cursor
def save_analyzed_repo(repo_data):
conn = get_db_connection()
cursor = conn.cursor()
# Owner
cursor.execute('INSERT OR IGNORE INTO owners (name) VALUES (?)', (repo_data['owner'],))
cursor.execute('SELECT id FROM owners WHERE name = ?', (repo_data['owner'],))
owner_id = cursor.fetchone()[0]
# Repository
cursor.execute('SELECT id FROM repositories WHERE url = ?', (repo_data['url'],))
existing = cursor.fetchone()
if existing:
cursor.execute('''
UPDATE repositories
SET stars = ?, forks = ?, description = ?
WHERE id = ?
''', (repo_data['stars'], repo_data['forks'], repo_data['description'], existing[0]))
else:
cursor.execute('''
INSERT INTO repositories (name, owner_id, url, description, stars, forks, language)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (
repo_data['name'],
owner_id,
repo_data['url'],
repo_data['description'],
repo_data['stars'],
repo_data['forks'],
repo_data['language']
))
conn.commit()
conn.close()
return True