Grails get() generating incomplete SQL - database

I'm upgrading an app from Grails 1.3.6 to 2.1.1. Have hit what seems to be a very strange issue.
I have a table called SECTION. This call:
def testSection = Section.get(94725)
Generates this SQL in Grails 1.3.6:
select
section0_.ID as ID42_0_,
section0_.concept_tag_uri as concept2_42_0_,
section0_.INDEX_ID as INDEX3_42_0_,
section0_.LIVE_IND as LIVE4_42_0_,
section0_.NAME as NAME42_0_,
section0_.PARENT_ID as PARENT6_42_0_,
section0_.ASST_INDEX_ASSET_ID as ASST7_42_0_,
section0_.ORDER_NO as ORDER8_42_0_,
section0_.SITE_ID as SITE9_42_0_,
section0_.TYPE_ID as TYPE10_42_0_,
(SELECT
uk_sec.id
FROM
section uk_sec,
site uk_site
WHERE
uk_sec.index_id = section0_.index_id
AND uk_sec.site_id = uk_site.siteid
AND uk_site.audienceid = 1) as formula1_0_
from
SECTION section0_
where
section0_.ID=?
But this SQL in 2.1.1:
select
section0_.ID as ID45_0_,
section0_.concept_tag_uri as concept2_45_0_,
section0_.INDEX_ID as INDEX3_45_0_,
section0_.LIVE_IND as LIVE4_45_0_,
section0_.NAME as NAME45_0_,
section0_.PARENT_ID as PARENT6_45_0_,
section0_.ASST_INDEX_ASSET_ID as ASST7_45_0_,
section0_.ORDER_NO as ORDER8_45_0_,
section0_.SITE_ID as SITE9_45_0_,
(SELECT
uk_sec.id
FROM section uk_sec,
site uk_site
WHERE uk_sec.index_id = section0_.index_id
AND uk_sec.site_id = uk_site.siteid
AND uk_site.audienceid = 1) as formula1_0_
from
SECTION section0_ where section0_.ID=?
Looks similar, but this part is missing in the 2.1.1 version:
section0_.TYPE_ID as TYPE10_42_0_,
This is the mapping in the class:
...
String sectionType
...
static mapping =
{
table 'SECTION'
version false
id generator:'assigned'
columns {
...
sectionType column: 'TYPE_ID'
...
}
}
The DB field in question is: TYPE_ID CHAR(1) but as the SQL is generated as a result of the get(), am assuming it's nothing to do with the actual DB schema or column(?).
The fact that it's the final element in the list of select fields makes me think it's somehow related, but I don't know how to change that, or if doing so would just 'delete' another field from the statement.
Anyone seen anything like this?
Cheers,
spacebot

Maybe somewhat hasty to think it might be a bug.
The parameter type of the overridden setter is required for Grails2.1.1, it's more forgiving in 1.x.
public void setType(type) { ... } // works in 1.3.6, breaks 2.1.1
public void setType(String type) { ... } // ok in 2.1.1

Related

I am getting an error in Kapt Debug Kotlin. I have update versions of dependencies in gradle file. still facing this issue

My app was running smoothly but I am getting this error now.I am getting an error in Kapt Debug Kotlin. I have update versions of dependencies in gradle file. still facing this issue. How it can be resolved? I saw somewhere to see your room database , dao and data class. still not able to figure out what is the issue.
The error is showing this file
ROOM DATABASE
#Database(entities = [Transaction::class], version = 1, exportSchema = false)
abstract class MoneyDatabase : RoomDatabase(){
abstract fun transactionListDao():transactionDetailDao
companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
#Volatile
private var INSTANCE: MoneyDatabase? = null
fun getDatabase(context: Context): MoneyDatabase {
// if the INSTANCE is not null, then return it,
// if it is, then create the database
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
MoneyDatabase::class.java,
"transaction_database"
).build()
INSTANCE = instance
// return instance
instance
}
}
}
}
DAO
#Dao
interface transactionDetailDao {
#Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(transaction : Transaction)
#Delete
suspend fun delete(transaction : Transaction)
#Update
suspend fun update(transaction: Transaction)
#Query("SELECT * FROM transaction_table ORDER BY id ASC")
fun getalltransaction(): LiveData<List<Transaction>>
}
DATA CLASS
enum class Transaction_type(){
Cash , debit , Credit
}
enum class Type(){
Income, Expense
}
#Entity(tableName = "transaction_table")
data class Transaction(
val name : String,
val amount : Float,
val day : Int,
val month : Int,
val year : Int,
val comment: String,
val datePicker: String,
val transaction_type : String,
val category : String,
val recurring_from : String,
val recurring_to : String
){
#PrimaryKey(autoGenerate = true) var id :Long=0
}
The error is resolved. I was using the kotlin version 1.6.0. I reduced it to 1.4.32. As far as I understood, above(latest) version of Kotlin along with Room and coroutines doesn’t work well.
I believe that your issue is due to the use of an incorrect class being inadvertently used, a likely culprit being Transaction as that it also a Room class
Perhaps in transactionDetailDao (although it might be elsewhere)
See if you have import androidx.room.Transaction? (or any other imports with Transaction)?
If so delete or comment out the import
As an example with, and :-
And with the import commented out :-
Imported from github, had a play issue definitely appears to be with co-routines. commented out suspends in the Dao :-
#Dao
interface transactionDetailDao {
#Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(transaction : Transaction)
#Delete
suspend fun delete(transaction : Transaction)
#Update
suspend fun update(transaction: Transaction)
#Query("SELECT * FROM transaction_table ORDER BY id ASC")
fun getalltransaction(): LiveData<List<Transaction>>
}
Compiled ok and ran and had a play e.g. :-

