how to pass a constant value to nested column with mybaits association - associations

how can I pass constant value '1111' to a nested column with mybatis association?
<association property="certificateType" column="{VALUE=CERTIFICATE_TYPE,TYPE='1111'}" select="getDict"/>

Depending on what your db is, you can use
IFNULL(#{TYPE}, '1111') (mysql)
ISNULL(#{TYPE}, '1111') (sql server)
In your subquery (getDict).
Another solution is that your parent query can return TYPE as a value
For example by saying:
SELECT '1111' AS TYPE, .....
Then in your column you can just say
column={VALUE=CERTIFICATE_TYPE,TYPE=TYPE}
Hope that helps.

Related

Setting query result as default column value without using triggers

I'm trying to create table with default primary key (not autoincrement), similar with oracle
fk_id varchar2(32) default sys_guid()
so table definition is
CREATE TABLE `t_table` (
`fk_id` TEXT DEFAULT 'select lower(hex(randomblob(16)))' UNIQUE,
`fv_name` TEXT,
PRIMARY KEY(`fk_id`)
);
and yes, i'm getting this select as string value while inserting.
so is there any solution without using triggers?
thank you.
Using 'select lower(hex(randomblob(16)))' is enclosing the subquery as a string/text literal and will work only once as UNIQUE has been specified (no need as making the column PRIMARY KEY implies UNIQUE) thus any subsequent inserts would fail.
Assuming that you want the DEFAULT value to be the result of the of lower(hex(randomblob(16)) then you cannot use a subquery as the value is then not considered as a CONSTANT.
For the purposes of the DEFAULT clause, an expression is considered
constant if it contains no sub-queries, column or table references,
bound parameters, or string literals enclosed in double-quotes instead
of single-quotes.SQL As Understood By SQLite - CREATE TABLE
Instead you could remove the select and just use the expression, which is then considered CONSTANT.
However, to do so, you need to adhere to
If the default value of a column is an expression in parentheses, then
the expression is evaluated once for each row inserted and the results
used in the new row. SQL As Understood By SQLite - CREATE TABLE
Thus you could use :-
CREATE TABLE IF NOT EXISTS `t_table` (
`fk_id` TEXT DEFAULT (lower(hex(randomblob(16)))) UNIQUE,
`fv_name` TEXT,
PRIMARY KEY(`fk_id`)
);
Of course should the value not be unique, which would be increasingly likely, then this would result in the row not being inserted.

how to use Queryresult variable as input in other query in robot framework

I am doing 1 simple db connection test in Robot framework.I am doing as following-
${queryResults1} Query <sql query>
now I want to use the value of ${queryResults1} as input to another query. I am doing
Execute Sql String select * from customer where customer_id=${queryResults1}
here I am getting error .Execute Sql String doesnot get value of queryresult
how can I do this ?
thanks in Advance!!!
The problem is that your first query is returning a list of tuples -- a list of rows, each of which is a tuple of columns. Even though you're apparently expecting a single value from a single column in a single row, the data is still in this format. You need to pull the value out of that list of tuples before passing it to your second query.
For example:
Execute Sql String select * from customer where customer_id=${queryResults1[0][0]}

SQLite.NET output parameter

I have a object that contains data from a DB. The object has a ID, which in the DB is auto increment field. When creating an object i dont know his ID, until i insert a new record to the DB. Then i want to get back the auto generated value of ID field.
I tried to do so by adding a Output parameter, but SQLite.NET throws a NotSupportedException when i try to set the Direction to ParameterDirection.Output.
What can i do? The other fields except ID are not unique.
A other option is to auto increment manually, in the program, but it seems to be a bit complicated to implement.
Thanks a lot.
SELECT last_insert_rowid();
This will get the last inserted ROWID. If the table has a column of type INTEGER PRIMARY KEY then that column is another alias for the rowid.

Column ' ' in where clause is ambiguous Error in mysql

SELECT tbl_user.userid,
tbl_user.firstname,
tbl_user.lastname,
tbl_user.email,
tbl_user.created,
tbl_user.createdby,
tbl_organisation.organisationname
FROM tbl_user
INNER JOIN tbl_organisation
ON tbl_user.organisationid = tbl_organisation.organisationid
WHERE organisationid = #OrganisationID;
I am using this statement to do a databind. I am getting a error here.
Column 'OrganisationID' in where clause is ambiguous
What should I do is it wrong to name the OrganisationID in tbl_user same as tbl_organisation.
OrganisationID is a foreign key from tbl_Organisation
Since you have two columns with the same name on two different tables (and that's not a problem, it's even recommended on many cases), you must inform MySQL which one you want to filter by.
Add the table name (or alias, if you were using table aliases) before the column name. In your case, either
WHERE tbl_user.OrganisationID
or
WHERE tbl_Organisation.OrganisationID
should work.
You just need to indicate which table you are targeting with that statement, like "tbl_user.OrganisationID". Otherwise the engine doesn't know which OrganisationID you meant.
It is not wrong the have the same column names in two tables. In many (even most) cases, it is actually perferred.

linq to sql "Contains"

if i have a field in my table that i want to verify exists, how do i use the contains method to determine if it exists.
i would have thought the contains method just takes in a string but it seems to take in my whole linq data object
Contains is an extension method for IEnumerable that determines whether a given object is present in the enumerable. That's not what you want here.
I'm guessing that you have a LINQ query like this:
IEnumerable<string> productNames = from p in db.Products select p.ProductName;
And now you want to verify that the ProductName field actually exists to avoid run-time errors. There is actually no need to check that. Try replacing p.ProductName by a field that doesn't exist. The compiler will complain.
Of course, this assumes that the actual database schema matches the one used to generate the database class with MSLinqToSQLGenerator.
Not sure how to do it with LINQ but you could do:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE _NAME ='MyTable' and COLUMN _NAME='MyColumn'
then based on the count returned from the query you will know if the column exists or not.

Resources