How would I pass parameter and check it in mybatis dynamic query creation - ibatis

I have written a select query inside my mapper.xml
<select id="getCatCount" parameterType="String" resultType="int">
select count(*) from Categories where status is not null
<if test="catTypeId != 0">
AND cat_type_id = #{catTypeId,jdbcType=INTEGER}
</if>
</select>
and in mapper.java method is
int getCatCount(int catTypeId);
How can I check for catTypeId in if condition. I know the above statement is incorrect but I want to put condition like this so I check if catTypeId not zero then only add AND condition. Or do I need to pass whole object of Category class?

You do not need to pass the whole Category class. Just do it as you described:
int getCatCount(int catTypeId);
Your mapper.xml should look like this:
<select id="getCatCount" parameterClass="map" resultType="int">
select count(*) from Categories where status is not null
<if test="param1 != 0">
AND cat_type_id = #{param1,jdbcType=INTEGER}
</if>
</select>
Notice that as parameterClass you need to specify a map.
Let's say that now, you want to pass two parameters:
int getCatCount(int catTypeId, int mySecondParam);
In the mapper, you should work with param1 and param2
See, how you need to use "paramX" nomenclature. But let's say, that instead using that nomenclature, you would like to use a customized parameter name as "catTypeId". For that, in you Java code you need to do this:
int getCatCount( #Param("cat_type_id ") String cat_type_id);
The XML mapper would be the one that I put it about, but using cat_type_id instead param1.

Related

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

How to select a specific array number in a for each loop ASP Classic

I am programming a select box in which a user can select from multiple values (up to 72). Im population the select box by using a for each loop to go through the array, like so:
For Each item In myArray
ItemUur = PadDigits(Hour(item),2)
ItemMinuut = PadDigits(Minute(item),2)
ItemTotaal = ItemUur & ":" & ItemMinuut
%>
<OPTION NAME="van" VALUE="<%=ItemTotaal%>"><%=ItemTotaal %></OPTION>
<%
Next
The array are time values (I am using the paddigits function to output the time as 05:00).
This works great. Though I want the select box to start at a default value, which isnt the first array (05:00) but instead the 10th array value (07:30).
I tried accomplishing this like so:
<OPTION NAME="van" VALUE="<%=ItemTotaal%>"><%=ItemTotaal(10) %></OPTION>
This is not working so then I thought I should be using the original array so I tried with item(10) as well, got the same error though..
The error message I am receiving is:
type mismatch 'item' / 'ItemTotaal' (depends on which I am using)
How can I get this to work?
<OPTION NAME="van"><%=myArray(10)%></OPTION>
ItemTotaal in your case -- just variable (not array)
myArray in your case -- array. so need to use it for access items in array
error you get because you are trying to use "Not Array" variable as Array variable
You were trying to use the variable as array.
The format should be as ArrayName(ArrayElementPosition)
<OPTION NAME="van" VALUE="<%=myArray(9) %>"><%=myArray(9) %></OPTION>
Note: to get the 10th element, i have mentioned as myArray(9) instead of myArray(10) as the array starts its position in 0.
Hope this helps.

With sunspot-rails, how do I conditionally index specific fields?

I would like to add a conditionally index some data inside the sunspot 'searchable' method in my model. Ideally it would look something like this:
searchable do
string :important_text
if address_visible?
string :address
end
end
In the above example, I would like to index the address field only if the address_visible? method (on the model) returns true. Unfortunately, the address_visible? method throws a 'NoMethodError' because the context is now a Sunspot::DSL::Fields, not the model.
I don't think you can actually do exactly what you want. Nonetheless you could index a different value for address when address is not visible. For example:
searchable do
string :important_text
string :address { |model| model.address_visible? ? model.address : '' }
end

CakePHP - the SQL log shows a working query which returns a row, but results variable is NULL?

So, here's my controller code:
public function admin_index(){
$clients = $this->paginate();
$this->set(compact('clients'));
}
And here's the relevant query from my SQL dump for this page:
SELECT `Client`.`id`, `Client`.`user_id`, `Client`.`first_name`, `Client`.`last_name`, `Client`.`company`, `Client`.`phone`, `Client`.`created`, `Client`.`modified`, (IF(LENGTH(company) > 0,company,CONCAT(first_name," ",last_name))) AS `Client__name` FROM `clients` AS `Client` WHERE 1 = 1 LIMIT 20
This query returns 1 row (under Num rows, 1 is displayed, just to be clear :).
In the view (or the controller), var_dump($clients) gives NULL
I can't understand why that is happening!
In the controller change:
$this->set(compact('clients'));
for
$this->set('clients', compact('clients'));
Because if not the variable in the view has no name, so you can't access it.
Well, I feel like a bit of a chump...
The offending code was actually in the model file, client.php.
public function afterFind(){
//todo
}
I was planning on writing some code here, but neglected to. afterFind needs to return an array of results. I wasn't doing so, hence all calls to Client::find (ie via ClientsController::paginate) were returning NULL, not even an empty array.

Filtering a DataSet using Select NOT LIKE (ASP.net 2 Webforms)

I'm trying to query a dataset from a stored-procedure but I'm making a mess somewhere..
specials.DataSource = ds.Tables["specials"].Select("mycolumnname NOT Like 'BB'%");
Any ideas whats wrong with the above statement.
Percent symbol in the wrong place
specials.DataSource = ds.Tables["specials"].Select("mycolumnname NOT Like 'BB'%");
should be
specials.DataSource = ds.Tables["specials"].Select("mycolumnname NOT Like 'BB%'");
I believe you need to include the percent sign inside the single quotes:
.Select("mycolumnname NOT Like 'BB%'");

Resources