ADF ViewObject- Dynamically change where clause - oracle

<ViewObject name ="emp" selectList="select * from employees" Where= "empno=?" />
inside my action class, I'd like to change the where condition to sal=10 leading to select * from employees Where sal =10.
I don't want the empno column in the WHERE clause.
the vo.setwhereclause(null) isn't clearing the empno =?. It's appending sal=? to existing where clause.
How to solve this?

A simplest way to do this is don't include where param in your view object query but use your where param dynamically through back bean or managed bean function code like,
ViewObject vo1 = applicationmoduleobject.findViewObject("viewobjectname");
vo.setWhereClause(" CREATED_BY = userId and ASSIGNMENT_ID = assignId");
long count = vo.getEstimatedRowCount();
Here, in your view object use simple "select * query" and dynamically add where clause using bean method and execute your view object.

Maybe this will help you solve it:
<ViewObject name="emp" selectList="select * from employees" Where="sal=10" />

By default ViewObject augments where clause to existing sql code.
To avoid this trouble you should use ViewObject.FULLSQL_MODE_AUGMENTATION property. Example:
ViewObject vo = getViewObject();
vo.setFullSqlMode(voi.FULLSQL_MODE_AUGMENTATION);
vo.setWhereClause("sal=:sal"); //or use setSql()
vo.setWhereClauseParam("sal",10.00);
vo.clearCache();
vo.executeQuery();

Dynamic clauses can only be appended to VO query. If there is a need to remove any clause from VO query, there is no method present for that, so the below code can be used as a workaround:
System.out.println("Original query: " +sanctVo.getQuery());
sanctVo.setQuery("select * from employees where sal = :1");
sanctVo.setWhereClauseParam(0, 10);
System.out.println("New query: " +sanctVo.getQuery());
sanctVo.executeQuery();

Related

How to add a parameter to a SQL query?

I would like to add a dynamic parameter in addition to parameters from Maximo inside SQL query.
Something like that :
select *
from workorder a
where params["where"] or a.parent = :Param
with params["where"] with wonum='1234' and :Param = '1234'
Is it possible with Birt to get wonum value and put it also to :Param ?
Or maybe another way ?
Thanks
open is like that (query is more complicated so I simplify it):
maximoDataSet = MXReportDataSetProvider.create(this.getDataSource().getName(), this.getName());
maximoDataSet.open();
var sqlText = new String();
sqlText = "select column1, column2 as woParent, etc... from workorder where " + params["where"] + " or woParent=:param";
maximoDataSet.setQuery(sqlText);
beforeopen is like that(just to see the query) :
importPackage( Packages.java.io );
out = new PrintWriter( new FileWriter( "c:/birteaump.log", true ) );
out.println( "\nMy Query: " + this.queryText);
out.close();
I had some code to manipulate :param to replace it with wonum but this.queryText is null.
I'm a newbie to birt report maybe I have to think differently to solve my problem.
Thanks
I used Birt 3.7.1. I saw in a video that we can add a query in a dataset's dialog box. But with my report I only have "scripted data set" when I use the "new data set" button.
Is it possible that my query is null in "beforeopen" is in relation with that ?
If I create another sort of datasource, I will have acces to another sort of dataset ?
thanks
ok I solved my problem.
I created a JDBC data source and I have access to a new sort of data set.
I can put my query in this data set and I have access to "beforeopen" and my query is not null.
Thanks

Find max value of a column in laravel

The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database.
Like this select, but with eloquent ORM (Laravel):
SELECT MAX(Id) FROM Clientes
How can I do this?
I tried:
Cliente::with('id')->max(id);
Cliente::select('id')->max(id);
I prefer not to make a simple raw SELECT MAX(ID) FROM Clientes
I cannot make it.
Thanks all!
The correct syntax is:
Cliente::max('id')
https://laravel.com/docs/5.5/queries#aggregates
Laravel makes this very easy, in your case you would use
$maxValue = Cliente::max('id');
But you can also retrieve the newest record from the table, which will be the highest value as well
$newestCliente = Cliente::orderBy('id', 'desc')->first(); // gets the whole row
$maxValue = $newestCliente->id;
or for just the value
$maxValue = Cliente::orderBy('id', 'desc')->value('id'); // gets only the id
Or, if you have a created_at column with the date you could get the value like this
$maxValue = Cliente::latest()->value('id');
Relevant Laravel Documentation: https://laravel.com/docs/5.5/queries#aggregates
$maxValue = DB::table('Clientes')->max('id');
Cliente::where('column_name', $your_Valu)->max('id') // You get any max column
We can use the following code :
$min_id = DB::table('table_name')->max('id');
https://laravel.com/docs/8.x/queries#aggregates

Laravel get all the values of a column beginning with a number

I have a model called "Image" and a table called "images". The table has a column for the "id" and another for the "name". I need to fetch only the rows with the name beginning with a number.
I need to fetch are called something like
16783521_facebook.png
While the others are something like...
twiter.png
Try this:
Image::whereRaw("name REGEXP '^[0-9]'") -> get();
If it's something you're going to use in more than 1 place, consider moving it to a scope.
In your image model define something like:
public function scopeNumeric($query)
{
return $query -> whereRaw("name REGEXP '^[0-9]'");
}
Then you can just use:
Image::numeric() -> get();
I dont know much about laravel, but this plain query will help -
SELECT * FROM mytable WHERE mycolumn REGEXP '^[0-9]+$' or
SELECT * FROM myTable WHERE col1 REGEXP '[0-9]+';
Laravel doesn't have that built-in, so you'll have to make do with raw queries. In its base form:
$results = SomeModel::whereRaw("some_column REGEXP '^[0-9]'")->get();
You can modify this as usual with selects, other limitations, etc. as you require.
Filter the images after the query using one of the collection methods. Like below solved me.
$onlyNumeric = $photos->filter(function ($value, $key) {
return is_numeric(substr($value, 0, 1));
});

Nested where clauses codeigniter mysql query

Is there any way to get nested where clauses? e.g.:
SELECT * FROM table WHERE (colA = 'valueA' AND colB = 'valueB') OR (colA = 'valueC' AND colB = 'valueD')
I know I could just write this into a query function call e.g.:
$this->db->query("SELECT ...")
But I was wondering if there was a "proper" way to do it in codeigniter e.g.:
$this->db->where(array('colA'=>'valueA'), array('colB'=>valueB'))->or_where(array('colA'=>'valueC'), array('colB'=>'valueD'))
thanks
With codeigniter 3, now there is, see the update!
There's no where() method usage variant with arrays that would allow you to do that. In these situations i usually just build the part in one long string like this:
$this->db->where("
(
(colA = '".$this->db->escape($v0)."' and colB = '".$this->db->escape($v1)."')
or
(colA = '".$this->db->escape($v2)."' and colB = '".$this->db->escape($v3)."')
)
");
Escaping can be done with escape(does some autodetection) or escape_str or escape_like_str manually depending on what the parameter expected to be or what the predicate in use.
If i'm on a project that uses the Datamapper library, i prefer to use the group_start() and group_end() methods when building these kind of queries, they have a lot of different flavor of these.
Update
Now with, Codeigniter 3 which have grouping methods in the query builder, so you can do ->group_start()s and ->group_end()s.
You can also try like
$this->db->where(condition1);
$this->db->or_where(condition2);

Adding an artificial row to an anonymous Linq resultset

I'm wondering what the best approach to adding an artificial row to an anonymous linq result set would be.
I have a linq statement which uses "select new" to form the data required. Each record comes back as an anonymous object with ID and Name properties. However, I require that the first row of the data becomes an object with ID = NULL, Name="All".
Is there a way to union in an artificial result into the Linq query? Or else, how do I add a new instance of the anonymous type into the anonymous result collection?
You can use the Concat method:
var q = new[]{ new { ID = null, Name = "All" } }.Concat(dbQuery);

Resources