#!/usr/bin/env python
import os, sys
import pprint
def import_module(name, path):
import imp
try:
mod_fp, mod_path, mod_desc = imp.find_module(name, [path])
mod = getattr( imp.load_module(name, mod_fp, mod_path, mod_desc), name )
except ImportError as exc:
mod = None
sys.stderr.write("Error: failed to import module ({})".format(exc))
finally:
if mod_fp: mod_fp.close()
return mod
# import the GrammarTemplate.py engine (as a) module, probably you will want to place this in another dir/package
GrammarTemplate = import_module('GrammarTemplate', os.path.join(os.path.dirname(__file__), '../src/python/'))
if not GrammarTemplate:
print ('Could not load the GrammarTemplate Module')
sys.exit(1)
else:
pass
def echo( s='' ):
print (s)
echo('GrammarTemplate.VERSION = ' + GrammarTemplate.VERSION)
echo( )
tpl = "SELECT <column.select>[, <*column.select>]\nFROM <table.from>[, <*table.from>][\nWHERE (<?required.where>) AND (<?condition.where>)][\nWHERE <?required.where><?!condition.where>][\nWHERE <?!required.where><?condition.where>][\nGROUP BY <?group>[, <*group>]][\nHAVING (<?required.having>) AND (<?condition.having>)][\nHAVING <?required.having><?!condition.having>][\nHAVING <?!required.having><?condition.having>][\nORDER BY <?order>[, <*order>]][\nLIMIT <offset|0>, <?count>]"
sql = GrammarTemplate(tpl)
echo("input template:")
echo(tpl)
echo( )
echo("output:")
echo(sql.render({
'column' : { 'select' : [ 'field1', 'field2', 'field3', 'field4' ] },
'table' : { 'from' : [ 'tbl1', 'tbl2' ] },
'condition' : { 'where' : 'field1=1 AND field2=2', 'having' : 'field3=1 OR field4=2' },
'count' : 5
}))
|