example:
If i pass value for 1 parameter (only name) : search by name
"select * from address where name = ?";
If i pass value for 2 parameter (name and gender) - search by name and gender:
"select * from address where name = ? and gender = ?";
I have mupliple search fields. 5 fields. If user enters any combination. i have search only based on parameter. How to dynamically pass the parameters to the sql. Need snippet/Example how to achieve this.
Related
I have the following model. Each instance is part of a group which is only defined as the string GroupName here because the actual group is defined in a different service using a different database.
type Instance struct {
gorm.Model
UserID uint
Name string `gorm:"index:idx_name_and_group,unique"`
GroupName string `gorm:"index:idx_name_and_group,unique"`
StackName string
DeployLog string `gorm:"type:text"`
Preset bool
PresetID uint
}
I'd like to scan, the above model, into the following struct. Thus grouping instances why their group name.
type GroupWithInstances struct {
Name string
Instances []*model.Instance
}
I'm been trying my luck with the following gorm code
var result []GroupWithInstances
err := r.db.
Model(&model.Instance{}).
Where("group_name IN ?", names).
Where("preset = ?", presets).
Group("group_name").
Scan(&result).Error
indent, _ := json.MarshalIndent(result, "", " ")
log.Println(string(indent))
But I'm getting the following error
ERROR: column "instances.id" must appear in the GROUP BY clause or be used in an aggregate function (SQLSTATE 42803)
I'm not sure how to deal with that since I don't want to group by instances but rather their groups.
The error indicates that your RDBMS is in Full Group By Mode - cannot select field that isn't in group by clause or used in an aggregate function (SUM, AVG,...). There are 2 solutions:
Disable Full Group By mode. Example in MySQL
Modify the query
Even when we go with Solution 1, gorm will throw another error about relationship between GroupWithInstances and Instance.
So I think we should review the feature and go with Solution 1 - only select what is needed.
I'm trying to get specific rows from the table by ids, but what I have below is not working.
#Query(value = "SELECT * FROM row r where r.row_id = :row_ids", nativeQuery = true)
List<Object> temp(#Param("albumsIds") String row_ids);
row_ids is all the ids separated by an "or" - id:1 or id:2 or id:3
I'm just trying to do select * from row r where r.row_id = id:1, or r.row_id = id:2, or r.row_id = id:3
Does anyone have an idea what the problem is, or is there a better way to do it?
Simply use:
List<Object> findByIdIn(Collection<Integer> ids);
That will automatically setup the derived query from the query method name itself.
Alternatively, if you want to provide the query programmatically, then the query should be:
#Query(value = "SELECT * FROM row R WHERE R.row_id IN :ids", nativeQuery = true)
List<Object> temp(#Param("ids") Collection<Integer> ids);
MySQL lets use use the IN keyword in queries, which let's us provide a CSV for the IDs to parse and returns any records which ID is in the CSV. This obviously can be used on any data, just for this purpose we'll use the IDs as an example.
Using the method above should minimize the risk of SQL injection significantly, as the Java Collection type casting shouldn't allow a user to provide any values that can cause issues.
I am using Odoo v10. While scanning a barcode, a string contains some characters of a char field value. For example,
A field value ('tracknum') = "20171103"
Search the field by entering a string "xxxxxx20171103" or "xxxx20171103yyy"
is there any way to do it?
I have modified the search view :
<field name="tracknum" string="Tracknum" filter_domain="..."/>
How to dig out related records?
You can create an auxiliar computed field like this
custom_name = fields.Char(
string='Custom',
compute='_compute_custom_name',
search='_search_custom_name'
)
#api.multi
#api.depends()
def _compute_custom_name(self):
''' The field has to be a computed field
You do not need to do anything here
'''
pass
def _search_custom_name(self, operator, value):
''' Actually this converts a domain into another one.
With this new domain Odoo can search well
Arguments:
* operator: if you are searchig words it is going to be ilike
* value: the string ro search
The method could return something like this
* [('id', 'in', id_list)]
'''
all_records = self.search([]) # recordset with all the values of the current model
ids = []
if operator == 'ilike':
ids = all_records.filtered(lambda r: r.tracknum in value).mapped('id')
return [('id', 'in', ids)]
Then you can add this field to the search view like this:
<field name="custom_name" string="Tracking Number" />
Keep in mind that it is not a stored field, so it is going to be very inefficient. And you should iterate over all the values each time you want to make a search.
Once you have added the field to the search view it shoul look like this, Tracking Number should appear in the field name
I need to find all the documents in a database that the field wfsCurrApp contains a given role. in the search bar I entered field WFSCurrApp contains [Finance] but I get the error that the query is not understandable. If I change it to field WFSCurrApp contains "[Finance]" the query runs but returns nothing.
What I'm ultimately trying to get done is something like this because the user may possess several roles in the target database I believe the issue is the [] as that is used to delimit dates, but ????:
var uRoles = thisDB.queryAccessRoles( context.getUser().getDistinguishedName());
var userRoles:String = "";
var it:java.util.Iterator = uRoles.iterator();
while (it.hasNext()){
var thisRole = it.next()
if (userRoles == "") {
userRoles = thisRole
}else{
userRoles = userRoles + " OR " + thisRole;
}
}
tmpArray[cTerms++] = "(FIELD WFSCurrApp CONTAINS (\"" + userRoles + "\"))";
Edited:
tmpArray has this in it
(FIELD WFSCurrApp CONTAINS ("[Finance]")) when the user has a single role and
(FIELD WFSCurrApp CONTAINS ("[Finance]" OR "[WFSAdmin]")) if the user has multiple roles. Neither of these return any documents that I know have the role [Finance] in the Current Approvers field WFSCurrApp.
The query has a number of other tags so tmpArray is split and each element is seperated with an "OR". This is the final query
(FIELD WFSCurrApp CONTAINS "Barney Rubble/Workflo Systems") OR (FIELD WFSCurrApp CONTAINS ("[Finance]"))
the query returns the documents where WFSCurrApp contains Barney but not the ones where Barney's role is [Finance] and there are some that Barney is not in the current approvers but he possess the role [Finance] and that role is in the Current Approvers.
The query returns without error and returns the documents where Barney is listed in the Current Approvers so there are no errors raised, just does not find the matching role.
Started Working correctly so guess it might have been an index update issue
I am using asp(vb)
In SQL Server database I made a table cars which has two columns:
productid int
name varchar(50)
Now I am collecting the value of name attribute from user through a text field:
Enter car name
<input type="text" name="name" value="" />
and storing it in a variable:
name = Request.Form("name")
But when I run this query, it gives error:
query = "SELECT * FROM cars where name = " & name
Unable to figure out why?
Because name is defined as a VARCHAR string datatype, this would mean you need to quote the value of name in your SQL query, ie
query = "SELECT * FROM cars WHERE name = '" & name & "'"
or better still, use a parameterised query via a ADODB.Command object if you're using ADODB
I think u first run a query for INSERT the data
Using MSSql Server? Try:
query = "SELECT * FROM cars where [name] = " & name
Note the [] around the name column.