mysql - execute SELECT query only when TABLE EXIST - mysql

I am working on a Joomla Module where images can be shown and be linked to a Joomla Article, Virtuemart product or an external link.
To have to option to select a Virtuemart Product, I added the following parameter to the module manifest xml:
<field
name="image_1_product"
type="sql"
default="10"
query="SELECT #__virtuemart_products.virtuemart_product_id, #__virtuemart_products_en_gb.product_name
FROM #__virtuemart_products, #__virtuemart_products_en_gb
WHERE #__virtuemart_products.virtuemart_product_id = #__virtuemart_products_en_gb.virtuemart_product_id
AND #__virtuemart_products.published = 1"
key_field="virtuemart_product_id"
value_field="product_name"
label="LABEL_PRODUCT"
description="DESC_PRODUCT">
</field>
This works just fine, when Virtuemart is installed. When Virtuemart is not installed, you get an error 'Unknown column' - which makes sense.
Because this is a Module Manifest Option, I can use only 1 query. Is there a way to run a query that checks if a table exists before it executes the select? Like:
IF EXISTS TABLE `table_a` (SELECT * FROM table_a)
If so, can it return a row like:
| id | value |
| 0 | Virtuemart not found |
Thank you!

I'm afraid its not possible with just single database query.
The only workaround that I can think of is to create a parameter helper for your module and use an addfieldpath to your module fieldset tag. With this solution you can write an method to do the query and create the output, then you can check if the table exists or not before creating your parameter.
for example:
<config>
<fields name="params">
<fieldset name="basic" addfieldpath="/modules/mod_mymodule/elements/">
<field name="image_1_product" type="product" default="10" label="LABEL_PRODUCT" description="DESC_PRODUCT"/>
</fieldset>
</fields>
</config>
now in /modules/mod_mymodule/elements/ create a PHP file named product.php and write your custom query and field descriptions as described in Joomla's documentation.
Joomla documentation is available for Custom parameter types and Creating custom template parameter types

Related

How to create a new column in Apache Solr during full import?

This is the current query I am using for my Apache Solr full import.
<document>
<entity name="id" query="select f_name as 'id', f_name, l_name, AES_DECRYPT(l_name,'key')as decp_col from table_name;"/>
</document>
This is the format I get on Apache Solr.
l_name":["4\u0007{¾Nen•øÕ+·$Õ\u0002"],
"f_name":["Jayde"],
"id":"Jayde",
"_version_":1650635178055303168},
I want it to be imported in a way that I get a decp_col with the decrypted values.
I have tried multiple queries but am unable to generate anything on Sokr side.

How to use the CSV query language?

In JasperSoft Studio or iReport, how do you query on csv data? It has an option to do CSV query language but I cannot find any proper documentation or instructions on how it works.
I need to do group by and add conditional parameters without the need to transfer it first into a database.
The JRCsvQueryExecuter lets you sort and filter data from a cvs file
The sort command can be done on single or multiple fields.
<sortField name="name"/>
<sortField name="city" order="Descending"/>
The filtering is done by the filterExpression
<filterExpression><![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]></filterExpression>
You can not use a normal sql statement but with these 2 properties, you are fairly close to order by and where. Specially since jasper reports have build in support for sum, avg (through variables) and the group by through groups.
<group name="YourGroup">
<groupExpression><![CDATA[$F{fieldToGroupOn}]]></groupExpression>
...the group bands ..
</group>
You can find a full running sample in the jasper reports distribution under demo\samples\csvdatasource\reports\CsvQueryExecuterReport.jrxml, this is the sample reference

For mySQL trigger, need to set field value to selected value