Unable to get the returned value while calling Stored Procedure from JPA

The SP looks like:-
CREATE PROCEDURE <spnamegoeshere>
AS
BEGIN
/* Some code goes here */
return 102323 /*Need to get this value*/
END
GO
We are executing a Stored Procedure (hosted in SQL SERVER), through JPA using
createStoredProcedureQuery("spnamegoeshere");
I need to get that returned value "102323". Unfortunatly, using above command, unable to get.
After searching a bit, got this link.
https://learn.microsoft.com/en-us/sql/connect/jdbc/using-a-stored-procedure-with-a-return-status?view=sql-server-2017
As per the above doc, I can able to get the value, if I use .
CallableStatement
Any way to do the same using the JPA, without modifying the SP?
Could someone share some input here.
This works for me:
#Test
public void testProc() {
SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate);
Map<String, Object> map = call.withProcedureName("retValTestProc").withReturnValue().execute();
Assert.assertNotNull("Expected not null result but it is", Objects.nonNull(map));
Assert.assertTrue("Expected size = 1, actual=" + map.size(), map.size() == 1);
Assert.assertTrue("Expected value=102323, actual=" + map.get("RETURN_VALUE"),
Objects.equals(102323, map.get("RETURN_VALUE")));
}
Where retValTestProc = <spnamegoeshere>
Also you can create a SimpleJdbcCall with a DataSource constructor

DateTime Issue with Laravel 5.2 & MS SQL Server

