Use xml.modify to dynamically create an XML file - xml.modify

I want to dynamically create an XML file using the xml.modify command.
I have a table with a column of type xml and I would read that table with a cursor and create an XML file.
I'm trying this code just to make some tests but it doesn't work.
declare #root xml
declare #x xml
set #root = '<Shipment></Shipment>'
set #x = '<ShipmentHeader><ShipID>0001</ShipID></ShipmentHeader>'
select #x
set #root.modify('insert {sql:variable("#x")} into (/Shipment)')
select #root
I would have
<Shipment>
<ShipmentHeader>
<ShipID>0001</ShipID>
</ShipmentHeader>
</Shipment>

i fixed by myself after modified a bit the code
the working code is below
declare #x xml
set #root = '<Shipment></Shipment>'
set #x = '<ShipmentHeader><ShipID>0001</ShipID></ShipmentHeader>'
select #x
set #root.modify('insert (sql:variable("#x")) into (/Shipment)[1]')
select #root
i removed the {} and i have specified the first node for Shipment[1]
in this way i got the result expected

Related

Sending variables via partial in symfony

I am told to do not define variables in template, instead define them in a partial and then retrieve them.
Let's say my _examplePartial.php looks like:
`<?php
$partial_a = 1;
$partial_b = 2;
echo "exapmlePartial is included correctly!";
?>`
Now I need to save value of $partial_a in $template_a in my template code.
By following code I see that exapmlePartial is included correctly, but I don't have access to $partial_a:
`include_partial('exampePartial');
`
Does anyone know how to get value of $partial_a?
Thanks.

Dynamic named lua objects

Lets imagine this:
tree = {}
tree.__newindex = tree
num = math.random(5,5)
tree.meta = {}
What I want to do here is replace the 'meta' in tree.meta with the var num without it creating a new object just simply called num. That way I can do something like tree.01415 for example. Maybe there is some syntax that I can put in there to designate meta as the variable num?
If I understand you correctly, I think this is what you want:
tree[num] = "whatever"
Then whatever will be added to table tree with the value of num as its key.

lua, combining strings with variables inside tables when imported from file

I am having a problem with variables inside tables. this is essential since I use tables as configuration for my program.
so I have tested the following code that works:
> x = "X"
> t = {["ref"]="table with value: "..x}
> print(t["ref"])
table with value: X
> x = "Y"
> t = {["ref"]="table with value: "..x}
> print(t["ref"])
table with value: Y
it however doesn't work without the second > t = ["ref"]="table with value: "..x
now I went to implement this into my main program witch consists of two files, one witch returns the configuration table. And one file with all the functions and stuff. it looks as following
FILE A (main.lua):
testString = "test1"
print(testString)
local CONFIG = require'config'
print(CONIFG[1].test)
testString = "test2"
print(testString)
local CONFIG = require'config'
print(CONIFG[1].test)
FILE B (config.lua):
local CONFIG = {
{["test"]=[[this is a test: ]]..testString}
}
return CONFIG
now when i run file A (a.k.a. main.lua) i get the following output:
test1
this is a test: test1
test2
this is a test: test1
i can't figure out what i am doing wrong here.. i thought it had something to do with that it was a single string so i made testString a table but that gave me the same result...
(that title really seems scary.. sorry)
require, by design, caches the return value. So if you call require with the same string, it will not execute the script again. It will simply return the previously returned value.
require is for loading modules. And modules should not change their return values based on other global state.
The function you're probably looking for is dofile. This will always load and execute the file (but it has none of the path searching properties of require). Alternatively, you can use loadfile to load the file as a function, then execute that function to regenerate the table whenever you want.
Also:
I am having a problem with variables inside tables.
There are no "variables inside tables". Or at least not the way you mean. Expecting a change to a variable to affect some other value is like expecting this:
a = 5
b = a + 5
a = 10
assert(b == 15, "This will never be true.")
When an expression (whether a + 5 or "table with value: " .. x) is evaluated, it results in a value. The resulting value is in no way dependent on any value or variable from the expression that generated it.
That's why you had to regenerate the value; because values don't change just because some variable changes.

Undeclare prefix in XML issue for SQL Server

I am currently trying to insert an xml node into a table field within SQL server using the below code. I have tried to declare the namespace within the insert but I continue to get undeclared prefix. What am I doing wrong?
Code:
Declare #Term XML = '<wma:Term>My First Term</wma:Term>'
Declare #x XML
Select #x = MetaData from [Products] where Id = 1
Set #x.modify ('declare namespace wma="http://www.google.com/wma"; insert sql:variable("#Term") as last into (/wma:WMA/wma:List)[1]
Expected results:
<wma:WMA xmlns:wma="http://www.google.com/wma">
<wma:List>
<wma:Term>My First Term</wma:Term>
</wma:List>
</wma:WMA>

Lua create multiple closure instances

I have some lua code in a file. I want to create multiple closure instances of this code, each with a different _ENV upvalue. I can use luaL_loadfile to load the file and set the first upvalue, N times with different tables, to create N instances. But wouldn't this load and compile the file N times?
The lua equivalent of what i want to do is the following, except without the loadfile
func_list = {}
for i = 1, 10 do
local new_env = {hello=i, print=print}
func_list[i] = loadfile("Code.lua", "t", new_env)
end
for i = 1, 10 do
func_list[i]()
end
------ Code.lua ------
print(hello*hello)
is there a better way to do this?
Whenever you load a string/file in Lua, what you get in return is a function to call to actually run the file. What load does for you is just some additional processing to set the _ENV.
However, nothing prevents you from setting _ENV yourself. You could do it with something like this:
-- Code.lua --
_ENV = ...
print(hello * hello)
Then, you could load/compile the file just once, and use multiple instances as such:
local code = loadfile("Code.lua")
env_list = {}
for i = 1, 10 do
local new_env = {hello=i, print=print}
code(new_env)
env_list[i] = new_env
end
If you do not want the user to write _ENV = ... in every file, you could instead load the file into a string, prepend the line yourself and use load to compile the source. But this would not work on compiled files.
Use the IO libraries to load the file into a string, and then call loadstring on it.
Alternatively, just get one chunk and then change it's env prior to executing it

Resources