mirror of
https://github.com/Xevion/tcp-chat.git
synced 2026-01-31 06:26:11 -06:00
create baseclass Database and create new ClientDatabase alongside renamed ServerDatabase class, table construction
This commit is contained in:
Vendored
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
# Repository specific
|
# Repository specific
|
||||||
.idea
|
.idea
|
||||||
messages.db
|
server.db
|
||||||
|
client.db
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
+2
-1
@@ -8,7 +8,8 @@ __BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|||||||
|
|
||||||
HEADER_LENGTH = 10
|
HEADER_LENGTH = 10
|
||||||
MINIMUM_CONTRAST = 4.65
|
MINIMUM_CONTRAST = 4.65
|
||||||
DATABASE = os.path.join(__BASE_DIR, 'messages.db')
|
CLIENT_DATABASE = os.path.join(__BASE_DIR, 'client.db')
|
||||||
|
SERVER_DATABASE = os.path.join(__BASE_DIR, 'server.db')
|
||||||
|
|
||||||
DEFAULT_IP = "127.0.0.1"
|
DEFAULT_IP = "127.0.0.1"
|
||||||
DEFAULT_PORT = 5555
|
DEFAULT_PORT = 5555
|
||||||
|
|||||||
+36
-14
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import threading
|
import threading
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import constants
|
import constants
|
||||||
|
|
||||||
@@ -12,9 +12,9 @@ lock = threading.Lock()
|
|||||||
|
|
||||||
|
|
||||||
class Database(object):
|
class Database(object):
|
||||||
def __init__(self):
|
def __init__(self, database: str):
|
||||||
logger.debug(f"Connected to '{constants.DATABASE}'")
|
logger.debug(f"Connected to './{os.path.basename(database)}'")
|
||||||
self.conn = sqlite3.connect(constants.DATABASE)
|
self.conn = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||||
self.__isClosed = False
|
self.__isClosed = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -31,6 +31,38 @@ class Database(object):
|
|||||||
self.conn.close()
|
self.conn.close()
|
||||||
self.__isClosed = True
|
self.__isClosed = True
|
||||||
|
|
||||||
|
def construct(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class ClientDatabase(Database):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(constants.CLIENT_DATABASE)
|
||||||
|
|
||||||
|
def construct(self) -> None:
|
||||||
|
with lock:
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
try:
|
||||||
|
cur.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name = '?';''', 'connection')
|
||||||
|
if cur.fetchone() is None:
|
||||||
|
self.conn.execute('''CREATE TABLE connection
|
||||||
|
(id INTEGER PRIMARY KEY,
|
||||||
|
address TEXT NOT NULL,
|
||||||
|
port INTEGER NOT NULL,
|
||||||
|
nickname TEXT NOT NULL,
|
||||||
|
password TEXT,
|
||||||
|
connections INTEGER DEFAULT 1,
|
||||||
|
favorite BOOLEAN DEFAULT FALSE,
|
||||||
|
initial_time TIMESTAMP NOT NULL,
|
||||||
|
latest_time TIMESTAMP NOT NULL);''')
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
|
||||||
|
class ServerDatabase(Database):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(constants.SERVER_DATABASE)
|
||||||
|
|
||||||
def construct(self):
|
def construct(self):
|
||||||
with lock:
|
with lock:
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
@@ -71,13 +103,3 @@ class Database(object):
|
|||||||
return cur.lastrowid
|
return cur.lastrowid
|
||||||
finally:
|
finally:
|
||||||
cur.close()
|
cur.close()
|
||||||
|
|
||||||
def get_messages(self, columns: List[str] = None):
|
|
||||||
with lock:
|
|
||||||
cur = self.conn.cursor()
|
|
||||||
try:
|
|
||||||
if columns is None:
|
|
||||||
cur.execute('''SELECT * FROM message''')
|
|
||||||
return cur.fetchall()
|
|
||||||
finally:
|
|
||||||
cur.close()
|
|
||||||
|
|||||||
+3
-3
@@ -23,12 +23,12 @@ class BaseClient(object):
|
|||||||
|
|
||||||
def __init__(self, conn: socket.socket, all_clients: List['Client'], address) -> None:
|
def __init__(self, conn: socket.socket, all_clients: List['Client'], address) -> None:
|
||||||
self.conn, self.all_clients, self.address = conn, all_clients, address
|
self.conn, self.all_clients, self.address = conn, all_clients, address
|
||||||
self.db: Optional[db.Database] = None
|
self.db: Optional[db.ServerDatabase] = None
|
||||||
|
|
||||||
def connect_database(self):
|
def connect_database(self):
|
||||||
if self.db is None:
|
if self.db is None:
|
||||||
logger.debug('Connecting client to database.')
|
logger.debug('Connecting client to database.')
|
||||||
self.db = db.Database()
|
self.db = db.ServerDatabase()
|
||||||
|
|
||||||
def send(self, message: bytes) -> None:
|
def send(self, message: bytes) -> None:
|
||||||
"""Sends a pre-encoded message to this client."""
|
"""Sends a pre-encoded message to this client."""
|
||||||
@@ -89,7 +89,7 @@ class Client(BaseClient):
|
|||||||
def connect_database(self) -> None:
|
def connect_database(self) -> None:
|
||||||
if self.db is None:
|
if self.db is None:
|
||||||
logger.debug(f'Connecting Client({self.id[:8]}) to the database.')
|
logger.debug(f'Connecting Client({self.id[:8]}) to the database.')
|
||||||
self.db = db.Database()
|
self.db = db.ServerDatabase()
|
||||||
|
|
||||||
def request_nickname(self) -> None:
|
def request_nickname(self) -> None:
|
||||||
"""Send a request for the client's nickname information."""
|
"""Send a request for the client's nickname information."""
|
||||||
|
|||||||
Reference in New Issue
Block a user