private static function generateBatchQueryCore(array $rows, $tableName, \mysqli $mysqli)
    {
        $firstRow = true;
        $dataStringRows = array(); # will hold an array list of strings like "('x', 'y', 'z')"
        
        if (count($rows) == 0)
        {
            throw new \Exception("Cannot create batch query with no data.");
        }
        
        foreach ($rows as $row)
        {
            if ($firstRow)
            {
                $columns = array_keys($row);
                sort($columns);
                $firstRow = false;
            }
            
            ksort($row);
            $escapedRow = self::escapeValues($row, $mysqli);
            
            $quotedValues = array();
            # Need just the values, but order is very important.
            foreach ($escapedRow as $columnName => $value)
            {
                if ($value !== null)
                {
                    $quotedValues[] = "'" . $value . "'";
                }
                else
                {
                    $quotedValues[] = 'NULL';  
                }
            }
            
            $dataStringRows[] = "(" . implode(",", $quotedValues) . ")";
        }
        
        $columns = ArrayLib::wrapElements($columns, '`');
        $query = "INTO `" . $tableName . "` (" . implode(',', $columns) . ") " .
                 "VALUES " . implode(",", $dataStringRows);
        
        return $query;
    }