Delete html response now reflects number of affected rows
This commit is contained in:
@@ -11,6 +11,8 @@ Simple, lightweight, universal SQLite API, based on cherrypy.
|
|||||||
6. row deletes
|
6. row deletes
|
||||||
7. row modifies
|
7. row modifies
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
## REST API
|
## REST API
|
||||||
https://www.restapitutorial.com/
|
https://www.restapitutorial.com/
|
||||||
|
|
||||||
@@ -30,8 +32,10 @@ https://cherrypy.org/
|
|||||||
- validate number of fields and types
|
- validate number of fields and types
|
||||||
- error handling and tracebacks
|
- error handling and tracebacks
|
||||||
- SELECT
|
- SELECT
|
||||||
- *STRUCTURE:* http://server/{operation}/\[{db}/\[{table}\]\]/\[?json\]
|
- **STRUCTURE:** http://server/{operation}/{db}/{table}/?json=true
|
||||||
|
- operation, db, table, json are optional
|
||||||
- SELECT {fields} FROM {table} WHERE {condition} {operator} {value}
|
- SELECT {fields} FROM {table} WHERE {condition} {operator} {value}
|
||||||
|
- TODO: conditions and filters
|
||||||
- the whole where cluase is optional, but needs to be implemented
|
- the whole where cluase is optional, but needs to be implemented
|
||||||
- DELETE
|
- DELETE
|
||||||
- DELETE FROM {table} WHERE {condition} {operator} {value}
|
- DELETE FROM {table} WHERE {condition} {operator} {value}
|
||||||
|
|||||||
BIN
db/test.db
BIN
db/test.db
Binary file not shown.
@@ -137,14 +137,13 @@ class select(object):
|
|||||||
'''
|
'''
|
||||||
db_file = f"{config._ROOT}/{config._DB_PATH}/{db}"
|
db_file = f"{config._ROOT}/{config._DB_PATH}/{db}"
|
||||||
if self.sqlite_check_db(db):
|
if self.sqlite_check_db(db):
|
||||||
self.sqlite_get_table_error = None
|
self.sqlite_get_table_error = False
|
||||||
try:
|
try:
|
||||||
conn = sqlite3.connect(db_file)
|
conn = sqlite3.connect(db_file)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
query = f"SELECT {value} FROM {table}"
|
query = f"SELECT {value} FROM {table}"
|
||||||
c.execute(query)
|
c.execute(query)
|
||||||
result = c.fetchall()
|
result = c.fetchall()
|
||||||
print(result)
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -181,12 +180,10 @@ class select(object):
|
|||||||
self.sqlite_check_db_error = False
|
self.sqlite_check_db_error = False
|
||||||
result = True
|
result = True
|
||||||
else:
|
else:
|
||||||
self.sqlite_check_db_error = f"{db_file} is not a standard file"
|
self.sqlite_check_db_error = f"DB: {db_file} is not a standard file"
|
||||||
#html_result = htmlize.read_html('error', '/templates/')
|
|
||||||
#result = html_result.format(_error=error)
|
|
||||||
result = False
|
result = False
|
||||||
else:
|
else:
|
||||||
self.sqlite_check_db_error = f"{db_file} does not exist"
|
self.sqlite_check_db_error = f"DB: {db_file} does not exist"
|
||||||
#html_result = htmlize.read_html('error', '/templates/')
|
#html_result = htmlize.read_html('error', '/templates/')
|
||||||
#result = html_result.format(_error=error)
|
#result = html_result.format(_error=error)
|
||||||
result = False
|
result = False
|
||||||
@@ -285,6 +282,7 @@ class insert(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.html = ''
|
self.html = ''
|
||||||
self.json = ''
|
self.json = ''
|
||||||
|
self.error = False
|
||||||
|
|
||||||
def _cp_dispatch(self, vpath):
|
def _cp_dispatch(self, vpath):
|
||||||
''' Modify the request path, REST way
|
''' Modify the request path, REST way
|
||||||
@@ -307,23 +305,51 @@ class insert(object):
|
|||||||
*N/A*
|
*N/A*
|
||||||
|
|
||||||
'''
|
'''
|
||||||
dbs = os.listdir(f"{config._ROOT}/{config._DB_PATH}")
|
if len(vpath) == 2:
|
||||||
if len(vpath) == 1:
|
|
||||||
schema = vpath.pop()
|
|
||||||
if schema in dbs:
|
|
||||||
cherrypy.request.params['schema'] = schema
|
|
||||||
return self
|
|
||||||
else:
|
|
||||||
del schema
|
|
||||||
return self
|
|
||||||
|
|
||||||
elif len(vpath) == 2:
|
|
||||||
cherrypy.request.params['table'] = vpath.pop()
|
cherrypy.request.params['table'] = vpath.pop()
|
||||||
cherrypy.request.params['db'] = vpath.pop()
|
cherrypy.request.params['db'] = vpath.pop()
|
||||||
cherrypy.request.params['values'] = '*'
|
self.error = False
|
||||||
return self
|
return self
|
||||||
|
else:
|
||||||
|
self.error = 'Param error'
|
||||||
return vpath
|
return vpath
|
||||||
|
|
||||||
|
def sqlite_insert_values(self, db, table, values):
|
||||||
|
''' Calling the actual data from DB
|
||||||
|
|
||||||
|
Connect to DB, execute a query and return data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*db:* str(), database file name
|
||||||
|
***kwargs:* dict(), additional arguments to specify the
|
||||||
|
table and filters to retrieve the data by
|
||||||
|
|
||||||
|
Sets:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
*result:* tuple(), rows from table, or DB schema as a tuple
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
'''
|
||||||
|
db_file = f"{config._ROOT}/{config._DB_PATH}/{db}"
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(db_file)
|
||||||
|
c = conn.cursor()
|
||||||
|
query = f"INSERT INTO {table} VALUES({values})"
|
||||||
|
c.execute(query)
|
||||||
|
result = c.lastrowid
|
||||||
|
print(result)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
except Exception as e:
|
||||||
|
self.error = e
|
||||||
|
print(e)
|
||||||
|
result = False
|
||||||
|
return result
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def index(self, **kwargs):
|
def index(self, **kwargs):
|
||||||
''' Extending the expose - index method
|
''' Extending the expose - index method
|
||||||
@@ -353,16 +379,146 @@ class insert(object):
|
|||||||
db = kwargs['db']
|
db = kwargs['db']
|
||||||
table = kwargs['table']
|
table = kwargs['table']
|
||||||
values = kwargs['values']
|
values = kwargs['values']
|
||||||
|
print(f"(inser into {db}/{table} values({values})")
|
||||||
|
result = self.sqlite_insert_values(db, table, values)
|
||||||
|
self.html = htmlize.read_html('error','/templates/')
|
||||||
|
self.html = self.html.format(_error=result)
|
||||||
|
self.json.update({
|
||||||
|
"result":{"response": result},
|
||||||
|
"status": self.error
|
||||||
|
})
|
||||||
|
print(dir(result.pop()))
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e))
|
||||||
|
#e = "ERROR: check DB / TABLE / VALUES"
|
||||||
|
result = htmlize.read_html('error', '/templates/')
|
||||||
|
self.html = self.html.format(_error=self.error)
|
||||||
if 'json' in kwargs.keys():
|
if 'json' in kwargs.keys():
|
||||||
return json.dumps(self.json)
|
return json.dumps(str(self.json))
|
||||||
else:
|
else:
|
||||||
return self.html
|
return self.html
|
||||||
|
|
||||||
|
|
||||||
class delete(object):
|
class delete(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
self.html = ''
|
||||||
|
self.json = ''
|
||||||
|
self.error = False
|
||||||
|
|
||||||
|
def _cp_dispatch(self, vpath):
|
||||||
|
''' Modify the request path, REST way
|
||||||
|
|
||||||
|
Format the variables in a row:
|
||||||
|
http://server/method/db_file/table/?values=<>&filter=<>
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*vpath* vpath - I don't quite understand this one,
|
||||||
|
is it internal, or just a dummy?
|
||||||
|
|
||||||
|
Sets:
|
||||||
|
*cherrypy.request.params:* list(), various variable parameters
|
||||||
|
for processing
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
*vpath:* list(), list of path elements
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
'''
|
||||||
|
if len(vpath) == 2:
|
||||||
|
cherrypy.request.params['table'] = vpath.pop()
|
||||||
|
cherrypy.request.params['db'] = vpath.pop()
|
||||||
|
self.error = False
|
||||||
|
return self
|
||||||
|
else:
|
||||||
|
self.error = 'Param error'
|
||||||
|
return vpath
|
||||||
|
|
||||||
|
def sqlite_delete_values(self, db, table, item, value):
|
||||||
|
''' Calling the actual data from DB
|
||||||
|
|
||||||
|
Connect to DB, execute a query and return data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*db:* str(), database file name
|
||||||
|
***kwargs:* dict(), additional arguments to specify the
|
||||||
|
table and filters to retrieve the data by
|
||||||
|
|
||||||
|
Sets:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
*result:* tuple(), rows from table, or DB schema as a tuple
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
'''
|
||||||
|
db_file = f"{config._ROOT}/{config._DB_PATH}/{db}"
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(db_file)
|
||||||
|
c = conn.cursor()
|
||||||
|
query = f"DELETE FROM {table} WHERE {item} = '{value}'"
|
||||||
|
c.execute(query)
|
||||||
|
result = c.rowcount
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
self.error = False
|
||||||
|
self.statement = query
|
||||||
|
except Exception as e:
|
||||||
|
self.error = e
|
||||||
|
print(e)
|
||||||
|
result = False
|
||||||
|
return result
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def index(self):
|
def index(self, **kwargs):
|
||||||
return 'INSERT'
|
''' Extending the expose - index method
|
||||||
|
|
||||||
|
Returns html / json output
|
||||||
|
|
||||||
|
Args:
|
||||||
|
***kwargs:* dict(), arguments needed for SELECT / SCHEMA
|
||||||
|
schema=<db_name> or
|
||||||
|
db=<db_name> AND table=<table_name> OPTINALLY
|
||||||
|
values=<columns_to_select>, filter=<conditions>
|
||||||
|
|
||||||
|
Sets:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
*result:* str(), rhtml code to be rendered, or JSON
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
*N/A*
|
||||||
|
|
||||||
|
'''
|
||||||
|
self.html = ''
|
||||||
|
self.json = {}
|
||||||
|
url = cherrypy.url()
|
||||||
|
try:
|
||||||
|
db = kwargs['db']
|
||||||
|
table = kwargs['table']
|
||||||
|
value = kwargs['value']
|
||||||
|
item = kwargs['item']
|
||||||
|
print(f"(delete from {db}/{table} where {item} = '{value}'")
|
||||||
|
result = self.sqlite_delete_values(db, table, item, value)
|
||||||
|
self.html = htmlize.read_html('delete','/templates/')
|
||||||
|
self.html = self.html.format(
|
||||||
|
_rows_affected=result,
|
||||||
|
_statement=self.statement
|
||||||
|
)
|
||||||
|
self.json.update({
|
||||||
|
"result":{"response": result},
|
||||||
|
"status": self.error
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e))
|
||||||
|
#e = "ERROR: check DB / TABLE / VALUES"
|
||||||
|
result = htmlize.read_html('error', '/templates/')
|
||||||
|
self.html = self.html.format(_error=self.error)
|
||||||
|
if 'json' in kwargs.keys():
|
||||||
|
return json.dumps(str(self.json))
|
||||||
|
else:
|
||||||
|
return self.html
|
||||||
|
|||||||
4
templates/delete.html
Normal file
4
templates/delete.html
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<h1>DELETE</h1>
|
||||||
|
<p>{_statement}</p>
|
||||||
|
<hr>
|
||||||
|
<p>Rows affected: {_rows_affected}</p>
|
||||||
Reference in New Issue
Block a user