phpbb\db\extractor\mssql_extractor::write_data_mssqlnative PHP Метод

write_data_mssqlnative() защищенный Метод

Extracts data from database table (for MSSQL Native driver)
protected write_data_mssqlnative ( string $table_name ) : null
$table_name string name of the database table
Результат null
    protected function write_data_mssqlnative($table_name)
    {
        if (!$this->is_initialized) {
            throw new extractor_not_initialized_exception();
        }
        $ary_type = $ary_name = array();
        $ident_set = false;
        $sql_data = '';
        // Grab all of the data from current table.
        $sql = "SELECT * FROM {$table_name}";
        $this->db->mssqlnative_set_query_options(array('Scrollable' => SQLSRV_CURSOR_STATIC));
        $result = $this->db->sql_query($sql);
        $retrieved_data = $this->db->mssqlnative_num_rows($result);
        if (!$retrieved_data) {
            $this->db->sql_freeresult($result);
            return;
        }
        $sql = "SELECT COLUMN_NAME, DATA_TYPE\n\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\tWHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = '" . $this->db->sql_escape($table_name) . "'";
        $result_fields = $this->db->sql_query($sql);
        $i_num_fields = 0;
        while ($row = $this->db->sql_fetchrow($result_fields)) {
            $ary_type[$i_num_fields] = $row['DATA_TYPE'];
            $ary_name[$i_num_fields] = $row['COLUMN_NAME'];
            $i_num_fields++;
        }
        $this->db->sql_freeresult($result_fields);
        $sql = "SELECT 1 as has_identity\n\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\tWHERE COLUMNPROPERTY(object_id('{$table_name}'), COLUMN_NAME, 'IsIdentity') = 1";
        $result2 = $this->db->sql_query($sql);
        $row2 = $this->db->sql_fetchrow($result2);
        if (!empty($row2['has_identity'])) {
            $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n";
            $ident_set = true;
        }
        $this->db->sql_freeresult($result2);
        while ($row = $this->db->sql_fetchrow($result)) {
            $schema_vals = $schema_fields = array();
            // Build the SQL statement to recreate the data.
            for ($i = 0; $i < $i_num_fields; $i++) {
                $str_val = $row[$ary_name[$i]];
                // defaults to type number - better quote just to be safe, so check for is_int too
                if (is_int($ary_type[$i]) || preg_match('#char|text|bool|varbinary#i', $ary_type[$i])) {
                    $str_quote = '';
                    $str_empty = "''";
                    $str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
                } else {
                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                        if (empty($str_val)) {
                            $str_quote = '';
                        } else {
                            $str_quote = "'";
                        }
                    } else {
                        $str_quote = '';
                        $str_empty = 'NULL';
                    }
                }
                if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val))) {
                    $str_val = $str_empty;
                }
                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                $schema_fields[$i] = $ary_name[$i];
            }
            // Take the ordered fields and their associated data and build it
            // into a valid sql statement to recreate that field in the data.
            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
            $this->flush($sql_data);
            $sql_data = '';
        }
        $this->db->sql_freeresult($result);
        if ($ident_set) {
            $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n";
        }
        $this->flush($sql_data);
    }