PetaPoco test for Existance - petapoco

Looking through the documentation for PetaPoco
https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Querying-LINQ-Style
Specifically:
result = db.Exist<Person>("WHERE Id = #0", 12);
A couple of problems:
1) the method "Exist" doesn't exist on my version. PetaPoco is bundled with Umbraco v7.7.7 so it may be an old version. Lets suppose its a typo.
2) The "Exists" method only takes one parameter (the PK) so my compiler doesn't like this syntax.
It looks like this method only accepts an Int PK as a parameter
This works fine but I don't want to use the PK (which doesn't exist yet) as a parameter
var result = db.Exists<Person>(12);
Am I missing something?
Thanks

Figured it out and thought I'd share:
instead of...
bool result = db.Exist<Person>("WHERE Id = #0", 12);
Use...
bool result = db.Fetch<Person>("WHERE id=#0", 12).Any();
a bit of a clunk but there you have it.

Related

Is there a way for variables to have a few types in GDScript

I know GDScript has dynamic typing, but I was wondering if there was a way to give a variable only a few types it can have. Something like this:
var myVar : int : string = 12;
myVar = "Hello";
Does it have a feature like this?
No. This is not supported in GDScript.
There is a relevant proposal: https://github.com/godotengine/godot-proposals/issues/737 (The syntax would be var myVar: int|string). However, it is not part of the Godot 4.0 milestone.

Accessing variable from other script

How do I call a variable from other script components?
mirror.transform.position = cop.GetComponent("AI_Car").mirrorPos;
It seems that the public variable carPos declared in the script AI-Car-script cannot be accessed:
'mirrorPos' is not a member of 'UnityEngine.Component'.
2 things:
1) I work in C#, so I may be wrong about this, but I think you have to get the component and THEN get the variable. For example:
var otherScript: OtherScript = GetComponent("AI_CAR");
var newPosition = otherScript.mirrorPos;
2) I think it's best practice to make a temporary variable and then access it. So in the above example, I would then change mirror.transform.position like this:
mirror.transform.position = newPosition;
Obviously it's not not always great to work in vars (sometimes it is, that's an entirely different conversation) but this is just a simple pseudocode example. Hope this helps!
EDIT: here are the docs
You can cast it to the right type:
mirror.transform.position = ((AI_Car)cop.GetComponent("AI_Car")).mirrorPos;
mirror.transform.position = cop.GetComponent<AI_Car>().mirrorPos;
Anyway, the best is to make an AI_Car property, then get it on start, so you can simply read it anywhere in the class by aiCar.mirrorPos or similar.

How to run a function using a nhibernate criteria?

We have a search routine that uses criteria to build SQL query (because its restrictions added dynamically).
In a particular case (a very complicated case) we need to search over a table-valued function.(our model object is mapped to the function).
The result would be something like this :
SELECT count(*) FROM dbo.GetSubStaffsLetterInstances(#staffId) WHERE LetterNumber="1234";
The problem is I don't know how to pass #staffId to my criteria(I tried adding an Eq restrictions without success since restrictions are working on properties)
I know I can add a parameter to an IQuery but I don't know how I can do it using an ICriteria object.
If I understand your question completely, you can resort back to standard SQL:-
var sql = "SELECT count(*) FROM dbo.GetSubStaffsLetterInstances(:staffId)
WHERE LetterNumber=:letterNum";
var count = session.CreateSqlQuery(sql)
.setInt32("staffId", 12345)
.setString("letternum", "A1")
.UniqueResult<int>();
or try .UniqueResult<long>(); as I can't remember which one HQL returns

NHibernate ISQLQuery SetParameter issue

This is probably fairly straightforward but i can't seem to find a reasonable explanation in any documentation.
I'm trying to use an NHibernate.ISQLQuery and using SetResultTransformer() to return a custom set of results from a custom SQL query. Like so:
public virtual IList<T> GetSQLObject<T>(string sql, IDbParameter[] parameters = null)
{
ISQLQuery qry = _sess.CreateSQLQuery(sql);
qry.SetResultTransformer(Transformers.AliasToBean(typeof(T)));
if (parameters != null) {
foreach (IDbParameter parameter in parameters) {
qry.SetParameter(parameter.Name, parameter.Value);
}
}
return qry.List<T>();
}
From looking at the examples, it seems that in the sql query I have to use parameters in the format :param1 instead of #param1 as I would in a standard SQL query. If i use the latter syntax in the query, it throws an error at qry.SetParameter().
Is there a reason why ISQLQuery/NHibernate requires them in this format and won't work with the normal syntax?
SQL Server uses #param, but not every other database does. For example, MySQL uses ?param
NHibernate allows you to swap out 1 database implementation for another with little to no reworking of your DAL. It sets the parameters based on the database you configured when you setup the NH Configuration.
Edit: Also I think :param came about from Hibernate being targeted at Oracle when it was initially developed, since Oracle uses :param
Phil has answered the "why"; so perhaps I can recommend a "how"; why not just add a new extension method to the IDbParameter type (something like .GetNHibernateName() ) that will return the parameter name with the "#" replaced with a ":"; that should be trivial to implement.

Objective-C Strings

I am reviewing code that makes the following call:
id<PLResultSet> results = [sqliteDatabase executeQuery:#"select * where id=?",Id];
sqliteDatabase is an instance of PlausibleDatabase (from GoogleCode, I gather). The code works, but I don't understand it. Specifically, how does this section work?
#"select * where id=?",Id
Is the query being made with ? being replaced by Id? Or is the exeuteQuery function somehow combining the strings? How does this syntax make sense.
(Yes, I am new to Obj-C)
Thanks,
KF
This bit:
#"select * where id=?"
is an NSString (as opposed to a c-style string) which is being passed into a executeQuery: : method on the sqliteDatabase object. The second (unnamed) argument to the method is Id, presumably a local variable.
Guessing from the name of the method, the sqlite wrapper probably creates a parameterized query. The question mark is the syntax used by sqlite to mark where to insert the parameters.
It's specific to the method executeQuery, in which ? is used as a placeholder, and the appropriate positional argument is used as filler for that placeholder (with quoting, etc. added as necessary).