Skip to content

Commit

Permalink
Assorted fixes. (#90)
Browse files Browse the repository at this point in the history
Multiple fixes and clarifications.

Issue #87.
  • Loading branch information
vpetrovykh authored Dec 8, 2022
1 parent 30d81e3 commit e6de9ae
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
22 changes: 22 additions & 0 deletions chapter1/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ Lastly, we need to do a migration. This will give the database the structure we
- Then you add the types we mentioned above, and finish up the block by ending with a `}`. Then outside of that, type `POPULATE MIGRATION` to add the data.
- Finally, you type `COMMIT MIGRATION` and the migration is done.

Putting all of that together we get:
```edgeql
START MIGRATION TO {
module default {
type Person {
required property name -> str;
multi link places_visited -> City;
}
type City {
required property name -> str;
property modern_name -> str;
}
}
};
POPULATE MIGRATION;
COMMIT MIGRATION;
```

There are naturally a lot of other commands beyond this, though we won't need them for this book. You could bookmark these four pages for later use, however:

- {ref}`Admin commands <docs:ref_cheatsheet_admin>`: Creating user roles, setting passwords, configuring ports, etc.
Expand Down Expand Up @@ -417,6 +437,8 @@ Success! Now we get the output we wanted:

Of course, Jonathan Harker has been inserted with a connection to every city in the database. Right now we only have three `City` objects, so this is no problem yet. But later on we will have more cities and won't be able to just write `places_visited := City` for all the other characters. For that we will need `FILTER`, which we will learn to use in the next chapter.

Note that if you have inserted "Johnathan Harker" multiple times, you will have multiple `Person` objects with that name corresponding to each `INSERT` command. This is OK for now. In [Chapter 7](../chapter7/index.md) we will learn how to make sure the database doesn't allow multiple copies of `Person` with the same name.

[Here is all our code so far up to Chapter 1.](code.md)

<!-- quiz-start -->
Expand Down
2 changes: 1 addition & 1 deletion chapter10/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Without the `$$` it will look like four separate strings with three unknown keyw

## All the scalar types

You now have an understanding of all the EdgeDB scalar types. Summed up, they are: `int16`, `int32`, `int64`, `float32`, `float64`, `bigint`, `decimal`, `sequence`, `str`, `bool`, `datetime`, `duration`, `cal::local_datetime`, `cal::local_date`, `cal::local_time`, `uuid`, `json`, and `enum`. You can see the documentation for them {ref}`here <docs:ref_datamodel_scalar_types>`.
You now have an understanding of all the EdgeDB scalar types. Summed up, they are: `int16`, `int32`, `int64`, `float32`, `float64`, `bigint`, `decimal`, `sequence`, `str`, `bool`, `datetime`, `duration`, `cal::local_datetime`, `cal::local_date`, `cal::local_time`, `cal::relative_duration`, `cal::date_duration`, `uuid`, `json`, and `enum`. You can see the documentation for them {ref}`here <docs:ref_datamodel_scalar_types>`.

## UNLESS CONFLICT ON + ELSE + UPDATE

Expand Down
4 changes: 2 additions & 2 deletions chapter13/answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ Here's the output:
{2, 3, 3, 4}
```

You can see that `DISTINCT` works individually on each set, so `SELECT DISTINCT {1, 2} + {1, 2};` and `SELECT {1, 2} + {1, 2};` are the same. But if you were to write `SELECT DISTINCT {2, 2}` the output would be just `{2}`.
The `DISTINCT` operator binds stronger than `+`. So it only applies to the part of the expression before `+`, which is `{1, 2}`. This is why `SELECT DISTINCT {1, 2} + {1, 2};` and `SELECT {1, 2} + {1, 2};` are the same. But if you were to write `SELECT DISTINCT {2, 2}` the output would be just `{2}`.

#### 5. What do you think the output of `SELECT DISTINCT {2, 2} + {2, 2};` will be?

The output will be `{4, 4}` because `DISTINCT` only works on the first set.

To get the output `{4}`, you can repeat the `DISTINCT`: `SELECT DISTINCT {2, 2} + DISTINCT {2, 2};`. Or you can wrap the whole thing like this: `SELECT DISTINCT({2, 2} + {2,2})`.
To get the output `{4}`, you can repeat the `DISTINCT`: `SELECT DISTINCT {2, 2} + DISTINCT {2, 2};`. Or you can apply `DISTINCT` to the whole thing by using parentheses like this: `SELECT DISTINCT({2, 2} + {2, 2})`.
2 changes: 1 addition & 1 deletion chapter16/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ type City extending Place {

4. How would you display the name of every `Person` in uppercase followed by a space and then the same name in lowercase?

Hint: the {eql:func}`docs:std::str_repeat` function could help (though there is more than one way to do it)
Hint: the {eql:func}`docs:std::str_upper` function could help (though you will also need another function)

5. How would you use `re_match_all()` to display all the `Person.name`s with `Crewman` in the name? e.g. Crewman 1, Crewman 2, etc.

Expand Down
2 changes: 1 addition & 1 deletion chapter5/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The `T` inside there is just a separator, and the `Z` at the end stands for "zer

One other way to get a `datetime` is to use the `to_datetime()` function. {eql:func}`Here is its signature <docs:std::to_datetime>`, which shows that there are six ways to make a `datetime` with this function depending on how you want to make it. EdgeDB will know which one of the six you have chosen depending on what input you give it.

By the way, you'll notice one unfamiliar type inside called a {eql:type}` ``decimal`` <docs:std::decimal>` type. This is a float with "arbitrary precision", meaning that you can give it as many numbers after the decimal point as you want. This is because float types on computers [become imprecise after a while](https://www.youtube.com/watch?v=-3c8G0JMM5Q) thanks to rounding errors. This example shows it:
By the way, you'll notice one unfamiliar type inside called a {eql:type}` ``decimal`` <docs:std::decimal>` type. This is a float with "arbitrary precision", meaning that you can give it as many numbers after the decimal point as you want. This is because float types on computers [become imprecise after a while](https://www.youtube.com/watch?v=PZRI1IfStY0&ab_channel=Computerphile) thanks to rounding errors. This example shows it:

```edgeql-repl
edgedb> SELECT 6.777777777777777; # Good so far
Expand Down
16 changes: 15 additions & 1 deletion chapter6/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,28 @@ SELECT <json>Vampire {
};
```

The output is:
This will transform the results into JSON. However, what the REPL will show by default looks more like this:

```
{
"{\"name\": \"Count Dracula\", \"slaves\": [{\"name\": \"Woman 1\"}, {\"name\": \"Woman 2\"}, {\"name\": \"Woman 3\"}]}",
}
```

Let's go through the result together. The outer curly braces are just telling you that what's inside is one or more results returned by the query. Then the actual result is a string containing JSON. Because the JSON part is inside a string all the `"` there need to be escaped, so they appear as `\"`.

To make REPL show JSON in a nicer format just type `\set output-format json-pretty`. Then the results will look more familiar:

```json
{
"name": "Count Dracula",
"slaves": [{"name": "Woman 1"}, {"name": "Woman 2"}, {"name": "Woman 3"}]
}
```

To restore the default format type: `\set output-format default`.


## Converting back from JSON

So what about the other way around, namely JSON to an EdgeDB type? You can do this too, but remember to think about the JSON type that you are giving to cast. The EdgeDB philosophy is that casts should be symmetrical: a type cast into JSON should only be cast back into that type. For example, here is the first date in the book Dracula as a string, then cast to JSON and then into a `cal::local_date`:
Expand Down
2 changes: 1 addition & 1 deletion translations/zh/chapter10/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ SELECT $$ "Dr. Van Helsing would like to tell "them" about "vampires" and how to

## 所有标量类型

你现在已经了解了 EdgeDB 的所有标量类型。总结起来有:`int16``int32``int64``float32``float64``bigint``decimal``sequence``str``bool``datetime``duration``cal::local_datetime``cal::local_date``cal::local_time``uuid``json``enum`。你可以在 {ref}`这里 <docs:ref_datamodel_scalar_types>` 查看它们的文档。
你现在已经了解了 EdgeDB 的所有标量类型。总结起来有:`int16``int32``int64``float32``float64``bigint``decimal``sequence``str``bool``datetime``duration``cal::local_datetime``cal::local_date``cal::local_time``cal::relative_duration`, `cal::date_duration`, `uuid``json``enum`。你可以在 {ref}`这里 <docs:ref_datamodel_scalar_types>` 查看它们的文档。

## 关键词 UNLESS CONFLICT ON + ELSE + UPDATE

Expand Down
4 changes: 2 additions & 2 deletions translations/zh/chapter16/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tags: Indexing, String Functions

> 亚瑟·霍姆伍德(Arthur Holmwood)的父亲去世了,现在亚瑟成为了一家之主。他的新头衔是戈达尔明勋爵(Lord Godalming),他有很多很多钱。有了这笔钱,他可以帮助大家找出所有德古拉(Dracula)用来藏匿棺材的房子。
> 与此同时,范海辛(Van Helsing)对伦菲尔德(Renfield)感到好奇,于是他问约翰·西沃德(John Seward)是否可以安排见面。相见后,他很惊讶地发现伦菲尔德受到过良好的教育,而且口才很好。伦菲尔德谈论着范海辛的研究、政治、历史等——他看起来一点都不疯狂!但后来,伦菲尔德又突然不想说话了,不停地骂他是个白痴。这令范海辛感到很困惑。一天晚上,伦菲尔德非常认真地要求离开。他说:“你们难道不知道我是理智认真的吗?……是一个为灵魂而战的正常人!听我的,听我的,让我走!让我走!”。他们想相信他,但又无法说服自己真的信任。最后,伦菲尔德停了下来并平静地对大家说:“记住,今晚我已经尽力说服你们了。”
> 与此同时,范海辛(Van Helsing)对伦菲尔德(Renfield)感到好奇,于是他问约翰·西沃德(John Seward)是否可以安排见面。相见后,他很惊讶地发现伦菲尔德受到过良好的教育,而且口才很好。伦菲尔德谈论着范海辛的研究、政治、历史等——他看起来一点都不疯狂!但后来,伦菲尔德又突然不想说话了,不停地骂他是个白痴。这令范海辛感到很困惑。一天晚上,伦菲尔德非常认真地要求离开。他说:“你们难道不知道我是理智认真的吗?……是一个为灵魂而战的正常人!听我的,听我的,让我走!让我走!”。他们想相信他,但又无法说服自己真的信任。最后,伦菲尔德停了下来并平静地对大家说:“记住,今晚我已经尽力说服你们了。”
## 使用 index on 加快查询

Expand Down Expand Up @@ -284,7 +284,7 @@ type City extending Place {

4. 如何在展示所有 `Person` 对象的名称时,先以大写形式显示该名称,然后跟一个空格并以小写形式再显示一次其名称?

提示:{eql:func}`docs:std::str_repeat` 函数可以提供帮助(尽管还有很多其他方法)
提示:{eql:func}`docs:std::str_upper` 函数可以提供帮助。

5. 如何使用 `re_match_all()` 来显示名称中带有 `Crewman` 的所有 `Person.name`?例如:Crewman 1,Crewman 2,等等。

Expand Down

0 comments on commit e6de9ae

Please sign in to comment.