I faced problem, write unit test, using sqlite for it, for some query using doctrine extension for IF ELSE and when running test have error with that
for another error,md5 or concat I create sqlite function in PostConnectEventSubscriber class in postConnect function
in this part I use ifelse
this is my extension
Now I create stub without logic, but how implementation this for sqlite ?
$args->getConnection()
->getWrappedConnection()
->sqliteCreateFunction('IF', function ($st) {
return '';
});
SQLLite does not support advanced conditional statements, you should use CASE instead of IF...ELSE.
Related
I've got many questions :)
I've started to use Cosmos DB (SQL API). Loving it!
But I'm struggling to discover the best way of building and managing code. I'm using Visual Studio.
Here's some code, and I'll base my questions on these 4 javascript functions:
function createYayBase(something) {
// UDF
return {
pk: "Yay",
id: "Ptr=" + something
};
}
function createYayDoc(whatever, something) {
// UDF
var o = createYayBase(something);
o.Message = whatever;
return o;
}
function validateYayDoc(doc) {
// UDF
if (doc.pk !== "Yay") { throw new Error("non-Yay! :("); }
}
function processYay(val) {
// stored procedure
var doc = CreateYayDoc(val, "Rec");
validateYayDoc(doc);
...createDocument(selflink, doc, ...);
}
These 4 functions are in 4 different .js files in my solution. I would deploy the first 3 as their own user-defined function in my collection. The last, I would deploy as a stored procedure.
My app would execute the "processYay" stored procedure to do some operations. The UDFs are supportive.
Questions:
Can I call a UDF from a stored procedure directly, similar to how I've done it above in "processYay"? Or are they only accessible as part of a query?
Can I call a UDF from another UDF, as I did inside "createYayDoc"?
Can a UDF 'throw' or is that considered a side effect in this case?
If the answers are "nope", "nope" and "nope", there seems to be another solution: Define the functions inside the stored procedure itself. It would work perfectly like this:
function processYay(val) {
// stored procedure
function createYayBase(something) { ... }
function createYayDoc(whatever, something) { ... }
function validateYayDoc(val) { ... }
var doc = CreateYayDoc(val, "Rec");
validateYayDoc(doc);
...createDocument(selflink, doc, ...);
}
But then...
How can I reuse code?
My javascript sits inside a .NET class library. I'd love a "#include " trick :). Do I hack something together with webpack, etc?
Or... is there some accepted approach people use?
1.Can I call a UDF from a stored procedure directly, similar to how I've done it above in "processYay"? Or are they only accessible as
part of a query?
2.Can I call a UDF from another UDF, as I did inside "createYayDoc"?
3.Can a UDF 'throw' or is that considered a side effect in this case?
The answer is nope.You could find following clear statement in the official UDF doc.
User-defined functions (UDFs) are used to extend the Azure Cosmos DB
SQL query language grammar and implement custom business logic. They
can only be called from inside queries. They do not have access to the
context object and are meant to be used as compute-only JavaScript.
Therefore, UDFs can be run on secondary replicas of the Cosmos DB
service.
I think you have some misconceptions about the user defined function's application scenario. It just helps you process the result set of your query according to your custom business logic. It can not be invoked by stored procedure and can not call other methods or even manipulate the database.
Stored procedures are just JS code scripts which running on the server side.It can help you do some custom database operations ,even bulk operations. It's about special needs so that you can't think of it is an ordinary function in a complete project.
Considering reusability, you can pass some key variables in the JS code as parameters to the stored procedure.
Hope it helps you.
In Kotlin (1.0.6), through reflection I need to iterate over the members of a class (let's call it Foo), and do something based on the return type. I can write the following, which works:
Foo::class.members{ m ->
if(Integer.TYPE.isAssignableFrom(m.returnType.javaType as Class<*>)){
//do something here
} else if ...
}
the problem is that the if statement (to handle kotlin.Int) is quite ugly. Is there any better way in Kotlin to achieve the same result without having to rely directly on the Java API?
No, there is not a better way pre-1.1 Kotlin.
You can use Int::class.javaObjectType instead of Integer.TYPE to avoid using java.lang.Integer in Kotlin code but that makes the statement even longer (although more idiomatic).
In Kotlin 1.1 you can use isSubtypeOf or query jvmErasure.allSupertypes directly.
In the past I have avoided ORM and always handcrafted parameterised queries etc. This is very time consuming and a real pain when first developing an application. Recently I decided to have another look at ORM specifically the Sqlite.NET ORM.
I would like to use SQLite ORM features but also be able to run a batch of native SQL commands to prepopulate a database.
We are using the SqliteNetExtensions-MvvmCross dll to enable one-to-many relationships etc and this all looks fine. My issues comes to when I want to seed the database with configuration data. I was hoping to simply provide a sql file that contained a series of sql statements that it would run one after another.
I have grabbed the SQlite.NET code from GITHub and run the tests. I have then extended the StringQueryTests class that has a simple [Product] table to do the following:-
[Test]
public void AlanTest()
{
StringBuilder sb = new StringBuilder(200);
sb.Append(" DELETE FROM Product;");
sb.Append(" INSERT INTO Product VALUES (1,\"Name1\",1,1);");
sb.Append(" INSERT INTO Product VALUES (2,\"Name2\",2,3);");
db.Execute(sb.ToString());
}
When I run this it does not throw an error and in fact the behaviour seems to be that it will only run the first command. If I paste the contents of sb.ToString() into a sqlite database query window it will work just fine.
Is this the expected behaviour? If so, how do I go about overcoming this so that I can use an approach like above. I don’t really want to have to create objects to manage all SQL statements if possible.
I can see that there are a number of approaches that could be adopted to overcome this issue - anyone got a work around or suggestions that they think can solve this issue?
Kind regards
Alan.
I just ran into this issue too. I found a blog post that explains why.
Here is what the post says in case it goes missing.
All of the code [in sqlite-net] correctly checks the result codes and throws exceptions accordingly.
Although I haven't posted all relevant code here, I did review it, and the real origin of this behavior is elsewhere - in the native sqlite3.dll sqlite3_prepare_v2 method. Here's the relevant part of the documentation:
These routines only compile the first statement in zSql, so *pzTail is left pointing to what remains uncompiled.
Since sqlite-net doesn't do anything with the uncompiled tail, only the first statement in the command is actually executed. The remainder is silently ignored. In most cases you won't notice that when using sqlite-net. You will either use its micro ORM layer or execute individual statements. The only common exception that comes to mind, is trying to execute DDL or migration scripts which are typically multi statement batches.
Can't you do :
[Test]
public void AlanTest()
{
var queries = new List<string> ()
{
" DELETE FROM Product",
" INSERT INTO Product VALUES (1,\"Name1\",1,1)",
" INSERT INTO Product VALUES (2,\"Name2\",2,3)"
};
db.BeginTransaction ();
queries.ForEach (query => db.Execute (query));
db.Commit ();
}
You don't really need the transaction, just faster execution / checkpoint rollback...
I want to tell if an XML document has been constructed (e.g. using xdmp:unquote) or has been retrieved from a database. One method I have tried is to check the document-uri property
declare variable $doc as document-node() external;
if (fn:exists(fn:document-uri($doc))) then
'on database'
else
'in memory'
This seems to work well enough but I can't see anything in the MarkLogic documentation that guarantees this. Is this method reliable? Is there some other technique I should be using?
I think that behavior has been stable for a while. You could always check for the URI too, as long as you expect it to be from the current database:
xdmp:exists(fn:doc(fn:document-uri($doc)))
Or if you are in an update context and need ACID guarantees, use fn:exists.
The real test would be to try to call xdmp:node-replace or similar, and catch the expected error. Those node-level update functions do not work on constructed nodes. But that requires an update context, and might be tricky to implement in a robust way.
If your XML document is in-memeory, you can use in-mem-update API
import module namespace mem = "http://xqdev.com/in-mem-update" at "/MarkLogic/appservices/utils/in-mem-update.xqy";
If your XML document exists in your database you can use fn:exists() or fn:doc-available()
The real test of In-memory or In-Db is xdmp:node-replace .
If you are able to replace , update , delete a node then it is in database else if it throws exception then it's not in database.
Now there are two situation
1. your document is not created at all:
you can use fn:empty() to check if it is created or not.
2. Your document is created and it's in memory:
if fn:empty() returns false and xdmp:node-replace throws exception then it's in-memory
Can you create functions in SQLite like you can in MSSQL?
If so, how? What is the syntax?
Thanks
SQLite does not have a stored function/stored procedure language. So CREATE FUNCTION does not work. What you can do though is map functions from a c library to SQL functions (user-defined functions). To do that, use SQLite's C API (see: http://www.sqlite.org/c3ref/create_function.html)
If you're not using the C API, your wrapper API may define something that allows you access to this feature, see for example:
PHP sqlite_create_function() (http://www.php.net/manual/en/function.sqlite-create-function.php)
Python sqlite3.create_function() (http://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function)
Perl $dbh->sqlite_create_function($name,$argc,$code_ref,$flags) (https://metacpan.org/pod/DBD::SQLite#$dbh-%3Esqlite_create_function(-$name,-$argc,-$code_ref,-$flags-))
This could be useful to many: in SQLiteStudio it is possible to define new functions and collations easily from interface through a sql built-in plugin for example.
https://github.com/pawelsalawa/sqlitestudio/wiki/Official_plugins#sql-built-in
Through the function editor.
You can write arbitrary functions in SQL with the define extension:
-- define a function to sum the numbers 1..n
select define('sumn', ':n * (:n + 1) / 2');
-- use it as a regular function
select sumn(3);
6
select sumn(5);
15