Define constant in SQLite C/C++ API - c++

Does the SQLite C/C++ API support a way to define a constant for usage in queries? Something like this:
SELECT user_defined_function(USER_DEFINED_CONSTANT)
where USER_DEFINED_CONSTANT evaluates to some specified value.

You can create a user-defined function that always returns the same value. Alternatively, since you're talking about the C API, you could consider using a variable or a constant at the C level to help create the text of your prepared statements (containing a bona fide SQL literal).
As far as I know or can determine, however, you cannot define a symbol that SQLite will automatically transform to a constant.

Related

How to identify uninitialized variables in a Lua script, without running it

I'd like to be able to write some Lua code like this:
y=x+1
and be able to get the names of all variables (x and y in this case) so that I can read from/write to them in the calling C++ program. The problem is that x is uninitialized, so this chunk will not execute and therefore neither variable will appear in the globals table. My current work-around is to have the user explicitly declare that they want to initialize x externally (as well as how to initialize it), then I pre-pend the Lua script with an appropriate declaration for x, so that the final script looks like this:
x= /*some value calculated outside of the Lua script*/
y=x+1
Although this works, I'd really like to have a way to automatically list all uninitialized variables in the Lua code and present them to the user, instead of the user having to remember to explicitly declare them. A function that parses the Lua code without executing it would probably be what I want. I've tried the function luaL_loadstring, but x and y don't show up in the globals table.
Since this is a bit vague, I'll give an actual use case. My C++ code basically performs optimizations on functions, such as finding a root or a maximum. I want the user to be able to define custom functions (in the form of Lua scripts), which in general will have one or more inputs and one or more outputs. The user will define which parameters the optimizer should operate on. For example, the user may want to find the minimum of y=x^2. The way I'd like it to work is that the user writes a Lua script consisting of nothing more than y=x^2, and then tells the optimizer to vary x in order to minimize y. On each iteration of the optimizer, the current guess for x would be automatically pasted into the user script, which is then executed, and then the value of y is pulled from the Lua state to be fed back to the optimizer. This is how I have it working now, however it's a bit clumsy from a UX perspective because the user has to manually declare that x is a Lua variable. This gets tedious when there are many variables that require manual declaration. It would be much better if I could automatically scan the script and show the user a list of their undeclared variables so they could then use drag-and-drop and other GUI sugar to do the manual declaration.
Lua isn't meant to work like that. Lua/C interop is intended to be collaborative; it's not supposed to be that C can do whatever it wants.
Using your example, if you have a Lua script that is supposed to take a value from C and return that value + 1, then you spell that in Lua like this:
local x = ... --Get the first parameter to the chunk.
return x + 1 --Adds 1 to the value and returns it.
You compile this string into a Lua chunk and call it like a Lua function. You pass it the value you want to manipulate and get the return value from the Lua stack.
The idea is not that C code can just reach into a Lua script and shove data into it arbitrarily. The above chunk takes parameters from the user and provides return values to the user. That's typically how C interfaces with Lua.
Yes, you can write values to globals and have the Lua script read them, and write its "results" to globals that the external code reads. But this is not the most effective way to interact with scripts.
I'd really like to have a way to automatically list all uninitialized variables
There's no such thing in Lua as an "uninitialized variable". Not in the way that you mean.
Yes, there are globals. But whether that global has a value or not is not something the Lua script can control. A global is global after all; you can set a global variable from outside of the script (for example, see lua_setglobal). If you do, then a script that reads from it will read the value you set. But it doesn't know anything about that.
What you want is a static code analyzer/Lua linter. Take a look at Luacheck:
Luacheck is a static analyzer and a linter for Lua. Luacheck detects
various issues such as usage of undefined global variables, unused
variables, and values, accessing uninitialized variables, unreachable
code and more. Most aspects of checking are configurable: there are
options for defining custom project-related globals, for selecting set
of standard globals (version of Lua standard library), for filtering
warnings by type and name of related variables, etc. The options can
be used on the command line, put into a config or directly into
checked files as Lua comments.
There is also Lualint, and similar Lua linters for Atom, VSCode, or your fav IDE.

Pass a C++ method return to a Pro*C procedure

I am aware that in Pro*C is possible to pass to a procedure either a host variable or a String literal:
dbms_pipe.purge(:pipe_name);
dbms_pipe.purge('pipe_name');
Is it possible to pass a method return to a Pro*C procedure? The following call don't work:
dbms_pipe.purge(pipe_name.c_str());
Late answer, but still:
First of all, Pro*C is quite dumb. It becomes even more dumb when switching from C to C++-Mode.
Your second example does not challenge Pro*C at all, because the string constant is just part of your sql-statement.
Your first example is just what it can do. You cannot access members of structs (But you can read in whole structs), call functions or whatever. The only way to deal with this is first to copy the result of the function call into a host-variable and then pass that to Pro*C. To find the Manual, try google search for "oracle pro*c developer guide". If you read it carefully, you will understand what you can do and what not...

c++ how to process the argu which uncertain type?

