sqlmap/lib/core/replication.py

92 lines
2.9 KiB
Python
Raw Normal View History

2010-03-26 23:23:08 +03:00
#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2010-03-26 23:23:08 +03:00
"""
import sqlite3
2010-03-26 23:19:18 +03:00
class Replication:
2010-03-27 00:30:36 +03:00
"""
This class holds all methods/classes used for database
replication purposes.
"""
2010-09-24 17:19:35 +04:00
2010-03-26 23:19:18 +03:00
def __init__(self, dbpath):
self.dbpath = dbpath
self.connection = sqlite3.connect(dbpath)
self.connection.isolation_level = None
self.cursor = self.connection.cursor()
2010-09-24 17:19:35 +04:00
2010-03-26 23:19:18 +03:00
class DataType:
2010-03-27 00:30:36 +03:00
"""
Using this class we define auxiliary objects
used for representing sqlite data types.
"""
2010-09-24 17:19:35 +04:00
2010-03-26 23:19:18 +03:00
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def __repr__(self):
return "<DataType: %s>" % self
2010-09-24 17:19:35 +04:00
2010-03-26 23:19:18 +03:00
class Table:
2010-03-27 00:30:36 +03:00
"""
This class defines methods used to manipulate table objects.
"""
2010-09-24 17:19:35 +04:00
2010-03-26 23:51:55 +03:00
def __init__(self, parent, name, columns=None, create=True, typeless=False):
2010-03-26 23:19:18 +03:00
self.parent = parent
self.name = name
self.columns = columns
2010-03-26 23:51:55 +03:00
if create:
self.parent.cursor.execute('DROP TABLE IF EXISTS %s' % self.name)
if not typeless:
self.parent.cursor.execute('CREATE TABLE %s (%s)' % (self.name, ','.join('%s %s' % (colname, coltype) for colname, coltype in self.columns)))
else:
self.parent.cursor.execute('CREATE TABLE %s (%s)' % (self.name, ','.join(colname for colname in self.columns)))
2010-09-24 17:19:35 +04:00
def insert(self, values):
2010-03-27 00:30:36 +03:00
"""
This function is used for inserting row(s) into current table.
"""
2010-09-24 17:19:35 +04:00
self.parent.cursor.execute('INSERT INTO %s VALUES (%s)' % (self.name, ','.join(['?']*len(values))), values)
2010-03-26 23:19:18 +03:00
2010-03-27 00:30:36 +03:00
def select(self, condition=None):
"""
This function is used for selecting row(s) from current table.
2010-09-24 17:19:35 +04:00
"""
2010-03-27 00:30:36 +03:00
stmt = 'SELECT * FROM %s' % self.name
if condition:
stmt += 'WHERE %s' % condition
return self.parent.cursor.execute(stmt)
2010-03-26 23:19:18 +03:00
2010-03-27 00:30:36 +03:00
# sqlite data types
NULL = DataType('NULL')
2010-03-26 23:19:18 +03:00
INTEGER = DataType('INTEGER')
2010-03-27 00:30:36 +03:00
REAL = DataType('REAL')
TEXT = DataType('TEXT')
BLOB = DataType('BLOB')
2010-09-24 17:19:35 +04:00
def createTable(self, tblname, columns=None, typeless=False):
2010-03-27 00:30:36 +03:00
"""
This function creates Table instance with current connection settings.
"""
2010-09-24 17:19:35 +04:00
return Replication.Table(parent=self, name=tblname, columns=columns, typeless=typeless)
2010-03-27 00:30:36 +03:00
def dropTable(self, tblname):
"""
This function drops table with given name using current connection.
"""
self.cursor.execute('DROP TABLE IF EXISTS %s' % tblname)
2010-09-24 17:19:35 +04:00
2010-03-26 23:19:18 +03:00
def __del__(self):
self.cursor.close()
self.connection.close()