]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # This file is used to create/update/query/erase a common table\r | |
3 | #\r | |
f7496d71 | 4 | # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r |
2e351cbe | 5 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r |
30fdf114 LG |
6 | #\r |
7 | \r | |
8 | ##\r | |
9 | # Import Modules\r | |
10 | #\r | |
11 | import Common.EdkLogger as EdkLogger\r | |
12 | \r | |
13 | ## TableFile\r | |
14 | #\r | |
15 | # This class defined a common table\r | |
f7496d71 | 16 | #\r |
30fdf114 LG |
17 | # @param object: Inherited from object class\r |
18 | #\r | |
19 | # @param Cursor: Cursor of the database\r | |
20 | # @param TableName: Name of the table\r | |
21 | #\r | |
22 | class Table(object):\r | |
23 | def __init__(self, Cursor):\r | |
24 | self.Cur = Cursor\r | |
25 | self.Table = ''\r | |
26 | self.ID = 0\r | |
f7496d71 | 27 | \r |
30fdf114 LG |
28 | ## Create table\r |
29 | #\r | |
30 | # Create a table\r | |
31 | #\r | |
32 | def Create(self, SqlCommand):\r | |
33 | self.Cur.execute(SqlCommand)\r | |
34 | self.ID = 0\r | |
35 | EdkLogger.verbose(SqlCommand + " ... DONE!")\r | |
36 | \r | |
37 | ## Insert table\r | |
38 | #\r | |
39 | # Insert a record into a table\r | |
40 | #\r | |
41 | def Insert(self, SqlCommand):\r | |
42 | self.Exec(SqlCommand)\r | |
f7496d71 | 43 | \r |
30fdf114 LG |
44 | ## Query table\r |
45 | #\r | |
46 | # Query all records of the table\r | |
f7496d71 | 47 | #\r |
30fdf114 | 48 | def Query(self):\r |
fb0b35e0 | 49 | EdkLogger.verbose("\nQuery table %s started ..." % self.Table)\r |
30fdf114 LG |
50 | SqlCommand = """select * from %s""" % self.Table\r |
51 | self.Cur.execute(SqlCommand)\r | |
52 | for Rs in self.Cur:\r | |
53 | EdkLogger.verbose(str(Rs))\r | |
f7496d71 | 54 | \r |
30fdf114 LG |
55 | TotalCount = self.GetCount()\r |
56 | EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) )\r | |
57 | EdkLogger.verbose("Query tabel %s DONE!" % self.Table)\r | |
58 | \r | |
59 | ## Drop a table\r | |
60 | #\r | |
61 | # Drop the table\r | |
62 | #\r | |
63 | def Drop(self):\r | |
64 | SqlCommand = """drop table IF EXISTS %s""" % self.Table\r | |
65 | self.Cur.execute(SqlCommand)\r | |
66 | EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table)\r | |
f7496d71 | 67 | \r |
30fdf114 LG |
68 | ## Get count\r |
69 | #\r | |
70 | # Get a count of all records of the table\r | |
71 | #\r | |
72 | # @retval Count: Total count of all records\r | |
73 | #\r | |
74 | def GetCount(self):\r | |
75 | SqlCommand = """select count(ID) from %s""" % self.Table\r | |
76 | self.Cur.execute(SqlCommand)\r | |
77 | for Item in self.Cur:\r | |
78 | return Item[0]\r | |
f7496d71 | 79 | \r |
30fdf114 LG |
80 | ## Generate ID\r |
81 | #\r | |
82 | # Generate an ID if input ID is -1\r | |
83 | #\r | |
f7496d71 | 84 | # @param ID: Input ID\r |
30fdf114 LG |
85 | #\r |
86 | # @retval ID: New generated ID\r | |
87 | #\r | |
88 | def GenerateID(self, ID):\r | |
89 | if ID == -1:\r | |
90 | self.ID = self.ID + 1\r | |
91 | \r | |
92 | return self.ID\r | |
f7496d71 | 93 | \r |
30fdf114 LG |
94 | ## Init the ID of the table\r |
95 | #\r | |
96 | # Init the ID of the table\r | |
97 | #\r | |
98 | def InitID(self):\r | |
99 | self.ID = self.GetCount()\r | |
f7496d71 | 100 | \r |
30fdf114 LG |
101 | ## Exec\r |
102 | #\r | |
103 | # Exec Sql Command, return result\r | |
104 | #\r | |
105 | # @param SqlCommand: The SqlCommand to be executed\r | |
106 | #\r | |
107 | # @retval RecordSet: The result after executed\r | |
108 | #\r | |
109 | def Exec(self, SqlCommand):\r | |
110 | EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand)\r | |
111 | self.Cur.execute(SqlCommand)\r | |
112 | RecordSet = self.Cur.fetchall()\r | |
113 | EdkLogger.debug(4, "RecordSet: %s" % RecordSet)\r | |
114 | return RecordSet\r |