Modules\Core\Source\Libs\Logger\SqliteMonologHandler::initialize PHP Method

initialize() private method

Initializes this handler by creating the table if it not exists
private initialize ( )
    private function initialize()
    {
        $this->pdo->exec('CREATE TABLE IF NOT EXISTS `' . $this->table . '` ' . '(id integer not null primary key autoincrement, channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED)');
        //Read out actual columns
        $actualFields = array();
        $rs = $this->pdo->query('PRAGMA table_info(' . $this->table . ')');
        while ($col = $rs->fetch(PDO::FETCH_ASSOC)) {
            $actualFields[] = $col['name'];
        }
        //Calculate changed entries
        $removedColumns = array_diff($actualFields, $this->additionalFields, array('channel', 'level', 'message', 'time'));
        $addedColumns = array_diff($this->additionalFields, $actualFields);
        //Remove columns
        if (!empty($removedColumns)) {
            foreach ($removedColumns as $c) {
                $this->pdo->exec('ALTER TABLE `' . $this->table . '` DROP `' . $c . '`;');
            }
        }
        //Add columns
        if (!empty($addedColumns)) {
            foreach ($addedColumns as $c) {
                $this->pdo->exec('ALTER TABLE `' . $this->table . '` add `' . $c . '` TEXT NULL DEFAULT NULL;');
            }
        }
        //Prepare statement
        $columns = "";
        $fields = "";
        foreach ($this->additionalFields as $f) {
            $columns .= ", {$f}";
            $fields .= ", :{$f}";
        }
        $this->statement = $this->pdo->prepare('INSERT INTO `' . $this->table . '` (channel, level, message, time' . $columns . ') VALUES (:channel, :level, :message, :time' . $fields . ')');
        $this->initialized = true;
    }