I'm building a Joomla website for a homeowner's association. When a user makes a payment, I want the mySQL jos_Payments table (where the payment gets saved) to automatically grab the ID for the home that user lives in.
I've created a trigger on the jos_Payments table, BEFORE INSERT. I'm trying to set the home_id field of the newly added row to be equal to the cb_home_id value that is looked up in the jos_comprofiler table.
SET NEW.home_id = (SELECT cb_home_id FROM jos_comprofiler WHERE jos_comprofiler.user_id = NEW.user_id);
I've searched for help on this, and have tried defining a variable as an intermediate step, and none of it seems to help. It just keeps telling me I have an error in my SQL syntax. The SELECT statment works outside of the trigger, if I use a valid user_id instead of NEW.user_id.
It seems like this should be so simple. Where am I going wrong?
I presume the code sample is part of that BEFORE INSERT trigger which fails creation.
I had a simular issue with Joomla 2.5, unable to create a trigger during the install or update procedure while the sql statements work fine.
You will not be able to create a trigger, function, procedure or any sql statement containing more than 1 semi-colon (;). That's why all other statements seem to work in your script.
I tried many appoaches which did not work including these:
In the manifest file use sql-file:
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
In the manifest file use install-queries:
<install>
<queries>
<query id="1">
CREATE TRIGGER `...` AFTER UPDATE ON `#__...` FOR EACH ROW
BEGIN
...codeline...;
...codeline...;
END
</query>
<query id="2">
...
</query>
</queries>
</install>
(sorry for using blockquotes in stead of code sample, otherwise post will not be shown correctly)
The only way it did work was via the installscript option available since Joomla 2.5, see https://docs.joomla.org/J2.5:Developing_a_MVC_Component/Adding_an_install-uninstall-update_script_file.
This did not work out-of-the-box. I had to remove the access check (the first line with define) and put the following code in the install($parent) and update($parent) functions to add the trigger:
$db = JFactory::getDbo();
$db->setQuery("DROP TRIGGER IF EXISTS `...`");
$db->query();
$query="CREATE TRIGGER `...` AFTER UPDATE ON `#__...` FOR EACH ROW
BEGIN
...codeline...;
...codeline...;
END";
$db->setQuery($query);
$db->query();
if ($error = $db->getErrorMsg()) {
$this->setError($error);
return false;
}
...next sqlstatement or code...
return true;
Make sure the classname in this installer php file matches your component name including init caps, something like:
class com_YourComponentNameInstallerScript
And also, don't forget to open the file with <?php tag but do not put the ?> end tag at the end.
Save the file as php file in the root of your install package at the same level as the manifest file, and add this filename to the manifest file at somewhere between the component description and administration or site tags:
<scriptfile>yourcomponentname_install.php</scriptfile>
This method will also work with creating Stored Procedures and Functions.

Liquibase script for MYSQL

I have a liquibase xml script. When I run it on Postgres I don't face any problem but when I run it for MYSQL it gives error when the structure is of the following type:-
<insert tableName="user_table">
<column name="id" valueComputed="(select max(id)+1 from user_table)"/>
<column name="name" value="someName"/>
</insert>
When the above script is executed for MYSQL it gives error:-
You can't specify target table 'user_table' for update in FROM clause.
I found a solution to this by using alias like this :-
<insert tableName="user_table">
<column name="id" valueComputed="(select max(id)+1 from (Select * from user_table) t)" />
<column name="name" value="someName"/>
</insert>
But there are thousands of entries like this. Is there any generic way of doing it so that I don't have to change the script at so many places. Thanks.
The easiest approach would be to just update the XML, either with an simple XML parser program or even a regexp search and replace in your text editor.
Alternately, you can override the standard liquibase logic to look for that particular valueComputed pattern and replace it. There are a couple points you could make the change at:
Override the liquibase.parser.core.xml.XMLCHangeLogSAXParser class, probably the parseToNode() method to search through the generated ParsedNode for valueComputed nodes
Override the liquibase.change.core.InsertDataChange class generateStatements() method or addColumn() method to replace valueComputed fields.
See http://liquibase.org/extensions for more info on writing extensions.

How can I set the Nhibernate type generator to increment when working with SQL Server 2008?

Using HBM files to map my types.
One of my classes uses bag of items called PartnerEnv. One of their fields is set to be the id which should be generated using increment. for some reason I am getting the following error:
could not fetch initial value for increment generator[SQL: SQL not available]
Inner details: "{"Invalid object name 'jj.dbo.Partners2Env'."}"
If I change the generation method to assigned everything is ok.
I will appreciate any help given!
Can you set your Id column on the PartnerEnv table (or whatever that table is called) to an Identity column and then use the following in the .hbm file for that class?
<id name="Id" type="Int32">
<column name="Id" />
<generator class="identity" />
</id>