• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / c932eaa1-4b2f-4146-8188-fa66e08b3128

02 Sep 2023 05:59PM UTC coverage: 93.55% (-0.1%) from 93.658%
c932eaa1-4b2f-4146-8188-fa66e08b3128

push

circle-ci

web-flow
release: merge staging into master (#1032)

Co-authored-by: Joy Arulraj <arulraj@gatech.edu>
Co-authored-by: Andy Xu <xzdandy@gmail.com>
Co-authored-by: Jiashen Cao <caojiashen24@gmail.com>
Co-authored-by: rohith mulumudy <mulumudyrohith@gmail.com>
Co-authored-by: Chitti Ankith <chittiankith@gmail.com>
Co-authored-by: Ankith Reddy Chitti <chitti@Ankiths-Air.attlocal.net>

157 of 157 new or added lines in 13 files covered. (100.0%)

10574 of 11303 relevant lines covered (93.55%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.25
/evadb/third_party/databases/sqlite/sqlite_handler.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
import sqlite3
1✔
16

17
import pandas as pd
1✔
18

19
from evadb.third_party.databases.types import (
1✔
20
    DBHandler,
21
    DBHandlerResponse,
22
    DBHandlerStatus,
23
)
24

25

26
class SQLiteHandler(DBHandler):
1✔
27
    def __init__(self, name: str, **kwargs):
1✔
28
        """
29
        Initialize the handler.
30
        Args:
31
            name (str): name of the DB handler instance
32
            **kwargs: arbitrary keyword arguments for establishing the connection.
33
        """
34
        super().__init__(name)
1✔
35
        self.database = kwargs.get("database")
1✔
36
        self.connection = None
1✔
37

38
    def connect(self):
1✔
39
        """
40
        Set up the connection required by the handler.
41
        Returns:
42
            DBHandlerStatus
43
        """
44
        try:
×
45
            self.connection = sqlite3.connect(
×
46
                database=self.database, isolation_level=None  # Autocommit mode.
47
            )
48
            return DBHandlerStatus(status=True)
×
49
        except sqlite3.Error as e:
50
            return DBHandlerStatus(status=False, error=str(e))
51

52
    def disconnect(self):
1✔
53
        """
54
        Close any existing connections.
55
        """
56
        if self.connection:
×
57
            self.connection.close()
×
58

59
    def check_connection(self) -> DBHandlerStatus:
1✔
60
        """
61
        Check connection to the handler.
62
        Returns:
63
            DBHandlerStatus
64
        """
65
        if self.connection:
×
66
            return DBHandlerStatus(status=True)
×
67
        else:
68
            return DBHandlerStatus(status=False, error="Not connected to the database.")
×
69

70
    def get_tables(self) -> DBHandlerResponse:
1✔
71
        """
72
        Return the list of tables in the database.
73
        Returns:
74
            DBHandlerResponse
75
        """
76
        if not self.connection:
×
77
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
78

79
        try:
×
80
            query = "SELECT name AS table_name FROM sqlite_master WHERE type = 'table'"
×
81
            tables_df = pd.read_sql_query(query, self.connection)
×
82
            return DBHandlerResponse(data=tables_df)
×
83
        except sqlite3.Error as e:
84
            return DBHandlerResponse(data=None, error=str(e))
85

86
    def get_columns(self, table_name: str) -> DBHandlerResponse:
1✔
87
        """
88
        Returns the list of columns for the given table.
89
        Args:
90
            table_name (str): name of the table whose columns are to be retrieved.
91
        Returns:
92
            DBHandlerResponse
93
        """
94
        if not self.connection:
×
95
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
96
        """
×
97
        SQLite does not provide an in-built way to get the column names using a SELECT statement.
98
        Hence we have to use the PRAGMA command and filter the required columns.
99
        """
100
        try:
×
101
            query = f"PRAGMA table_info('{table_name}')"
×
102
            pragma_df = pd.read_sql_query(query, self.connection)
×
103
            columns_df = pragma_df[["name", "type"]].copy()
×
104
            columns_df.rename(columns={"type": "dtype"}, inplace=True)
×
105
            return DBHandlerResponse(data=columns_df)
×
106
        except sqlite3.Error as e:
107
            return DBHandlerResponse(data=None, error=str(e))
108

109
    def _fetch_results_as_df(self, cursor):
1✔
110
        try:
×
111
            res = cursor.fetchall()
×
112
            res_df = pd.DataFrame(
×
113
                res,
114
                columns=[desc[0] for desc in cursor.description]
115
                if cursor.description
116
                else [],
117
            )
118
            return res_df
×
119
        except sqlite3.ProgrammingError as e:
120
            if str(e) == "no results to fetch":
121
                return pd.DataFrame({"status": ["success"]})
122
            raise e
123

124
    def execute_native_query(self, query_string: str) -> DBHandlerResponse:
1✔
125
        """
126
        Executes the native query on the database.
127
        Args:
128
            query_string (str): query in native format
129
        Returns:
130
            DBHandlerResponse
131
        """
132
        if not self.connection:
×
133
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
134
        try:
×
135
            cursor = self.connection.cursor()
×
136
            cursor.execute(query_string)
×
137
            return DBHandlerResponse(data=self._fetch_results_as_df(cursor))
×
138
        except sqlite3.Error as e:
139
            return DBHandlerResponse(data=None, error=str(e))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc