Dota 2 Вики
Регистрация
Advertisement

Документация для Модуль:VarQuery Перейти к коду ↴ [ править | очистить ]

Reality Rift icon
▶️ Planeshift.
Документацию для этого шаблона или модуля можно найти в Шаблон:VarQuery.
Вы можете быть перенаправлены на другой язык вики, если перевод недоступен.

Зависимости

local p = {}
local cargo = mw.ext.cargo
local getArgs = require( 'Модуль:Arguments' ).getArgs
local split = require( 'Модуль:Split' )
local vardefine = require( 'Модуль:Variables' ).vardefine
local varexists = require( 'Модуль:Variables' ).varexists

local i18n = {
  error = {
    multiple_results = 'Более чем один результат.',
    no_results = 'Не найдено результатов.',
    no_table = 'Не указана таблица',
  }
}


function p.main( frame )
  local args = getArgs( frame )
  local tables = args['table'] or args['tables']
  local fields = args['field'] or args['fields'] or '_pageName'
  local prefix = args['prefix'] or 'cargo_'

  assert( tables, i18n.error.no_table )

  -- Clear already existing variables that could interfere with the new ones.
  for _,field in ipairs( split( fields, '%s*,%s*' ) ) do
    local var_key = prefix .. field
    if varexists( var_key ) then
      vardefine( var_key, '' )
    end
  end
  
  local data = cargo.query( tables, fields, {
    where = args['where'],
    join = args['join on'],
    groupBy = args['group by'],
    having = args['having'],
    orderBy = args['order by'],
    limit = args['limit']
  } )

  if #data ~= 1 then
    if #data == 0 then
      error( i18n.error.no_results )
    else
      error( i18n.error.multiple_results )
    end
  end

  for key, value in pairs( data[1] ) do
    local var_key = prefix .. key
    vardefine( var_key, value )
  end
end


return p
Advertisement