compile Insert RTI_BROLIST_DATASOURCE_TEMPLATE

Function RTI_BROLIST_DATASOURCE(ACTION, PARAM1, PARAM2, PARAM3)
/*
**  Mimic using An SQL stored procedure to build a recordset
**  Build a workging table were oconv, sort is all done "server-side"
**  So report can just have groups with Break-on, minimal chatter with OI.
**
**
*/

Declare Function dcount

$Insert logical
If Assigned(ACTION) Else ACTION = ""
If Assigned(PARAM1) Else PARAM1 = ""
If Assigned(PARAM2) Else PARAM2 = ""
If Assigned(PARAM3) Else PARAM3 = ""
* {CMD}

common /brolist_ds_Com/selected%, keys%
If Assigned(selected%) Else selected% = 0
RSLT = ""
Begin Case
	Case ACTION _EQC "FIELDS"
	    * New report, cleasr the selected cache
	    selected% = 0
		
		* return:
		* <1> Field Name]
		* <2> Field #]
		* <3> conv code]
		* <4> MVFlag (0/1)]
		* <6> Association]
		
		* For speed define a dict that is all hard fields, no oConv
		* Then on the read, do the calculations and oconv here, send result to BRW
		* Colname                ; * Position    ; * Conv
		{FIELDS_BLOCK}
		
	Case action _eqc "DATATYPE"
		whichField = param1
		
		{DATATYPE_BLOCK}
		
		
	Case ACTION _EQC "KEYS"
		* return:
		* <1-n> key
		* Build a dynamic select
		* Param1 looks like "100 WHERE FILTER = trUE
		
		* Seems to come here twice, so do the select the first time, return the keys the second time
		If selected% Then
			transfer keys% To rslt
			selected% = 0
		End else
			keys% = ''
			selected% = 1
			selstmt = trim(param1)
			cmds = ''
			
			firstword = selstmt[1,' ']
			tablename = "{TABLE}"
			If Num(firstword) Then
				num_to_select = firstword
				with_clause = trim(Field(selstmt, ' ', 2, Len(selstmt)))
				by_clause = ''
			End Else
				num_to_select = ""
				with_clause = selstmt
				by_clause = ''
			End
			
			Begin Case
				Case with_clause[1,2] _eqc 'BY'
					by_clause = with_clause
					with_clause  = ''
					
				Case indexc(with_clause, ' BY ', 1) Or indexc(with_clause, ' BY-DSND', 1)
					pos1 = indexc(with_clause, ' BY ', 1)
					pos2 =  indexc(with_clause, ' BY-DSND', 1)
					pos = If pos1 gt pos2 Then pos1 Else pos2
					If pos then
						by_clause = with_clause[pos,getByteSize(with_clause)]
						with_clause = with_clause[1,pos-1]
					End
			End case
			
			If indexc(with_clause, 'WITH', 1) then
				CMDS<-1> = 'SELECT ': num_to_select:' ':tablename:' ' : with_clause
			end
			If indexc(by_clause, 'BY', 1) Then
				* Do the sort after the select
				CMDS<-1> = 'SELECT ': num_to_select:' ':tablename: ' ' : by_clause
			End
			If cmds = '' Then
				CMDS<-1> = 'SELECT ': num_to_select:' ':tablename
			end
			col = ''
			Loop
				Remove cmd From cmds At col Setting mark
				If cmd # '' then
					Call Rlist(cmd,5, '', '', '')
				End
			While @list.active And mark
			Repeat
			
			rslt = ''
			If @list.active Then
				alloc_size = 0xFFFF
				buffer = space(alloc_size)
				bsize = alloc_Size
				bpos = 1
				
				done = false$
				Loop
					Readnext val Else done = true$
				Until done
					Gosub Append
				Repeat
				If bpos gt 2 Then
					keys% = buffer[1,bpos-2]
				end
			end
		end
	Case ACTION _EQC "READ"
		rslt = ''
		@id = param1
		alloc_size = 500
		buffer = space(alloc_size)
		bsize = alloc_Size
		bpos = 1
		*LIST PERSON_SONG_PLAY FULL_NAME PLAY_COST PLAY_MONTH TITLE BY PLAY_MONTH BY TITLE
		tablename = "{TABLE}"
		dictname  = "{DICTIONARY}"
		colnames = "{DATASOURCE_COLS}"
		convs    = "{DATASOURCE_CONVS}"
		col_cnt  = "{DATASOURCE_CNT}"
		Convert  ',' To @vm In colnames
		Convert ','  To @vm In convs
		Open dictname To @dict Then
			Open tablename To f_psp Then
				ReadO @record From f_psp, @id Then
					clpos = 1
					cvpos = 1
					For col_nr = 1 To col_cnt
						colname = colnames[clpos,@vm,1];clpos = bcol2()+1
						conv = convs[cvpos,@vm,1];cvpos = bcol2()+1
						Begin Case
							Case colname _Eqc '@ID' Or colname _Eqc 'AT_ID'
								val = @id
							case Num(colname)
								val = @record<colname>
							Case 1
								val = calculate(colname)
						End case
						If conv # '' Then
							val = Oconv(val, conv)
						end
						Gosub append
					next
					If bpos gt 2 Then
						rslt = buffer[1,bpos-2]
					end
				end
			End
		End
		
	Case ACTION _EQC "WRITE"
		id = param2
		rec = param3
		
	Case ACTION _EQC "DELETE"
		id = param1
		
End Case

Return RSLT

append:
vlen = getByteSize(val) + 1
If vlen + bpos gt bsize then
	buffer := space(alloc_size)
	bsize += alloc_Size
end
putbinaryValue( buffer, bpos,CHAR, val:@fm)
bpos += vlen
return
