create_table.anubis 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ)
 * Date: 24/01/2016
 * Time: 18:13

Hayamiki is Copyright © 2016-2023 by David RENÉ
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
 */


read hayamiki_lib/types/hayamiki.anubis
read tools/basis.anubis
read tools/int.anubis
read system/string.anubis
read system/files.anubis
read data_base/sqlite.anubis
read tools/streams.anubis
read tools/ISO-8601.anubis

read fields.anubis
read columns.anubis

read utils/utils.anubis

//define List(String) sqlite_reserved_keywords = (List(String))
//  [
//    "ABORT",  
//    "ACTION",
//    "ADD",
//    "AFTER",
//    "ALL",
//    "ALTER",
//    "ALWAYS",
//    "ANALYZE",
//    "AND",
//    "AS",
//    "ASC",
//    "ATTACH",
//    "AUTOINCREMENT",
//    "BEFORE",
//    "BEGIN",
//    "BETWEEN",
//    "BY",
//    "CASCADE",
//    "CASE",
//    "CAST",
//    "CHECK",
//    "COLLATE",
//    "COLUMN",
//    "COMMIT",
//    "CONFLICT",
//    "CONSTRAINT",
//    "CREATE",
//    "CROSS",
//    "CURRENT",
//    "CURRENT_DATE",
//    "CURRENT_TIME",
//    "CURRENT_TIMESTAMP",
//    "DATABASE",
//    "DEFAULT",
//    "DEFERRABLE",
//    "DEFERRED",
//    "DELETE",
//    "DESC",
//    "DETACH",
//    "DISTINCT",
//    "DO",
//    "DROP",
//    "EACH",
//    "ELSE",
//    "END",
//    "ESCAPE",
//    "EXCEPT",
//    "EXCLUDE",
//    "EXCLUSIVE",
//    "EXISTS",
//    "EXPLAIN",
//    "FAIL",
//    "FILTER",
//    "FOLLOWING",
//    "FOR",
//    "FOREIGN",
//    "FROM",
//    "FULL",
//    "GENERATED",
//    "GLOB",
//    "GROUP",
//    "GROUPS",
//    "HAVING",
//    "IF",
//    "IGNORE",
//    "IMMEDIATE",
//    "IN",
//    "INDEX",
//    "INDEXED",
//    "INITIALLY",
//    "INNER",
//    "INSERT",
//    "INSTEAD",
//    "INTERSECT",
//    "INTO",
//    "IS",
//    "ISNULL",
//    "JOIN",
//    "KEY",
//    "LAST",
//    "LEFT",
//    "LIKE",
//    "LIMIT",
//    "MATCH",
//    "MATERIALIZED",
//    "NATURAL",
//    "NO",
//    "NOT",
//    "NOTHING",
//    "NOTNULL",
//    "NULL",
//    "NULLS",
//    "OF",
//    "OFFSET",
//    "ON",
//    "OR",
//    "ORDER",
//    "OTHERS",
//    "OUTER",
//    "OVER",
//    "PARTITION",
//    "PLAN",
//    "PRAGMA",
//    "PRECEDING",
//    "PRIMARY",
//    "QUERY",
//    "RAISE",
//    "RANGE",
//    "RECURSIVE",
//    "REFERENCES",
//    "REGEXP",
//    "REINDEX",
//    "RELEASE",
//    "RENAME",
//    "REPLACE",
//    "RESTRICT",
//    "RETURNING",
//    "RIGHT",
//    "ROLLBACK",
//    "ROW",
//    "ROWS",
//    "SAVEPOINT",
//    "SELECT",
//    "SET",
//    "TABLE",
//    "TEMP",
//    "TEMPORARY",
//    "THEN",
//    "TIES",
//    "TO",
//    "TRANSACTION",
//    "TRIGGER",
//    "UNBOUNDED",
//    "UNION",
//    "UNIQUE",
//    "UPDATE",
//    "USING",
//    "VACUUM",
//    "VALUES",
//    "VIEW",
//    "VIRTUAL",
//    "WHEN",
//    "WHERE",
//    "WINDOW",
//    "WITH",
//    "WITHOUT"
//].
  
define String
  create_table_generate_one_column
  (
    HK_Model_Column column,
    Int             fill_size
  )=
  since column is hk_column(name, col_type, attributes, _),

  fill(escaped_quoted_sqlite_reserved_keyword(name), fill_size)+ to_SQL_CREATE(col_type, attributes, name)
  .

