- Update 'webapp' library version
[gae-samples.git] / sql-shell / shell.py
blob3171514b278e34cb725d8136bea7f8ad1e850c1f
1 #!/usr/bin/python
3 # Copyright 2011 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """An AJAX SQL shell sample app for Google Cloud SQL.
19 Deployed at http://sql-shell.appspot.com/
20 """
22 import logging
24 from google.appengine.api import rdbms
25 from google.appengine.ext import webapp
26 from google.appengine.ext.webapp import util
29 # rdbms db-api connection, globally cached
30 conn = None
31 DEBUG = True
32 DATABASE_NAME = 'shell'
33 INSTANCE ='google.com:speckle-python-demos:sqlshell-55'
36 class StatementHandler(webapp.RequestHandler):
37 """Runs a SQL statement and returns the result."""
39 def get(self):
40 self.response.headers['Content-Type'] = 'text/plain'
42 statement = self.request.get('statement')
43 if not statement:
44 return
46 try:
47 cursor = conn.cursor()
48 logging.info('Executing %r' % statement)
49 cursor.execute(statement)
50 for results in cursor.fetchall():
51 self.response.out.write('%s\r\n' % str(results))
52 conn.commit()
53 except rdbms.Error, e:
54 logging.exception('Error:')
55 self.response.out.write(str(e))
58 def main():
59 global conn
60 conn = rdbms.connect(instance=INSTANCE, database=DATABASE_NAME)
61 application = webapp.WSGIApplication([('/shell.do', StatementHandler)],
62 debug=DEBUG)
63 util.run_wsgi_app(application)
66 if __name__ == '__main__':
67 main()