Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: 60-cannot-connect-with-external-dbs-eg-in-microsoft-studio #61

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion peepdb/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from .base import BaseDatabase
from .mysql import MySQLDatabase
from .postgresql import PostgreSQLDatabase
from .mariadb import MariaDBDatabase
from .mongodb import MongoDBDatabase
from .sqlite import SQLiteDatabase
from .firebase import FirebaseDatabase
from .firebase import FirebaseDatabase
from .mssql import MSSQLDatabase

__all__ = [
'BaseDatabase',
'MySQLDatabase',
'PostgreSQLDatabase',
'MariaDBDatabase',
'MongoDBDatabase',
'SQLiteDatabase',
'FirebaseDatabase',
'MSSQLDatabase'
]
67 changes: 67 additions & 0 deletions peepdb/db/mssql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pyodbc
from .base import BaseDatabase
from typing import List, Dict, Any
import logging

class MSSQLDatabase(BaseDatabase):
def __init__(self, host: str, user: str, password: str, database: str, port: int = 1433, trusted: bool = False, **kwargs):
super().__init__(host, user, password, database, port, **kwargs)
self.trusted = trusted
self.logger = logging.getLogger(__name__)

def connect(self) -> None:
driver = '{ODBC Driver 17 for SQL Server}'
server = f"{self.host},{self.port}" if self.port else self.host
if self.trusted:
connection_string = f"DRIVER={driver};SERVER={server};DATABASE={self.database};Trusted_Connection=yes;"
else:
connection_string = f"DRIVER={driver};SERVER={server};DATABASE={self.database};UID={self.user};PWD={self.password};"
self.logger.debug(f"Connection String: {connection_string}")
try:
self.connection = pyodbc.connect(connection_string, autocommit=False)
self.cursor = self.connection.cursor()
self.logger.info(f"Connected to MSSQL database: {self.database}")
except pyodbc.Error as e:
self.logger.error(f"Error connecting to MSSQL database: {e}")
raise

def disconnect(self) -> None:
if self.cursor:
self.cursor.close()
if self.connection:
self.connection.close()
self.logger.info(f"Disconnected from MSSQL database: {self.database}")

def fetch_tables(self) -> List[str]:
# Get both schema and table name
self.cursor.execute("""
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
""")
# Return something like "schemaName.tableName"
return [f"{row[0]}.{row[1]}" for row in self.cursor.fetchall()]

def fetch_data(self, table: str, page: int = 1, page_size: int = 100) -> Dict[str, Any]:
offset = (page - 1) * page_size
# Count total rows
self.cursor.execute(f"SELECT COUNT(*) FROM {table}")
total_rows = self.cursor.fetchone()[0]

# Fetch data using OFFSET FETCH
self.cursor.execute(f"""
SELECT * FROM {table}
ORDER BY (SELECT NULL)
OFFSET {offset} ROWS
FETCH NEXT {page_size} ROWS ONLY;
""")
columns = [desc[0] for desc in self.cursor.description]
rows = self.cursor.fetchall()
data = [dict(zip(columns, row)) for row in rows]

return {
'data': data,
'page': page,
'total_pages': (total_rows + page_size - 1) // page_size,
'total_rows': total_rows
}
45 changes: 34 additions & 11 deletions peepdb/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def mock_db():
}
return db


