/* * 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 ) . ") }}} .