tableactions.py :  » Database » PyTable » pytable-0.8.20a » pytable » mysql » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyTable 
PyTable » pytable 0.8.20a » pytable » mysql » tableactions.py
"""Actions for the MySQL table

These are meta-queries/actions, they tell us about
the structure of the database schema by directly
querying the postgresql system catalogs.  They are
therefor entirely non-portable, evil things, but
they do appear to get the job done :) .
"""
from pytable import sqlquery,dbschema
from basicproperty import common

class TableStructure (sqlquery.SQLQuery ):
  """Reverse-engineer table structure/schema from database

  This is a very heavy mechanism for design-time use
  which attempts to describe a table in the database
  using the dbschema objects which would normally
  be used to proactively define how we interact with
  the table.

  There are actually three different queries being
  done during the TableStructure query.  The first
  is the base query, which simply retrieves the
  DB API 2.0 column descriptions.  These provide
  much of the basic information required.

  The second stage retrieves the foreign-key
  constraints for the table.  Eventually this should
  also return general constraints (check restraints)
  to allow for automatically setting up constraint
  numeric and/or string data types.

  The third stage retrieves information about indices
  on the table.  This includes primary, unique and
  multi-field indices, but not check indices.
  """
  sql = """
  SELECT *
  FROM %(tableName)s
  LIMIT 1;"""
  def processResults(
    self, cursor, tableName,
    **namedarguments ):
    """Build Table and Field descriptors through introspection
    """
    table = dbschema.TableSchema( name=tableName )
    descriptors = []
    nameMap = {}
    tableDescription = cursor.description
    
    for index,description in zip(range(len(tableDescription)),tableDescription):
      extras = {}
      try:
        extras['dbDataType'] = cursor.connection.driver.localToSQLType(
          description [1]
        )
      except KeyError:
        pass
      else:
        try:
          extras['dataType'] = cursor.connection.driver.sqlToDataType(
            extras['dbDataType']
          )
        except KeyError:
          pass
      new = dbschema.FieldSchema(
        name = description [0],
        nullOk = description [6],
        index = index,
        table = table,
        internalSize = description[3] or -1, # can be None
        displaySize = description[2] or -1, # can be None
        **extras
      )
      descriptors.append( new )
      nameMap[new.name] = new
    table.fields = descriptors
    
    ## now get the index information
    indices = {}
    # XXX primary keys apparently can't be multi-column? <shrug>
    for item in ListIndices()( cursor, tableName=tableName ):
      name = item[2]
      unique = not item[1] # non-unique is the column spec
      fieldPosition = item[3] # position of the field in the column spec
      field = nameMap[ item[4]] # the field object...
      # we don't currently use collation or sub-part
      indices.setdefault( name, (name,unique,name=='PRIMARY',[]))[-1].append(
        (fieldPosition, item[4]),
      )
    #Okay, now build the actual index objects...
    indices = indices.values()
    indices.sort( )
    newIndices = []
    for name, unique,primary, fields in indices:
      fields.sort()
      fields = [ field[1] for field in fields ]
      new = dbschema.IndexSchema(
        name = name,
        unique = unique,
        primary=primary,
        fields = fields,
      )
      newIndices.append(new)
    if newIndices:
      table.indices = newIndices
    return table


class ListDatabases( sqlquery.SQLQuery ):
  """Queries PostgreSQL server for list of database-names

  returns a simple list of string names
  """
  sql = """SHOW DATABASES;"""
  def processResults( self, cursor, **namedarguments ):
    """Read database name list from cursor"""
    return [ row[0] for row in cursor.fetchall() ]

class ListTables( sqlquery.SQLQuery ):
  """Queries connection/cursor for list of table-names

  returns a simple list of string names
  """
  sql = """SHOW TABLES;"""
  def processResults( self, cursor, **namedarguments ):
    """Read table name list from cursor"""
    return [ row[0] for row in cursor.fetchall() ]


class ListIndices( sqlquery.SQLQuery ):
  """Get index-data-records for a given table

  Returns a mysql-specific table format...
  """
  sql = """SHOW INDEX FROM %(tableName)s;"""
  def processResults(self, cursor, **named ):
    """returns results of the selection as an unadorned set"""
    return cursor.fetchall()
  
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.