MyBatis, UPPER in a where condition - mybatis

I have to do a select query with a lot of parameters in MyBatis, I want to use the default mapper xml file structure and add an "UPPER(column_name) = ?" to the result converted query. How Can I do ?
Here is my where clause
<sql id="My_Select">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and UPPER(${criterion.condition} #{criterion.value})
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value}
and
#{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem"
open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
and here is the complete select statement:
<select id="mySelect" parameterType="myDataExample"
resultMap="myResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="my_Column_List" />
from istanza_metadato
<if test="_parameter != null">
<include refid="My_Select" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
I always receive error like this one
The error occurred while setting parameters
...
WHERE ( UPPER(id_metadato = ?) and UPPER(valore = ?) )
I feel I'm near to the solution, but I can't find a way. Thanks to all

I resolved my problem, specs were changed and now I do not need the upper anymore.
By the way before that I handled that problem changing the mybatis like query language to pure SQL language.

Related

IBM Host On Demand Macro <condition> message warning action

I am trying to get 3 options condition, but I am going crazy:
In a specific field may appear a number, a text or be an empty field
Conditions:
if any text = warning message pops up: "This in an error "
if any number= [pagedown] action
if empty = warning message pops up: "pls change code "
May someone help me?
Thanks!
<HAScript name="new condition" description="" timeout="60000" pausetime="300" promptall="te" blockinput="false" author="gennaro" creationdate="Nov 19, 2022 8:07:14 PM" supressclearevents="false" usevars="true" ignorepauseforenhancedtn="true" delayifnotenhancedtn="0" ignorepausetimeforenhancedtn="true">
<vars>
<create name="$text$" type="string" value="" />
<create name="$num$" type="integer" value="0" />
<create name="$emptyfield$" type="field" />
</vars>
<screen name="Screen1" entryscreen="true" exitscreen="true" transient="false">
<description >
<oia status="NOTINHIBITED" optional="false" invertmatch="false" />
</description>
<actions>
<extract name="&apos;Extract&apos;" planetype="TEXT_PLANE" srow="6" scol="66" erow="6" ecol="70" unwrap="false" continuous="false" assigntovar="$text$" />
<extract name="&apos;Extract&apos;" planetype="TEXT_PLANE" srow="6" scol="66" erow="6" ecol="70" unwrap="false" continuous="false" assigntovar="$num$" />
<extract name="&apos;Extract&apos;" planetype="TEXT_PLANE" srow="6" scol="66" erow="6" ecol="70" unwrap="false" continuous="false" assigntovar="$emptyfield$" />
<if condition="$string$" >
<input value="" row="0" col="0" movecursor="true" xlatehostkeys="true" encrypted="false" />
<message title="$string$" value="" />
</if>
<if condition="$num$" >
<input value="&apos;[pagedn]&apos;" row="0" col="0" movecursor="true" xlatehostkeys="true" encrypted="false" />
</if>
<if condition="$emptyfield$" >
<message title="$emptyfield$" value="" />
</if>
</actions>
<nextscreens timeout="0" >
</nextscreens>
</screen>
</HAScript>
I was expecting
Conditions:
if any text = warning message pops up: "This in an error "
if any mumber= [pagedoen] action
if empty = warning message pops up: "pls change code "

How to patch insertSelective into database using Mybatis xml foreach tag

<insert id="insertSelectiveBatch" parameterType="cn.indweb.blogcore.repository.model.Article">
<!--
WARNING - #mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Aug 22 10:38:37 CST 2019.
-->
<foreach item="item" collection="list" open="" close="" separator=";">
insert into tbl_article
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="item.id != null">
id,
</if>
<if test="item.title != null">
title,
</if>
<if test="item.author != null">
author,
</if>
<if test="item.content != null">
content,
</if>
<if test="item.createdTime != null">
created_time,
</if>
<if test="item.deletedTime != null">
deleted_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="item.id != null">
#{item.id,jdbcType=VARCHAR},
</if>
<if test="item.title != null">
#{item.title,jdbcType=VARCHAR},
</if>
<if test="item.author != null">
#{item.author,jdbcType=VARCHAR},
</if>
<if test="item.content != null">
#{item.content,jdbcType=VARCHAR},
</if>
<if test="item.createdTime != null">
#{item.createdTime,jdbcType=TIMESTAMP},
</if>
<if test="item.deletedTime != null">
#{item.deletedTime,jdbcType=TIMESTAMP},
</if>
</trim>
</foreach>
This is the code I have tried, but not worked. Error message was "java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into tbl_article...'"
Your statement is provided a parameter of Collection type. You don't need parameterType at all. Please try to remove it and subsequently try run again.