define String
  create_table_generate_columns
  (
    List(HK_Model_Column) columns,
    String                indent
  )=
  //get the longest column name size, to be able to make formatting according to column name.
  with column_max_size = max(columns),
  join(",\n"+indent, map((HK_Model_Column column) |-> create_table_generate_one_column(column, column_max_size+1), columns))
  .

define String
  to_String
  (
    HK_On_Conflict on_conflict
  )=
  if on_conflict is
  {
     rollback  then "ROLLBACK", 
     abort     then "ABORT", 
     fail      then "FAIL", 
     ignore    then "IGNORE", 
     replace   then "REPLACE" 
  }
.

define String
  create_table_generate_one_constraint
  (
    HK_Constraint constraint,
    String        indent
  )=
  since constraint is unique(_name, columns, _on_conflict),
  with name = if _name = "" then "" else "CONSTRAINT "+_name+" ",
  ",\n"+
  indent+name+"UNIQUE (\n"+
    indent+indent+join(",\n"+indent+indent, escaped_quoted_sqlite_reserved_keyword(columns))+"\n"+
    indent+")\n"+
    indent+"ON CONFLICT "+to_String(_on_conflict)
.

define String
  create_table_generate_constraints
  (
    List(HK_Constraint) constraints,
    String              indent
  )=
  join(",\n"+indent, map((HK_Constraint constraint) |-> create_table_generate_one_constraint(constraint, indent), constraints))
.

define String
  create_table_generate_one_table
  (
    HK_Table  table
  )=
  since table is hk_table(name, short_name, model, display),
  since model is hk_model( columns, constraints,  _),
  
  "\n"+
  "  // "+to_upper(name)+"\n"+
  "public define String create_table_"+name+" =\n"+
  "\"CREATE TABLE "+name+" (\n"+
  //columns
   "   "+create_table_generate_columns([hk_column("id", p_key) . columns], "   ")+
   create_table_generate_constraints(constraints, "   ")+
  "\n)\".\n"
  .

define String
  create_table_generate_all_tables
  (
    List(HK_App)    apps,     //
    List(HK_Table)  tables,   //list of the table model description
    String          so_far
  )=
  if tables is
  {
    []          then  //no more table to generate
      if apps is
      {
        []            then  //no more apps to generate
          so_far,           //return the all create table generated so far
        [app. t_apps] then  //another app available
          since app is hk_app(name, _, _, app_tables),
          create_table_generate_all_tables(t_apps, app_tables, so_far + "\n /************ TABLES for Application '"+name+"' ***************/")
      },

    [table . t] then  
      //generate the current table "CREATE TABLE"
      create_table_generate_all_tables(apps, t, so_far + create_table_generate_one_table(table))
  }.


  
define String
  generate_all_tables_list
  (
    List(HK_App)    apps,
//    List(HK_Table)  tables,  //list of the table model description
    String          indent
  )=
  //calculate then longest table name and add 4 for enclosed quote + coma + final space
  //hence we can make a beautiful formatting
  with max_space = longest_table_name_size(apps, 0) + 4,  
  join(",\n",
    map_append((HK_App  app)  |-> since app is hk_app(_, _, _, tables),
      map((HK_Table table)    |-> indent+"table("+fill("\""+table.name+"\",", max_space)+"create_table_"+table.name+")", tables), apps)).
  
