function write_data_mssqlnative($table_name) { global $db; $ary_type = $ary_name = array(); $ident_set = false; $sql_data = ''; // Grab all of the data from current table. $sql = "SELECT * FROM {$table_name}"; $db->mssqlnative_set_query_options(array('Scrollable' => SQLSRV_CURSOR_STATIC)); $result = $db->sql_query($sql); $retrieved_data = $db->mssqlnative_num_rows($result); if (!$retrieved_data) { $db->sql_freeresult($result); return; } $sql = "SELECT * FROM {$table_name}"; $result_fields = $db->sql_query_limit($sql, 1); $row = new result_mssqlnative($result_fields); $i_num_fields = $row->num_fields(); for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = $row->field_type($i); $ary_name[$i] = $row->field_name($i); } $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 = $db->sql_query($sql); $row2 = $db->sql_fetchrow($result2); if (!empty($row2['has_identity'])) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n"; $ident_set = true; } $db->sql_freeresult($result2); while ($row = $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 = ''; } $db->sql_freeresult($result); if ($ident_set) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n"; } $this->flush($sql_data); }
/** * Seek to given row number * rownum is zero-based */ function sql_rowseek($rownum, &$query_id) { global $cache; if (isset($cache->sql_rowset[$query_id])) { return $cache->sql_rowseek($rownum, $query_id); } $seek = new result_mssqlnative($query_id); $row = $seek->seek($rownum); return ($row = $seek->fetch()) ? $row : false; }