How to do an IF/ELSE in NANT 0.91?

We're using Nant 0.91 so <choose> is not available. How would you do an IF/ELSE in Nant 0.91? I would like to do something like (using NAnt 0.92 syntax) in NAnt 0.91. [I am not allowed to modify the current installation of NAnt 0.91]:
<choose>
<when test="${deploy.env=='PROD'}">
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
</when>
<otherwise>
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
</otherwise>
</choose>
The simplest solution, which we use here, is to just use two if tasks, one with a negative test to the other:
<if test="${deploy.env == 'PROD'}">
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
</if>
<if test="${deploy.env != 'PROD'}">
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
</if>
However in your case, you can also make use of the fact that the property task has inbuilt if/unless functionality:
<property name="deploy.root.dir" if="${deploy.env == 'PROD'}" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
<property name="deploy.root.dir" unless="${deploy.env == 'PROD'}" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
You can set a default property value and then an override if the condition is true:
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
<if test="${deploy.env == 'PROD'}">
<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
</if>

Mule and JDBC Query Result

I am using mule to connect to a Postgres database server.
I can't use the datamapper since I am using the community edition.
The object to xml transformer is not giving a good formatted result.
Any ideas of how I can transform the jdbc response into a good format for parsing.
Here is my configuration file.
This is the datasource with the connector.
<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
user="USERNAME" password="PASSWORD"
url="jdbc:postgresql://*******:***/*****"
transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />
<jdbc:connector name="organizations" dataSource-ref="PostgreSQL_Data_Source"
validateConnections="true" queryTimeout="-1" pollingFrequency="0"
doc:name="JDBC">
<jdbc:query key="getOrganizations" value="SELECT * FROM organization" />
<jdbc:query key="getOrganizationApplications"
value="SELECT * FROM organization o,application a,organization_apps oa WHERE o.id=oa.org_id AND a.id=oa.app_id AND o.id=#[flowVars['org_id']]" />
<jdbc:query key="insertOrganization"
value="INSERT INTO organization(name, phone, email, address, website) VALUES (?, ?, ?, ?, ?)" />
<jdbc:query key="getOrganizationUsers"
value="SELECT * FROM &quot;public&quot;.user WHERE org_id=#[flowVars['org_id']]" />
</jdbc:connector>
and this is the flow:
<flow name="logixy-platform-organizations-workflow" doc:name="logixy-platform-organizations-workflow">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8082" doc:name="HTTP" path="organization" />
<set-variable variableName="choice"
value="#[message.inboundProperties['choice']]" doc:name="Set Choice" />
<choice doc:name="Choice">
<when expression="#[flowVars['choice'] == '0']">
<jdbc:outbound-endpoint exchange-pattern="request-response"
queryTimeout="-1" doc:name="getAllOrganizations" connector-ref="organizations"
queryKey="getOrganizations" />
</when>
<when expression="#[flowVars['choice'] == '2']">
<set-variable variableName="org_id"
value="#[(int)(message.inboundProperties['org_id'])]" doc:name="Set Organization ID" />
<jdbc:outbound-endpoint exchange-pattern="request-response"
queryKey="getOrganizationUsers" queryTimeout="-1" connector-ref="organizations"
doc:name="getOrganizationUsers" />
</when>
<when expression="#[flowVars['choice'] == '1']">
<set-variable variableName="#['org_id']"
value="#[(int)(message.inboundProperties['org_id'])]" doc:name="Set Organization ID" />
<jdbc:outbound-endpoint exchange-pattern="request-response"
queryKey="getOrganizationApplications" queryTimeout="-1"
connector-ref="organizations" doc:name="getOrganizationApplications" />
</when>
</choice>
<mulexml:object-to-xml-transformer
doc:name="Object to XML" />
</flow>
You should really define "a good format for parsing", but I guess you don't like the "entry", "string", etc field names.
You have plenty of options to format the XML, for example:
use XSLT to transform the generated XML to your preferred format
define a class in Java or Groovy that corresponds to your return data and then either
use object-to-json and then json-to-object with your class as return class
assign the return data to objects of your class in Java or Groovy
Once you have your data as custom objects, object-to-xml-transformer will print produce a much cleaner output.

Join two related tables into a single entity using Entity Framework