public define Maybe(One)
  generate_create_table
  (
    String        app_name,
    List(HK_App)  apps,     //list of the table model description
    String        dest_dir,
    String        version,
    String        _sha1
  )=

  with version_str = find_and_replace(version,".","_"), //replace the dot by underscore because dot is not allowed in function name by Anubis language
   
  with file_name            = dest_dir+"database/generated/migration/to_"+version+".anubis",
  with create_file_name     = dest_dir+"database/generated/migration/to_"+version+"_create.anubis",
  //with script_file_name = dest_dir+"database/generated/migration/to_"+version+"_SQL.anubis",  
  //create all necessary directories if doesn't exist.
  make_directories(file_name);
  
  //create script file name as new file. Previous file with same name will be erased
  if file(create_file_name, new) is
  {
    failure             then  println("Can't create script "+create_file_name);failure,
    success(fd_script)  then
      with script_stream = make_stream(fd_script),
      //write into script file the generated create table
      if write_string(script_stream,
"/*
 * Created by 早見木 (Hayamiki).
 * Date: "+_Int_to_ISO_8601_date(now)+"
 * Time: "+_Int_to_ISO_8601_time(now)+"
 */

read system/logger.anubis
read xlib/database/migration_common_sqlite3.anubis
read data_base/sqlite_foreign_key.anubis
read data_base/db_types.anubis\n"+
  //call the generation of all tables
  create_table_generate_all_tables(apps, [], "")+
  //TODO make indexes
  "\npublic define List(String) db_indexes = [].\n"+
  "// ///////////////////////////////\n"+
  "// Foreign keys \n"+
  "public define List((String, String, String, String, FK_Null, FK_Cascade)) db_relations = [].\n\n\n
define String _version = \""+version+"\".

public define Maybe(One)
  create_v"+version_str+"_tables
  (
    SQLite3DataBase           db,
    (LogLevel, String) -> One logger
  )=
  logger(logInfo, \"DB migration create_v"+version_str+"_tables\");
  if drop_all_triggers(db, logger) is failure then 
      logger(logError, \"DB migration create_v"+version_str+"_tables: drop_all_triggers error\");
      failure 
  else
  
  with tables = (List(DbTable)) [
"+generate_all_tables_list(apps, "                  ")+"
                ],
  if create_tables(db, logger, tables, _version) is 
  {
    failure    then 
      logger(logError, \"DB migration create_v"+version_str+"_tables: create_tables error\");
      failure,
    success(_) then
      if make_foreign_keys(db, logger, db_relations, _version) is failure then failure else
      success(unique)
  }
.

public define DB_Version db_model_version = db_version(\""+version+"\", \""+_sha1+"\").
\n\n"
  
  ) is
  {
    failure     then println ("Can't generate script "+create_file_name);failure,
    success(_)  then
      //create file with create table management (alter_table, etc...)
      if file_exists(file_name) then
        success(println("Skip creation of "+file_name+" because it's already exists and preventing to erase custom migration stuff"))
      else
      if file(file_name, new) is
      {
        failure     then  println("can't create file "+file_name);failure,
        success(fd) then 
      with stream = make_stream(fd),
      write_string(stream,
"/*
 * Created by 早見木 (Hayamiki).
 * Date: "+_Int_to_ISO_8601_date(now)+"
 * Time: "+_Int_to_ISO_8601_time(now)+"
 */
 * 
read tools/basis.anubis
read system/files.anubis
read system/logger.anubis
read data_base/db_tools.anubis

read xlib/database/db_utils.anubis
read xlib/database/migration_common_sqlite3.anubis
read xlib/database/migration_sqlite3.anubis
read data_base/alter_table.anubis

read types/app_types.anubis
read app/app_constants.anubis
read app/app_loggers.anubis

read database/generated/migration/to_"+version+"_create.anubis

define String _version = \""+version+"\".
           
public define Maybe(One)
  create_v"+version_str+"_indexes
  (
    SQLite3DataBase           db,
    (LogLevel, String) -> One logger
  )=
  logger(logInfo, \"DB migration create_v"+version_str+"_indexes\");
  if create_indexes(db, logger, db_indexes, _version) is failure then failure else
  success(unique)
  .

define Maybe(One)
  to_v"+version_str+"_pre
  (
    SQLite3DataBase           db,
    (LogLevel, String) -> One logger
  )=
  logger(logInfo, \"DB migration to_v"+version_str+"_pre\");
  success(unique)
.

define Maybe(One)
  to_v"+version_str+"_post
  (
    SQLite3DataBase           db,
    (LogLevel, String) -> One logger
  )=
  logger(logInfo, \"DB migration to_v"+version_str+"_post\");
  success(unique)
.

public define Maybe(One)
  migrate_to_v"+version_str+"
  (
    SQLite3DataBase           db,
    (LogLevel, String) -> One logger,
  )=
       if        to_v"+version_str+"_pre(db, logger) is failure then failure
  else if create_v"+version_str+"_tables(db, logger) is failure then failure
  else if       to_v"+version_str+"_post(db, logger) is failure then failure
  else   create_v"+version_str+"_indexes(db, logger)
.

global define Migration
  "+app_name+"_dbm_to_v"+version_str+"
  =
  migration(db_model_version, migrate_to_v"+version_str+", create_v"+version_str+"_tables, create_v"+version_str+"_indexes)
.

read hayamiki_lib/view/view_table_manager_types.anubis
read src/database/generated/vtm.anubis

global define VTM
  "+app_name+"_vtm_v"+version_str+"
  =
  vtm(
    db_model_version,
    vtm_view_list(generated_vtm_view_list),
    vtm_edit_list(generated_vtm_edit_list),
    generated_hk_table_controller_list
  )
.
  ")
 }}}
.