Skip to content

Commit

Permalink
#288 : Added sqlite3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ceccopierangiolieugenio committed Nov 12, 2024
1 parent 85cfe8c commit 04a4383
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 1 deletion.
3 changes: 2 additions & 1 deletion TermTk/TTkCore/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def fill(self, pos=(0,0), size=None, char=' ', color=TTkColor.RST):
self._colors[iy][ix] = color.mod(fxa+ix,fya+iy)
else:
fillColor = [color]*(fxb-fxa)
self._colors[iy][fxa:fxb] = fillColor
for iy in range(fya,fyb):
self._colors[iy][fxa:fxb] = fillColor

def drawVLine(self, pos, size, color=TTkColor.RST):
if size == 0: return
Expand Down
1 change: 1 addition & 0 deletions TermTk/TTkWidgets/TTkModelView/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .tablewidgetitem import *
from .tablemodellist import *
from .tablemodelcsv import *
from .tablemodelsqlite3 import *
196 changes: 196 additions & 0 deletions TermTk/TTkWidgets/TTkModelView/tablemodelsqlite3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

__all__=['TTkTableModelSQLite3']

import sqlite3
import threading

from TermTk.TTkCore.log import TTkLog
from TermTk.TTkCore.constant import TTkK
from TermTk.TTkAbstract.abstracttablemodel import TTkAbstractTableModel, TTkModelIndex

class _TTkModelIndexSQLite3(TTkModelIndex):
__slots__ = ('_col','_rowId','_sqModel')
def __init__(self, col:int, rowId:str, sqModel) -> None:
self._col = col
self._rowId = rowId
self._sqModel = sqModel
super().__init__()

def row(self) -> int:
return self._sqModel._getRow(self._rowId)-1
def col(self) -> int:
return self._col

def data(self) -> object:
return self._sqModel.data(row=self.row(),col=self.col())

def setData(self, data: object) -> None:
return self._sqModel.setData(row=self.row(),col=self.col(),data=data)

class TTkTableModelSQLite3(TTkAbstractTableModel):
'''
:py:class:`TTkTableModelSQLite3` extends :py:class:`TTkAbstractTableModel`,
allowing to map an sqlite3 table to this table model
Quickstart:
In This example i assume i have a database named **sqlite.database.db** which contain a table **users**
Please refer to `test.ui.032.table.10.sqlite.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tests/t.ui/test.ui.026.toolTip.py>`_ (`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?fileUri=https://raw.githubusercontent.com/ceccopierangiolieugenio/pyTermTk/main/tests/t.ui/test.ui.032.table.10.sqlite.py>`__) for working eample.
.. code-block:: python
import TermTk as ttk
filename='sqlite.database.db'
tablename='users'
root = ttk.TTk(mouseTrack=True, layout=ttk.TTkGridLayout())
basicTableModel = ttk.TTkTableModelSQLite3(fileName=filename, table=tablename)
table = ttk.TTkTable(parent=root, tableModel=basicTableModel, sortingEnabled=True)
table.resizeColumnsToContents()
root.mainloop()
'''

__slots__ = (
'_conn', '_cur', '_table',
'_key', '_columns', '_count',
'_sort', '_sortColumn',
'_sqliteMutex',
'_idMap')

def __init__(self, *,
fileName:str,
table:str,
# header:list[str]=None
) -> None:
'''
:param fileName: the sqlite3 file database
:type fileName: str
:param table: the name of the sqlite3 table to be mapped
:type table: str
'''
self._sqliteMutex = threading.Lock()
self._table = table
self._columns = []
self._idMap = {}
self._sort = ''
self._sortColumn = -1

self._sqliteMutex.acquire()
self._conn = conn = sqlite3.connect(fileName, check_same_thread=False)
self._cur = cur = self._conn.cursor()

res = cur.execute(f"SELECT COUNT(*) FROM {table}")
self._count = res.fetchone()[0]

for row in cur.execute(f"PRAGMA table_info({table})"):
if row[-1] == 0:
self._columns.append(row[1])
if row[-1] == 1:
self._key = row[1]

self._refreshIdMap()
self._sqliteMutex.release()

super().__init__()

def _refreshIdMap(self):
self._idMap = {
_id : _rn
for _rn,_id in self._cur.execute(f"SELECT ROW_NUMBER() OVER ({self._sort}) RN, {self._key} from users")}

def rowCount(self) -> int:
return self._count

def columnCount(self) -> int:
return len(self._columns)

def _getRow(self, key:str) -> int:
return self._idMap[key]

