#!/usr/bin/env python3.1
#-*- coding: utf-8 -*-

""" Testing the cmd interface """

import cmd
import sys
import sqlite3

class commandPromt(cmd.Cmd):
	""" Testing here """

	# init of DB and shit
	conn = sqlite3.connect("./awnsers.sqlite")
	db = conn.cursor()

	startQueryShow = "select * from awnsers where("
	startQueryCount = "select count(*) from awnsers where("
	endQuery =");"


	def do_show(self,line):
		""" Shows the query where
		IE: select * from <table> where <Command>
		example:
			show test2 = 1 and test = 1"""
		query = commandPromt.startQueryShow + line + commandPromt.endQuery
		try:
			result = commandPromt.db.execute(query)
		except:
			print("Dave, I can not show you that....")
		else:
			for row in result:
				print(row)
	
	def do_count(self,line):
		""" counts the query where
		IE: select count(*) from <table> where <Command>
		example:
			count test2 = 1 and test = 1"""
		query = commandPromt.startQueryCount + line + commandPromt.endQuery
		try:
			result = commandPromt.db.execute(query)
		except:
			print("Dave, I can not count that....")
		else:
			for row in result:
				print(row[0])
	#TODO: Add a countAll command, it will show the numbor of each true value in the DB.
	#IE:
	#gud: 13
	#vegan: 4
	#etc etc.

	def do_exit(self,line):
		import awnsers_and_db_edit
		print("====== returning to main menu ======")##FIXME: why is this not printed before going into the main menu?
		awnsers_and_db_edit.menu()

	def de_EOF(self, line):
		return(true)
