コード例 #1
0
ファイル: bfsql.py プロジェクト: cbears/octoform
def getForm ( bdb_id, refNo=False, lim="*"):   
  """
  Each form is uniquely keyed by the berkeley Form Id, and the ref. no

  If refNo is False, then return all forms associated with a specific bdb_id.
  
  When you request a form, if bdb_id is a revision, find the base form first. 
   - newMap.sort() changes the order somehow. Probably the original question ordering.
  """
  conn= pool.getconn()
  cur = conn.cursor()

  # Get Mapping.
  bForm = bfbdb.loadObject (bdb_id) 
  newMap, parent = bfbdb.loadMap(bForm)
  newMap.sort()

  if not parent:
    parent = bdb_id

  # Do Select
  try: 
    if refNo: 
      cur.execute("SELECT %s FROM BF%s where id=%d" % (lim,parent,int(refNo)))
      result = cur.fetchall()
    else: 
      # When doing a select *, remember that you are always selecting
      # against the base form. Would be nice to optimize this to take
      # adavantage of named items (which don't currently exist)...
      cur.execute ( "SELECT %s FROM BF%s" % (lim, parent) )
      result = cur.fetchall()
  except db.DatabaseError, e:
    log.debug ( "Error performing select ... Error was %s " % e )
    # conn.rollback() # Don't need to to rollback, no data modified
    cur.close()
    pool.putconn(conn)
    return []
コード例 #2
0
ファイル: bfsql.py プロジェクト: cbears/octoform
def insertForm ( bdb_id, data, recursionCount=0, metaData={}, cur=None, conn=None ):
  """
   @data   = A dict (key, value) of the items we are inserting. key's are in
               the format q001, q002, q003, etc.
   @bdb_id = Berkeley form from which the data is based.

   XXX1: This model is based on append only database's. In this model, you need
         a key of something to determine when new data is being added.
        
   XXX2: We should have some sort of trigger indicating when data has been
         appended which results in the obsoletion of an existing form.
  """

  if recursionCount > 5:
    cur.close()
    pool.putconn(conn)
    raise ValueError("INFINITE LOOP in insertForm. Error creating TABLE?.")
  if not cur:
    conn = pool.getconn()
    cur = conn.cursor()

  original_data = data
  bForm = bfbdb.loadObject (bdb_id) 
  newMap, parent = bfbdb.loadMap(bForm)

  data = mapNewToOld( newMap, data )
    
  filteredData = []
  for key, value in data:
    if value:
      filteredData.append( (key,value) )
  data = dict(filteredData)

  # Convert newID's to oldID's to save in database. 
  metaData["dateCreated"] = datetime.datetime.now()
  data.update(metaData)

  # Do the insert
  insertionId = None
  if cur:
    items = [ keys for keys in data  ] # copies items from data
    item  = ','.join(items)
    value = ')s,%('.join(items)

    try:
      #sql = "INSERT INTO BF%s (%s) values (%%(%s)s) RETURNING id" % (parent, item, value)
      # FOR 8.1
      sql = "INSERT INTO BF%s (%s) values (%%(%s)s) " % (parent, item, value)
      log.debug("SQL.form.insert: |%s| (%s)" % (sql,data) )
      cur.execute ( sql, data )
      # FOR 8.1
      cur.execute ( "SELECT lastval()" )
    except:
      if recursionCount < 5:
        conn.rollback()
        createColumn ( cur, parent, item, data )
        conn.commit()
        return insertForm ( bdb_id, original_data, recursionCount+1, metaData=metaData, cur=cur, conn=conn )
      raise # This raise is redundant with the INFINITE LOOP CHECK above.

    insertionId =  cur.fetchone()[0]
    conn.commit()
    if not insertionId:
      conn.rollback()
      cur.close()
      pool.putconn(conn)
      log.warn("An insert failed, SQL was %s" % locals().get('sql', 'NO SQL') )
      return 1
  else:
    raise ValueError("The connection to our database is broken :(.")

  cur.close()
  pool.putconn(conn)
  return insertionId