Skip to content

Commit

Permalink
refactor for simplification
Browse files Browse the repository at this point in the history
- moved docstrings
- moved method to object collections
  • Loading branch information
ea-rus committed Sep 14, 2023
1 parent 0c57059 commit 628a7c7
Show file tree
Hide file tree
Showing 17 changed files with 1,081 additions and 986 deletions.
1 change: 0 additions & 1 deletion mindsdb_sdk/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from mindsdb_sdk.connectors.rest_api import RestAPI



def connect(url: str = None, login: str = None, password: str = None, is_managed: bool = False) -> Server:
"""
Create connection to mindsdb server
Expand Down
181 changes: 0 additions & 181 deletions mindsdb_sdk/database.py

This file was deleted.

133 changes: 133 additions & 0 deletions mindsdb_sdk/databases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from typing import List

from mindsdb_sql.parser.dialects.mindsdb import CreateDatabase
from mindsdb_sql.parser.ast import DropDatabase, Identifier

from mindsdb_sdk.utils.objects_collection import CollectionBase

from .query import Query
from .tables import Tables


class Database:
"""
Allows to work with database (datasource): to use tables and make raw queries
To run native query
At this moment query is just saved in Qeury object and not executed
>>> query = database.query('select * from table1') # returns Query
This command sends request to server to execute query and return dataframe
>>> df = query.fetch()
Has list of tables in .tables attribute.
"""

def __init__(self, server, name):
self.server = server
self.name = name
self.api = server.api

self.tables = Tables(self, self.api)

# old api
self.get_table = self.tables.get
self.list_tables = self.tables.list
self.create_table = self.tables.create
self.drop_table = self.tables.drop

def __repr__(self):
return f'{self.__class__.__name__}({self.name})'

def query(self, sql: str) -> Query:
"""
Make raw query to integration
:param sql: sql of the query
:return: Query object
"""
return Query(self.api, sql, database=self.name)


class Databases(CollectionBase):
"""
Databases
----------
>>> databases.list()
>>> db = databases[0] # Database type object
# create
>>> db = databases.create('example_db',
... type='postgres',
... connection_args={'host': ''})
# drop database
>>> databases.drop('example_db')
# get existing
>>> db = databases.get('example_db')
"""

def __init__(self, api):
self.api = api

def _list_databases(self):
data = self.api.sql_query(
"select NAME from information_schema.databases where TYPE='data'"
)
return list(data.NAME)

def list(self) -> List[Database]:
"""
Show list of integrations (databases) on server
:return: list of Database objects
"""
return [Database(self, name) for name in self._list_databases()]

def create(self, name: str, engine: str, connection_args: dict) -> Database:
"""
Create new integration and return it
:param name: Identifier for the integration to be created
:param engine: Engine to be selected depending on the database connection.
:param connection_args: {"key": "value"} object with the connection parameters specific for each engine
:return: created Database object
"""
ast_query = CreateDatabase(
name=Identifier(name),
engine=engine,
parameters=connection_args,
)
self.api.sql_query(ast_query.to_string())
return Database(self, name)

def drop(self, name: str):
"""
Delete integration
:param name: name of integration
"""
ast_query = DropDatabase(name=Identifier(name))
self.api.sql_query(ast_query.to_string())

def get(self, name: str) -> Database:
"""
Get integration by name
:param name: name of integration
:return: Database object
"""
if name not in self._list_databases():
raise AttributeError("Database doesn't exist")
return Database(self, name)


Loading

0 comments on commit 628a7c7

Please sign in to comment.