edgeDB入门使用记录(二)
- 
关于重载, 相当于给原父类临时加了一个属性, 被当前子类接受, 而且不改变原父类 type NPC extending Person { age: HumanAge; overloaded multi places_visited: Place { default := (select City filter .name = 'London'); } }
- 
使用元组 tuple, 以类似键值的方式批量插入数据// 作为元组数据,每个括号内容相当于一个带index的数组项, 且其内部数据也有类似对应的index for data in {('Buda-Pesth', 402706), ('London', 3500000), ('Munich', 230023), ('Bistritz', 9100)} union ( update City filter .name = data.0 set { population := data.1 } );
- 
排序, order by降序:desc, 升序(默认):ascselect City { name, population } order by .population desc;
- 
其他函数 - count()计算数量
- all()如果所有项目都匹配,则返回- {true},否则返回- {false}
- sum()对所有项目进行相加
- max()给出数值最大的项目
- min()给出数值最小的项目
- math::mean()给出所有项目的平均值
- any()只要有一个项目匹配,则返回- {true},否则返回- {false},
- math::stddev()给出所有项目的标准差
 
- 
引入模块, with module AAA, 重命名模块asselect math::mean(City.population); //---------------- with M as module math, select M::mean(City.population);
- 
转义字符及原始字符串 - \n 创建新行
- \t 将文本右移一个制表符
- $$ 包裹字符串,输出原始字符串,类似js的模板字符串; $$"测试"我是字符""$$
 
- 
处理插入冲突, 如果数据存在则更新 insert City { name := 'Munich', population := 261023, } unless conflict on .name else ( update City set { population := 261023, } );
- 
笛卡尔乘法(暂未理解) 
- 
封装查询函数 // 需要指定set of function get_lucies() -> set of Person using ( select Person filter .name = 'Lucy Westenra' ); // 使用 select get_lucies() { name, places_visited: {name} };
- 
生成随机数, random()四舍五入取整,round()
// 生成1到5
select <int16>round(random()*5)
- 
返回去重的结果, distinctselect distinct Array; // {2,4,7,5,8,2,7} 返回{2,4,7,5,8}
- 
返回数据的字段类型 __type__// .name 会返回名称, 而__type__会返回所有name属性值的类型 select Person.__type__ { name }; /* 返回 { schema::ObjectType {name: 'default::NPC'}, schema::ObjectType {name: 'default::Crewman'}, } */
- 
类型注解 annotation@, 即添加注释内容// 注解 type City extending Place { annotation description := 'A place with 50 or more buildings. Anything else is an OtherPlace'; population: int64; } // 查询注解key(name) 和 value(@value) select (introspect City) { name, properties: {name}, annotations: { name, // 'std::description' @value // 'A place with 50 or more buildings. Anything else is an OtherPlace' } };
- 
一般查询指令 // 统计结果数量 select count(Person) // 枚举项目 select enumerate(Person.name) // {(0,'aa'), (1, 'bb')}
- 
反向链接查询, 类似单向的一对多, 通过多反向查到一 .<select MinorVampire { name, master := .<slaves[is Vampire] {name}, }; // 多级反向 select City { name, region_name := .<cities[is Region].name, country_name := .<cities[is Region].<regions[is Country].name } filter exists .country_name; // exists 用于确定是否有反向的值 有返回 没有则空
- 
更多约束限制 constraintmax_len_value()one_of()expression on自定义错误信息errmessagetype PC extending Person { required class: Class; overloaded required name: str { constraint max_len_value(30); } } // 类似 枚举 只能设定其中已有的值 month: int64 { constraint one_of(5, 6, 7, 8, 9, 10) } // arg1包含arg2 contains函数 type Lord extending Person { constraint expression on { // 这里的 __subject__ 是指类型本身 contains(__subject__.name, 'Lord') // 前者name 必须包含 'Lord' 数据才能插入 } } // 约束 可以设定 自定义 抛出的 错误信息 errmessage 类似 new ThrowErr("错误") type Lord extending Person { constraint expression on (contains(__subject__.name, 'Lord')) { errmessage := "All lords need \'Lord\' in their name"; } }
- 
添加索引,类似主键,字典的ABC索引 index ontype BookExcerpt { required date: cal::local_datetime; required excerpt: str; index on (.date); required author: Person; }
- 
常用功能: - 字符串转小写 str_lower()
- 字符串转大写 str_upper()
- 转换成字符串 to_str()
- find():用于查找第二个字符串参数在第一个字符串参数中首次出现的位置索引,没有则返回 -1
- 字符串分割   str_split()
- re_match()(用于第一次匹配)和- re_match_all()](用于所有匹配)
 
- 字符串转小写 
- 
使用别名 alias, 区别于类型继承和生成新的类型, 别名用于想基于原始类数据包装使用别的名称// 此处CrewmanInBulgaria所有数据都来自Crewman,只有查询功能,不能操作和影响原始数据Crewman, 但通过 // CrewmanInBulgaria查询时,返回的name 会加上'Gospodin '前缀 alias CrewmanInBulgaria := Crewman { name := 'Gospodin ' ++ .name, current_location := (select Place filter .name = 'Bulgaria'), };
- 
同样有别名功能的还有 with// 以别名的形式 对 2组数据进行操作 with M := MinorVampire, select fight_2(M, MinorVampire); // 过滤掉 2组 数据内的 相同值 的处理 with M := MinorVampire, select fight_2(M, MinorVampire) filter M != MinorVampire; // 解释: 假如数据A是{1,2,3} 那么别名a也是{1,2,3} 没有过滤{1,1}{1,2}{1,3} 过滤后 {1,2} {1,3} // 内部数据不再和本身进行处理
- 
批量插入 批量属性 数据, 类似枚举 for city in { ('City 1\'s name', 'City 1\'s modern name', 800), ('City 2\'s name', 'City 2\'s modern name', 900), ('City 3\'s name', 'City 3\'s modern name', 455), } union ( insert City { name := city.0, modern_name := city.1, population := city.2 } );