My question relates to the "table joining" function in the ADO.NET Entity Framework.
Imagine a database that contains a table "Product" and another table "ProductPrice". The price table stores a history of all price changes for a product, with a start and end date, where the line containing the current price is indicated by a NULL value in the end date column. This database structure could be useful for statistical purposes, for example the average daily sales volume could be mapped to each change in the product price. However, for the online ordering website, only the current price is required.
Here's the structure of the two tables:
Product
ProductID (PK, int, NOT NULL, auto increment)
Name (varchar 50, NOT NULL)
ProductPrice
ProductPriceID (PK, int, NOT NULL, auto increment)
ProductID (INT, NOT NULL)
StartDate (DATETIME, NOT NULL)
EndDate (DATETIME)
Price (MONEY, NOT NULL)
Here's an example of an SQL statement to retrieve the product plus the current price:
SELECT Product.ProductID, Product.Name, ProductPrice.Price AS CurrentPrice
FROM Product
LEFT JOIN ProductPrice
ON Product.ProductID = ProductPrice.ProductID
AND ProductPrice.EndDate IS NULL
I'd like to use the Entity Framework to join the entities Product and ProductPrice together, so that I can access the current price directly from the Product entity, as in the following example:
var product = (from p in context.Product where p.ProductID == 2 select p).FirstOrDefault();
Console.WriteLine(product.Name);
Console.WriteLine(product.CurrentPrice);
Unfortunately, I'm getting stuck with errors that I can't resolve.
Here are the entities from the storage model:
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductID" />
</Key>
<Property Name="ProductID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
</EntityType>
<EntityType Name="ProductPrice">
<Key>
<PropertyRef Name="ProductPriceID" />
</Key>
<Property Name="ProductPriceID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ProductID" Type="int" Nullable="false" />
<Property Name="Price" Type="money" Nullable="false" />
<Property Name="StartDate" Type="datetime" Nullable="false" />
<Property Name="EndDate" Type="datetime" />
</EntityType>
<Association Name="FK_ProductPrice_Product">
<End Role="Product" Type="TestingModel.Store.Product" Multiplicity="1" />
<End Role="ProductPrice" Type="TestingModel.Store.ProductPrice" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Product">
<PropertyRef Name="ProductID" />
</Principal>
<Dependent Role="ProductPrice">
<PropertyRef Name="ProductID" />
</Dependent>
</ReferentialConstraint>
</Association>
And from the conceptual model:
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductID" />
</Key>
<Property Name="ProductID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<Property Name="SKU" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<Property Type="Decimal" Name="CurrentPrice" Nullable="false" Precision="19" Scale="4" />
</EntityType>
And finally the mapping between the two:
<EntitySetMapping Name="Product">
<EntityTypeMapping TypeName="TestingModel.Product">
<MappingFragment StoreEntitySet="Product">
<ScalarProperty Name="ProductID" ColumnName="ProductID" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="SKU" ColumnName="SKU" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(TestingModel.Product)">
<MappingFragment StoreEntitySet="ProductPrice">
<ScalarProperty Name="CurrentPrice" ColumnName="Price" />
<Condition ColumnName="EndDate" IsNull="true" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
And here are the error messages that I'm currently struggling with:
Error 1 Error 3024: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (Product.ProductID) of the EntitySet Product.
Error 2 Error 3025: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (ProductPrice.ProductPriceID) of table ProductPrice.
I'm not sure if this is even possible with the Entity Framework, maybe I should just do the join manually myself in LINQ.
Any suggestions would be greatly appreciated.
You do not meet the conditions for entity splitting:
You should only map an entity type to multiple tables if the following conditions are true:
The tables to which you are mapping share a common key.
The entity type that is being mapped has entries in each underlying table. In other words, the entity type represents data that has a one-to-one correspondence between the two tables; the entity type represents an inner join of the two tables.
You are smarter than EF. You know that the condition EndDate == null yields one record. EF has no clue to know that and thus never knows that it can create one object from the two tables. In terms of the exception message: PriceId and EndDate == null should somehow deliver all key properties of your ProductPrice records, which is impossible, obviously.
Alternatives:
A. You can create a one-to-many association between the two and query:
products.Where(p => p.ProductId == 2)
.Select(p => new
{
Product = p,
CurrentPrice = p.ProductPrices.Where(pp => pp.EndDate == null)
.FirstOrDefault()
})
Not as convenient as you'd like, but maybe not too bad when wrapped in a repository.
B. Create a database view and use separate paths for viewing and updating (which is not an uncommon thing to do).
You need to do a foreign key between the two tables (i.e. on column ProductID)
Then to retrieve all the products together with their prices you need to do the following:
var x = from p in context.Product where p.ProductID == 2 && p.ProductPrice.EndDate == null select new { p.ProductID, p.Name, p.ProductPrice.Price }.FirstOrDefault();