You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.5 KiB
75 lines
2.5 KiB
# 数据库初始化脚本
|
|
import sqlite3
|
|
import os
|
|
|
|
def init_database():
|
|
"""初始化数据库"""
|
|
db_path = "code_scanner.db"
|
|
|
|
# 如果数据库文件不存在,创建它
|
|
if not os.path.exists(db_path):
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# 创建项目表
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS projects (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name VARCHAR(100) NOT NULL,
|
|
description TEXT,
|
|
language VARCHAR(20) NOT NULL,
|
|
repository_url VARCHAR(500),
|
|
project_path VARCHAR(500),
|
|
config TEXT,
|
|
is_active BOOLEAN DEFAULT 1,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME
|
|
)
|
|
''')
|
|
|
|
# 创建扫描表
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS scans (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
project_id INTEGER NOT NULL,
|
|
scan_type VARCHAR(50) NOT NULL,
|
|
status VARCHAR(20) NOT NULL,
|
|
total_files INTEGER DEFAULT 0,
|
|
scanned_files INTEGER DEFAULT 0,
|
|
total_vulnerabilities INTEGER DEFAULT 0,
|
|
started_at DATETIME,
|
|
completed_at DATETIME,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (project_id) REFERENCES projects (id)
|
|
)
|
|
''')
|
|
|
|
# 创建漏洞表
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS vulnerabilities (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
scan_id INTEGER NOT NULL,
|
|
rule_id VARCHAR(100) NOT NULL,
|
|
message TEXT NOT NULL,
|
|
category VARCHAR(50) NOT NULL,
|
|
severity VARCHAR(20) NOT NULL,
|
|
file_path VARCHAR(500) NOT NULL,
|
|
line_number INTEGER,
|
|
status VARCHAR(20) DEFAULT 'open',
|
|
ai_enhanced BOOLEAN DEFAULT 0,
|
|
ai_confidence REAL,
|
|
ai_suggestion TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (scan_id) REFERENCES scans (id)
|
|
)
|
|
''')
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
print("数据库初始化完成!")
|
|
else:
|
|
print("数据库已存在")
|
|
|
|
if __name__ == "__main__":
|
|
init_database()
|