Liferay CustomSQL table doesn't exist - liferay

I have a service entity declaration which is as follows:
<entity name="MyContentRepo" local-service="true" remote-service="true" table="contentrepo">
</entity>
I am trying to use Custom SQL to fetch some details:
session = openSession();
String sqlQueryString = CustomSQLUtil.get("query_id");
SQLQuery query = session.createSQLQuery(sqlQueryString);
query.addEntity("MyContentRepo", MyContentRepoImpl.class);
QueryPos qPos = QueryPos.getInstance(query);
qPos.add("someparameter");
list = (List<MyContentRepo>) query.list();
But I get the following error upon execution:
08:02:26,640 ERROR [http-bio-8090-exec-72][JDBCExceptionReporter:82] Table 'mysqldb.mycontentrepo' doesn't exist
com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.SQLGrammarException: could not execute query
The query is taking the name of entity declared not the table="contentrepo". can anyone tell me how to bypass this issue?

AddEntity once the SQLQuery has been translated to QueryPos. Change your code as given below:
session = openSession();
String sqlQueryString = CustomSQLUtil.get("query_id");
SQLQuery query = session.createSQLQuery(sqlQueryString);
QueryPos qPos = QueryPos.getInstance(query);
qPos.add("someparameter");
query.addEntity("MyContentRepo", MyContentRepoImpl.class);
list = (List<MyContentRepo>) query.list();

Related

N1QL query not recognizing parameters

I am trying to use parameterized N1QL Query but its not recognizing the json place holders and throwing incorrect syntax exception.
Code:
public static final String LMR_DETAILS
= "SELECT * FROM $bucketName WHERE lmr.lmrStatusDescriptionTe ='PENDING'and STR_TO_MILLIS(lmr.recordExpirationTs) BETWEEN STR_TO_MILLIS($startTime) AND STR_TO_MILLIS($endTime)";
String bucketName = bucket.bucketManager().info().name();
JsonObject placeHolders = JsonObject.create().put("bucketName", bucketName).put("startTime", reqDates[0]).put("endTime", reqDates[1]);
N1qlQuery query = N1qlQuery.parameterized(QueryString.LMR_DETAILS, placeHolders);
N1qlQueryResult result = bucket.query(query);
After FROM clause requires as static identifier refer to bucket or subquery, It can't be parameterized variable. Remove $bucketName as parameterized variable and replaces with actual bucket name by constructing LMR_DETAILS dynamically.

Putting string after a where clause in a soql query in salesforce

I have the filter logic parsed and put as a string in a variable called "where_clause". I have to use this where_clause in a query to fetch data.How can I use a string of this type after the where part of the query?I am working on salesforce with custom objects.
I suggest you check Dynamic SOQL.
In particular
String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');
If you have your Custom Object as MyCustomObject__c
String myTestString = 'TestName';
List<MyCustomObject__c> sobjList = (List<MyCustomObject__c>)Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');

Liferay custom-sql using IN operator

I am using Liferay 6.1, Tomcat and MySQL. I have a custom-sql sentence for a list portlet. The custom-sql uses two parameters: an array of groupIds and a result limit.
SELECT
count(articleId) as count,
...
FROM comments
WHERE groupId IN (?)
GROUP BY articleId
ORDER BY count DESC
LIMIT 0, ?
My FinderImpl class has this method:
public List<Comment> findByMostCommented(String groupIds, long maxItems) {
Session session = null;
session = openSession();
String sql = CustomSQLUtil.get(FIND_MOST_COMMENTS);
SQLQuery query = session.createSQLQuery(sql);
query.addEntity("Comment", CommentImpl.class);
QueryPos queryPos = QueryPos.getInstance(query);
queryPos.add(groupIds);
queryPos.add(maxItems);
List<Comment> queryResult = query.list();
return queryResult;
}
This returns 0 results. If I remove the WHERE IN(), it works.
Is IN a valid operator? If not, how can search within different groups?
Perhaps hibernate is quoting your string of groupIds (presumably it is in the form of "1,2,3,4" and when hibernate translates this to sql it is putting quotes around it for you?
You may want to try something like this (from Liferay itself):
String sql = CustomSQLUtil.get(FIND_BY_C_C);
sql = StringUtil.replace(sql, "[$GROUP_IDS$]", groupIds);
And include ([$GROUP_IDS$]) in place of the (?) in your SQL

How can I access array index in Kohana

I am getting the result set in an array. Now I am trying to access the id that is the first index of the array but getting an error. Please let me know how to access the indexes of array.
$email_template = DB::query(Database::SELECT,"select * from mail_settings where id = " .$email['id'])->execute();
When you just run the execute method on a query, you get back a Database_MySQL_Result object.
To return an array instead, use the as_array method like so:
$email_template = DB::query(Database::SELECT,
"select * from mail_settings where id = " .$email['id'])
->execute()->as_array();
Now you will be able to access the resultset as an array.
If all you want/need is the first or current row from the query, you can use the current method which you can read more about in the Kohana_Database_MySQL_Result class:
$email_template = DB::query(Database::SELECT,
"select * from mail_settings where id = " .$email['id'])
->execute()->current();
You are getting a Database_MySQL_Result object, not an array.
This will be correct.
$email_template = DB::query(Database::SELECT,
"select * from mail_settings where id = " .$email['id'])
->execute()->current();

Cant browse through results of GetListItems using LINQ to XML

I'm getting the data from sharepoint wbesite using
activeItemData = ws.GetListItems(listGUID, activeItemViewGUID,
qNode, vNode, rowLimit, null, "");
Here is what I got from the sharepoint its one record.
<**rs:data** ItemCount="1" ListItemCollectionPositionNext="Paged=TRUE&p_ID=1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<**z:row** ows_ID="1" ows_Title="My RFC Title number one" ows_GUID="{A73B8E91-98BF-4CA7-8ADB-A3B933D6D8DA}" ows_Req_x0020_Number="112343" ows_Ends="2010-07-17 00:00:00" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_Start="2010-07-08 00:00:00" ows_owshiddenversion="13" ows_UniqueId="1;#{EA9E9C87-1B28-47DE-B7F4-DA4ADDF913F6}" ows_FSObjType="1;#0" ows_Created="2010-07-07 11:05:56" ows_Status="1. In-Work" ows_FileRef="1;#sites/PS/Lists/Change Control/1_.000" xmlns:z="#RowsetSchema" />
</rs:data>
Now to use LINQ to XML I change the object which hold this data from XMLDocument to XDocument by using
XDocument results = XDocument.Parse(activeItemData.OuterXml);
and the problem is that once I try to browse through the data to obtain ID and Title I got the error: Object reference not set to an instance of an object.
Here is the piece of code which I'm trying to use to get those fields:
var items = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
select new
{
Title = item.Attribute("Title").Value,
Id = item.Attribute("ID").Value
};
Seems that item is unable to locate this XName element
Thanks
Try:
var items = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
select new
{
Title = item.Attribute("ows_Title").Value,
Id = item.Attribute("ows_ID").Value
};
If you look at the row element:
<z:row ows_ID="1" ows_Title="My R.....
you were missing ows_ from the attribute name.

Resources