From e6de9ae9f7da7d881526912fe5da5ee4be791335 Mon Sep 17 00:00:00 2001 From: Victor Petrovykh Date: Thu, 8 Dec 2022 05:37:03 -0500 Subject: [PATCH] Assorted fixes. (#90) Multiple fixes and clarifications. Issue #87. --- chapter1/index.md | 22 ++++++++++++++++++++++ chapter10/index.md | 2 +- chapter13/answers.md | 4 ++-- chapter16/index.md | 2 +- chapter5/index.md | 2 +- chapter6/index.md | 16 +++++++++++++++- translations/zh/chapter10/index.md | 2 +- translations/zh/chapter16/index.md | 4 ++-- 8 files changed, 45 insertions(+), 9 deletions(-) diff --git a/chapter1/index.md b/chapter1/index.md index 64fedf80..4df79653 100644 --- a/chapter1/index.md +++ b/chapter1/index.md @@ -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 `: Creating user roles, setting passwords, configuring ports, etc. @@ -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) diff --git a/chapter10/index.md b/chapter10/index.md index dc4b08ea..728d7573 100644 --- a/chapter10/index.md +++ b/chapter10/index.md @@ -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 `. +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 `. ## UNLESS CONFLICT ON + ELSE + UPDATE diff --git a/chapter13/answers.md b/chapter13/answers.md index 015524d1..476629e0 100644 --- a/chapter13/answers.md +++ b/chapter13/answers.md @@ -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})`. diff --git a/chapter16/index.md b/chapter16/index.md index 1b773f50..d58ec22e 100644 --- a/chapter16/index.md +++ b/chapter16/index.md @@ -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. diff --git a/chapter5/index.md b/chapter5/index.md index 1eb08e9c..d06d906a 100644 --- a/chapter5/index.md +++ b/chapter5/index.md @@ -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 `, 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`` ` 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`` ` 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 diff --git a/chapter6/index.md b/chapter6/index.md index 5f8c4172..86920051 100644 --- a/chapter6/index.md +++ b/chapter6/index.md @@ -222,7 +222,7 @@ SELECT Vampire { }; ``` -The output is: +This will transform the results into JSON. However, what the REPL will show by default looks more like this: ``` { @@ -230,6 +230,20 @@ The output is: } ``` +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`: diff --git a/translations/zh/chapter10/index.md b/translations/zh/chapter10/index.md index 9774056a..8e64c412 100644 --- a/translations/zh/chapter10/index.md +++ b/translations/zh/chapter10/index.md @@ -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}`这里 ` 查看它们的文档。 +你现在已经了解了 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}`这里 ` 查看它们的文档。 ## 关键词 UNLESS CONFLICT ON + ELSE + UPDATE diff --git a/translations/zh/chapter16/index.md b/translations/zh/chapter16/index.md index 949fb089..04f5a8d7 100644 --- a/translations/zh/chapter16/index.md +++ b/translations/zh/chapter16/index.md @@ -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 加快查询 @@ -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,等等。