@patch('peepdb.cli.get_connection')
@patch('peepdb.core.MySQLDatabase')
def test_view_command_with_pagination(mock_mysql_db, mock_get_connection, runner, mock_db):
Expand All @@ -46,6 +47,7 @@ def test_view_command_with_pagination(mock_mysql_db, mock_get_connection, runner
mock_get_connection.assert_called_once_with('testconn')
mock_db.fetch_data.assert_called_once_with('users', 2, 50)


@patch('peepdb.cli.get_connection')
@patch('peepdb.core.MySQLDatabase')
def test_view_command_with_json_format(mock_mysql_db, mock_get_connection, runner, mock_db):
Expand All @@ -61,6 +63,7 @@ def test_view_command_with_json_format(mock_mysql_db, mock_get_connection, runne
assert '"page": 1' in result.output
assert '"total_rows": 2' in result.output


@patch('peepdb.cli.get_connection')
@patch('peepdb.core.MySQLDatabase')
def test_view_command_with_scientific(mock_mysql_db, mock_get_connection, runner, mock_db):
Expand All @@ -79,11 +82,13 @@ def test_view_command_with_scientific(mock_mysql_db, mock_get_connection, runner

assert result.exit_code == 0
assert "Table: users" in result.output
assert any(scientific in result.output for scientific in ["1.234568e+09", "1.23457e+09"]) # Ensure salary is displayed in scientific notation
# Check for scientific notation in the output
assert any(scientific in result.output for scientific in ["1.234568e+09", "1.23457e+09"])
assert any(scientific in result.output for scientific in ["9.876543e+09", "9.87654e+09"])

mock_get_connection.assert_called_once_with('testconn')


@patch('peepdb.cli.get_connection')
def test_view_command_invalid_connection(mock_get_connection, runner):
mock_get_connection.return_value = None
Expand All @@ -93,34 +98,50 @@ def test_view_command_invalid_connection(mock_get_connection, runner):
assert result.exit_code == 0
assert "Error: No saved connection found with name 'invalid_conn'." in result.output


@patch('peepdb.cli.save_connection')
def test_save_command(mock_save_connection, runner):
result = runner.invoke(cli, [
'save',
'testconn',
'--db-type', 'mysql',
'--host', 'localhost',
'--user', 'testuser',
'--password', 'testpassword', # Include the password as a command-line option
'save',
'testconn',
'--db-type', 'mysql',
'--host', 'localhost',
'--user', 'testuser',
'--password', 'testpassword',
'--database', 'testdb'
])

if result.exit_code != 0:
print(f"Command output: {result.output}")
print(f"Exception: {result.exception}")

assert result.exit_code == 0
mock_save_connection.assert_called_once_with('testconn', 'mysql', 'localhost', 'testuser', 'testpassword', 'testdb')

# The real code calls:
# save_connection(connection_name, db_type, host, port, user, password, database, trusted)
# so let's assert with the full argument list:
mock_save_connection.assert_called_once_with(
'testconn', # connection_name
'mysql', # db_type
'localhost', # host
None, # port (not provided -> None)
'testuser', # user
'testpassword', # password
'testdb', # database
False # trusted
)


@patch('peepdb.cli.list_connections')
def test_list_command(mock_list_connections, runner):
mock_list_connections.return_value = None # The function prints directly, so we return None
mock_list_connections.return_value = None # The function prints directly, so we just return None

result = runner.invoke(cli, ['list'])

assert result.exit_code == 0
mock_list_connections.assert_called_once()


@patch('peepdb.cli.remove_connection')
def test_remove_command(mock_remove_connection, runner):
mock_remove_connection.return_value = True
Expand All @@ -131,6 +152,7 @@ def test_remove_command(mock_remove_connection, runner):
assert "Connection 'testconn' has been removed." in result.output
mock_remove_connection.assert_called_once_with('testconn')


@patch('peepdb.cli.remove_all_connections')
def test_remove_all_command(mock_remove_all_connections, runner):
mock_remove_all_connections.return_value = 2
Expand All @@ -141,5 +163,6 @@ def test_remove_all_command(mock_remove_all_connections, runner):
assert "2 connection(s) have been removed." in result.output
mock_remove_all_connections.assert_called_once()


if __name__ == '__main__':
pytest.main()
pytest.main()
22 changes: 12 additions & 10 deletions peepdb/tests/test_peepdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from peepdb.config import save_connection, get_connection, list_connections, remove_connection, remove_all_connections
from peepdb.db import MySQLDatabase, PostgreSQLDatabase, MariaDBDatabase, MongoDBDatabase, FirebaseDatabase


@patch('peepdb.core.FirebaseDatabase')
@patch('peepdb.core.MongoDBDatabase')
@patch('peepdb.core.MySQLDatabase')
Expand All @@ -31,7 +32,7 @@ def test_peep_db(mock_mariadb, mock_postgresql, mock_mysql, mock_mongodb, mock_f
mock_mongodb.return_value = mock_db
mock_firebase_db.return_value = mock_db

# Expected result for non-scientific format
# Expected result for non-scientific, JSON format
expected_result = {
'table1': {
'data': [
Expand Down Expand Up @@ -84,13 +85,13 @@ def test_peep_db(mock_mariadb, mock_postgresql, mock_mysql, mock_mongodb, mock_f
result = peep_db('firebase', 'path/to/serviceAccountKey.json', '', '', '', format='json', scientific=False)
assert result == expected_result

# Test MySQL with scientific notation
# Test MySQL with scientific notation (JSON unaffected by the 'scientific' flag)
result = peep_db('mysql', 'host', 'user', 'password', 'database', format='json', scientific=True)
assert result == expected_result # JSON output unaffected by scientific flag
assert result == expected_result

# Test Firebase with scientific notation
# Test Firebase with scientific notation (JSON output also unaffected)
result = peep_db('firebase', 'path/to/serviceAccountKey.json', '', '', '', format='json', scientific=True)
assert result == expected_result # JSON output unaffected by scientific flag
assert result == expected_result

# Test PostgreSQL with scientific notation
result = peep_db('postgres', 'host', 'user', 'password', 'database', table='table1', format='json', scientific=True)
Expand All @@ -111,6 +112,7 @@ def test_peep_db(mock_mariadb, mock_postgresql, mock_mysql, mock_mongodb, mock_f
# Test MySQL with scientific notation and table format
result = peep_db('mysql', 'host', 'user', 'password', 'database', format='table', scientific=True)
assert 'Table: table1' in result
# Check for large numeric values in scientific notation
assert '9.223372e+18' in result or '9.22337e+18' in result
assert '1.000000e+16' in result or '1e+16' in result

Expand All @@ -119,7 +121,6 @@ def test_peep_db(mock_mariadb, mock_postgresql, mock_mysql, mock_mongodb, mock_f
peep_db('unsupported', 'host', 'user', 'password', 'database')


# Test configuration functions
@patch('peepdb.config.os.path.exists')
@patch('peepdb.config.open')
@patch('peepdb.config.json.load')
Expand All @@ -132,10 +133,12 @@ def test_config_functions(mock_decrypt, mock_encrypt, mock_json_dump, mock_json_
mock_json_load.return_value = {}
mock_encrypt.side_effect = lambda x: f'encrypted_{x}'

save_connection('test_conn', 'mysql', 'host', 'user', 'password', 'database')
# Provide 8 arguments: (name, db_type, host, port, user, password, database, trusted)
save_connection('test_conn', 'mysql', 'host', None, 'user', 'password', 'database', False)
mock_json_dump.assert_called_once()

# Test get_connection
mock_json_dump.reset_mock()
mock_json_load.return_value = {
'test_conn': {
'db_type': 'mysql',
Expand All @@ -148,7 +151,7 @@ def test_config_functions(mock_decrypt, mock_encrypt, mock_json_dump, mock_json_
mock_decrypt.side_effect = lambda x: x.replace('encrypted_', '')

result = get_connection('test_conn')
assert result == ('mysql', 'host', 'user', 'password', 'database')
assert result == ('mysql', 'host', 'user', 'password', 'database', False)

# Test list_connections
with patch('builtins.print') as mock_print:
Expand All @@ -157,13 +160,12 @@ def test_config_functions(mock_decrypt, mock_encrypt, mock_json_dump, mock_json_

# Test remove_connection
mock_json_load.return_value = {'test_conn': {}, 'other_conn': {}}
assert remove_connection('test_conn') == True
assert remove_connection('test_conn') is True
mock_json_dump.assert_called()

# Test remove_all_connections
mock_json_load.return_value = {'test_conn': {}, 'other_conn': {}}
assert remove_all_connections() == 2
# Assert that os.remove was called with the correct arguments


if __name__ == '__main__':
Expand Down
47 changes: 23 additions & 24 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
mysql-connector-python
psycopg2-binary
pymysql
mariadb
pymongo
click
coverage==7.6.1
cryptography==43.0.0
iniconfig==2.0.0
mysql-connector-python==9.0.0
packaging==24.1
pluggy==1.5.0
psycopg2-binary==2.9.9
pycparser==2.22
PyMySQL==1.1.1
pytest==8.3.2
pytest-cov==5.0.0
tabulate==0.8.9
cachetools==5.5.0
keyring==25.4.1
dnspython==2.6.1
pymongo==4.9.1
async-timeout==4.0.3
backports.tarfile==1.2.0
CacheControl==0.14.0
cachetools==5.5.0
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
click==8.1.7
coverage==7.6.1
cryptography==43.0.0
dnspython==2.6.1
exceptiongroup==1.2.2
firebase-admin==6.5.0
google-api-core==2.20.0
Expand All @@ -42,21 +26,36 @@ grpcio-status==1.66.2
httplib2==0.22.0
idna==3.10
importlib_metadata==8.5.0
iniconfig==2.0.0
jaraco.classes==3.4.0
jaraco.context==6.0.1
jaraco.functools==4.1.0
keyring==25.4.1
mariadb==1.1.10
more-itertools==10.5.0
msgpack==1.1.0
mysql-connector-python==9.0.0
packaging==24.1
-e git+https://github.com/evangelosmeklis/peepDB.git@4db25b049fce0058fa21e8f95b4b8840a9f3261e#egg=peepdb
pluggy==1.5.0
proto-plus==1.24.0
protobuf==5.28.2
psycopg2-binary==2.9.9
pyasn1==0.6.1
pyasn1_modules==0.4.1
pycparser==2.22
PyJWT==2.9.0
pymongo==4.9.1
PyMySQL==1.1.1
pyodbc==5.2.0
pyparsing==3.1.4
pytest==8.3.2
pytest-cov==5.0.0
redis==5.2.0
requests==2.32.3
rsa==4.9
setuptools==65.5.0
tabulate==0.8.9
tomli==2.0.1
uritemplate==4.1.1
urllib3==2.2.3
zipp==3.20.2
zipp==3.20.2
Loading