Variables in Queries - Server-side Scripts - google-app-maker

I want to be able to build an SQL query using variables.
How can I create the equivalent of this PHP statement in a Google App Maker server-side script?
$query = "SELECT * FROM table WHERE orderId = $orderNo";
-('$orderNo' as a variable)
This may be achieved through a calculated model, if so, I need to understand how to use variables within the calculated SQL query.
Like this: https://developers.google.com/appmaker/models/cloudsql#sql_calculated_model
The variable here ultimately needs to be an ID property on the page.
Entirely different approaches are welcome
Thanks a lot.

You can use datasource Query Builder (https://developers.google.com/appmaker/models/datasources#query_builder):
or hack into 'where' query property (server script):
var newQuery = app.models.Company.newQuery();
newQuery.where = 'Name contains? :SearchText or Address contains? :SearchText or Website contains? :SearchText';
newQuery.parameters.SearchText = 'search term';
companies = newQuery.run()
You can find examples in templates (https://developers.google.com/appmaker/templates/). Q&A Forum (query builder), Partner Management (how to use 'where' query property).

Related

Suggest Box with Calculated tables

I am trying to use a SuggestBox on a Calculated Table. I am able to see the values, however the results are not filtered based on the value I type in the field. Is there a limitation with its used on Calculated tables?
Thanks,
Maria.
Currently App Maker filters data automatically when SuggestBox in "Use specified field as value" mode only.
In "Use whole record as value" mode query parameter supplied to the Calculated Model script has startsWith filter for the Field which the script should respect.
This filter can be mapped to underlying DB query or processed in code like:
var records = ...records generation code...;
var prefix = query.filters.FIELDNAME._startsWith.toLowerCase();
return records.filter(function(record){
return record.FIELDNAME.toLowerCase().lastIndexOf(prefix, 0) === 0;
});
It is so for performance reasons, but I'll consider to improve documentation and add an automatic results filtering option.

How to update security stamp of users in bulk? Asp.net Identity

I am updating users security stamp when a user is being deactivated. I can do this for one user like this.
await UserManager.UpdateSecurityStampAsync(userId);
I want to do this form multiple users, like 500 or may bee 100.
Is there any way I can do this in bulk?
I am using entity framework.
Security Stamp is a random string with no significant importance of the actual value, so you can just stuff new GUID there.
Best way to do it in bulk would be to execute SQL query:
update ApplicationUsers set SecurityStamp = NEWID()
You can specify where clause as needed to limit users you need to update, and most likely you'll need to change the name of the table to what you actually have in your DB.
There different ways to execute this query - you can do this with EF:
context.Database.ExecuteSqlCommand("UPDATE ApplicationUsers........");
Or you can look into already recommended EF.Extended library - bulk update is possible without dipping into SQL.
You can use Entity Framework Extended Library.Which is the fastest way to do the update.Extended Library batch update eliminates the need to retrieve and load an entity before modifying it.So it is very fast.Try that.
Example from their doc :
//update all tasks with status of 1 to status of 2
context.Tasks
.Where(t => t.StatusId == 1)
.Update(t => new Task { StatusId = 2 });
You can read more about it here : Entity Framework Extended Library

SchemaTitleCriteria yield no results in SDL Tridion Broker Query

I have a simple SDL Tridion 2011 SP1 Broker Query to retrieve a list of Component URIs. All of my Components are embedded on Pages, and not using Dynamic Component Templates. The following code returns 50 results (which is to be expected). One of which is the URI tcm:123-456-16.
List<Criteria> criteria = new List<Criteria>();
criteria.Add(new ItemTypeCriteria(16));
criteria.Add(new PublicationCriteria(337));
Query query = new Query(CriteriaFactory.And(criteria.ToArray<Criteria>()));
String[] results = query.ExecuteQuery();
The Component tcm:123-456-16 is based on a Schema with the name “News Portal”. I would like to add additional criteria to my query so that I only get items based on that Schema, so I tried the following code:
List<Criteria> criteria = new List<Criteria>();
criteria.Add(new ItemTypeCriteria(16));
criteria.Add(new PublicationCriteria(337));
criteria.Add(new SchemaTitleCriteria("News Portal"));
Query query = new Query(CriteriaFactory.And(criteria.ToArray<Criteria>()));
String[] results = query.ExecuteQuery();
This returns no results at all. I have double checked my Schema name. Is this response expected? Does the SchemaTitleCriteria require the Components to be published as Dynamic Component Presentations. Any advice would be greatly appreciated.
Yes and No on DCPs. You don't need to have all your components published as Dynamic Component Presentations (DCPs). We noticed the same and observed that if you don't publish at least one DCP based on the schema, the schema title does not get published into Schemas table of the Tridion Broker DB (not sure it is by design). Once you publish one DCP based on the schema, the schema title is stored and subsequent queries work, but until you publish that first one you will not get any.
However in practical scenario, you do broker queries to get the dcps so you should not see this behavior except a mistake or someone missed it.
Why would the component be present in the first result set, but not in the second?
I suspect this is unintended behaviour and worth raising with SDL.
To fix it you'll need to use ItemSchemaCriteria instead of SchemaTitleCriteria and obtain the components based on the schema ID, rather than the schema title.
To use the SchemaTitleCriteria I should imagine you need to have at least published one component based on the news portal schema alonside a dynamic component template so that the content delivery database contains the schema title information.

how to join two tables in linq? Using WCF Data Service

I want to join two tables in one of my linq query. I have written one code but it gives me an error like below
The method 'Join' is not supported.
I have tried this code:
var query = (from ls in this.testEntities.abc
join itm in this.testEntities.edf on ls.ID equals itm.ID
where itm.val == param
select new
{
ls.Name,
ls.Contact
}).ToList();
Am I missing something?
If anyone have any idea about it than please help me...
WCF Data Services is able to directly expand related objects based upon the Entity Data Model. (Don't worry about it if you don't know a lot about an EDM; it's not particularly important to the answer.) Since WCF Data Services is already aware, for instance, that a Product has a Category, I can fire up LinqPad, give it this URL, and issue a query like the following:
Products.Expand("Category").Take(2)
The result is two products, each with a property of type Category.
I'm not really sure what that translates to in the other LINQ syntax, sorry.
HTH,
Mark

Wordpress Custom Table Query using Multiple Values

I have been researching the last hour of whether or not I can run a query within wordpress on a custom table that I made using two WHERES. Now I'm new to mysql and php ( 9 months in ) and am just wondering if there is a way to do it besides a AND.
Here is an example of my code
$check_current = $wpdb->get_row("SELECT * FROM $weather_table WHERE status = 'current' AND condition = $current_weather");
status and condition are both fields and I'm trying to find out whether both exist to rewrite the current field. The closest I've come is possibly using the prepare function in Wordpress and php but don't know what that is exactly just know it helps with sql injection and security. Can you run a multiple value query in Wordpress?
Using the AND operator in MySQL is the right way to test for a compound condition.
Be sure to put quotes around any variables (like $current_weather) if the underlying data type is a string.
Also, best practice is to avoid using "*" in the select. Rather, explicitly declare the fields you want to return. This is good for performance.

Resources