Skip to content
This repository was archived by the owner on Nov 7, 2020. It is now read-only.

Commit 054223b

Browse files
committed
Initial Import for SVN
0 parents  commit 054223b

21 files changed

+2054
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.svn*
2+
.project
3+
.pydevproject

LICENSE

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5+
Everyone is permitted to copy and distribute verbatim copies
6+
of this license document, but changing it is not allowed.
7+
8+
9+
This version of the GNU Lesser General Public License incorporates
10+
the terms and conditions of version 3 of the GNU General Public
11+
License, supplemented by the additional permissions listed below.
12+
13+
0. Additional Definitions.
14+
15+
As used herein, "this License" refers to version 3 of the GNU Lesser
16+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
17+
General Public License.
18+
19+
"The Library" refers to a covered work governed by this License,
20+
other than an Application or a Combined Work as defined below.
21+
22+
An "Application" is any work that makes use of an interface provided
23+
by the Library, but which is not otherwise based on the Library.
24+
Defining a subclass of a class defined by the Library is deemed a mode
25+
of using an interface provided by the Library.
26+
27+
A "Combined Work" is a work produced by combining or linking an
28+
Application with the Library. The particular version of the Library
29+
with which the Combined Work was made is also called the "Linked
30+
Version".
31+
32+
The "Minimal Corresponding Source" for a Combined Work means the
33+
Corresponding Source for the Combined Work, excluding any source code
34+
for portions of the Combined Work that, considered in isolation, are
35+
based on the Application, and not on the Linked Version.
36+
37+
The "Corresponding Application Code" for a Combined Work means the
38+
object code and/or source code for the Application, including any data
39+
and utility programs needed for reproducing the Combined Work from the
40+
Application, but excluding the System Libraries of the Combined Work.
41+
42+
1. Exception to Section 3 of the GNU GPL.
43+
44+
You may convey a covered work under sections 3 and 4 of this License
45+
without being bound by section 3 of the GNU GPL.
46+
47+
2. Conveying Modified Versions.
48+
49+
If you modify a copy of the Library, and, in your modifications, a
50+
facility refers to a function or data to be supplied by an Application
51+
that uses the facility (other than as an argument passed when the
52+
facility is invoked), then you may convey a copy of the modified
53+
version:
54+
55+
a) under this License, provided that you make a good faith effort to
56+
ensure that, in the event an Application does not supply the
57+
function or data, the facility still operates, and performs
58+
whatever part of its purpose remains meaningful, or
59+
60+
b) under the GNU GPL, with none of the additional permissions of
61+
this License applicable to that copy.
62+
63+
3. Object Code Incorporating Material from Library Header Files.
64+
65+
The object code form of an Application may incorporate material from
66+
a header file that is part of the Library. You may convey such object
67+
code under terms of your choice, provided that, if the incorporated
68+
material is not limited to numerical parameters, data structure
69+
layouts and accessors, or small macros, inline functions and templates
70+
(ten or fewer lines in length), you do both of the following:
71+
72+
a) Give prominent notice with each copy of the object code that the
73+
Library is used in it and that the Library and its use are
74+
covered by this License.
75+
76+
b) Accompany the object code with a copy of the GNU GPL and this license
77+
document.
78+
79+
4. Combined Works.
80+
81+
You may convey a Combined Work under terms of your choice that,
82+
taken together, effectively do not restrict modification of the
83+
portions of the Library contained in the Combined Work and reverse
84+
engineering for debugging such modifications, if you also do each of
85+
the following:
86+
87+
a) Give prominent notice with each copy of the Combined Work that
88+
the Library is used in it and that the Library and its use are
89+
covered by this License.
90+
91+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
92+
document.
93+
94+
c) For a Combined Work that displays copyright notices during
95+
execution, include the copyright notice for the Library among
96+
these notices, as well as a reference directing the user to the
97+
copies of the GNU GPL and this license document.
98+
99+
d) Do one of the following:
100+
101+
0) Convey the Minimal Corresponding Source under the terms of this
102+
License, and the Corresponding Application Code in a form
103+
suitable for, and under terms that permit, the user to
104+
recombine or relink the Application with a modified version of
105+
the Linked Version to produce a modified Combined Work, in the
106+
manner specified by section 6 of the GNU GPL for conveying
107+
Corresponding Source.
108+
109+
1) Use a suitable shared library mechanism for linking with the
110+
Library. A suitable mechanism is one that (a) uses at run time
111+
a copy of the Library already present on the user's computer
112+
system, and (b) will operate properly with a modified version
113+
of the Library that is interface-compatible with the Linked
114+
Version.
115+
116+
e) Provide Installation Information, but only if you would otherwise
117+
be required to provide such information under section 6 of the
118+
GNU GPL, and only to the extent that such information is
119+
necessary to install and execute a modified version of the
120+
Combined Work produced by recombining or relinking the
121+
Application with a modified version of the Linked Version. (If
122+
you use option 4d0, the Installation Information must accompany
123+
the Minimal Corresponding Source and Corresponding Application
124+
Code. If you use option 4d1, you must provide the Installation
125+
Information in the manner specified by section 6 of the GNU GPL
126+
for conveying Corresponding Source.)
127+
128+
5. Combined Libraries.
129+
130+
You may place library facilities that are a work based on the
131+
Library side by side in a single library together with other library
132+
facilities that are not Applications and are not covered by this
133+
License, and convey such a combined library under terms of your
134+
choice, if you do both of the following:
135+
136+
a) Accompany the combined library with a copy of the same work based
137+
on the Library, uncombined with any other library facilities,
138+
conveyed under the terms of this License.
139+
140+
b) Give prominent notice with the combined library that part of it
141+
is a work based on the Library, and explaining where to find the
142+
accompanying uncombined form of the same work.
143+
144+
6. Revised Versions of the GNU Lesser General Public License.
145+
146+
The Free Software Foundation may publish revised and/or new versions
147+
of the GNU Lesser General Public License from time to time. Such new
148+
versions will be similar in spirit to the present version, but may
149+
differ in detail to address new problems or concerns.
150+
151+
Each version is given a distinguishing version number. If the
152+
Library as you received it specifies that a certain numbered version
153+
of the GNU Lesser General Public License "or any later version"
154+
applies to it, you have the option of following the terms and
155+
conditions either of that published version or of any later version
156+
published by the Free Software Foundation. If the Library as you
157+
received it does not specify a version number of the GNU Lesser
158+
General Public License, you may choose any version of the GNU Lesser
159+
General Public License ever published by the Free Software Foundation.
160+
161+
If the Library as you received it specifies that a proxy can decide
162+
whether future versions of the GNU Lesser General Public License shall
163+
apply, that proxy's public statement of acceptance of any version is
164+
permanent authorization for you to choose that version for the
165+
Library.

