Commit 5556931bc5176b955d2385f7f1df73ab2a539f28
1 parent
238ee506
created an example for metaSQL.
Showing
2 changed files
with
231 additions
and
0 deletions
Show diff stats
anubis_dev/library/data_base/metaSQL_example_database.anubis
0 → 100644
| 1 | + | |
| 2 | + This is an example of a formal description of a database | |
| 3 | + that can be used by metaSQL. | |
| 4 | + | |
| 5 | + | |
| 6 | +read data_base/metaSQL.anubis | |
| 7 | + | |
| 8 | +public define MetaSQL_DBStruct the_example_database. | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + --- That's all for the public part ! -------------------------------------------------------- | |
| 13 | + | |
| 14 | + | |
| 15 | + *** [1] Descriptions of all the tables of the example database. | |
| 16 | + | |
| 17 | + Warning: The database will not create a table refering to a not yet existing table. Hence, the following | |
| 18 | + tables are recorded (in the 'tables' component of the database datum) in the same order as below. Because | |
| 19 | + SQL has so many reserved keywords, all table names have the prefix 'exm_tb_' (example table). | |
| 20 | + | |
| 21 | + For this example, we have only 3 tables, a table for storing texts in English and French, a table | |
| 22 | + for recording users (of a web site say), and a table for sandboxes (where users can write their | |
| 23 | + stories). | |
| 24 | + | |
| 25 | + | |
| 26 | +public define MetaSQL_TabStruct texts_table = | |
| 27 | + table("exm_tb_texts",no_pk, | |
| 28 | + [ | |
| 29 | + col ("symbolic", char(100), [unique,indexed]), | |
| 30 | + col ("en", text, []), // English text | |
| 31 | + col ("entime", date, []), // date for English text | |
| 32 | + col ("fr", text, []), // French text | |
| 33 | + col ("frtime", date, []) // date for French text | |
| 34 | + ]). | |
| 35 | + | |
| 36 | + | |
| 37 | +public define MetaSQL_TabStruct user_table = | |
| 38 | + table("exm_tb_user",with_pk, | |
| 39 | + [ | |
| 40 | + col ("email", char(100), [unique,indexed]), | |
| 41 | + col ("firstname", char(100), [indexed]), | |
| 42 | + col ("lastname", char(100), [indexed]), | |
| 43 | + col ("institution", text, []), | |
| 44 | + col ("pwhash", char(40), []) // ascii (hexadecimal) of sha1 hash of password | |
| 45 | + ]). | |
| 46 | + | |
| 47 | + | |
| 48 | +public define MetaSQL_TabStruct sandbox_table = | |
| 49 | + table("exm_tb_sandbox",no_pk, | |
| 50 | + [ | |
| 51 | + col ("owner", foreign("exm_tb_user"), [indexed]), | |
| 52 | + col ("name", char(100), []), // name of sandbox | |
| 53 | + col ("content", text, []), // content of sandbox | |
| 54 | + col ("trash", text, []), // whatever the user wants to keep | |
| 55 | + col ("created", date, []), | |
| 56 | + col ("lastmod", date, []) | |
| 57 | + ]). | |
| 58 | + | |
| 59 | + | |
| 60 | + *** [2] Description of the database. | |
| 61 | + | |
| 62 | +public define MetaSQL_DBStruct | |
| 63 | + the_example_database | |
| 64 | + = | |
| 65 | + database("example", | |
| 66 | + [ | |
| 67 | + texts_table, | |
| 68 | + user_table, | |
| 69 | + sandbox_table, | |
| 70 | + ]). | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | ... | ... |
anubis_dev/library/data_base/metaSQL_example_queries.anubis
0 → 100644
| 1 | + | |
| 2 | + | |
| 3 | + This file contains the stuff which according to 'library/data_base/metaSQL.anubis' generates the | |
| 4 | + interface with the 'example database'. | |
| 5 | + | |
| 6 | +read data_base/metaSQL.anubis | |
| 7 | + | |
| 8 | + The description of all our tables is given in: | |
| 9 | + | |
| 10 | +read metaSQL_example_database.anubis | |
| 11 | + | |
| 12 | + | |
| 13 | + --- table of contents ---------------------------------------------------------------- | |
| 14 | + | |
| 15 | + *** (1) The list of all metaqueries. | |
| 16 | + *** (2) Generating the query fonctions, i.e. the file 'generated/queries.anubis'. | |
| 17 | + | |
| 18 | + -------------------------------------------------------------------------------------- | |
| 19 | + | |
| 20 | + | |
| 21 | + *** (1) The list of all metaqueries. | |
| 22 | + | |
| 23 | +define List(MetaSQL_DML) all_meta_queries = | |
| 24 | + [ | |
| 25 | + | |
| 26 | + // counting the total number of users | |
| 27 | + _SELECT("count_users", | |
| 28 | + "", | |
| 29 | + "exm_tb_user", | |
| 30 | + count["id"], | |
| 31 | + true), | |
| 32 | + | |
| 33 | + // adding a user | |
| 34 | + _INSERT("add_user", | |
| 35 | + "exm_tb_user", | |
| 36 | + ["email","firstname","lastname", | |
| 37 | + "institution","pwhash"]), | |
| 38 | + | |
| 39 | + // updating a user | |
| 40 | + _UPDATE("update_user", | |
| 41 | + "exm_tb_user", | |
| 42 | + ["email","firstname","lastname", | |
| 43 | + "institution"], | |
| 44 | + "id" = var("id")), | |
| 45 | + | |
| 46 | + // creating a new sandbox | |
| 47 | + _INSERT("add_sandbox", | |
| 48 | + "exm_tb_sandbox", | |
| 49 | + ["owner","name","content","trash","created","lastmod"]), | |
| 50 | + | |
| 51 | + // updating the content of a sandbox | |
| 52 | + _UPDATE("update_sandbox", | |
| 53 | + "exm_tb_sandbox", | |
| 54 | + ["content","lastmod"], | |
| 55 | + "owner" = var("owner") & "name" = var("name")), | |
| 56 | + | |
| 57 | + // destroying a sandbox | |
| 58 | + _DELETE("destroy_sandbox", | |
| 59 | + "exm_tb_sandbox", | |
| 60 | + "owner" = var("owner") & "name" = var("name")), | |
| 61 | + | |
| 62 | + // check if a email/password combination is valid for login | |
| 63 | + _SELECT("email_pass_valid", | |
| 64 | + "Id_fName_lName", | |
| 65 | + "exm_tb_user", | |
| 66 | + columns["id","firstname","lastname"], | |
| 67 | + "email" = var("email") & "pwhash" = var("pwhash")), | |
| 68 | + | |
| 69 | + // check if an email is already registred | |
| 70 | + _SELECT("email_already_exists", | |
| 71 | + "EMAIL", | |
| 72 | + "exm_tb_user", | |
| 73 | + columns["email"], | |
| 74 | + "email" = var("email")), | |
| 75 | + | |
| 76 | + // getting a text in English | |
| 77 | + _SELECT("get_en_text", | |
| 78 | + "TEXT", | |
| 79 | + "exm_tb_texts", | |
| 80 | + columns["en"], | |
| 81 | + "symbolic" = var("symb")), | |
| 82 | + | |
| 83 | + // getting a text in French | |
| 84 | + _SELECT("get_fr_text", | |
| 85 | + "TEXT", | |
| 86 | + "exm_tb_texts", | |
| 87 | + columns["fr"], | |
| 88 | + "symbolic" = var("symb")), | |
| 89 | + | |
| 90 | + // getting all users whose firstname/lastname match given masks | |
| 91 | + _SELECT("get_all_users", | |
| 92 | + "USER_A", | |
| 93 | + "exm_tb_user", | |
| 94 | + columns["id","firstname","lastname","institution"], | |
| 95 | + contains("firstname",var("mask1")) | contains("lastname",var("mask2"))), | |
| 96 | + | |
| 97 | + // finding a user by its id | |
| 98 | + _SELECT("get_user_by_id", | |
| 99 | + "USER", | |
| 100 | + "exm_tb_user", | |
| 101 | + columns["email","firstname","lastname", | |
| 102 | + "institution"], | |
| 103 | + "id" = var("user_id"), | |
| 104 | + all), | |
| 105 | + | |
| 106 | + // finding a user by its email | |
| 107 | + _SELECT("get_userid_by_email", | |
| 108 | + "USERID", | |
| 109 | + "exm_tb_user", | |
| 110 | + columns["id"], | |
| 111 | + "email" = var("email")), | |
| 112 | + | |
| 113 | + // getting a user id from its email | |
| 114 | + _SELECT("get_user_id_by_email", | |
| 115 | + "USER_ID", | |
| 116 | + "exm_tb_user", | |
| 117 | + columns["id"], | |
| 118 | + "email" = var("email")), | |
| 119 | + | |
| 120 | + // updating (changing) a password | |
| 121 | + _UPDATE("update_pass", | |
| 122 | + "exm_tb_user", | |
| 123 | + ["pwhash"], | |
| 124 | + "id" = var("userid")), | |
| 125 | + | |
| 126 | + // getting a list of sandboxes of a given user | |
| 127 | + _SELECT("get_sandbox_list", | |
| 128 | + "SANDBOX_DESC", | |
| 129 | + "exm_tb_sandbox", | |
| 130 | + columns["name","created","lastmod"], | |
| 131 | + "owner" = var("owner_id") & "lastmod" >= var("modification_date")), | |
| 132 | + | |
| 133 | + // getting the list of names of all sandboxes of a given user | |
| 134 | + _SELECT("get_sandbox_names_list", | |
| 135 | + "SANDBOX_NAME", | |
| 136 | + "exm_tb_sandbox", | |
| 137 | + columns["name"], | |
| 138 | + "owner" = var("owner_id")) | |
| 139 | + ]. | |
| 140 | + | |
| 141 | + | |
| 142 | + *** (2) Generating the query fonctions, i.e. the file 'metaSQL_example_output.anubis'. | |
| 143 | + | |
| 144 | +global define One | |
| 145 | + make_all_queries | |
| 146 | + ( | |
| 147 | + List(String) args | |
| 148 | + ) = | |
| 149 | + make_queries("metaSQL_example_output.anubis", // the generated file | |
| 150 | + [ ], // files which must be 'read' by the generated file. | |
| 151 | + all_meta_queries, | |
| 152 | + the_example_database). | |
| 153 | + | |
| 154 | +execute anbexec make_all_queries // generate the file | |
| 155 | +read metaSQL_example_output.anubis // check that it is correct | |
| 156 | + | |
| 157 | + | ... | ... |