Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Vocal datatable: Difference between revisions

From Sekaipedia
Content added Content deleted
mNo edit summary
mNo edit summary
Line 52: Line 52:
end
end
local singer_wheres = mutli_where(
local singer_wheres = multi_where(
'Song_Vocal_Versions.singers',
'Song_Vocal_Versions.singers',
'HOLDS',
'HOLDS',

Revision as of 07:02, 18 December 2021


To generate {{Vocal datatable}}, invoke using the main function.


local getArgs   = require('Module:Arguments').getArgs
local builder   = require('Module:DatatableBuilder')
local cargo     = mw.ext.cargo

local p = {}

local function cargoQuery(cargo_args)

	local wheres = {
		'Songs.song_id > 0'
	}
	
	local function multi_where(field, fun, arg, delim)
		if arg and arg ~= '' then
			local splits = mw.text.split(arg, delim)
			local split_wheres = {}
			
			for i_split,split in ipairs(splits) do
				table.insert(
					split_wheres,
					string.format(
						"%s %s '%s'",
						field,
						fun,
						split
					)
				)
			end
			
			return '(' .. table.concat(split_wheres, ' OR ') .. ')'
		end
	end
	
	local unit_wheres = multi_where(
		'Songs.unit',
		'HOLDS',
		cargo_args['unit'],
		';'
	)
	if unit_wheres then
		table.insert(wheres, unit_wheres)
	end
	
	local version_wheres = multi_where(
		'Song_Vocal_Versions.version',
		'=',
		cargo_args['version'],
		','
	)
	if version_wheres then
		table.insert(wheres, version_wheres)
	end
	
	local singer_wheres = multi_where(
		'Song_Vocal_Versions.singers',
		'HOLDS',
		cargo_args['singer'],
		','
	)
	if singer_wheres then
		table.insert(wheres, singer_wheres)
	end

	local tables = 'Song_Vocal_Versions, Songs'
	local fields = [[
		Songs._pageName,
		Songs.song_name,
		Songs.image,
		Song_Vocal_Versions.version,
		Song_Vocal_Versions.singers
	]]
	local args = {
		where = table.concat(wheres, ' AND '),
		limit = 500,
		join = 'Song_Vocal_Versions._pageID = Songs._pageID',
		orderBy = 'Songs.date DESC',
	}
	
	return cargo.query( tables, fields, args )
end


function p.main(frame)
	local args = getArgs(frame)
	
	local results = cargoQuery({
		['unit'] = args['unit'],
		['version'] = args['version'],
		['singer'] = args['singer']
	})

	local datatable = builder.new()
	
	datatable
		:setColumns{
			{ name = 'jacket', header = 'Jacket art', data_fields = { 'Songs.image' }, func = formatImage },
			{ name = 'name', header = 'Song name', data_fields = { 'Songs._pageName', 'Songs.song_name' }, func = formatName },
			{ name = 'unit', header = 'Unit(s)', data_fields = { 'Songs.unit' }, func = formatUnit },
			{ name = 'version', header = 'Version', data_fields = { 'Song_Vocal_Versions.version' } },
			{ name = 'singer', header = 'Singer(s)', data_fields = { 'Song_Vocal_Versions.singers' } }
		}
		:setHeaders{
			'jacket',
			'name',
			'unit',
			'version',
			'singer'
		}
		:setData(results)
	
	return datatable:tostring()
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.