]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Table/Table.py
BaseTools: Various typo
[mirror_edk2.git] / BaseTools / Source / Python / Table / Table.py
CommitLineData
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
40d841f6 5# This program and the accompanying materials\r
30fdf114
LG
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14##\r
15# Import Modules\r
16#\r
17import Common.EdkLogger as EdkLogger\r
18\r
19## TableFile\r
20#\r
21# This class defined a common table\r
f7496d71 22#\r
30fdf114
LG
23# @param object: Inherited from object class\r
24#\r
25# @param Cursor: Cursor of the database\r
26# @param TableName: Name of the table\r
27#\r
28class Table(object):\r
29 def __init__(self, Cursor):\r
30 self.Cur = Cursor\r
31 self.Table = ''\r
32 self.ID = 0\r
f7496d71 33\r
30fdf114
LG
34 ## Create table\r
35 #\r
36 # Create a table\r
37 #\r
38 def Create(self, SqlCommand):\r
39 self.Cur.execute(SqlCommand)\r
40 self.ID = 0\r
41 EdkLogger.verbose(SqlCommand + " ... DONE!")\r
42\r
43 ## Insert table\r
44 #\r
45 # Insert a record into a table\r
46 #\r
47 def Insert(self, SqlCommand):\r
48 self.Exec(SqlCommand)\r
f7496d71 49\r
30fdf114
LG
50 ## Query table\r
51 #\r
52 # Query all records of the table\r
f7496d71 53 #\r
30fdf114 54 def Query(self):\r
fb0b35e0 55 EdkLogger.verbose("\nQuery table %s started ..." % self.Table)\r
30fdf114
LG
56 SqlCommand = """select * from %s""" % self.Table\r
57 self.Cur.execute(SqlCommand)\r
58 for Rs in self.Cur:\r
59 EdkLogger.verbose(str(Rs))\r
f7496d71 60\r
30fdf114
LG
61 TotalCount = self.GetCount()\r
62 EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) )\r
63 EdkLogger.verbose("Query tabel %s DONE!" % self.Table)\r
64\r
65 ## Drop a table\r
66 #\r
67 # Drop the table\r
68 #\r
69 def Drop(self):\r
70 SqlCommand = """drop table IF EXISTS %s""" % self.Table\r
71 self.Cur.execute(SqlCommand)\r
72 EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table)\r
f7496d71 73\r
30fdf114
LG
74 ## Get count\r
75 #\r
76 # Get a count of all records of the table\r
77 #\r
78 # @retval Count: Total count of all records\r
79 #\r
80 def GetCount(self):\r
81 SqlCommand = """select count(ID) from %s""" % self.Table\r
82 self.Cur.execute(SqlCommand)\r
83 for Item in self.Cur:\r
84 return Item[0]\r
f7496d71 85\r
30fdf114
LG
86 ## Generate ID\r
87 #\r
88 # Generate an ID if input ID is -1\r
89 #\r
f7496d71 90 # @param ID: Input ID\r
30fdf114
LG
91 #\r
92 # @retval ID: New generated ID\r
93 #\r
94 def GenerateID(self, ID):\r
95 if ID == -1:\r
96 self.ID = self.ID + 1\r
97\r
98 return self.ID\r
f7496d71 99\r
30fdf114
LG
100 ## Init the ID of the table\r
101 #\r
102 # Init the ID of the table\r
103 #\r
104 def InitID(self):\r
105 self.ID = self.GetCount()\r
f7496d71 106\r
30fdf114
LG
107 ## Exec\r
108 #\r
109 # Exec Sql Command, return result\r
110 #\r
111 # @param SqlCommand: The SqlCommand to be executed\r
112 #\r
113 # @retval RecordSet: The result after executed\r
114 #\r
115 def Exec(self, SqlCommand):\r
116 EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand)\r
117 self.Cur.execute(SqlCommand)\r
118 RecordSet = self.Cur.fetchall()\r
119 EdkLogger.debug(4, "RecordSet: %s" % RecordSet)\r
120 return RecordSet\r