ImportModel::_InsertTable PHP Method

_InsertTable() protected method

protected _InsertTable ( $TableName, array $Sets = [] ) : boolean | integer | void
$TableName
$Sets array
return boolean | integer | void
    protected function _InsertTable($TableName, $Sets = array())
    {
        if (!array_key_exists($TableName, $this->Tables())) {
            return;
        }
        if (!Gdn::structure()->TableExists($TableName)) {
            return 0;
        }
        $TableInfo =& $this->Tables($TableName);
        $Columns = $TableInfo['Columns'];
        foreach ($Columns as $Key => $Value) {
            if (stringBeginsWith($Key, '_')) {
                unset($Columns[$Key]);
            }
        }
        // Build the column insert list.
        $Insert = "insert ignore :_{$TableName} (\n  " . implode(",\n  ", array_map(array('ImportModel', 'BackTick'), array_keys(array_merge($Columns, $Sets)))) . "\n)";
        $From = "from :_z{$TableName} i";
        $Where = '';
        // Build the select list for the insert.
        $Select = array();
        foreach ($Columns as $Column => $X) {
            $BColumn = self::BackTick($Column);
            if (strcasecmp($this->Overwrite(), 'Overwrite') == 0) {
                // The data goes in raw.
                $Select[] = "i.{$BColumn}";
            } elseif ($Column == $TableName . 'ID') {
                // This is the primary key.
                $Select[] = "i._NewID as {$Column}";
                $Where = "\nwhere i._Action = 'Insert'";
            } elseif (substr_compare($Column, 'ID', -2, 2) == 0) {
                // This is an ID field. Check for a join.
                foreach ($this->Tables() as $StructureTableName => $TableInfo) {
                    $PK = $StructureTableName . 'ID';
                    if (strlen($Column) >= strlen($PK) && substr_compare($Column, $PK, -strlen($PK), strlen($PK)) == 0) {
                        // This table joins and must update it's ID.
                        $From .= "\nleft join :_z{$StructureTableName} z{$Column}\n  on i.{$Column} = z{$Column}.{$PK}";
                        $Select[] = "z{$Column}._NewID";
                    }
                }
            } else {
                // This is a straight columns insert.
                $Select[] = "i.{$BColumn}";
            }
        }
        // Add the original table to prevent duplicates.
        //      $PK = $TableName.'ID';
        //      if(array_key_exists($PK, $Columns)) {
        //      if(strcasecmp($this->Overwrite(), 'Overwrite') == 0)
        //            $PK2 = $PK;
        //         else
        //            $PK2 = '_NewID';
        //
        //         $From .= "\nleft join :_$TableName o0\n  on o0.$PK = i.$PK2";
        //         if($Where)
        //            $Where .=  "\n  and ";
        //         else
        //            $Where = "\nwhere ";
        //         $Where .= "o0.$PK is null";
        //      }
        //}
        // Add the sets to the select list.
        foreach ($Sets as $Field => $Value) {
            $Select[] = Gdn::database()->connection()->quote($Value) . ' as ' . $Field;
        }
        // Build the sql statement.
        $Sql = $Insert . "\nselect\n  " . implode(",\n  ", $Select) . "\n" . $From . $Where;
        //$this->query($Sql);
        $RowCount = $this->query($Sql);
        if ($RowCount > 0) {
            return (int) $RowCount;
        } else {
            return false;
        }
    }