Given the following schema created using the OrientDB Document API:
OClass team = getoDocDatabase().getMetadata().getSchema().createClass(TEAM);
team.createProperty(NAME, OType.STRING);
OClass driver = getoDocDatabase().getMetadata().getSchema().createClass(DRIVER);
driver.createProperty(NAME, OType.STRING);
OClass car = getoDocDatabase().getMetadata().getSchema().createClass(CAR);
car.createProperty(NAME, OType.STRING);
// Relationships
team.createProperty(CARS_HERITAGE, OType.LINKSET, car);
car.createProperty(BUILT_BY, OType.LINK, team);
car.createProperty(DRIVEN_BY, OType.LINKSET, driver);
driver.createProperty(DRIVER_OF, OType.LINKSET, car);
What's the sql query to fetch all the teams that Fernando Alonso has driven for?
In relational SQL would be as easy as
SELECT team.name FROM {the join} where driver.name = 'Fernando Alonso'
I have try with this db
create class Team
CREATE PROPERTY Team.name String
create class DRIVER
CREATE PROPERTY DRIVER.name String
create class Car
CREATE PROPERTY Car.name String
CREATE PROPERTY Team.CARS_HERITAGE LINKSET Car
CREATE PROPERTY Car.BUILT_BY LINK Team
CREATE PROPERTY Car.DRIVEN_BY LINKSET DRIVER
CREATE PROPERTY DRIVER.DRIVER_OF LINKSET Car
INSERT
INSERT INTO TEAM(name) values ("Ferrari"),("Renault") // Ferrari 12:0 Renault 12:1
insert into Driver(name) values ("Fernando Alonso"),("Giancarlo Fisichella") // Alonso 13:0 Fisichella 13:1
insert into car(name,BUILT_BY,DRIVEN_BY) values ("car ferrari",#12:0,[#13:0,#13:1])
insert into car(name,BUILT_BY,DRIVEN_BY) values ("car renault",#12:1,[#13:0])
Query
select BUILT_BY.name as TeamName from car where DRIVEN_BY.name contains "Fernando Alonso"
Hope it helps.
UPDATE 1
select distinct(BUILT_BY.name) as team from car where DRIVEN_BY.name contains "Fernando Alonso"
FROM JAVA API
UPDATE 2
FROM JAVA API
The answer from #Alessandro is correct, however I've found some weirdnesses in the way the sql is interpreted. Let me explain myself.
I've simplified the goal, let's try to find the query to fetch the teams which have had at least a car.
This first query works, is the one suggested by Alessandro. It returns a list of documents containing one property, that is the name of the team.
select distinct(team.name) as name from Car
This second query works as well, returning the list of teams (as documents).
select expand(distinct(team)) from Car
This third query works and returns exactly the same result that the previous, so it ignores the ".name" part of the select.
select expand(distinct(team)).name from Car
This last query fails. Well it doesn't fail, but it doesn't return what I expected, it returns a list of links to the teams.
select distinct(team).name from Car
Tests running the queries: Tests.
Related
Imagine we have the following models:
type Company struct {
gorm.Model
Name string
Addresses []Address
}
type Address struct {
gorm.Model
CompanyID uint64
Street string
City string
Country string
}
I want to take all the companies(and their addresses) which have address in a specific location. Something like this:
SELECT company.*, address.* FROM company
INNER JOIN address ON address.company_id = company.id AND address.country = 'Bulgaria'
So if a company does not have address at the specific location, I will not get it as a result at all. I was trying something like that:
db.Joins("Addresses", "addresses.country = ?", "Bulgaria").Find(&companies)
However, it doesn't work, because GORM doesn't take the second argument of Joins(when preloading join used), so I should check the generated query and make something like that:
db.Where(`"Address".country = ?`, "Bulgaria").Joins("Addresses").Find(&companies)
Is there a better way/not hacky way? Have in mind all of the above code is mock of the real problem, I didn't want to expose the original models/queries.
You can use Preload to load Addresses into the Company object.
Based on your described conditions, where you don't want to load companies that don't match your filter, you should use an INNER JOIN
Two options:
First, if your table is named company, then your query should look like this:
db.Table("company").
Preload("Addresses").
Joins("INNER JOIN addresses a ON a.company_id = company.id").
Where("a.country = ?", "Bulgaria").
Find(&companies)
Second, if your table is named companies, then you should try this:
db.Preload("Addresses").
Joins("INNER JOIN addresses a ON a.company_id = companies.id").
Where("a.country = ?", "Bulgaria").
Find(&companies)
If you are using Gorm v2 you perform CRUD operations on has-one and one-to-many via associations:
var companies []Company
var addresses []Address
countries := []string{"Bulgaria"}
db.Model(&Address).Where("country IN (?)", countries).Find(&addresses)
// The key is using the previously fetched variable as the model for the association query
db.Model(&addresses).Association("CompanyID").Find(&companies)
This also works for many-to-many relations.
When comparing Gorm VS raw SQL queries (db.Raw(SQLquery)) you will typically see a performance hit. On the upside, you will not have to deal with the added complexity of raw sql string building in Go, which is pretty nice. I personally use a combination of both raw and gorm-built queries in my projects :)
Suppose I have 3 hypothetical models;
class State(models.Model):
name = models.CharField(max_length=20)
class Company(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignField(State)
class Person(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignField(State)
I want to be able to return results in a Django app, where the results, if using SQL directly, would be based on a query such as this:
SELECT a.name as 'personName',b.name as 'companyName', b.state as 'State'
FROM Person a, Company b
WHERE a.state=b.state
I have tried using the select_related() method as suggested here, but I don't think this is quite what I am after, since I am trying to join two tables that have a common foreign-key, but have no key-relationships amongst themselves.
Any suggestions?
Since a Person can have multiple Companys in the same state. It is not a good idea to do the JOIN at the database level. That would mean that the database will (likely) return the same Company multiple times, making the output quite large.
We can prefetch the related companies, with:
qs = Person.objects.select_related('state').prefetch_related('state__company')
Then we can query the Companys in the same state with:
for person in qs:
print(person.state.company_set.all())
You can use a Prefetch-object [Django-doc] to prefetch the list of related companies in an attribute of the Person, for example:
from django.db.models import Prefetch
qs = Person.objects.prefetch_related(
Prefetch('state__company', Company.objects.all(), to_attr='same_state_companies')
)
Then you can print the companies with:
for person in qs:
print(person.same_state_companies)
I'm new to CakePHP and stuck on this situation:
I'm developing a test system and I have following three tables: Category, SubCategory, Test
When I create a test, I store category_id, subcategory_id with other data in Test table
Now in TestsController, I want to read the number of records & rows matched by the query
I wish to execute queries similar to these:
Query 1
select count(*) from Test where user_id='$user_id' and category_id=$category_id and subcategory_id=$subcategory_id
Query 2
select
a.test_id,
a.test_name,
a.user_id,
a.test_type,
a.create_date,
a.create_time,
a.category_id,
a.subcategory_id,
a.randomize,
a.test_password
from Test a, Category b, SubCategory c
where user_id='$user_id' and
a.category_id=$category_id and
a.subcategory_id=$subcategory_id and
a.category_id=b.category_id and
a.subcategory_id=c.subcategory_id
Now, I don't want (I actually don't know) to use the $hasMany, $belongsTo etc.
I simply want to execute these queries in Controller & pass the data to the view
Note: In all simple cases, I used the CakePHP style of coding
I wish to know, how can I get an array of rows after executing Query 2
Is it possible to execute such query directly (like mysql_query)
you could create a function in Test Model and access the query, like:
class TestModel extends Model {
..
public function getQueryData($user_id, $cat_id, $subcat_id) {
return $this->query("SELECT a.test_id, a.test_name ..... ;");
}
}
and in Test controller, you could do:
$this->loadModel("Test");
$result = $this->Test->getQueryData($user_id, $cat_id, $subcat_id);
Using these tables:
*student {'s_id','s_name,'...} , class {'c_id','c_name',...} student2class {'s_id','c_id'}, grades {'s_id','c_id','grade'}*
Is it possible to perform a query (nested query?) put class name as subtitle and then all students (of that class) and grades, next class name as subtitle ...
The result I need is:
Maths
John .... C
Anna .... B
[...]
Biology
Anna .... C
Jack .... A
[...]
For each row from class I'll have a subquery fetching all data related with this class
No need of any sub-query. You can get your data this way:
SELECT c_name, s_name_, grade
FROM student, class, grades
WHERE student.s_id = grades.s_id and class.c_id = grades.c_id
ORDER BY c_name
The presentation of results depends on your system/tools, as others already said. This is a link to the solution for Microsoft Access:
http://office.microsoft.com/en-001/access-help/create-a-grouped-or-summary-report-HA001159160.aspx
The solution should be implemented in your client side code and not in the database. From database you should just get a simple table formatted data (subject, student, grade)
Then convert the above recordset to the format you want:
For an example in C# you could convert the recordset into lookup
var Lookup = DataSet.Tables[0].Rows.ToLookup(x => x["subjectColumn"]);
now you can loop through the lookup and format your result
foreach (var grade in Lookup)
{
subject = grade.Key;
...
}
I have a simple test object model in which there are schools, and a school has a collection of students.
I would like to retrieve a school and all its students who are above a certain age.
I carry out the following query, which obtains a given school and the children which are above a certain age:
public School GetSchoolAndStudentsWithDOBAbove(int schoolid, DateTime dob)
{
var school = this.Session.CreateCriteria(typeof(School))
.CreateAlias("Students", "students")
.Add(Expression.And(Expression.Eq("SchoolId", schoolid), Expression.Gt("students.DOB", dob)))
.UniqueResult<School>();
return school;
}
This all works fine and I can see the query going to the database and returning the expected number of rows.
However, when I carry out either of the following, it gives me the total number of students in the given school (regardless of the preceding request) by running another query:
foreach (Student st in s.Students)
{
Console.WriteLine(st.FirstName);
}
Assert.AreEqual(s.Students.Count, 3);
Can anyone explain why?
You made your query on the School class and you restricted your results on it, not on the mapped related objects.
Now there are many ways to do this.
You can make a static filter as IanL said, however its not really flexible.
You can just iterate the collection like mxmissile but that is ugly and slow (especially considering lazy loading considerations)
I would provide 2 different solutions:
In the first you maintain the query you have and you fire a dynamic filter on the collection (maintaining a lazy-loaded collection) and doing a round-trip to the database:
var school = GetSchoolAndStudentsWithDOBAbove(5, dob);
IQuery qDob = nhSession.CreateFilter(school.Students, "where DOB > :dob").SetDateTime("dob", dob);
IList<Student> dobedSchoolStudents = qDob.List<Student>();
In the second solution just fetch both the school and the students in one shot:
object result = nhSession.CreateQuery(
"select ss, st from School ss, Student st
where ss.Id = st.School.Id and ss.Id = :schId and st.DOB > :dob")
.SetInt32("schId", 5).SetDateTime("dob", dob).List();
ss is a School object and st is a Student collection.
And this can definitely be done using the criteria query you use now (using Projections)
Unfortunately s.Students will not contain your "queried" results. You will have to create a separate query for Students to reach your goal.
foreach(var st in s.Students.Where(x => x.DOB > dob))
Console.WriteLine(st.FirstName);
Warning: That will still make second trip to the db depending on your mapping, and it will still retrieve all students.
I'm not sure but you could possibly use Projections to do all this in one query, but I am by no means an expert on that.
You do have the option of filtering data. If it there is a single instance of the query mxmissle option would be the better choice.
Nhibernate Filter Documentation
Filters do have there uses, but depending on the version you are using there can be issues where filtered collections are not cached correctly.