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.
44
5- These are the coded methods:
5+ These are the coded methods:
66
77
8- __init__( host, user, passwd, name, [maxConnsInQueue=10] )
8+ __init__( host, user, passwd, name, [maxConnsInQueue=10] )
99
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.
1616
1717
18- _except( methodName, exception, errorMessage )
18+ _except( methodName, exception, errorMessage )
1919
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.
2424
2525
26- _connect()
26+ _connect()
2727
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.
3030
3131
32- _query( cmd, [conn=conn] )
32+ _query( cmd, [conn=conn] )
3333
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.
4040
4141
42- _update( cmd, [conn=conn] )
42+ _update( cmd, [conn=conn] )
4343
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.
5050
5151
52- _createTables( tableDict )
52+ _createTables( tableDict )
5353
54- Create a new Table in the DB
54+ Create a new Table in the DB
5555
5656
57- _getConnection()
57+ _getConnection()
5858
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.
6363
6464
6565
6666
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.
7070
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 ):
7474
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
8383
8484
85- insertFields( self, tableName, inFields = None, inValues = None, conn = None, inDict = None ):
85+ insertFields( self, tableName, inFields = None, inValues = None, conn = None, inDict = None ):
8686
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.
9191
9292
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 ):
9999
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.
106106
107107
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,
121109 condDict = None,
122110 limit = False, conn = None,
123111 older = None, newer = None,
124112 timeStamp = None, orderAttribute = None ):
125113
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+
119+
120+ getFields( self, tableName, outFields = None,
121+ condDict = None,
122+ limit = False, conn = None,
123+ older = None, newer = None,
124+ timeStamp = None, orderAttribute = None ):
131125
132- for compatibility with other methods condDict keyed argument is added
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.
133131
132+ for compatibility with other methods condDict keyed argument is added
134133
135- getCounters( self, table, attrList, condDict = None, older = None,
136- newer = None, timeStamp = None, connection = False ):
137134
138- Count the number of records on each distinct combination of AttrList, selected
139- with condition defined by condDict and time stamps
135+ getCounters( self, table, attrList, condDict = None, older = None,
136+ newer = None, timeStamp = None, connection = False ):
140137
138+ Count the number of records on each distinct combination of AttrList, selected
139+ with condition defined by condDict and time stamps
141140
142- getDistinctAttributeValues( self, table, attribute, condDict = None, older = None,
143- newer = None, timeStamp = None, connection = False ):
144141
145- Get distinct values of a table attribute under specified conditions
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
146146
147147
148148"""
149+
149150import collections
150151import functools
151152import json
@@ -760,6 +761,7 @@ def _update(self, cmd, *, args=None, conn=None, debug=True):
760761
761762 :return: S_OK with number of updated registers upon success.
762763 S_ERROR upon error.
764+ lastRowId: if set, added to the returned dictionary
763765 """
764766
765767 self .log .debug (f"_update: { self ._safeCmd (cmd )} " )
@@ -774,9 +776,11 @@ def _update(self, cmd, *, args=None, conn=None, debug=True):
774776 try :
775777 cursor = connection .cursor ()
776778 res = cursor .execute (cmd , args = args )
779+
777780 retDict = S_OK (res )
778781 if cursor .lastrowid :
779782 retDict ["lastRowId" ] = cursor .lastrowid
783+
780784 except Exception as x :
781785 retDict = self ._except ("_update" , x , "Execution failed." , cmd , debug )
782786
0 commit comments