def index(self, row:int, col:int) -> TTkModelIndex:
self._sqliteMutex.acquire()
res = self._cur.execute(
f"SELECT {self._key} FROM {self._table} "
f"{self._sort} "
f"LIMIT 1 OFFSET {row}")
key=res.fetchone()[0]
self._sqliteMutex.release()
return _TTkModelIndexSQLite3(col=col,rowId=key,sqModel=self)

def data(self, row:int, col:int) -> None:
self._sqliteMutex.acquire()
res = self._cur.execute(
f"SELECT {self._columns[col]} FROM {self._table} "
f"{self._sort} "
f"LIMIT 1 OFFSET {row}")
self._sqliteMutex.release()
return res.fetchone()[0]

def setData(self, row:int, col:int, data:object) -> None:
self._sqliteMutex.acquire()
res = self._cur.execute(
f"SELECT {self._key} FROM {self._table} "
f"{self._sort} "
f"LIMIT 1 OFFSET {row}")
key=res.fetchone()[0]
res = self._cur.execute(
f"UPDATE {self._table} "
f"SET {self._columns[col]} = '{data}' "
f"WHERE {self._key} = {key} ")
self._conn.commit()
if col == self._sortColumn:
self._refreshIdMap()
self._sqliteMutex.releases()
return True

def headerData(self, num:int, orientation:int):
if orientation == TTkK.HORIZONTAL:
if self._columns:
return self._columns[num]
return super().headerData(num, orientation)

def flags(self, row:int, col:int) -> TTkK.ItemFlag:
return (
TTkK.ItemFlag.ItemIsEnabled |
TTkK.ItemFlag.ItemIsEditable |
TTkK.ItemFlag.ItemIsSelectable )

def sort(self, column:int, order:TTkK.SortOrder) -> None:
self._sortColumn = column
if column == -1:
self._sort = ''
else:
if order ==TTkK.SortOrder.AscendingOrder:
self._sort = f"ORDER BY {self._columns[column]} ASC"
else:
self._sort = f"ORDER BY {self._columns[column]} DESC"
self._sqliteMutex.acquire()
self._refreshIdMap()
self._sqliteMutex.release()
109 changes: 109 additions & 0 deletions tests/t.ui/test.ui.032.table.10.sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python3

# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import sys
import csv
import re
import argparse
import operator
import json
import random
import sqlite3

sys.path.append(os.path.join(sys.path[0],'../..'))
import TermTk as ttk

words = [
"Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit,", "sed",
"do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna",
"aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis", "nostrud", "exercitation",
"ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat.",
"Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit",
"esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
"occaecat", "cupidatat", "non", "proident,", "sunt", "in", "culpa", "qui", "officia",
"deserunt", "mollit", "anim", "id", "est", "laborum."]


parser = argparse.ArgumentParser()
parser.add_argument('-f', help='Full Screen (default)', action='store_true')
parser.add_argument('-w', help='Windowed', action='store_true')
# parser.add_argument('-t', help='Track Mouse', action='store_true')
parser.add_argument('file', help='Open SQlite3 File', type=str)

args = parser.parse_args()

fullScreen = not args.w
mouseTrack = True
path = args.file

def _createDB(fileName):
# Connect to a database (or create one if it doesn't exist)
conn = sqlite3.connect(fileName)

# Create a cursor object
cur = conn.cursor()

# Create a table
cur.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
surname TEXT,
location TEXT,
code INTEGER,
age INTEGER)''')
# Insert data
sqlDef = "INSERT INTO users (name, surname, location, code, age) VALUES (?, ?, ?, ?, ?)"
for _ in range(20):
cur.execute(sqlDef,
(random.choice(words), random.choice(words),
random.choice(words),
random.randint(0x10000,0x100000000), random.randint(18,70)))

conn.commit()
conn.close()

# Create the DB if the file does not exists
if not os.path.exists(path):
_createDB(path)

root = ttk.TTk(title="pyTermTk Table Demo",
mouseTrack=mouseTrack,
sigmask=(
ttk.TTkTerm.Sigmask.CTRL_Q |
ttk.TTkTerm.Sigmask.CTRL_S |
# ttk.TTkTerm.Sigmask.CTRL_C |
ttk.TTkTerm.Sigmask.CTRL_Z ))

if fullScreen:
rootTable = root
root.setLayout(ttk.TTkGridLayout())
else:
rootTable = ttk.TTkWindow(parent=root,pos = (0,0), size=(150,40), title="Test Table", layout=ttk.TTkGridLayout(), border=True)

basicTableModel = ttk.TTkTableModelSQLite3(fileName=path, table='users')
table = ttk.TTkTable(parent=root, tableModel=basicTableModel, sortingEnabled=True)
table.resizeColumnsToContents()

root.mainloop()

0 comments on commit 04a4383

Please sign in to comment.