55import pandas as pd
66
77from mindsdb_sql .parser .ast import DropTables
8- from mindsdb_sql .parser .ast import Select , Star , Identifier , Constant , Delete , Insert , Update
8+ from mindsdb_sql .parser .ast import Select , Star , Identifier , Constant , Delete , Insert , Update , Last , BinaryOperation
99
10- from mindsdb_sdk .utils .sql import dict_to_binary_op
10+ from mindsdb_sdk .utils .sql import dict_to_binary_op , add_condition
1111from mindsdb_sdk .utils .objects_collection import CollectionBase
1212
1313from .query import Query
@@ -20,6 +20,7 @@ def __init__(self, db, name):
2020 self .db = db
2121 self ._filters = {}
2222 self ._limit = None
23+ self ._track_column = None
2324 self ._update_query ()
2425
2526 def _filters_repr (self ):
@@ -45,6 +46,7 @@ def filter(self, **kwargs):
4546 'select * from table1 where a=1 and b=2'
4647
4748 :param kwargs: filter
49+ :return: Table object
4850 """
4951 # creates new object
5052 query = copy .deepcopy (self )
@@ -57,17 +59,46 @@ def limit(self, val: int):
5759 Applies limit condition to table query
5860
5961 :param val: limit size
62+ :return: Table object
6063 """
6164 query = copy .deepcopy (self )
6265 query ._limit = val
6366 query ._update_query ()
6467 return query
6568
69+ def track (self , column ):
70+ """
71+ Apply tracking column to table. ('LAST' keyword in mindsdb)
72+ First call returns nothing
73+ The next calls return new records since previous call (where value of track_column is greater)
74+
75+ Example:
76+
77+ >>> query = con.databases.my_db.tables.sales.filter(type='house').track('created_at')
78+ >>> # first call returns no records
79+ >>> df = query.fetch()
80+ >>> # second call returns rows with created_at is greater since previous fetch
81+ >>> df = query.fetch()
82+
83+ :param column: column to track new data from table.
84+ :return: Table object
85+ """
86+ query = copy .deepcopy (self )
87+ query ._track_column = column
88+
89+ query ._update_query ()
90+ return query
91+
6692 def _update_query (self ):
93+ where = dict_to_binary_op (self ._filters )
94+ if self ._track_column is not None :
95+ condition = BinaryOperation (op = '>' , args = [Identifier (self ._track_column ), Last ()])
96+ where = add_condition (where , condition )
97+
6798 ast_query = Select (
6899 targets = [Star ()],
69100 from_table = Identifier (self .name ),
70- where = dict_to_binary_op ( self . _filters )
101+ where = where
71102 )
72103 if self ._limit is not None :
73104 ast_query .limit = Constant (self ._limit )
0 commit comments