From SemanticLab
import re
import bibnew.database as database
bst = 'alpha'
fobj = open("sqlFnc_"+bst+".sql", "r")
db = database.database()
cnx=db.getCnx()
cur = cnx.cursor()
cur2 = cnx.cursor()
fncName =
lineIter = 0
reName = re.compile(r"(NAME)\s*=\s*'([\w\.]+?)'\[(.+)?\]")
reSqlL = re.compile(r"(\s*)(.*)")
for a, fline in enumerate(fobj):
#print a, fline
#reName = re.compile(r"(NAME)")
mobj = reName.match(fline)
if mobj:
fncName = mobj.group(2)
fncType = mobj.group(3)
print'New function: '+fncName
lineIter = 0
bindV ={'bst': bst
, 'name':fncName}
sql = "DELETE FROM META_FUNCTIONITEMS WHERE M_TYPEF_ID IN \
(SELECT ID FROM META_TYPEFUNCTIONS WHERE BST_NAME =:bst AND NAME = :name )"
cur.execute(sql, bindV)
sql = "DELETE FROM META_TYPEFUNCTIONS WHERE BST_NAME =:bst AND NAME = :name "
cur.execute(sql, bindV)
id = db.getNextval()
bindV ={'bst': bst
,'id': id
, 'type':fncType
, 'name':fncName}
sql = "INSERT INTO META_TYPEFUNCTIONS (ID,TYPE,NAME,BST_NAME) VALUES (:id,:type,:name,:bst)"
cur.execute(sql, bindV)
else:
lineAction=fline[:2]
if lineAction == ' ' or lineAction == '||':
sqlLine = fline[2:]
else:
sqlLine = fline
if lineAction != '||':
lineAction = None
mobj = reSqlL.match(sqlLine)
if mobj:
print 'insert line '+str(a)
blank = mobj.group(1)
sqlText = mobj.group(2)
if sqlText:
sqlText=sqlText.strip()
itemId = db.getNextval()
if blank:
indent=len(blank)
else:
indent=str(0)
bindV ={'id':itemId
, 'item_position' : lineIter
, 'indent': indent
, 'line_action' : lineAction
, 'sql_text':sqlText
, 'm_typef_id':id
}
sql = "INSERT INTO META_FUNCTIONITEMS(ID, ITEM_POSITION, INDENT, LINE_ACTION, SQL_TEXT, M_TYPEF_ID)\
VALUES (:id, :item_position, :indent, :line_action, :sql_text, :m_typef_id)"
cur2.execute(sql, bindV)
lineIter = lineIter +10
cnx.commit()
print 'commit executed'
db.close()
fobj.close