Found this, but it didn't help: https://laracasts.com/discuss/channels/eloquent/timestampdatetime-issue-with-51-sqlserver
When I set my date field on the model like so protected $dates = ['OrderDate','TimeStamp']; then call $order->OrderDate I get the following error:
InvalidArgumentException with message 'Unexpected data found.
Unexpected data found.
The separation symbol could not be found
The format separator does not match
Trailing data'
But, if I manually create a Carbon date object using the same formatting and copying\pasting the date directly from SQL Server, it does so successfully:
>>> Carbon\Carbon::createFromFormat('Y-m-d H:i:s.000', '2015-12-29 00:00:00.000');
=> Carbon\Carbon {#835
+"date": "2015-12-29 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
What am I missing?
For some reason Laravel was seeing it as M j Y h:i:s:000A -- probably the driver. I added protected $dateFormat = 'M j Y h:i:s:000A'; to all the MS SQL Server models I am using.
The workaround would be to setup accessors and mutators and parse date manually, instead of putting it in $dates array.
Something like that:
public function getOrderDateAttribute($value)
{
return Carbon::createFromFormat('Y-m-d H:i:s.000', $value);
}
public function getTimeStampAttribute($value)
{
return Carbon::createFromFormat('Y-m-d H:i:s.000', $value);
}
public function setOrderDateAttribute($value)
{
$this->attributes['OrderDate'] = Carbon::createFromFormat('Y-m-d H:i:s', $value)->format('Y-m-d H:i:s.000');
}
public function setTimeStampAttribute($value)
{
$this->attributes['TimeStamp'] = Carbon::createFromFormat('Y-m-d H:i:s.000', $value)->format('Y-m-d H:i:s.000');
}

LINQ to SQL Data Context logs don't show the WHERE clause

The following is the C# code and generated SQL in a LINQ to SQL query for two cases.
Case 1
using (JulianDataContext dc = new JulianDataContext(this.CurrentConnectionString))
{
#if DEBUG
dc.Log = new DebugTextWriter();
#endif
IEnumerable<UserNewsfeedDeliveryTime> temp = dc.UserNewsfeedDeliveryTimes.Where(u => u.NewsfeedEmailPeriodicity > 0 && DateTime.Today >= u.NextNewsfeedDelivery.Value.Date);
ids = temp.Select(p => p.Id).ToList();
}
SELECT [t0].[Id], [t0].[NewsfeedEmailPeriodicity], [t0].[LastSentNewsfeedEmail], [t0].[NextNewsfeedDelivery]
FROM [dbo].[UserNewsfeedDeliveryTimes] AS [t0]
WHERE ([t0].[NewsfeedEmailPeriodicity] > #p0) AND (#p1 >= CONVERT(DATE, [t0].[NextNewsfeedDelivery]))
-- #p0: Input Int (Size = -1; Prec = 0; Scale = 0) [0]
-- #p1: Input DateTime (Size = -1; Prec = 0; Scale = 0) [15-11-2012 00:00:00]
Case 2
using (JulianDataContext dc = new JulianDataContext(this.CurrentConnectionString))
{
#if DEBUG
dc.Log = new DebugTextWriter();
#endif
IEnumerable<UserNewsfeedDeliveryTime> temp = dc.GetTable<UserNewsfeedDeliveryTime>();
temp = temp.Where(u => u.NewsfeedEmailPeriodicity > 0 && DateTime.Today >= u.NextNewsfeedDelivery.Value.Date);
ids = temp.Select(p => p.Id).ToList();
}
SELECT [t0].[Id], [t0].[NewsfeedEmailPeriodicity], [t0].[LastSentNewsfeedEmail], [t0].[NextNewsfeedDelivery]
FROM [dbo].[UserNewsfeedDeliveryTimes] AS [t0]
The difference
The difference between these two linq queries:
dc.UserNewsfeedDeliveryTimes
and
dc.GetTable<UserNewsfeedDeliveryTime>()
Why? Could it be that, in case 2, LINQ to SQL is retrieving all data from database and finish the query by filtering all objects in memory?
If so, how can we make keep this generic and still force all the T-SQL to be generated?
Solution
Both answers, are correct but I had to pick one, sorry! I think also it is interesting to add that in this case, since I changed to work with an IQueryable (inherits from IEnumerable), in this line:
temp = temp.Where(u => u.NewsfeedEmailPeriodicity > 0 && DateTime.Today >= u.NextNewsfeedDelivery.Value.Date);
I had two overload methods, one from the IQueryable interface and another to the IEnumerable interface.
public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
So I had to convert my predicate explicitly to Expression> predicate, otherwise the IEnumerable interface method would have been picked up at compile time and, if I am not mistaken, I would get some dynamic sql exception saying the T-SQL could not have been generated.
From my understanding, IEnumerable does not transform the original query information that IQueryable holds. It's almost as if the cast freezes any changes to the IQueryable query at the point of casting. If you look at MSDN, it turns out that IQueryable inherits IEnumerable:
http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx
Hence, you see this behaviour. It is important with LINQ-SQL to work with IQueryable unless you want the query frozen at the point it is turned to an IEnumerable.
In your first example, the where is inclusive of the original query. The select is not hence the query generated.
In your second example, you capture the table itself into an IEnumerable. Any changes on top of this are done in memory on top of the original query.
When you think, the IEnumerable version of where will not be able to transform the original data of the IQueryable due to the cast and how inheritance works.
When you also consider deferred loading, and how LINQ works, this seems to make sense.
To me it is a big annoyance, as it can lead you into generating some terrible performing code.
Try using IQueryable instead of IEnumerable.
Weird, because on my examples I get the opposite results from you, ie with IEnumerable, case 1 works fast and case 2 retrieves all the data. But using IQueryable fixes the issue.

RIA-Services - how to WhereOr or use an IN style construct

I am using SL 4, WCF RIA Services against Entity Framework 4.0. I have an Entity, Visit, that has a string Status field. I have a search screen where I need to display results that have StatusA or StatusB. I am struggling to find a way to specify a client-side query that specifies a collection of statuses that should be matched. If I was to write what I want in SQL it would look something like:
select * from Visit where Status in ('StatusA', 'StatusB');
Client side, it appears to be straightforward to chain Where methods for a WhereAnd effect:
var query = this.PqContext.GetVisitsQuery();
if (!string.IsNullOrEmpty(this.PracticeName))
{
query = query.Where(v => v.PracticeName.ToUpper().Contains(this.PracticeName.ToUpper()));
}
if (this.VisitDateAfter.HasValue)
{
query = query.Where(v => v.VisitDate > this.VisitDateAfter);
}
if (this.VisitDateBefore.HasValue)
{
query = query.Where(v => v.VisitDate < this.VisitDateBefore);
}
However, I can't seem to find a straightforward way to do a WhereOr style operation. I have tried this:
var statuses = new List<string>();
if (this.ShowStatusA)
{
statuses.Add("StatusA");
}
if (this.ShowStatusB)
{
statuses.Add("StatusB");
}
if (statuses.Any())
{
query = query.Where(BuildContainsExpression<Visit, string>(e => e.Status, statuses));
}
Where BuildContainsExpression looks like:
private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector)
{
throw new ArgumentNullException("valueSelector");
}
if (null == values)
{
throw new ArgumentNullException("values");
}
ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
{
return e => false;
}
var equals =
values.Select(
value =>
(Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
But this throws a "Bitwise operators are not supported in queries." exception. Any clues? Is there an alternative way to build an expression tree that works here or do I need to pass all the parameters over to the server and use the BuildContainsExpression there?
Your time and your guidance are much appreciated.
You can create a query method such as the following in your domain service:
GetVisitsByStatus(string[] statusList) {
// create the LINQ where clause here
}
And then from the client, call context.GetVistsByStatusQuery(string[]).
Not all of LINQ is (or even can) be exposed over the URL, so there are always cases where you need to use simple parameters, and have the middle tier construct the LINQ expressions that eventually define the query that goes to the back-end data store.
Hope that helps.

Resources