mysql data warehousing. * * * @author mike@givingassistant.org * @version 1.0 * * * HISTORY: * ======== * 05-08-18 mks _INF-188: original coding * 01-15-20 mks DB-150: PHP7.4 class member type-casting * 06-01-20 mks ECI-108: support for auth token * */ class gatWHC1ProdRegistrations { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CLASS PROPERTIES...``` //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public int $version = 1; // template version - not the same as the release version public string $service = CONFIG_DATABASE_SERVICE_SEGUNDO; // defines the mongo server destination public string $schema = TEMPLATE_DB_PDO; // defines the storage schema for the class public string $templateClass = TEMPLATE_CLASS_WHC1_PROD_REG; // defines the clear-text template class name public string $collection = WH_COOL_PDO_PROD_REGS; // sets the collection (table) name public ?string $whTemplate = null; // sets the WH(cool) collection name, null if not wh'd public string $extension = COLLECTION_PDO_PROD_REGS_EXT; // sets the extension for the collection public bool $closedClass = true; // set to false to allow partner instantiations public bool $setCache = false; // set to true to cache class data public bool $setDeletes = true; // set to true to allow HARD deletes (otherwise: SOFT) public int $setAuditing = AUDIT_NOT_ENABLED; // set to AUDIT_value constant public bool $setJournaling = false; // set to true to allow journaling public bool $setUpdates = false; // set to true to allow record updates public bool $setHistory = false; // set to true to enable detailed record history tracking public string $setDefaultStatus = STATUS_ACTIVE; // set the default status for each record public string $setSearchStatus = STATUS_ACTIVE; // set the default search status public bool $setLocking = false; // set to true to enable record locking for collection public bool $setTimers = true; // set to true to enable collection query timers public string $setPKey = DB_TOKEN; // sets the primary key for the collection public bool $setTokens = false; // set to true: adds the idToken field functionality public bool $selfDestruct = false; // set to false if class contains methods or migration public int $cacheTimer = 0; // number of seconds a tuple will remain in-cache public bool $isGA = true; // set to true is this class is a Namaste internal class public ?string $authToken = null; // if this data class is registered to a partner, you will // need to initialize this member in the constructor (hard-coded) // fields: a key-value paired array, defines the field name and the data type for each field. Prior to insertion, // all data is validated for type and membership. Data that does not satisfy these requirements is // silently dropped prior to insertion. // // Note that for PDO-type tables, the data type is more ... homogeneous... e.g.: data types define the data // type only. It does not define the actual column type in-use. For example, there is no distinction made // between a tinyInt, Int, or BigInt. As far as the framework is concerned, they're all just integers. // public array $fields = [ PDO_ID => DATA_TYPE_INTEGER, // sorting by the id is just like sorting by createdDate PRG_TYPE => DATA_TYPE_STRING, PRG_IID => DATA_TYPE_STRING, PRG_EAV => DATA_TYPE_STRING, PRG_PLATFORM => DATA_TYPE_STRING, PRG_BROWSER => DATA_TYPE_STRING, PRG_MAJOR_VERSION => DATA_TYPE_INTEGER, PRG_MINOR_VERSION => DATA_TYPE_INTEGER, PRG_IS_MOBILE => DATA_TYPE_INTEGER, PRG_IS_TABLET => DATA_TYPE_INTEGER, PRG_FIRST_SEEN => DATA_TYPE_STRING, PRG_LAST_SEEN => DATA_TYPE_STRING, DB_TOKEN => DATA_TYPE_STRING, // unique key (string) exposed externally and is REQUIRED, DB_EVENT_GUID => DATA_TYPE_STRING, // track-back identifier for broker/events DB_CREATED => DATA_TYPE_STRING, // dateTime type DB_STATUS => DATA_TYPE_STRING, // record status DB_ACCESSED => DATA_TYPE_STRING, // dateTime type //_________________________________________________________________________ // UP TO HERE IS THE ORIGINAL DATA -- BELOW IS THE WH_CLASS-SPECIFIC DATA //------------------------------------------------------------------------- DB_WH_CREATED => DATA_TYPE_STRING, DB_WH_EVENT_GUID => DATA_TYPE_STRING, DB_WH_TOKEN => DATA_TYPE_STRING ]; // protected fields are fields that a client is unable to modify or delete. If a client submits a query that // updates these fields, the query will be rejected (worst case) or the directive to update/delete the field // will be silently dropped (best case). In either way, updating or removing this fields cannot be accomplished. // // Minimally, this array should contain the following fields: // -- DB_TOKEN, DB_EVENT_GUID, DB_CREATED, DB_ACCESSED // -- the ID field (either PDO_ID or MONGO_ID) // -- DB_WH_CREATED, DB_WH_EVENT_GUID, DB_WH_TOKEN // public ?array $protectedFields = [ DB_TOKEN, DB_EVENT_GUID, DB_CREATED, PDO_ID, DB_WH_EVENT_GUID, DB_WH_CREATED, DB_WH_TOKEN ]; // all fields that appear in any of the index declarations must appear in this list as this is the property // that's used in the framework as an authoritative check to qualify discriminant fields as indexes. // // indexes are always declared with the template column name and not the cache-map column name // // warehouse indexes are limited to the original record's created date and the three WH fields only // // NOTE: if you're going to declare a single column as a property, then do NOT also declare it as a single index! // public array $indexFields = [ DB_CREATED => 1, DB_WH_TOKEN => 1, DB_WH_CREATED => 1, DB_WH_EVENT_GUID => 1 ]; // the primary key index is declared in the class properties section as $setPKey // unique indexes are to be used a values stored in these columns have to be unique to the table. Note that // null values are permissible in unique-index columns. Do not declare PDO_ID here, regardless of how badly // you may want to. public ?array $uniqueIndexes = [ DB_WH_TOKEN, DB_WH_EVENT_GUID ]; // single field index declarations -- since you can have a field in more than one index (index, multi) // the format for the single-field index declaration is a simple indexed array. public ?array $singleFields = [ DB_CREATED ]; // multi-column (or compound) indexes have format of: // [ INDEX-NAME => [ FIELD_NAME1, FIELD_NAME2, ..., FIELD_NAMEn ]] // where INDEX-NAME is a unique string // unless it's for mongoDB -- mongoDB does not use index labels // // PDO compound-indexes are left-most indexes - if it cannot use the entire index, the db must be able to use // one, or more, of the left-most fields in the index. public ?array $compoundIndexes = [ 'whC1PRG-I1' => [ DB_WH_CREATED, DB_WH_EVENT_GUID, DB_WH_TOKEN ] ]; // NOTE: foreign-key indexes are not explicitly enumerated in a template -- that relationship is defined in the // schema for the table. Foreign-key indexes appear implicitly in the indexing declarations above. // cache maps are requires for namaste service classes. Even if caching is disabled for a class, a cache map is // still required for the class. For PDO classes, the PDO_ID is never included in the mapping, nor is MONGO_ID. public ?array $cacheMap = null; /* * if there is no cache-mapping supported for the class, and you want to limit the fields returned, * then those fields are listed here as an associative array. * * NOTE: You can have caching disabled for the class and still have a cache-map -- this controls the labels * assigned to the returned data column names exposed to the client. Schema should never be exposed. * * NOTE: if you do not support caching for the class and this class is one that is returned to a client, * (some classes are limited to internal use only, like logging), then you should (at a minimum) * exclude the primary key field (integer). * * * This array is an associative array -- the key is the native column name and the value doesn't matter. The * important thing is that the keys are the column names that you want to return back to the client. * * If $exposedFields is to be undefined for the class, then assign it to null. * */ public ?array $exposedFields = [ PRG_TYPE => 1, PRG_IID => 1, PRG_EAV => 1, PRG_PLATFORM => 1, PRG_BROWSER => 1, PRG_MAJOR_VERSION => 1, PRG_MINOR_VERSION => 1, PRG_IS_MOBILE => 1, PRG_IS_TABLET => 1, PRG_FIRST_SEEN => 1, PRG_LAST_SEEN => 1, DB_CREATED => 1, // epoch time DB_STATUS => 1, // record status DB_ACCESSED => 1, // epoch time DB_WH_EVENT_GUID => 1, DB_WH_CREATED => 1, DB_WH_TOKEN => 1 ]; // in PDO-land, binary fields are your basic data blobs. All binary fields require special handling and so // need to be enumerated here as an indexed array. public ?array $binFields = null; // DB SQL: // ------- // PDO SQL is stored in the template and is keyed by the current namaste version (defined in the XML file) during // execution of the deployment script. Each version denotes a container of SQL commands that will be executed // for the targeted version. // // SQL is versioned in parallel with the Namaste (XML->application->id->version) version. Each PDO_SQL // sub-container has several fields - one of which has the version identifier. When the deployment script // executes, the release versions are compared and, if they're an exact match, the SQL is submitted for execution. // // The PDO_SQL container consists of these sub-containers: // // PDO_SQL_VERSION --> this is a float value in the form of x.y as namaste only supports versions as a major // and minor release number. (Patch releases are minor release increments.) // PDO_TABLE --> string value containing the full table name. // PDO_SQL_FC --> the FC means "first commit" -- when the table is first created, it will execute the // SQL in this block, if it exists, and if the version number for the sub-container // exactly matched the version number in the configuration XML. // PDO_SQL_UPDATE --> When the sub-container PDO_SQL_VERSION value exactly matches the XML release value, // then the ALTER-TABLE sql in this update block will be executed. // STRING_DROP_CODE_IDX --> The boilerplate code for dropping the indexes of the table. // STRING_DROP_CODE_DEV --> For version 1.0 only, this points to code to drop the entire table. // // Again, containers themselves are indexed arrays under the PDO_SQL tag. Within the container, data is stored // as an associative array with the keys enumerated above. // // // DB OBJECTS: // ----------- // DB objects are: views, procedures, functions and events. // All such objects assigned to a class are declared in this array under the appropriate header. // This is a safety-feature that prevents a one class (table) from invoking another class object. // The name of the object is stored as an indexed-array under the appropriate header. // // The format for these structures is basically the same. Each DBO is stored in an associative array with the // key defining the name of the object. Within each object, there are embedded associative arrays that have the // name of the object as the key and the object definition (text) and the value: // // objectType => [ objectName => [ objectContent ], ... ] // // Each created object should also have the directive to remove it's predecessor using a DROP statement. // // todo -- unset these objects post-instantiation so that schema is not revealed // // VIEWS: // ------ // Every namaste table will have at least one view which limits the data fetched from the table. At a minimum, // the id_{ext} field is filtered from the resulting data set via the view. Other fields can be withheld as well // but that is something that is individually set-up for each table. // // The basic view has the following syntax for declaring it's name: // view_basic_{tableName_ext} // All views start with the word "view" so as to self-identify the object, followed by the view type which, // optimally, you should try to limit to a single, descriptive word. // // Following this label, which points to a sub-array containing three elements: // STRING_VIEW ----------> this is the SQL code that defines the view as a single string value // STRING_TYPE_LIST -----> null or an array of types that corresponds to variable markers ('?') in the sql // STRING_DESCRIPTION' --> a string that describes the purpose of the view. // // At a minimum, every class definition should contain at-least a basic view as all queries that don't specify // a named view or other DBO, will default to the the basic view in the FROM clause of the generated SQL. // // PROCEDURES: // ----------- // For stored procedures, which are entirely optional, the array definition contains the following elements: // STRING_PROCEDURE -------> the SQL code that defined the stored procedure as a single string value // STRING_DROP_CODE -------> the sql code that drops the procedure (required for procedures!) // STRING_TYPE_LIST -------> an associative array of associative arrays -- in the top level, the key is the name // of the parameter that points to a sub-array that contains the parameter direction // as the key, and the parameter type as the value. There should be an entry for each // parameter to be passed to the stored procedure/function. // // ------------------------------------------------------ // | NOTE: IN params must precede INOUT and OUT params! | // ------------------------------------------------------ // // STRING_SP_EVENT_TYPE ---> Assign one of the DB_EVENT constants to this field to indicate the type of // query the stored-procedure will execute. // NOTE: there is not a defined PDO::PARAM constant for type float: use string. // STRING_DESCRIPTION -----> clear-text definition of the procedure's purpose // // Note that all of these containers are required; empty containers should contain a null placeholder. // // When a stored procedure contains a join of two or more tables/views, the first table listed is considered // to be the "owning" table and the procedure will be declared in the class template for that table, but it will // not be duplicated in other template classes referenced in the join. // public ?array $dbObjects = [ PDO_SQL => [ [ PDO_VERSION => 1.0, PDO_TABLE => 'gaCoolProductRegistrations_prg', PDO_SQL_FC => " -- -- Table structure for table `gaCoolProductRegistrations_prg` -- CREATE TABLE `gaCoolProductRegistrations_prg` ( `id_prg` int(10) UNSIGNED NOT NULL, `type_prg` char(16) NOT NULL, `iid_prg` char(64) DEFAULT NULL, `eav_prg` char(16) DEFAULT NULL, `platform_prg` char(32) DEFAULT NULL, `browser_prg` char(32) DEFAULT NULL, `majorVersion_prg` int(11) UNSIGNED DEFAULT NULL, `minorVersion_prg` int(11) UNSIGNED DEFAULT NULL, `isMobile_prg` tinyint(1) DEFAULT NULL, `isTablet_prg` tinyint(1) UNSIGNED DEFAULT NULL, `firstSeen_prg` datetime DEFAULT NULL, `lastSeen_prg` datetime DEFAULT NULL, `token_prg` char(36) DEFAULT NULL, `eventGuid_prg` char(36) DEFAULT NULL, `createdDate_prg` datetime DEFAULT NULL COMMENT 'replaces kinsert_date', `lastAccessedDate_prg` datetime DEFAULT NULL COMMENT 'replaces kupdate_date', `status_prg` varchar(25) DEFAULT NULL, `whCreated_prg` datetime DEFAULT NULL, `whEventGuid_prg` char(36) DEFAULT NULL, `whToken_prg` char(36) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ", PDO_SQL_UPDATE => " -- -- Indexes for dumped tables -- -- -- Indexes for table `gaCoolProductRegistrations_prg` -- ALTER TABLE `gaCoolProductRegistrations_prg` ADD UNIQUE KEY `whToken_prg` (`whToken_prg`); ", /* * example query return: * --------------------- * ALTER TABLE gaTest_tst DROP INDEX gaTest_tst_createdDate_tst_status_tst_index, DROP INDEX * gaTest_tst_lastAccessedDate_tst_index, DROP INDEX testInteger_tst, DROP INDEX * gaTest_tst_eventGuid_tst_index, DROP INDEX testDouble_tst, DROP INDEX testString_tst; * * NOTE: * ----- * The sql comment code tag (--) will be removed during mysqlConfig's run time processing */ STRING_DROP_CODE_IDX => "-- SELECT CONCAT('ALTER TABLE ', `Table`, ' DROP INDEX ', GROUP_CONCAT(`Index` SEPARATOR ', DROP INDEX '),';' ) FROM ( SELECT table_name AS `Table`, index_name AS `Index` FROM information_schema.statistics WHERE INDEX_NAME != 'PRIMARY' AND table_schema = 'XXXDROP_DB_NAMEXXX' AND table_name = 'XXXDROP_TABLE_NAMEXXX' GROUP BY `Table`, `Index`) AS tmp GROUP BY `Table`; ", STRING_DROP_CODE_DEV => "DROP TABLE IF EXISTS gaCoolProductRegistrations_prg;" // only executed if declared ] ], PDO_VIEWS => [ 'view_basic_gaWHC1ProductRegistrations' => [ STRING_VIEW => "DROP VIEW IF EXISTS view_basic_gaWHC1ProdRegistrations; CREATE VIEW view_basic_gaWHC1ProdRegistrations_prg AS SELECT type_prg, iid_prg, eav_prg, platform_prg, browser_prg, majorVersion_prg, minorVersion_prg, isMobile_prg, isTablet_prg, firstSeen_prg, lastSeen_prg, eventGUID_prg, createdDate_prg, lastAccessedDate_prg, status_prg, token_prg, whCreated_prg, whEventGuid_prg, whToken_prg FROM gaCoolProductRegistrations_prg;", STRING_TYPE_LIST => null, STRING_DESCRIPTION => 'basic query' ], ], PDO_PROCEDURES => [], PDO_FUNCTIONS => [], PDO_EVENTS => [], PDO_TRIGGERS => [] ]; //================================================================================================================= // MIGRATION DECLARATIONS // ---------------------- // Data in this section is used to handle migrations -- when we're pulling from legacy tables into the Namaste // framework. See online doc for more info. // // Note -- this section is not supported for WareHouse templates! (all settings should be null or empty) //================================================================================================================= /** * The migration map is an associative array that maps the Namaste fields (keys) to the corresponding * (remote) legacy fields in the source table to be migrated to Namaste. * * For example, if we were migrating a mysql table in the legacy production database to Namaste::mongo, then * the keys of the migration map would be the Namaste::mongo->fieldNames and the values would be the mysql * column names in the legacy table. * * If there is a value which cannot be mapped to a key, then set it to null. * * Fields that will be dropped in the migration are not listed as values or as keys. * * This map will only exist in the template object and will never be imported into the class widget. * * This is a required field. * */ public ?array $migrationMap = [ PDO_ID => null, PRG_TYPE => 'type', PRG_IID => 'iid', PRG_EAV => 'eav', PRG_PLATFORM => 'platform', PRG_BROWSER => 'browser', PRG_MAJOR_VERSION => 'major_version', PRG_MINOR_VERSION => 'minor_version', PRG_IS_MOBILE => 'is_mobile', PRG_IS_TABLET => 'is_tablet', PRG_FIRST_SEEN => 'first_seen', PRG_LAST_SEEN => 'last_seen', DB_TOKEN => null, DB_EVENT_GUID => null, // generated by broker event DB_CREATED => 'kinsert_date', // epoch time DB_STATUS => null, // record status DB_ACCESSED => 'kupdate_date' // epoch time ]; /* * the migrationSortKey defines the SOURCE field by which the fetch query will be sorted. ALL sort fields are * in ASC order so all we need to list here is the name of the field -- which MUST BE IN THE SOURCE TABLE. * * Populating this field may require preliminary examination of the data - what we want is a field that has * zero NULL values. * * This is a required field. * */ public ?array $migrationSortKey = null; /* * The migrationStatusKey defines the status field/column in the source table -- if the user requires that * soft-deleted records not be migrated, then this field must be set. Otherwise, set the value to null. * * The format is in the form of a key-value paired array. The key specifies the name of the column and the value * specifies the "deleted" value that, if found, will cause that row from the SOURCE data to be omitted from the * DESTINATION table. * * e.g.: $migrationStatusKV = [ 'some_field' => 'deleted' ] * * Note that both the key and the value are case-sensitive! * * This is an optional field. * */ public ?array $migrationStatusKV = null; // The $migrationSourceSchema defines the remote schema for the source table public $migrationSourceSchema = ''; // or STRING_MONGO // The source table in the remote repos (default defined in the XML) must be declared here public $migrationSourceTable = ''; //================================================================================================================= // WAREHOUSE DECLARATIONS ARE DISABLED FOR WAREHOUSE CLASS OBJECTS // ---------------------------------------------------------------------------------------------------------------- public ?array $wareHouse = null; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CLASS METHODS... //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * __construct() -- public method * * we have a constructor to register the destructor. * * @author mike@givingassistant.org * @version 1.0 * * HISTORY: * ======== * 03-23-18 mks CORE-852: original coding * */ public function __construct() { $this->authToken = NULL_TOKEN; register_shutdown_function([$this, STRING_DESTRUCTOR]); } /** * __clone() -- private function * * Silently disallows cloning of the object * * @author mike@givingassistant.org * @version 1.0 * * @return null * * HISTORY: * ======== * 03-23-18 mks CORE-852: original coding * */ private function __clone() { return(null); } /** * __destruct() -- public function * * As of PHP 5.3.10 destructors are not run on shutdown caused by fatal errors. * * The destructor is registered as a shut-down function in the constructor -- so any recovery * efforts should go in this method. * * @author mike@givingassistant.org * @version 1.0 * * HISTORY: * ======== * 03-23-18 mks CORE-852: original coding * */ public function __destruct() { ; } }