def execute(self): print "Printing out: " result = "GRANT " pars = sql_tokenizer() tokens = pars.AK_parse_grant(self.command) if isinstance(tokens, basestring): result = "Wrong command!" else: for user in tokens.users: msg = "...grant to: " + user + "\n" for privilege in tokens.privileges: msg += "...granted privilege: " + privilege + "\n" for table in tokens.tables: msg += "...on table: " + table + "\n" if tokens.group: res = ak47.AK_grant_privilege_group(user, table, privilege) result += str(res) if res != -1: print msg msg = "" else: print "ERROR: Group or table does not exsist: " + str(user) + str(table) else: res = ak47.AK_grant_privilege_user(user, table, privilege) result += str(res) if res != -1: print msg msg = "" else: print "ERROR: User or table does not exsist: " + str(user) + str(table) return result
def execute(self): parser = sql_tokenizer() token = parser.AK_parse_trigger(self.expr) # checking syntax if isinstance(token, str): print "Error: syntax error in expression" print self.expr print token return False #get table name table_name = str(token.tableName) # check if table exist ''' For some reason, AK_table_exist won't work, it always just exits here, so it's commented out if (ak47.AK_table_exist(table_name) == 0): print "Error: table '"+ table_name +"' does not exist" return False ''' #get trigger name trigger = str(token.name) p = list() p.append(token.EventOption1) p.append(token.EventOption2) p.append(token.EventOption3) #executing ''' Not working TypeError: in method 'AK_trigger_add', argument 3 of type 'AK_list *' ''' try: ak47.AK_trigger_add(trigger, token.whatOption, p, table_name, token.functionName) result = "Trigger created" except: result = "Error. Creating trigger failed." return result
def execute(self): parser = sql_tokenizer() token = parser.AK_parse_create_table(self.expr) # checking syntax if isinstance(token, str): print "Error: syntax error in expression" print self.expr print token return False # get table name table_name = str(token.tableName) # table should not exist yet ''' For some reason, AK_table_exist won't work, it always just exits here, so it's commented out if (ak47.AK_table_exist(table_name) == 1): print "Error: table'" + table_name + "' already exist" return False ''' # get attributes ''' Create table in table.c currently takes only name and type of attributes. Parsing works for other attribute properties as well, so it should be added here when possible. ''' create_table_attributes = [] for attribute in token.attributes: create_table_attributes.append([{'name': str(attribute[0])}, {'type':get_attr_type(str(attribute[1]))}]) attribute_count = len(create_table_attributes) # executing try: ak47.AK_create_table(table_name, create_table_attributes, attribute_count) result = "Table created" except: result = "Error. Creating table failed." return result
def execute(self, input): print "start parsing.." pars = sql_tokenizer() tok = pars.AK_create_sequence(input) # isinstance needs revision for swig ''' if isinstance(tok, str): print "Error: syntax error in expression" print string print tok return False ''' print "\nSequence name: ", tok.seq_name print "'AS' definition: ", tok.as_value print "'Start with' value: ", tok.start_with print "'Increment by' value: ", tok.increment_by print "'MinValue' value: ", tok.min_value print "'MaxValue' value: ", tok.max_value print "'Cache' value: ", tok.cache print "'Cycle' value: ", tok.cycle # Check for sequence name, if already exists in database return false # Needs more revision for swig after buffer overflow is handled ''' names = ak47.AK_get_column(1, "AK_sequence") for name in set(names): if(name==tok.seq_name): error = "ERROR the name is already used" return error ''' # executing create statement try: ak47.AK_sequence_add(str(tok.seq_name), int(tok.start_with), int( tok.increment_by), int(tok.max_value), int(tok.min_value), int(tok.cycle)) result = "Command succesfully executed" except: result = "ERROR creating sequence didn't pass" ak47.AK_print_table("AK_sequence") return result
def execute(self): parser = sql_tokenizer() token = parser.AK_parse_createIndex(self.expr) # checking syntax if isinstance(token, str): print "Error: syntax error in expression" print self.expr print token return False #get table name table_name = str(token.tablica) # check if table exist ''' For some reason, AK_table_exist won't work, it always just exits here, so it's commented out if (ak47.AK_table_exist(table_name) == 0): print "Error: table '"+ table_name +"' does not exist" return False ''' #get index name index = str(token.IndexIme) #get other expression tokens t = list() t.append(table_name) t.append(token.stupci) t.append(token.IndexVrsta) #executing ''' Not working TypeError: in method 'AK_create_Index', argument 2 of type 'AK_list *' Uncomment the next line before testing to see the problem ''' # print ak47.AK_create_Index(index, t) try: ak47.AK_create_Index(index, t) result = "Index created" except: result = "Error. Creating index failed." return result
def execute(self): parser = sql_tokenizer() token = parser.AK_parse_drop(self.expr) if isinstance(token, str): print "Error: syntax error in expression" print token return False objekt = str(token.objekt) if(objekt == "table"): # izvlacimo ime tablice table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li tablica if (ak47.AK_table_exist(table_name) == 0): print "Error: table '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(0, table_name) elif(objekt == "index"): # izvlacimo ime indexa table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li index if (ak47.AK_table_exist(table_name) == 0): print "Error: index '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(1, table_name) elif(objekt == "view"): # izvlacimo ime view-a table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li view if (ak47.AK_table_exist(table_name) == 0): print "Error: table '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(2, table_name) elif(objekt == "sequence"): # izvlacimo ime sequence-a table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li sequence if (ak47.AK_table_exist(table_name) == 0): print "Error: sequence '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(3, table_name) elif(objekt == "trigger"): # izvlacimo ime triggera table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li trigger if (ak47.AK_table_exist(table_name) == 0): print "Error: trigger '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(4, table_name) elif(objekt == "function"): # izvlacimo ime funkcije table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li funkcija if (ak47.AK_table_exist(table_name) == 0): print "Error: funkcija '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(5, table_name) elif(objekt == "user"): # izvlacimo ime usera table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li user if (ak47.AK_table_exist(table_name) == 0): print "Error: user '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(6, table_name) elif(objekt == "group"): # izvlacimo ime grupe table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li grupa if (ak47.AK_table_exist(table_name) == 0): print "Error: group '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(7, table_name) elif(objekt == "constraint"): # izvlacimo ime constrainta table_name = str(token.ime_objekta) table_name = table_name.translate(None, "'[]") # postoji li constraint if (ak47.AK_table_exist(table_name) == 0): print "Error: constraint '" + table_name + "' does not exist" return False ak47.AK_drop_test_helper(8, table_name)
def execute(self,expr): token = sql_tokenizer().AK_parse_where(expr) # Update table name table_name = str(token.tableName) if (ak47.AK_table_exist(table_name) == 0): print "Error: table '"+ table_name +"' does not exist" return False # Update values # update_attr_values = map(lambda x: x.replace("'",""),list(token.columnValues[0])) # Get table attribute list table_attr_names = str(ak47.AK_rel_eq_get_atrributes_char(table_name)).split(";") # Get table attribute type table_attr_types = str(ak47.AK_get_table_atribute_types(table_name)).split(";") # Attribute names for update update_attr_names = table_attr_names # WHERE condition condition = token.condition if (token.condition is not None) else '' # keep an eye on this test # Attributes for update if(token.columnNames): update_attr_names = [] table_types_temp = table_attr_types table_attr_types = [] update_columns = list(token.columnNames) for index,col in enumerate(update_columns): if col not in table_attr_names: print "\nError: table has no attribute " + str(col) + ":" akdbError(expr,col) return False # Check attributes for update for ic,col in enumerate(update_columns): for ia,tab in enumerate(table_attr_names): if col == tab: if tab not in update_attr_names: update_attr_names.append(tab) table_attr_types.append(int(table_types_temp[ia])) else: print "\nError: duplicate attribute " + tab + ":" akdbError(expr,tab) return False # UPDATE too many attributes if (len(update_attr_names) > len(table_attr_names)): print "\nError: too many attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False else: print "\nError: No attributes for for update!" return False # WHERE ... if (condition != ''): # condition attribute types condition_attr_types = map(lambda x: get_attr_type(x.replace("'","")),list(token.condition[1])) # Prepare update data element # This is Test Data! # Iteration required for more than one attribute! element = ak47.list_node() ak47.Ak_Init_L3(&element) ak47.Ak_DeleteAll_L3(&element) updateColumn = token.columnNames[0] whereColumn = token.condition[1][0] whereValue = token.condition[1][2] newValue = token.columnValues[0] if type(whereValue) == int: ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_INT, whereValue, table_name, updateColumn, element, 1) elif type(whereValue) == float: ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_FLOAT, whereValue, table_name, updateColumn, element, 1) #elif type(whereValue) == str: # ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_VARCHAR, whereValue, table_name, updateColumn, element, 1) if type(newValue) == int: ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_INT, newValue, table_name, whereColumn, element, 0) elif type(newValue) == float: ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_FLOAT, newValue, table_name, whereColumn, element, 0) #elif type(newValue) == str: #ak47.Ak_Insert_New_Element_For_Update(ak47.TYPE_VARCHAR, newValue, table_name, whereColumn, element, 0) #update_Row(table, column1, column2, key, new_value) #if(ak47.update_Row(table_name, 'weight', 'id_student', 1, 80) == EXIT_SUCCESS): #if(ak47.Ak_update_row(element) == ak47.EXIT_SUCCESS): #return True else: return False return False
def execute(self,expr): token = sql_tokenizer().AK_parse_where(expr) # Selection table name table_name = str(token.tableName) if (ak47.AK_table_exist(table_name) == 0): print "Error: table '"+ table_name +"' does not exist" return False # Get table attribute list table_attr_names = str(ak47.AK_rel_eq_get_atrributes_char(table_name)).split(";") # Get table attribute type table_attr_types = str(ak47.AK_get_table_atribute_types(table_name)).split(";") # Attribute names for selection (*) select_attr_names = table_attr_names # WHERE condition condition = token.condition if (token.condition is not None) else '' # keep an eye on this test # Expression expression = [] # Expression types expr_types = [] # Result table name (randomized) resultTable = "student" # Specific attributes for selection if(token.attributes): if(token.attributes[0] == '*'): table_types_temp = table_attr_types table_attr_types = [] for index,name in enumerate(select_attr_names): table_attr_types.append(int(table_types_temp[index])) expr_types.append(get_attr_type(table_types_temp[index])) else: select_attr_names = [] table_types_temp = table_attr_types table_attr_types = [] select_columns = list(token.attributes) for index,col in enumerate(select_columns): if col not in table_attr_names: print "\nError: table has no attribute " + str(col) + ":" akdbError(expr,col) return False # Check attributes for selection for ic,col in enumerate(select_columns): for ia,tab in enumerate(table_attr_names): if col == tab: if tab not in select_attr_names: select_attr_names.append(tab) table_attr_types.append(int(table_types_temp[ia])) expr_types.append(get_attr_type(table_types_temp[index])) else: print "\nError: duplicate attribute " + tab + ":" akdbError(expr,tab) return False # SELECT too many attributes if (len(select_attr_names) > len(table_attr_names)): print "\nError: too many attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False # SELECT * ... else: table_types_temp = table_attr_types table_attr_types = [] for index,name in enumerate(select_attr_names): table_attr_types.append(int(table_types_temp[index])) expr_types.append(get_attr_type(table_types_temp[index])) # WHERE ... if (condition != ''): # condition attribute types condition_attr_types = map(lambda x: get_attr_type(x.replace("'","")),list(token.condition.expression[0])) for cond in condition_attr_types: expr_types.append(cond) expression.append(expr) # TEST DATA! print expression expression = ["year", "1990", ">"] expr_types = [ak47.TYPE_ATTRIBS, ak47.TYPE_INT, ak47.TYPE_OPERATOR] #if(ak47.AK_selection(table_name, resultTable, expression) == EXIT_SUCCESS): if(ak47.selection_test(table_name, resultTable, expression, expr_types) == 1): return True else: return False return False
def execute(self): expr = self.matcher.group(0) parser = sql_tokenizer() token = parser.AK_parse_insert_into(expr) if isinstance(token, str): print "Error: syntax error in expression" print expr print token return False table_name = str(token.tableName) # postoji li tablica if (ak47.AK_table_exist(table_name) == 0): print "Error: table '"+ table_name +"' does not exist" return False # vrijednosti podataka za unos insert_attr_values = map(lambda x: x.replace("'",""),list(token.columnValues[0])) # tipovi podataka za unos insert_attr_types = map(lambda x: get_attr_type(x.replace("'","")),list(token.columnValues[0])) #Dohvatiti listu atributa tablice table_attr_names = str(ak47.AK_rel_eq_get_atrributes_char(table_name)).split(";") #Dohvatiti tipove atributa tablice table_attr_types = str(ak47.AK_get_table_atribute_types(table_name)).split(";") # imena atributa za unos insert_attr_names = table_attr_names # navedeni su atributi za unos if(token.columns): insert_attr_names = [] table_types_temp = table_attr_types table_attr_types = [] insert_columns = list(token.columns[0]) for index,col in enumerate(insert_columns): if col not in table_attr_names: print "\nError: table has no attribute '" + str(col) + "':" akdbError(expr,col) return False #provjera atributa za unos for ic,col in enumerate(insert_columns): for ia,tab in enumerate(table_attr_names): if col == tab: if tab not in insert_attr_names: insert_attr_names.append(tab) table_attr_types.append(int(table_types_temp[ia])) else: print "\nError: duplicate attribute " + tab + ":" akdbError(expr,tab) return False if (len(insert_columns) == len(insert_attr_values)): for index,tip in enumerate(insert_attr_types): if int(insert_attr_types[index]) != int(table_attr_types[index]): type_name = get_type_name(int(table_attr_types[index])) print "\nError: type error for attribute '" + insert_attr_names[index] + "':" akdbError(expr,insert_attr_values[index]) print "Expected: " + type_name return False else: print "\nError: attribute names number not matching attribute values number supplied for table '" + table_name + "':" akdbError(expr,insert_columns[0]) return False # navedene su samo vrijednosti za unos elif (len(table_attr_names) < len(insert_attr_values)): print "\nError: too many attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False elif (len(table_attr_names) > len(insert_attr_values)): print "\nError: too few attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False else: for index,tip in enumerate(insert_attr_types): if insert_attr_types[index] != int(table_attr_types[index]): type_name = get_type_name(int(table_attr_types[index])) print "\nError: type error for attribute '" + insert_attr_names[index] + "':" akdbError(expr,insert_attr_values[index]) print "Expected: " + type_name return False if(ak47.insert_data_test(table_name,insert_attr_names,insert_attr_values,insert_attr_types) == ak47.EXIT_SUCCESS): return True else: return False return False
from sql_tokenizer import * def get_attribute_tokens(tokens, attribute_name): return [token.get(attribute_name) for token in tokens] sql_tokenizer = sql_tokenizer() grant_commands = [ "GRANT SELECT, INSERT, UPDATE, DELETE ON album, song TO Elvis, Jimmy WITH ADMIN OPTION", "grant update on table1, table2 to Ivica, pero22foi1", "Grant insert on drivers to Hamilton, Raikkonen, Alonso" ] grant_tokens = [ sql_tokenizer.AK_parse_grant(cmd).asDict() for cmd in grant_commands ] def AK_parse_grant_test(): """ @author Boris Kisic @brief testing of sql parsing command GRANT @return No return value >>> get_attribute_tokens(grant_tokens, "privileges") [['SELECT', 'INSERT', 'UPDATE', 'DELETE'], ['update'], ['insert']] >>> get_attribute_tokens(grant_tokens, "tables") [['album', 'song'], ['table1', 'table2'], ['drivers']] >>> get_attribute_tokens(grant_tokens, "users") [['Elvis', 'Jimmy'], ['Ivica', 'pero22foi1'], ['Hamilton', 'Raikkonen', 'Alonso']]
def insert(self, expr): parser = sql_tokenizer() token = parser.AK_parse_insert_into(expr) if isinstance(token, str): print "Error: syntax error in expression" print expr print token return False table_name = str(token.tableName) # is there a table if (ak47.AK_table_exist(table_name) == 0): print "Error: table '" + table_name + "' does not exist" return False # data values for insert insert_attr_values = map(lambda x: x.replace( "'", ""), list(token.columnValues[0])) # data types for insert insert_attr_types = map(lambda x: get_attr_type( x.replace("'", "")), list(token.columnValues[0])) # get array of attributes for table table_attr_names = str( ak47.AK_rel_eq_get_atrributes_char(table_name)).split(";") # get attribute types for table table_attr_types = str( ak47.AK_get_table_atribute_types(table_name)).split(";") # attribute names for insert insert_attr_names = table_attr_names # attributes for insert if(token.columns): insert_attr_names = [] table_types_temp = table_attr_types table_attr_types = [] insert_columns = list(token.columns[0]) for index, col in enumerate(insert_columns): if col not in table_attr_names: print "\nError: table has no attribute '" + str(col) + "':" akdbError(expr, col) return False # check attributes for insert for ic, col in enumerate(insert_columns): for ia, tab in enumerate(table_attr_names): if col == tab: if tab not in insert_attr_names: insert_attr_names.append(tab) table_attr_types.append(int(table_types_temp[ia])) else: print "\nError: duplicate attribute " + tab + ":" akdbError(expr, tab) return False if (len(insert_columns) == len(insert_attr_values)): for index, tip in enumerate(insert_attr_types): if int(insert_attr_types[index]) != int(table_attr_types[index]): type_name = get_type_name(int(table_attr_types[index])) print "\nError: type error for attribute '" + insert_attr_names[index] + "':" akdbError(expr, insert_attr_values[index]) print "Expected: " + type_name return False else: print "\nError: attribute names number not matching attribute values number supplied for table '" + table_name + "':" akdbError(expr, insert_columns[0]) return False # values for insert elif (len(table_attr_names) < len(insert_attr_values)): print "\nError: too many attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False elif (len(table_attr_names) > len(insert_attr_values)): print "\nError: too few attibutes, table " + str(token.tableName) + " has " + str(len(table_attr_names)) return False else: for index, tip in enumerate(insert_attr_types): if insert_attr_types[index] != int(table_attr_types[index]): type_name = get_type_name(int(table_attr_types[index])) print "\nError: type error for attribute '" + insert_attr_names[index] + "':" akdbError(expr, insert_attr_values[index]) print "Expected: " + type_name return False if(ak47.insert_data_test(table_name, insert_attr_names, insert_attr_values, insert_attr_types) == ak47.EXIT_SUCCESS): return True else: return False return False