Example #1
0
 def parse_insert_into(self, statement):
     #remove last ';'
     statement = statement[:-1]
     tokens = statement.split(None, 4)
     table_name = tokens[2].replace(u'`',u'')
     working_table = None
     for tbl in self.tables:
         if tbl.identifier == table_name:
             working_table = tbl
     values = split_statements(tokens[4])
     for value in values:
         field_values = split_values(value[1:-1])
         idx = 0
         row = dbtypes.TableRow()
         for field in working_table.fields:
             field_val = dbtypes.FieldValue()
             field_val.field = field
             field_val.value = field_values[idx]
             idx += 1
             row.values.append(field_val)
         working_table.rows.append(row)
Example #2
0
 def parse_create_table(self, stmt):
     table = dbtypes.Table()
     pretty_statement = sqlparse.format(stmt, reindent=True, keyword_case = 'upper', strip_comments = True)
     keywords_len = len("CREATE TABLE")
     statement = pretty_statement[keywords_len+1:].lstrip(u' ')
     with_parenthesis = False
     if statement.find(u'('):
         with_parenthesis = True
     if with_parenthesis:
         table.identifier = statement[:statement.find('(')].strip().replace(u'`',u'')
     else:
         table.identifier = statement[:statement.find(';')].strip().replace(u'`',u'')
     
     parenthesis_stmts = split_statements(statement[statement.find('(')+1:statement.rfind(')')])
     for pstmt in parenthesis_stmts:
         pstmt = pstmt.strip()
         type = self.get_pstatement_type(pstmt)
         if type == "field":
             field = dbtypes.Field()
             field.name = pstmt.split()[0].replace(u'`',u'')
             field.type = pstmt.split()[1]
             if len(pstmt.split()) > 2:
                 field.attrs = " ".join(pstmt.split()[2:])
             table.fields.append(self.fix_field(field))
         elif type == "primary":
             index = dbtypes.Index()
             field_names = pstmt.split()[2].replace(u'`',u'')[1:-1].split(',')
             self.find_fields_by_name(table.fields, field_names, index.fields)
             index.type = "primary"
             table.indexes.append(index)
         elif type == "index":
             index = dbtypes.Index()
             index.name = pstmt.split()[1].replace(u'`',u'')
             field_names = pstmt.split()[2].replace(u'`',u'')[1:-1].split(',')
             self.find_fields_by_name(table.fields, field_names, index.fields)
             index.type = "key"
             table.indexes.append(index)
         elif type == "unique":
             index = dbtypes.Index()
             index.name = pstmt.split()[2].replace(u'`',u'')
             field_names = pstmt.split()[3].replace(u'`',u'')[1:-1].split(',')
             self.find_fields_by_name(table.fields, field_names, index.fields)
             index.type = "unique"
             table.indexes.append(index)
         elif type == "constraint":
             constraint = dbtypes.Constraint()
             tokens = pstmt.split(None, 8)
             constraint.name = tokens[1].replace(u'`',u'')
             field_names = tokens[4].replace(u'`',u'')[1:-1].split(',')
             self.find_fields_by_name(table.fields, field_names, constraint.index_fields)
             constraint.ref_name = tokens[6].replace(u'`',u'')
             constraint.ref_fields = tokens[7].replace(u'`',u'')[1:-1].split(',')
             constraint.actions = tokens[8]
             table.constraints.append(constraint)
     
     table.attrs = statement[statement.rfind(')') + 1:-1]
     if "AUTO_INCREMENT" in table.attrs:
         for field in table.fields:
             if field.auto_increment:
                 idx = table.attrs.find("AUTO_INCREMENT") + len("AUTO_INCREMENT")
                 idx += 1
                 field.auto_increment_start = int(table.attrs[idx: idx + table.attrs[idx:].find(" ")])
                 break
     return table