Is it possible to get the name of all tables used in a custom sql query using Metadata API? - metadata

I'm using Metadata API to get some information and when I ran this one bellow, I got some interesting data.
query tables {
tables {
name
id
columns{
id
name
downstreamDashboards{
name
luid
path
workbook{
name
}
}
}
}
}
Some registers where the name is "custom_sql_query", I would like to check what was the SQL code that generated this datasource. Is that possible? Or at least is possible to get the list of tables that were used in that query?

Related

Spring JPA Specification API with custom Query and custom response Object. Is this possible?

I have researched this for a few days but can't seem to find the right information.
Here is what I need, I have a Database, with multiple tables, I need to join a few tables together to make a sort of "search" API. I have to implement the ability to dynamically search fields (from various tables in the query), sortable, with pagination.
I have found that I cannot combine the #Query annotation with Specification API, and I looked into using the specification API to do the joins I needed but, the problem is the root must be one table/repository.
For example:
If I have a users table that has to join on addresses, phone_numbers, and preferences
the base repository will be UserResposiory and it will return the User entity model, but I need it to return a custom DTO
AccountUserDTO which contains fields from the User, Address, PhoneNumber, and Preference entities.
Would anyone know if this is possible at all??
I am at wits end here and I really want to build this the correct way.
Cheers!
You may do this way:
Build hql query as an string, depend on how the filter condition is requested, you can build the corresponding query, eg:
if (hasParam(searchName)) {
queryString = queryString + " myEntity.name = :queryName"
}
Query query = session.createQuery(queryString);
and the parameter providing
if (hasParam(searchName)) {
query.setParameter("queryName", searchName);
}
...
and execute it.
To create a customized object, the easiest way is treating the object as an array of field:
Query query = session.createQuery("select m.f1, m.f2, m.f3 from myTable m");
List managers = query.list();
Object[] manager = (Object[]) managers.get(0); //first row
System.out.println(manager[0]) //f1
System.out.println(manager[1]) //f2
System.out.println(manager[2]) //f3
There is also some other solution to select, such as
String query = "select new mypackage.myclass(m.f1, m.f2, m.f3) from myTable m";
-> And when execute the above query, it will return a list of object.
Or to be simpler, make your own view in db and map it to one entity.

How to select fields from related tables in dreamfactory

I am playing with the DreamFactory Schema and API Docs. Within the Schema i add a "Virtual Relationship". Now in API Docs i put the name of the Virtual Relationship in related, on execute everthing is fine. I'll get the fields from the related table. But i dont need all fields from the joined table. How to select only the fields i need? I try to add "name_of_related.field" under "fields".
Error:
"message": "Invalid field requested: fe_users_by_fe_user_id.username"
add <relation_name>.fields=fieldname1,fieldname2
in my case:
/api/v2/myservice/_table/mytable?fields=uid,tstamp&related=fe_users_by_fe_user_id&fe_users_by_fe_user_id.fields=username

How to fetch related object using JDOQL named query?

I'm using DataNucleus 2.0.5. And I need to run named JDOQL query to retrieve object with some of related entities.
Suppose I have
class Account{
Set<Role> roles;
//... other properties
}
Now I want to execute named query and retrieve account with roles. I need to do something very similar to join fetch in JPQL but using JDOQL named query.

Using Entity Framework 6 with Multiple DB Schemas but using One DBContext

I have an application using EF as ORM. The database used to have one schema, dbo and everything was working fine. I recently organized my tables into 4 different schemas. Some tables of one schema have dependencies on tables that reside on a different schema. All seems to be valid on the SQL side.
On the app side all db interactions through EF are not working anymore. The code compiles, the schemas are visible in the solution, the model mappings point to the right schemas, but once I try to insert a row to a table it does not work.
I have seen a few posts about using multiple schemas will require using multiple DBContexts but I would rather use one DBContext. All my schemas have the same owner dbo and I do not see a reason of using multiple DBContexts.
Does anyone know if there is a way to achieve this?
You can map each table to its own schema by fluent mapping only. In your DbContext subtype you should override OnModelCreating (if you haven't done so already) and add statements like this:
modelBuilder.Entity<Department>()
.ToTable("t_Department", "school");
Entities that you don't map like this explicitly will be placed in the default dbo schema, or you can provide your own default by
modelBuilder.HasDefaultSchema("sales");
(summarized from here)
In addition to the responce of Gert Arnold, you can also use Table attribute in your entity:
using System.ComponentModel.DataAnnotations.Schema;
[Table("t_Department", Schema = "school")]
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
}
#GertArnold is spot on with his answer. However for pure syntactic candy you can also do this via a convention to pull the schema from the namespace of your models. We found this useful dealing with multiple schemas
modelBuilder.Types().Configure(e => {
var schema = e.ClrType.Namespace.Split('.').Last().ToLower();
var name = entity.ClrType.Name;
return entity.ToTable(name, schema);
});
the above will take the final component of the namespace and use it as the schema name. This avoids the need for customising the table binding for every entity.
In my case, it's possibly that I use DB First to generate my EDMX file, so it doesn't invoke OnModelCreating method.
I finally remove all store:Schema="YourSchema" and Schema="YourSchema" in EDMX file, and I do it by write a bat file with powershell command as below, and execute the bat in the Projec Pre-Build Event:
powershell -Command "$varStr='store:Schema=""abcd""""'; $filePath='%~dp0SomeFolder\SomeFile.edmx'; (gc $filePath) -replace $varStr, '' | Out-File $filePath"

Adding a property to an Entity Framework Entity from another table

I'm just starting out with the Entity Framework and ADO.NET Data Services and I've run into an issue that I can't seem to figure out. I have two tables, one that has user information and the other that has a created by field. Within the database, there isn't a foreign key between these tables. The user table contains an arbitrary Id, a username, and a display name. The created by field contains the user's username. In my entity I would like to have the user's display name since this is what I need to display and expose over the ADO.NET Data Service? I'm aware that I could restructure the database, but I was hoping that I could do the join using the username as I would in a SQL statement.
Thanks in advance,
-Damien
You can make a view using a join of both tables, and then use this object to display the user's name.
There's some info on mapping custom queries here.