Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Chetan Gaonker | 41d2e07 | 2016-03-15 16:41:31 -0700 | [diff] [blame] | 16 | import sqlite3 |
| 17 | import sys |
| 18 | |
| 19 | class SubscriberDB: |
| 20 | def __init__(self, db = 'subscriber.db', create = False): |
| 21 | self.db = db |
| 22 | self.con = sqlite3.connect(db) |
| 23 | self.con.row_factory = sqlite3.Row |
| 24 | self.cur = self.con.cursor() |
A R Karthick | def103b | 2016-05-17 09:49:37 -0700 | [diff] [blame^] | 25 | self.services = [ 'TLS', 'DHCP', 'IGMP' ] |
Chetan Gaonker | 41d2e07 | 2016-03-15 16:41:31 -0700 | [diff] [blame] | 26 | self.create = create |
| 27 | if create == True: |
| 28 | self.cur.execute("DROP TABLE IF EXISTS Subscriber") |
| 29 | self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);") |
| 30 | |
| 31 | def load(self, name, service): |
| 32 | self.cur.execute("INSERT INTO Subscriber(Name, Service) VALUES (?, ?);", (name, service)) |
| 33 | |
| 34 | def commit(self): |
| 35 | self.con.commit() |
| 36 | |
| 37 | def generate(self, num = 100): |
| 38 | #create db if not created |
| 39 | if self.create is False: |
| 40 | self.cur.execute("DROP TABLE IF EXISTS Subscriber") |
| 41 | self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);") |
| 42 | self.create = True |
| 43 | service = ' '.join(self.services) |
| 44 | for i in xrange(num): |
| 45 | name = "sub%d" %self.lastrowid() |
| 46 | self.load(name, service) |
| 47 | self.commit() |
| 48 | |
| 49 | def read(self, num = 1000000, debug = False): |
| 50 | self.cur.execute("SELECT * FROM Subscriber LIMIT ?;", (num,)) |
| 51 | rows = self.cur.fetchall() |
| 52 | if debug is True: |
| 53 | for row in rows: |
| 54 | print('Id %d, Name %s, Service %s' %(row['Id'], row['Name'], row['Service'])) |
| 55 | return rows |
| 56 | |
| 57 | def lastrowid(self): |
| 58 | return 0 if self.cur.lastrowid == None else self.cur.lastrowid |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | create = False |
| 62 | if len(sys.argv) > 1: |
| 63 | try: |
| 64 | num_subscribers = int(sys.argv[1]) |
| 65 | except: |
| 66 | num_subscribers = 100 |
| 67 | print('Creating %d subscriber records' %num_subscribers) |
| 68 | create = True |
| 69 | sub = SubscriberDB(create = create) |
| 70 | if create == True: |
| 71 | sub.generate(num_subscribers) |
| 72 | else: |
| 73 | num_subscribers = 10 |
| 74 | subscribers = sub.read(num_subscribers) |
| 75 | for s in subscribers: |
| 76 | print('Name %s, Service %s' %(s['Name'], s['Service'])) |