|
1 |
| -""" DIRAC Basic MySQL Class |
2 |
| - It provides access to the basic MySQL methods in a multithread-safe mode |
3 |
| - keeping used connections in a python Queue for further reuse. |
| 1 | +"""DIRAC Basic MySQL Class |
| 2 | +It provides access to the basic MySQL methods in a multithread-safe mode |
| 3 | +keeping used connections in a python Queue for further reuse. |
4 | 4 |
|
5 |
| - These are the coded methods: |
| 5 | +These are the coded methods: |
6 | 6 |
|
7 | 7 |
|
8 |
| - __init__( host, user, passwd, name, [maxConnsInQueue=10] ) |
| 8 | +__init__( host, user, passwd, name, [maxConnsInQueue=10] ) |
9 | 9 |
|
10 |
| - Initializes the Queue and tries to connect to the DB server, |
11 |
| - using the _connect method. |
12 |
| - "maxConnsInQueue" defines the size of the Queue of open connections |
13 |
| - that are kept for reuse. It also defined the maximum number of open |
14 |
| - connections available from the object. |
15 |
| - maxConnsInQueue = 0 means unlimited and it is not supported. |
| 10 | +Initializes the Queue and tries to connect to the DB server, |
| 11 | +using the _connect method. |
| 12 | +"maxConnsInQueue" defines the size of the Queue of open connections |
| 13 | +that are kept for reuse. It also defined the maximum number of open |
| 14 | +connections available from the object. |
| 15 | +maxConnsInQueue = 0 means unlimited and it is not supported. |
16 | 16 |
|
17 | 17 |
|
18 |
| - _except( methodName, exception, errorMessage ) |
| 18 | +_except( methodName, exception, errorMessage ) |
19 | 19 |
|
20 |
| - Helper method for exceptions: the "methodName" and the "errorMessage" |
21 |
| - are printed with ERROR level, then the "exception" is printed (with |
22 |
| - full description if it is a MySQL Exception) and S_ERROR is returned |
23 |
| - with the errorMessage and the exception. |
| 20 | +Helper method for exceptions: the "methodName" and the "errorMessage" |
| 21 | +are printed with ERROR level, then the "exception" is printed (with |
| 22 | +full description if it is a MySQL Exception) and S_ERROR is returned |
| 23 | +with the errorMessage and the exception. |
24 | 24 |
|
25 | 25 |
|
26 |
| - _connect() |
| 26 | +_connect() |
27 | 27 |
|
28 |
| - Attempts connection to DB and sets the _connected flag to True upon success. |
29 |
| - Returns S_OK or S_ERROR. |
| 28 | +Attempts connection to DB and sets the _connected flag to True upon success. |
| 29 | +Returns S_OK or S_ERROR. |
30 | 30 |
|
31 | 31 |
|
32 |
| - _query( cmd, [conn=conn] ) |
| 32 | +_query( cmd, [conn=conn] ) |
33 | 33 |
|
34 |
| - Executes SQL command "cmd". |
35 |
| - Gets a connection from the Queue (or open a new one if none is available), |
36 |
| - the used connection is back into the Queue. |
37 |
| - If a connection to the the DB is passed as second argument this connection |
38 |
| - is used and is not in the Queue. |
39 |
| - Returns S_OK with fetchall() out in Value or S_ERROR upon failure. |
| 34 | +Executes SQL command "cmd". |
| 35 | +Gets a connection from the Queue (or open a new one if none is available), |
| 36 | +the used connection is back into the Queue. |
| 37 | +If a connection to the the DB is passed as second argument this connection |
| 38 | +is used and is not in the Queue. |
| 39 | +Returns S_OK with fetchall() out in Value or S_ERROR upon failure. |
40 | 40 |
|
41 | 41 |
|
42 |
| - _update( cmd, [conn=conn] ) |
| 42 | +_update( cmd, [conn=conn] ) |
43 | 43 |
|
44 |
| - Executes SQL command "cmd" and issue a commit |
45 |
| - Gets a connection from the Queue (or open a new one if none is available), |
46 |
| - the used connection is back into the Queue. |
47 |
| - If a connection to the the DB is passed as second argument this connection |
48 |
| - is used and is not in the Queue |
49 |
| - Returns S_OK with number of updated registers in Value or S_ERROR upon failure. |
| 44 | +Executes SQL command "cmd" and issue a commit |
| 45 | +Gets a connection from the Queue (or open a new one if none is available), |
| 46 | +the used connection is back into the Queue. |
| 47 | +If a connection to the the DB is passed as second argument this connection |
| 48 | +is used and is not in the Queue |
| 49 | +Returns S_OK with number of updated registers in Value or S_ERROR upon failure. |
50 | 50 |
|
51 | 51 |
|
52 |
| - _createTables( tableDict ) |
| 52 | +_createTables( tableDict ) |
53 | 53 |
|
54 |
| - Create a new Table in the DB |
| 54 | +Create a new Table in the DB |
55 | 55 |
|
56 | 56 |
|
57 |
| - _getConnection() |
| 57 | +_getConnection() |
58 | 58 |
|
59 |
| - Gets a connection from the Queue (or open a new one if none is available) |
60 |
| - Returns S_OK with connection in Value or S_ERROR |
61 |
| - the calling method is responsible for closing this connection once it is no |
62 |
| - longer needed. |
| 59 | +Gets a connection from the Queue (or open a new one if none is available) |
| 60 | +Returns S_OK with connection in Value or S_ERROR |
| 61 | +the calling method is responsible for closing this connection once it is no |
| 62 | +longer needed. |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
|
66 | 66 |
|
67 |
| - Some high level methods have been added to avoid the need to write SQL |
68 |
| - statement in most common cases. They should be used instead of low level |
69 |
| - _insert, _update methods when ever possible. |
| 67 | +Some high level methods have been added to avoid the need to write SQL |
| 68 | +statement in most common cases. They should be used instead of low level |
| 69 | +_insert, _update methods when ever possible. |
70 | 70 |
|
71 |
| - buildCondition( self, condDict = None, older = None, newer = None, |
72 |
| - timeStamp = None, orderAttribute = None, limit = False, |
73 |
| - greater = None, smaller = None ): |
| 71 | +buildCondition( self, condDict = None, older = None, newer = None, |
| 72 | + timeStamp = None, orderAttribute = None, limit = False, |
| 73 | + greater = None, smaller = None ): |
74 | 74 |
|
75 |
| - Build SQL condition statement from provided condDict and other extra check on |
76 |
| - a specified time stamp. |
77 |
| - The conditions dictionary specifies for each attribute one or a List of possible |
78 |
| - values |
79 |
| - greater and smaller are dictionaries in which the keys are the names of the fields, |
80 |
| - that are requested to be >= or < than the corresponding value. |
81 |
| - For compatibility with current usage it uses Exceptions to exit in case of |
82 |
| - invalid arguments |
| 75 | + Build SQL condition statement from provided condDict and other extra check on |
| 76 | + a specified time stamp. |
| 77 | + The conditions dictionary specifies for each attribute one or a List of possible |
| 78 | + values |
| 79 | + greater and smaller are dictionaries in which the keys are the names of the fields, |
| 80 | + that are requested to be >= or < than the corresponding value. |
| 81 | + For compatibility with current usage it uses Exceptions to exit in case of |
| 82 | + invalid arguments |
83 | 83 |
|
84 | 84 |
|
85 |
| - insertFields( self, tableName, inFields = None, inValues = None, conn = None, inDict = None ): |
| 85 | +insertFields( self, tableName, inFields = None, inValues = None, conn = None, inDict = None ): |
86 | 86 |
|
87 |
| - Insert a new row in "tableName" assigning the values "inValues" to the |
88 |
| - fields "inFields". |
89 |
| - Alternatively inDict can be used |
90 |
| - String type values will be appropriately escaped. |
| 87 | + Insert a new row in "tableName" assigning the values "inValues" to the |
| 88 | + fields "inFields". |
| 89 | + Alternatively inDict can be used |
| 90 | + String type values will be appropriately escaped. |
91 | 91 |
|
92 | 92 |
|
93 |
| - updateFields( self, tableName, updateFields = None, updateValues = None, |
94 |
| - condDict = None, |
95 |
| - limit = False, conn = None, |
96 |
| - updateDict = None, |
97 |
| - older = None, newer = None, |
98 |
| - timeStamp = None, orderAttribute = None ): |
| 93 | +updateFields( self, tableName, updateFields = None, updateValues = None, |
| 94 | + condDict = None, |
| 95 | + limit = False, conn = None, |
| 96 | + updateDict = None, |
| 97 | + older = None, newer = None, |
| 98 | + timeStamp = None, orderAttribute = None ): |
99 | 99 |
|
100 |
| - Update "updateFields" from "tableName" with "updateValues". |
101 |
| - updateDict alternative way to provide the updateFields and updateValues |
102 |
| - N records can match the condition |
103 |
| - return S_OK( number of updated rows ) |
104 |
| - if limit is not False, the given limit is set |
105 |
| - String type values will be appropriately escaped. |
| 100 | + Update "updateFields" from "tableName" with "updateValues". |
| 101 | + updateDict alternative way to provide the updateFields and updateValues |
| 102 | + N records can match the condition |
| 103 | + return S_OK( number of updated rows ) |
| 104 | + if limit is not False, the given limit is set |
| 105 | + String type values will be appropriately escaped. |
106 | 106 |
|
107 | 107 |
|
108 |
| - deleteEntries( self, tableName, |
109 |
| - condDict = None, |
110 |
| - limit = False, conn = None, |
111 |
| - older = None, newer = None, |
112 |
| - timeStamp = None, orderAttribute = None ): |
113 |
| -
|
114 |
| - Delete rows from "tableName" with |
115 |
| - N records can match the condition |
116 |
| - if limit is not False, the given limit is set |
117 |
| - String type values will be appropriately escaped, they can be single values or lists of values. |
118 |
| -
|
119 |
| -
|
120 |
| - getFields( self, tableName, outFields = None, |
| 108 | +deleteEntries( self, tableName, |
121 | 109 | condDict = None,
|
122 | 110 | limit = False, conn = None,
|
123 | 111 | older = None, newer = None,
|
124 | 112 | timeStamp = None, orderAttribute = None ):
|
125 | 113 |
|
126 |
| - Select "outFields" from "tableName" with condDict |
127 |
| - N records can match the condition |
128 |
| - return S_OK( tuple(Field,Value) ) |
129 |
| - if limit is not False, the given limit is set |
130 |
| - String type values will be appropriately escaped, they can be single values or lists of values. |
| 114 | + Delete rows from "tableName" with |
| 115 | + N records can match the condition |
| 116 | + if limit is not False, the given limit is set |
| 117 | + String type values will be appropriately escaped, they can be single values or lists of values. |
| 118 | +
|
131 | 119 |
|
132 |
| - for compatibility with other methods condDict keyed argument is added |
| 120 | +getFields( self, tableName, outFields = None, |
| 121 | + condDict = None, |
| 122 | + limit = False, conn = None, |
| 123 | + older = None, newer = None, |
| 124 | + timeStamp = None, orderAttribute = None ): |
133 | 125 |
|
| 126 | + Select "outFields" from "tableName" with condDict |
| 127 | + N records can match the condition |
| 128 | + return S_OK( tuple(Field,Value) ) |
| 129 | + if limit is not False, the given limit is set |
| 130 | + String type values will be appropriately escaped, they can be single values or lists of values. |
134 | 131 |
|
135 |
| - getCounters( self, table, attrList, condDict = None, older = None, |
136 |
| - newer = None, timeStamp = None, connection = False ): |
| 132 | + for compatibility with other methods condDict keyed argument is added |
137 | 133 |
|
138 |
| - Count the number of records on each distinct combination of AttrList, selected |
139 |
| - with condition defined by condDict and time stamps |
140 | 134 |
|
| 135 | +getCounters( self, table, attrList, condDict = None, older = None, |
| 136 | + newer = None, timeStamp = None, connection = False ): |
141 | 137 |
|
142 |
| - getDistinctAttributeValues( self, table, attribute, condDict = None, older = None, |
143 |
| - newer = None, timeStamp = None, connection = False ): |
| 138 | + Count the number of records on each distinct combination of AttrList, selected |
| 139 | + with condition defined by condDict and time stamps |
144 | 140 |
|
145 |
| - Get distinct values of a table attribute under specified conditions |
| 141 | +
|
| 142 | +getDistinctAttributeValues( self, table, attribute, condDict = None, older = None, |
| 143 | + newer = None, timeStamp = None, connection = False ): |
| 144 | +
|
| 145 | + Get distinct values of a table attribute under specified conditions |
146 | 146 |
|
147 | 147 |
|
148 | 148 | """
|
| 149 | + |
149 | 150 | import collections
|
150 | 151 | import functools
|
151 | 152 | import json
|
|
0 commit comments