There's a web api which output json format:
{"ret":0}
c++ program could get the value of "ret", and it's a INT type.
but if modify the api, output to this:
{"ret":"0"}
c++ program runs error.
what if the value of "ret" is uncertain type, maybe INT or maybe STRING?
is there a way to process the uncertain type value in c++?
No, C++ is a statically-typed language. It's my opinion that you should code against the datatypes of the API which should not change. It is commonly accepted that if the API chagnges, then the code calling that API has to change as well.
You could just use Regex for different cases.
Like checking if there are two " around your input

C++ Using a string variable to call and give names to other stuff

How can I give a name to a new variable or object using a string variable?
For example: After I compile the program, I enter the text "a_name", press [Enter] and then a vaiable or object with the name "a_name" gets declared. Another example: I enter the text "a_name", press [Enter] and then the variable called "a_name" shows it's value.
Are there any special Libraries for this, that have to be downloaded? Or are there ones that are included in the compilers files? If there are libraries that have to be downloaded, which are the easiest to understand and use? I'm using Visual C++, but with the Libraries Iostream, Math, String, e.t.c. copied from the DevC++ compiler.
You can't add "variables" to a program once it has been compiled. You
can get more or less the same effect, however, by using std::map, with
a string as the key type.
You will, of course, have to decide what type the new variable should
have, so you know what type to map it to. If there may be more than one type, something like boost::variant might be useful. (Note that unlike the set of names, the set of possible types must be completely defined at compile-time.)
As far as I know you can't. Variable names are set up on compile time and not on run time. C++ is not interpreted (like Perl, Python or JavaScript) thus it can't evaluate expressions on run time. C++ is ol' school.
I suggest you using arrays or C++ list/map classes to try to simulate this behavior.
You can't declare variables like that, what you may do however is use a map where the key of the map is the name of the variable you would like to refer to, and the value in the map having that key, is the value of the variable.
std::map<std::string, std::string> variables;
Obviously the value could be any type, and not just a string like I have used here, you can use double, int, bool or whatever suits your need, or if you need different types you might even use Boost variant as James Kanze suggested, or any other similar class.
Read more about maps here: http://www.sgi.com/tech/stl/Map.html and here: http://www.cplusplus.com/reference/stl/map/
You can use any kind of map, but a map using a sort of hashing to store the key might be your best bet. STL hash_map: http://www.sgi.com/tech/stl/hash_map.html
So basically what you are trying to do is include an interpreted language into your C++ program.
There are many languages that support being embedded into a C++ program Lua, JavaScript, Python to mention a few.

isDefined function?

In C++ is there any function that returns "true" when the variable is defined or false in vice versa. Something like this:
bool isDefined(string varName)
{
if (a variable called "varName" is defined)
return true;
else
return false;
}
C++ is not a dynamic language. Which means, that the answer is no. You know this at compile time, not runtime.
There is no such a thing in runtime as it doesn't make sense in a non-dynamic language as C++.
However you can use it inside a sizeof to test if it exists on compile time without side-effects.
(void)sizeof(variable);
That will stop compilation if var doesn't exist.
As already stated, the C++ runtime system does not support the querying of whether or not a variable is declared or not. In general a C++ binary doesn't contain information on variable symbols or their mappings to their location. Technically, this information would be available in a binary compiled with debugging information, and you could certainly query the debugging information to see if a variable name is present at a given location in code, but it would be a dirty hack at best (If you're curious to see what it might look at, I posted a terrible snippet # Call a function named in a string variable in C which calls a C function by a string using the DWARF debugging information. Doing something like this is not advised)
Microsoft has two extensions to C++ named: __if_exists and __if_not_exists. They can be useful in some cases, but they don't take string arguments.
If you really need such a functionality you can add all your variables to a set and then query that set for variable existance.
Already mentioned that C++ doesn't provide such facility.
On the other hand there are cases where the OS implement mechanisms close to isDefined(),
like the GetProcAddress Function, on Windows.
No. It's not like you have a runtime system around C++ which keeps remembers variables with names in some sort of table (meta data) and lets you access a variable through a dynamically generated string. If you want this, you have to build it yourself, for example using a std::map that maps strings to some objects.
Some compile-time mechanism would fit into the language. But I don't think that it would be any useful.
In order to achieve this first you need to implement a dynamic variable handling system, or at least find some on the internet. As previously mentioned the C++ is designed to be a native language so there are no built-in facilities to do this.
What I can suggest for the most easy solution to create a std::map with string keys storing global variables of interest with a boost::any, wxVariant or something similar, and store your variables in this map. You can make your life a bit easier with a little preprocessor directive to define a variables by their name, so you don't need to retype the name of the variable twice. Also, to make life easier I suggest to create a little inline function which access this variable map, and checks if the given string key is contained by the map.
There are implementation such a functionality in many places, the runtime property handling systems are available in different fashion, but if you need just this functionality I suggest to implement by yourself, because most of these solutions are quite general what you probably don't need.
You can make such function, but it wouldn't operate strings. You would have to send variable name. Such a function would try to add 0 to the variable. If it doesn't exists, an error would occur, so you might want to try to make exception handling with try...throw...catch . But because I'm on the phone, I don't know if this wouldn't throw an error anyways when trying to send non-existing variable to the function...