PySQLPool/ChangeLog

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
0.3.6
2+
* Started Sphinx/RST style Documentation
3+
* Added LICENSE File
4+
* Added README File
5+
* Updated Email in setup.py
6+
* Added favicon to docs
7+
* Added ChangeLog
8+
* Fixes to Thread Locking Contention
9+
* Improved Hash Generation for Pool Key
10+
* Cleaned up Import Statements
11+
* Starts to future improvements to thread based MySQL transactions
12+
* Improved Python2.4 Support
13+
* Added returns of affect rows to queryMulti and queryMany
14+
* Added alias's for executeMulti, executeMany, execute, executeOne matching queryMulti, queryMany, query, queryOne respectfully
15+
* Added support for use of with. ie Content Management. Submitted by Denis Malinovsky

PySQLPool/MANIFEST

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setup.py
2+
PySQLPool/PySQLConnection.py
3+
PySQLPool/PySQLPool.py
4+
PySQLPool/PySQLQuery.py
5+
PySQLPool/__init__.py
+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
"""
2+
@author: Nick Verbeck
3+
@since: 5/12/2008
4+
"""
5+
import MySQLdb
6+
import datetime
7+
from threading import Semaphore
8+
9+
try:
10+
from hashlib import md5
11+
except Exception, e:
12+
from md5 import md5
13+
14+
class PySQLConnection(object):
15+
"""
16+
Command Pattern Object to store connection information for use in PySQLPool
17+
18+
@author: Nick Verbeck
19+
@since: 5/12/2008
20+
@version: 0.1
21+
"""
22+
23+
def __init__(self, *args, **kargs):
24+
"""
25+
Constructor for the PySQLConnection class
26+
@param commitOnEnd: Default False, When query is complete do you wish to auto commit. This is a always on for this connection
27+
@author: Nick Verbeck
28+
@since: 5/12/2008
29+
@updated: 7/19/2008 - Added commitOnEnd
30+
@updated: 10/26/2008 - Switched to use *args and **kargs
31+
"""
32+
self.info = {
33+
'host': 'localhost',
34+
'user': 'root',
35+
'passwd': '',
36+
'db': '',
37+
'port': 3306
38+
}
39+
if kargs.has_key('host'):
40+
self.info['host'] = kargs['host']
41+
if kargs.has_key('user'):
42+
self.info['user'] = kargs['user']
43+
if kargs.has_key('passwd'):
44+
self.info['passwd'] = kargs['passwd']
45+
if kargs.has_key('db'):
46+
self.info['db'] = kargs['db']
47+
if kargs.has_key('port'):
48+
self.info['port'] = int(kargs['port'])
49+
if kargs.has_key('connect_timeout'):
50+
self.info['connect_timeout'] = kargs['connect_timeout']
51+
if kargs.has_key('use_unicode'):
52+
self.info['use_unicode'] = kargs['use_unicode']
53+
if kargs.has_key('charset'):
54+
self.info['charset'] = kargs['charset']
55+
if kargs.has_key('local_infile'):
56+
self.info['local_infile'] = kargs['local_infile']
57+
58+
#Support Legacy Username
59+
if kargs.has_key('username'):
60+
self.info['user'] = kargs['username']
61+
#Support Legacy Password
62+
if kargs.has_key('password'):
63+
self.info['passwd'] = kargs['password']
64+
#Support Legacy Schema
65+
if kargs.has_key('schema'):
66+
self.info['db'] = kargs['schema']
67+
68+
if kargs.has_key('commitOnEnd'):
69+
self.commitOnEnd = kargs['commitOnEnd']
70+
else:
71+
self.commitOnEnd = False
72+
73+
hashStr = ''.join([str(x) for x in self.info.values()])
74+
self.key = md5(hashStr).hexdigest()
75+
76+
def __getattr__(self, name):
77+
try:
78+
return self.info[name]
79+
except Exception, e:
80+
return None
81+
82+
83+
connection_timeout = datetime.timedelta(seconds=20)
84+
85+
class PySQLConnectionManager(object):
86+
"""
87+
Physical Connection manager
88+
89+
Used to manage the physical MySQL connection and the thread safe locks on that connection
90+
91+
@author: Nick Verbeck
92+
@since: 5/12/2008
93+
@version: 0.1
94+
"""
95+
def __init__(self, PySQLConnectionObj):
96+
"""
97+
Constructor for PySQLConnectionManager
98+
99+
@param PySQLConnectionObj: PySQLConnection Object representing your connection string
100+
@author: Nick Verbeck
101+
@since: 5/12/2008
102+
"""
103+
104+
self.connectionInfo = PySQLConnectionObj
105+
self.connection = None
106+
self.lock = Semaphore()
107+
self.activeConnections = 0
108+
self.query = None
109+
self.lastConnectionCheck = None
110+
self.Connect()
111+
112+
def updateCheckTime(self):
113+
self.lastConnectionCheck = datetime.datetime.now()
114+
115+
def Connect(self):
116+
"""
117+
Creates a new physical connection to the database
118+
119+
@author: Nick Verbeck
120+
@since: 5/12/2008
121+
"""
122+
self.connection = MySQLdb.connect(*[], **self.connectionInfo.info)
123+
self.updateCheckTime()
124+
125+
def ReConnect(self):
126+
"""
127+
Attempts to close current connection if open and re-opens a new connection to the database
128+
129+
@author: Nick Verbeck
130+
@since: 5/12/2008
131+
"""
132+
self.Close()
133+
self.Connect()
134+
135+
def TestConnection(self, forceCheck = False):
136+
"""
137+
Tests the current physical connection if it is open and hasn't timed out
138+
139+
@return: boolean True is connection is open, False if connection is closed
140+
@author: Nick Verbeck
141+
@since: 5/12/2008
142+
"""
143+
if self.connection is None:
144+
return False
145+
else:
146+
if forceCheck is True or (datetime.datetime.now() - self.lastConnectionCheck) >= connection_timeout:
147+
try:
148+
cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
149+
cursor.execute('select current_user')
150+
self.updateCheckTime()
151+
return True
152+
except Exception, e:
153+
self.connection.close()
154+
self.connection = None
155+
return False
156+
else:
157+
return True
158+
159+
def Commit(self):
160+
"""
161+
Commit MySQL Transaction to database
162+
163+
@author: Nick Verbeck
164+
@since: 5/12/2008
165+
"""
166+
try:
167+
self.connection.commit()
168+
self.updateCheckTime()
169+
except Exception, e:
170+
pass
171+
172+
def Close(self):
173+
"""
174+
Commits and closes the current connection
175+
176+
@author: Nick Verbeck
177+
@since: 5/12/2008
178+
"""
179+
if self.connection is not None:
180+
try:
181+
self.connection.commit()
182+
self.connection.close()
183+
self.connection = None
184+
except Exception, e:
185+
pass

0 commit comments

Comments
 (0)