blob: d6b3f755d25693a43b1041905d9bfebcf8952f6b [file] [log] [blame]
Chetan Gaonker41d2e072016-03-15 16:41:31 -07001import sqlite3
2import sys
3
4class SubscriberDB:
5 def __init__(self, db = 'subscriber.db', create = False):
6 self.db = db
7 self.con = sqlite3.connect(db)
8 self.con.row_factory = sqlite3.Row
9 self.cur = self.con.cursor()
Chetan Gaonker83ac4212016-03-22 15:36:31 -070010 self.services = [ 'DHCP', 'IGMP' ]
Chetan Gaonker41d2e072016-03-15 16:41:31 -070011 self.create = create
12 if create == True:
13 self.cur.execute("DROP TABLE IF EXISTS Subscriber")
14 self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
15
16 def load(self, name, service):
17 self.cur.execute("INSERT INTO Subscriber(Name, Service) VALUES (?, ?);", (name, service))
18
19 def commit(self):
20 self.con.commit()
21
22 def generate(self, num = 100):
23 #create db if not created
24 if self.create is False:
25 self.cur.execute("DROP TABLE IF EXISTS Subscriber")
26 self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
27 self.create = True
28 service = ' '.join(self.services)
29 for i in xrange(num):
30 name = "sub%d" %self.lastrowid()
31 self.load(name, service)
32 self.commit()
33
34 def read(self, num = 1000000, debug = False):
35 self.cur.execute("SELECT * FROM Subscriber LIMIT ?;", (num,))
36 rows = self.cur.fetchall()
37 if debug is True:
38 for row in rows:
39 print('Id %d, Name %s, Service %s' %(row['Id'], row['Name'], row['Service']))
40 return rows
41
42 def lastrowid(self):
43 return 0 if self.cur.lastrowid == None else self.cur.lastrowid
44
45if __name__ == "__main__":
46 create = False
47 if len(sys.argv) > 1:
48 try:
49 num_subscribers = int(sys.argv[1])
50 except:
51 num_subscribers = 100
52 print('Creating %d subscriber records' %num_subscribers)
53 create = True
54 sub = SubscriberDB(create = create)
55 if create == True:
56 sub.generate(num_subscribers)
57 else:
58 num_subscribers = 10
59 subscribers = sub.read(num_subscribers)
60 for s in subscribers:
61 print('Name %s, Service %s' %(s['Name'], s['Service']))