Example #1
0
def GetDbNames(user='******',password='******',dirName='.',dBase='::template1',cn=None):
  """ returns a list of databases that are available

    **Arguments**

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - a list of db names (strings)

  """
  if DbModule.getDbSql:
    if not cn:
      try:
        cn = DbModule.connect(dBase,user,password)
      except:
        print 'Problems opening database: %s'%(dBase)
        return []
    c = cn.cursor()
    c.execute(DbModule.getDbSql)
    if RDConfig.usePgSQL:
      names = ['::'+str(x[0]) for x in c.fetchall()]
    else:
      names = ['::'+str(x[0]) for x in c.fetchall()]
    names.remove(dBase)
  elif DbModule.fileWildcard:
    import os.path,glob
    names = glob.glob(os.path.join(dirName,DbModule.fileWildcard))
  else:
    names = []
  return names
Example #2
0
  def GetCursor(self):
    """ returns a cursor for direct manipulation of the DB
      only one cursor is available

    """
    if self.cursor is not None:
      return self.cursor
      
    self.cn = DbModule.connect(self.dbName,self.user,self.password)
    self.cursor = self.cn.cursor()
    return self.cursor
Example #3
0
def GetColumnNames(dBase,
                   table,
                   user='******',
                   password='******',
                   join='',
                   what='*',
                   cn=None):
    """ gets a list of columns available in a DB table

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause  (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      -  a list of column names

  """
    if not cn:
        cn = DbModule.connect(dBase, user, password)
    c = cn.cursor()
    cmd = 'select %s from %s' % (what, table)
    if join:
        if join.strip().find('join') != 0:
            join = 'join %s' % (join)
        cmd += ' ' + join
    c.execute(cmd)
    c.fetchone()
    desc = c.description
    res = map(lambda x: str(x[0]), desc)
    return res
Example #4
0
def GetColumnNamesAndTypes(dBase,
                           table,
                           user='******',
                           password='******',
                           join='',
                           what='*',
                           cn=None):
    """ gets a list of columns available in a DB table along with their types

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      - a list of 2-tuples containing:

          1) column name

          2) column type

  """
    if not cn:
        cn = DbModule.connect(dBase, user, password)
    c = cn.cursor()
    cmd = 'select %s from %s' % (what, table)
    if join:
        cmd += ' join %s' % (join)
    c.execute(cmd)
    return GetColumnInfoFromCursor(c)
Example #5
0
def GetTableNames(dBase,
                  user='******',
                  password='******',
                  includeViews=0,
                  cn=None):
    """ returns a list of tables available in a database

    **Arguments**

      - dBase: the name of the DB file to be used

      - user: the username for DB access

      - password: the password to be used for DB access

      - includeViews: if this is non-null, the views in the db will
        also be returned

    **Returns**

      - a list of table names (strings)

  """
    if not cn:
        try:
            cn = DbModule.connect(dBase, user, password)
        except:
            print 'Problems opening database: %s' % (dBase)
            return []
    c = cn.cursor()
    if not includeViews:
        comm = DbModule.getTablesSql
    else:
        comm = DbModule.getTablesAndViewsSql
    c.execute(comm)
    names = [str(x[0]).upper() for x in c.fetchall()]
    if RDConfig.usePgSQL and 'PG_LOGDIR_LS' in names:
        names.remove('PG_LOGDIR_LS')
    return names
Example #6
0
def GetDbNames(user='******',
               password='******',
               dirName='.',
               dBase='::template1',
               cn=None):
    """ returns a list of databases that are available

    **Arguments**

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - a list of db names (strings)

  """
    if DbModule.getDbSql:
        if not cn:
            try:
                cn = DbModule.connect(dBase, user, password)
            except:
                print 'Problems opening database: %s' % (dBase)
                return []
        c = cn.cursor()
        c.execute(DbModule.getDbSql)
        if RDConfig.usePgSQL:
            names = ['::' + str(x[0]) for x in c.fetchall()]
        else:
            names = ['::' + str(x[0]) for x in c.fetchall()]
        names.remove(dBase)
    elif DbModule.fileWildcard:
        import os.path, glob
        names = glob.glob(os.path.join(dirName, DbModule.fileWildcard))
    else:
        names = []
    return names
Example #7
0
def GetTableNames(dBase,user='******',password='******',
                  includeViews=0,cn=None):
  """ returns a list of tables available in a database

    **Arguments**

      - dBase: the name of the DB file to be used

      - user: the username for DB access

      - password: the password to be used for DB access

      - includeViews: if this is non-null, the views in the db will
        also be returned

    **Returns**

      - a list of table names (strings)

  """
  if not cn:
    try:
      cn = DbModule.connect(dBase,user,password)
    except:
      print 'Problems opening database: %s'%(dBase)
      return []
  c = cn.cursor()
  if not includeViews:
    comm = DbModule.getTablesSql
  else:
    comm = DbModule.getTablesAndViewsSql
  c.execute(comm)
  names = [str(x[0]).upper() for x in c.fetchall()]
  if RDConfig.usePgSQL and 'PG_LOGDIR_LS' in names:
    names.remove('PG_LOGDIR_LS')
  return names
Example #8
0
def GetColumnNames(dBase,table,user='******',password='******',
                   join='',what='*',cn=None):
  """ gets a list of columns available in a DB table

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause  (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      -  a list of column names

  """
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  cmd = 'select %s from %s'%(what,table)
  if join:
    if join.strip().find('join') != 0:
      join = 'join %s'%(join)
    cmd +=' ' + join
  c.execute(cmd)
  c.fetchone()
  desc = c.description
  res = map(lambda x:str(x[0]),desc)
  return res
Example #9
0
def GetColumnNamesAndTypes(dBase,table,
                           user='******',password='******',
                           join='',what='*',cn=None):
  """ gets a list of columns available in a DB table along with their types

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      - a list of 2-tuples containing:

          1) column name

          2) column type

  """
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  cmd = 'select %s from %s'%(what,table)
  if join:
    cmd += ' join %s'%(join)
  c.execute(cmd)
  return GetColumnInfoFromCursor(c)