"use strict";(self.webpackChunkdocs_site=self.webpackChunkdocs_site||[]).push([[8345],{561:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>d,contentTitle:()=>l,default:()=>u,frontMatter:()=>i,metadata:()=>r,toc:()=>a});const r=JSON.parse('{"id":"XXTLanControl.lua/tabledb/fetch_one","title":"Atomically Fetch And Update A Row (LCC.db.fetch_one)","description":"Signature","source":"@site/i18n/en/docusaurus-plugin-content-docs/current/XXTLanControl.lua/tabledb/fetch_one.md","sourceDirName":"XXTLanControl.lua/tabledb","slug":"/XXTLanControl.lua/tabledb/fetch_one","permalink":"/docs/en/XXTLanControl.lua/tabledb/fetch_one","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":80,"frontMatter":{"sidebar_position":80},"sidebar":"tutorialSidebar","previous":{"title":"Count Rows (LCC.db.count)","permalink":"/docs/en/XXTLanControl.lua/tabledb/count"},"next":{"title":"Fetch Row By ID (LCC.db.get)","permalink":"/docs/en/XXTLanControl.lua/tabledb/get"}}');var s=t(4848),o=t(8453);const i={sidebar_position:80},l="Atomically Fetch And Update A Row (LCC.db.fetch_one)",d={},a=[{value:"Signature",id:"signature",level:2},{value:"Parameters",id:"parameters",level:2},{value:"Returns",id:"returns",level:2},{value:"Notes",id:"notes",level:2},{value:"Example",id:"example",level:2}];function c(e){const n={blockquote:"blockquote",br:"br",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,o.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"atomically-fetch-and-update-a-row-lccdbfetch_one",children:"Atomically Fetch And Update A Row (LCC.db.fetch_one)"})}),"\n",(0,s.jsx)(n.h2,{id:"signature",children:"Signature"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-lua",children:"row, err = LCC.db.fetch_one(name, opts, timeout?)\n"})}),"\n",(0,s.jsx)(n.h2,{id:"parameters",children:"Parameters"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["name",(0,s.jsx)(n.br,{}),"\n","String. Table name. Must be non-empty and no longer than 256 bytes."]}),"\n",(0,s.jsxs)(n.li,{children:["opts",(0,s.jsx)(n.br,{}),"\n","Table with:","\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"conditions"}),": table of equality filters (default ",(0,s.jsx)(n.code,{children:"{}"}),")"]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"update"}),": table of fields to update after the row is claimed"]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"pick"}),": string, choose ",(0,s.jsx)(n.code,{children:'"min"'}),", ",(0,s.jsx)(n.code,{children:'"max"'}),", or ",(0,s.jsx)(n.code,{children:'"random"'}),"; defaults to ",(0,s.jsx)(n.code,{children:'"min"'})," based on ",(0,s.jsx)(n.code,{children:"id"})]}),"\n"]}),"\n"]}),"\n",(0,s.jsxs)(n.li,{children:["timeout",(0,s.jsx)(n.br,{}),"\n","Number, optional. Request timeout in seconds, default ",(0,s.jsx)(n.code,{children:"60"}),"."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"returns",children:"Returns"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["row",(0,s.jsx)(n.br,{}),"\n","Table. The claimed row; ",(0,s.jsx)(n.code,{children:"nil"})," when nothing matched."]}),"\n",(0,s.jsxs)(n.li,{children:["err",(0,s.jsx)(n.br,{}),"\n","String or ",(0,s.jsx)(n.code,{children:"nil"}),". Error description; ",(0,s.jsx)(n.code,{children:"'not found'"})," when no row matched, ",(0,s.jsx)(n.code,{children:"'unknown table'"})," when the table is missing."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"notes",children:"Notes"}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsxs)(n.p,{children:["Atomically reads and updates a single row, ideal for task assignment under concurrency.",(0,s.jsx)(n.br,{}),"\n","Fields in ",(0,s.jsx)(n.code,{children:"update"})," are persisted immediately so the same record is not handed out twice.",(0,s.jsx)(n.br,{}),"\n","Omitting ",(0,s.jsx)(n.code,{children:"update"})," still performs an atomic selection but skips the write-back."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"example",children:"Example"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-lua",children:'local row, err = LCC.db.fetch_one("tasks", {\n    conditions = { Status = "Idle" },\n    update = { Status = "Running" },\n    pick = "min"\n})\nif row then\n    print("Claimed task", row.id, row.Name)\nelseif err == "not found" then\n    print("No idle tasks available")\nend\n'})})]})}function u(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(c,{...e})}):c(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>i,x:()=>l});var r=t(6540);const s={},o=r.createContext(s);function i(e){const n=r.useContext(o);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),r.createElement(o.Provider,{value:n},e.children)}}}]);