Given this SQL statement written in ACCE:
SELECT
This,
D.ClassDescription,
D.Id myId,
D.MimeType myMimeType
FROM
Document D
WHERE
D.Id= '{00093471-0100-C09E-828F-6B22177A2922}'
How do I properly alias the ClassDescription field?, it does not work like the other columns (properties).
I've tried using 'AS' and Brackets '[]' but no such luck, the column is silently removed from the results of the query.
Thank you,
Stephen
The aliasing of columns is straight-forward, you can use the following:
foo AS bar
foo [bar]
foo bar
As a side note to anyone who may peruse this question and answer, the installation of the ACCE instance I was using was somewhat problematic (or a bug), it would NOT allow me to alias the ClassDescription Property. If I programmatically create the same query using the SearchSQL API, the aliasing works as expected as long as it conforms to SQL-92 dialect.
Related
So according to The oracle documentation on catsearch it's not possible to use left-truncated searches with a catsearch text-query.
However, I've found that using double wildcards does have the desired effect of left-truncated queries.
E.g. select * from foo where CATSEARCH(bar, '**zing', '')>0; would find records with bar having the value of "this is amazing" while
select * from foo where CATSEARCH(bar, '*zing', '')>0; wouldn't
For the configuration of my word list, I have enabled substring_index and prefix_index. In the substring_index description, it does mention left-truncated (and double-truncated) wildcard usage.
I cannot find any reason as to why/how the double wildcard usage works, how optimized it is and whether or not it has any other side-effects.
So the ultimate questions:
Why does this syntax work, is there anywhere that this is described?
Is it equal, performance-wise?
Are there any other side-effects people should be aware of when using this syntax?
In Sequel, one can use :a__b to generate SQL such as "a"."b". This syntax doesn't work though when we want a fully qualified statement to get to a column, such as:
> Sequel.version
=> "4.25.0"
> puts RDB[:store__albums].select(:store__albums__title___album_title).sql
SELECT "store"."albums__title" AS "album_title" FROM "store"."albums"
# I expected:
# SELECT "store"."albums"."title" AS "album_title" FROM "albums"."title"
I know I can use Sequel.lit to generate the correct value, but I expected the fluent syntax to support this. Does Sequel support this out of the box?
I am using the PostgreSQL driver.
According to Jeremy Evans, Sequel does not support the fluent syntax to 3 levels deep.
Instead, the solution is to use lower-level methods to do so:
puts RDB[:store__albums].
select(Sequel.qualify(:store, :albums__title).
as(:album_title)).sql
You're close. I think. :-)
RDB[:store__albums].select(:title___album_title)
Should get you the following SQL:
SELECT "title" AS "album_title" FROM "store"."albums"
Is that not acceptable?
I wonder how to add a function into the "SET" block of an SQL UPDATE with Propel 1.6.
E.g.
UPDATE foo SET myfield = length(:param) WHERE x = 3;
Such functions can be embedded into "->where()" but apparently not into "->update()".
What I'm looking for would be a syntax similar to this:
FooQuery::create()
->filterByX(3)
->update(array("MyField" => array("length(?)", 42));
Can I do this somehow or do I have to write my query as "custom SQL"?
The update array is a series of column names and values. The values are quoted values and hence whatever you put in there will be treated as a string (rather than a function).
Sorry, I can't see any other solution for you.
Can I write this query shorter and/or more elegantly using the AREL/ActiveRecord API?
Foo.where("(bar is not null and bar != '') or (baz is not null and baz !='')")
You can do an OR operator with Arel directly but the syntax isn't hugely pretty and can get a bit hard to read. Here is what it would look like in arel:
foo = Foo.arel_table
Foo.where(foo[:bar].not_eq(nil).and(foo[:bar].not_eq('')).or(foo[:baz].not_eq(nil).and(foo[:baz].not_eq(''))))
I'd suggest looking at the squeel gem. It gives you access to more arel functionality in active record. Your query would like like:
Foo.where{(bar.not_eq nil) & (bar.not_eq '') | (baz.not_eq nil) & (baz.not_eq nil)}
Here is the link for more info: http://erniemiller.org/projects/squeel/
There are a few different ways you can write that in squeel, it supports a few different syntax styles so if you don't liked the one above there are alternatives.
The sane way to deal with this (if you have the rights do do this of course on the database) is to disallow empty string values in the database. Then you can do Foo.where(:bar => nil). I use attribute_normalizer for this, and also to format values correctly.
https://github.com/mdeering/attribute_normalizer
A quick search also reveals nilify blanks, but I haven't tried that because attribute normalizer already provides that functionality for me.
https://github.com/rubiety/nilify_blanks
The resulting sql for the search you want if you have a well kept database (either null or empty string values for empty fields is probably as short as it gets. (I prefer empty strings myself).
using the linqtemplates, I tried getting the linq syntax close to what is in the docs
var query = from c in db.CountyLookups
join s in db.StateLookUps on
c.StateLookupID equals
s.StateLookupID
where c.Name2 == countyName &&
s.Abbr == stateAbbr
select new
{
Latitude = c.Latitude,
Longitude = c.Longitude
};
var result = query.SingleOrDefault();
but when .SingleOrDefault() is called, I get a yellow screen of darn that says:
System.NotSupportedException: The member 'StateLookupID' is not supported
the stack trace ends up at:
SubSonic.Linq.Structure.TSqlFormatter.VisitMemberAccess(MemberExpression m)
the StateLookupID column has underscores in the database and is a regular int pk/fk.
what am I doing wrong?
So apparently VisitMemberAccess has no idea what to do with an int, only string and datetime (starting on line 152 of SubSonic.Linq.Structure.TSqlFormatter). I don't know why this would be called on a join, since a join is usually between an int pk/fk (or guid if you like).
I ended up scrapping the linq query in favor of SubSonic.Query.Select. Here is my new code that works:
var query = db.Select.From<CountyLookup>()
.InnerJoin<StateLookUp>()
.Where(CountyLookupTable.Name2Column)
.IsEqualTo(countyName)
.And(StateLookUpTable.AbbrColumn)
.IsEqualTo(stateAbbr);
I then call ExecuteTypedList and map the results back to my model class. Works like buttah. Just wanted to use linq in this case.
I get this error when I've added properties to my models (the IsValid property as mentioned in ASP.Net MVC 1.0, thanks Rob).
I've had this problem on and off for a bit, and I think I've got it nailed down to the query builder trying to build a query for something that should be done in code, not TSQL.
When it tries to generate the SQL, it descends down the path to generate the TSQL via VisitMemberAccess on a complex type (maybe a another model) but it only knows how to perform operations on datetimes and strings in VisitMemberAccess. I'm sorry if this is a bit incoherent, but I'm trying to get my head around it.
To get around this consider using something like LinqKit AsExpandable prior to any operation which will do the TSQL generation. I've tried this on a simple OrderBy which was going BANG and it appears to work but i have no idea yet what it will do to performance.
Actually I take that back I overcame my problem problem by doing
Stuff.All().Where(x=>x.Someid == id).ToArray()
.AsQueryable()
.Where(x=>x.SomeProp.SomeFlag == true);
It is crud, but it works.
This still appears to be a problem; namely in simple cases such as:
var list = from lang in db.Languages
join site in db.SiteConfigLanguages on
lang.Code equals site.LanguageCode
select lang;
This should evaluate to simple SQL (although pointless in this example):
SELECT Language.* FROM Language LEFT JOIN SiteConfigLanguage ON Language.Code = SiteConfigLanguage.LanguageCode;
It fails inside the same VisitMemberAccess function as (in this case) Language is not a recognisable declaring type (i.e. String or DateTime). It is very similar to the description #matware provided above however it sounds as though the "IsValid" member is pure C# code whereas in this case lang.Code is simply a reference to a column in the database.
I'm currently investigating workarounds as this is only a portion of the larger LINQ query which is failing for me; if I find anything I will post it here. Otherwise, any other solutions/workarounds to this problem known?
UPDATE: Ignore me here; this is simply due to me missing a simple line on the LINQ statement; you need to make sure that you use the "into" keyword to complete things!
i.e.
var list = from lang in db.Languages
join site in db.SiteConfigLanguages on
lang.Code equals site.LanguageCode into sl
from siteLang in sl.DefaultIfEmpty()
select lang;
I've got another error mind you but at least this particular exception is solved. The next one looks a bit nastier unfortunately (inside System.Linq library).