def makeNextFreeFunc( table, var ): """ Make a function that finds the next highest value of an integer field; used to auto-increment inserts for integer parts of a primary key """ template = Template( file=templatesPath()+"get_next_free.func.tmpl" ) template.functionHeader = makeNextFreeHeader( var, CONNECTION_STRING, ' is' ) template.statement = "select coalesce( max( "+var.varname+" ) + 1, 1 ) from "+table.qualifiedName() template.functionName = "Next_Free_"+var.adaName template.adaName = var.adaName template.default = var.getDefaultAdaValue() template.adaType = var.getAdaType( True ) template.getFunction = makeValueFunction( var, "0", "0" ) return str( template );
def makeNextFreeFunc( table, var ): """ Make a function that finds the next highest value of an integer field; used to auto-increment inserts for integer parts of a primary key """ template = Template( file=templatesPath()+"get_next_free.func.tmpl" ) template.functionHeader = makeNextFreeHeader( var, CONNECTION_STRING, ' is' ) template.statement = "select max( "+var.varname+" ) from "+table.qualifiedName() template.functionName = "Next_Free_"+var.adaName template.adaName = var.adaName template.adaType = var.getAdaType( True ) template.default = var.getDefaultAdaValue() if( var.sqlType == 'BIGINT' ): template.whichBinding = 'L' else: template.whichBinding = 'I' s = str(template) return s
def makeRetrieveSFunc( table, database ): """ table - Table class from table_model.py database - complete enclosuing Database model from table_model.py Make a fuction that, for a given table (say 'fred'), takes an sql string as input (just the query part) and returns a single list of fred records (fred_list, as declared in the main database declarations ads file). This version has wide_string handling jammed off Structure is: - declare local versions of all fred's variables - declare aliases (pointers) to these variables, where needed - declare length holders for each one - make the query by appending the query part to a constant 'select part' - prepare the query - bind variables/aliase - execute - loop round, map the locals to one record -- add the variable to the collection There's a certain amount of trial an error in this.. """ instanceName = table.makeName( Format.ada, Qualification.unqualified, ItemType.instanceName ) outputRecord = table.makeName( Format.ada, Qualification.full, ItemType.table ) template = Template( file=templatesPath()+"retrieve_wstr.func.tmpl" ) template.functionHeader = makeRetrieveSHeader( table, CONNECTION_STRING, ' is' ) template.listType = table.makeName( Format.ada, Qualification.full, ItemType.alist ) template.addToMap = "l.append( "+ instanceName +" )" template.bindings = [] template.adaInstanceName = instanceName template.variableDecl = instanceName + " : " + outputRecord template.adaQualifiedOutputRecord = outputRecord pos = 0 for var in table.variables: binding = makeBinding( database.databaseAdapter, instanceName, var, pos ) pos += 1 template.bindings.append( binding ) s = str(template) return s
def makeRetrieveSFunc( table, database ): """ table - Table class from table_model.py database - complete enclosuing Database model from table_model.py Make a fuction that, for a given table (say 'fred'), takes an sql string as input (just the query part) and returns a single list of fred records (fred_list, as declared in the main database declarations ads file). This version has wide_string handling jammed off Structure is: - declare local versions of all fred's variables - declare aliases (pointers) to these variables, where needed - declare length holders for each one - make the query by appending the query part to a constant 'select part' - prepare the query - bind variables/aliase - execute - loop round, map the locals to one record -- add the variable to the collection There's a certain amount of trial an error in this.. """ template = Template( file=templatesPath()+"retrieve_wstr.func.tmpl" ) template.functionHeader = makeRetrieveSHeader( table, CONNECTION_STRING, ' is' ) template.listType = table.adaQualifiedListName template.variableDecl = table.adaInstanceName + " : " + table.adaQualifiedOutputRecord template.addToMap = table.adaQualifiedContainerName+".append( l, "+ table.adaInstanceName +" )" template.aliasDeclarations = [] template.pointers = [] template.lenDeclarations = [] template.mappingsFromAliasToRecord = [] template.bindings = [] pos = 0 for var in table.variables: pos += 1 alias = var.adaName + ": aliased " + var.odbcType; if( var.isStringType() ): isize = int(var.size); string_padd = '@' * isize alias += " := " + makePaddedString( 4, string_padd, isize ) template.aliasDeclarations.append( alias ); len_decl = var.adaName + "_len : aliased SQLLEN := " + var.adaName+"'Size"; template.lenDeclarations.append( len_decl ) binding = makeBinding( database.databaseAdapter, var, pos ) template.bindings.append( binding ) if( var.isRealOrDecimalType() or var.isDateType() or var.isStringType() ): ## NOTE: is String type is only because we're trying wide_strings here, otherwise if( var.isRealOrDecimalType() ): ## there's a simple SQLBind version that doens't need any of this pointer = var.adaName + "_access : Real_Access := "+var.adaName+"'access" mapping = table.adaInstanceName + "." + var.adaName + ":= " + var.adaType + "( "+var.adaName + "_access.all )" elif( var.isDateType()): pointer = var.adaName + "_access : Timestamp_Access := "+var.adaName+"'access" mapping = table.adaInstanceName + "." + var.adaName + ":= dodbc.Timestamp_To_Ada_Time( "+var.adaName + "_access.all )" if( var.isStringType() ): mapping = table.adaInstanceName + "." + var.adaName + " := Slice_To_Unbounded( " + var.adaName + ", 1, Natural( "+var.adaName+"_len ) )" pointer = var.adaName + "_access : String_Access := "+var.adaName+"'access" template.pointers.append( pointer ) else: ## integer/enum types if( var.schemaType == 'ENUM' ): mapping = table.adaInstanceName + "." + var.adaName + " := "+ var.adaType+"'Val( " + var.adaName + " )" elif( var.schemaType == 'BOOLEAN' ): mapping = table.adaInstanceName + "." + var.adaName + " := Boolean'Val( " + var.adaName + " )" else: mapping = table.adaInstanceName + "." + var.adaName + " := " + var.adaName template.mappingsFromAliasToRecord.append( mapping ) s = str(template) return s