0

I have a giant dump of an mssql file. I want to read that file via file_get_contents(), and plop it all into a bunch of arrays based on an ID from each line. Unfortunately, the ID is named something different in different tables. The lines below are all for one particular post (and are among 29 entries with the same ID). The ID is 1234567890 in the examples below.

Example:

[ID] below

INSERT [dbo].[sf_CmsContentBase] ([Application], [ID], [MimeType], [StreamingProviderName], [ParentID], [Url], [LoweredUrl], [CommentsCount], [ItemInfo], [Creator]) VALUES (N'/News', N'1234567890', N'text/html', NULL, NULL, N'/2012/URL', N'/2012/url', 0, NULL, N'author')

[ContentID] below

INSERT [dbo].[sf_CmsTaggedContent] ([Application], [ContentID], [TagID], [Owner]) VALUES (N'/News', N'1234567890', N'3434', N'author')

[ItemID] below

INSERT [dbo].[sf_VrsTxtData] ([Application], [ItemID], [CultureID], [Version], [KeyValue], [DataImpl], [HasDynamicLinks], [TypeCode]) VALUES (N'/', N'1234567890', 101, 1, N'Thumbnail', N'/news/images/thumbnail.jpg', 0, 18)

So essentially what I'd do is form an array out of each of these lines first, so the first one would be something like

$sqlLineArray = array(
    'Application'           => '/News',
    'ID'                    => '1234567890',
    'MimeType'              => 'text/html',
    'StreamingProviderName' => NULL,
    'ParentID'              => NULL,
    'Url'                   => '/2012/URL',
    'LoweredUrl'            => '/2012/url',
    'CommentsCount'         => '0',
    'ItemInfo'              => NULL,
    'Creator'               => 'author'
);

Then I would foreach over each of those arrays, apply some conditionals for the difference in ID naming, and then have another multidimensional array where the ID is the key, and it has all of the associate subarrays just created within in, so I've kind of linked the data together and I can normalize it for another database later.

Is there an easy way to do this with PHP without actually querying a database? I only have a data dump. I'm thinking I could just explode() in a bunch of places, but there's no rhyme or reason to the order where the ID appears, so I can't necessarily say that ID is $sqlLineArray[2] or something, because it's not always going to be that.

mssql_fetch_array() kind of seems appropriate, but the first parameter has to be a resource, not just a query string.

4

2 回答 2

1

更新

重新阅读问题后,我错过了从 SQL => PHP Array 开始的重要要求。鉴于所有INSERT语句都是标准化的,您必须按以下顺序解析:

  1. 找到每一INSERT行,
  2. 查找列列表
  3. 查找值

这是一个快速而肮脏的解决方案:

$sql  = "INSERT INTO [dbo].[table]";

//Implode keys
$sql .= " ([" . implode("], [", array_keys($sqlLineArray)) . "])";

//Implode values
$sql .= " VALUES ('" . implode("', '", $sqlLineArray) . "') ";

结果

插入 [dbo].[table] ([Application], [ID], [MimeType], [StreamingProviderName],
[ParentID], [Url], [LoweredUrl], [CommentsCount], [ItemInfo], [Creator]) VALUES ('/News',
'1234567890', 'text/html', '', '', '/2012/URL', '/2012/url', '0', '', '作者')

此解决方案将彻底准备您的数组,省略NULLs:

$cmd = "INSERT INTO [dbo].[table]";

foreach($sqlLineArray as $key=>$val) {
  if(isset($val)) {
    $colList[] = '[' . $key . ']';

    if(is_numeric($val)) {
      $valList[] = $val;
    }
    else {
      $valList[] = "N'" . $val . "'";
    }
  }
}

$sql = $cmd . '(' . implode(", ", $colList) . ') VALUES (' . implode(", ", $valList) . ')';

结果

插入 [dbo].[table]([Application], [ID], [MimeType], [Url], [LoweredUrl],
 [CommentsCount], [Creator]) VALUES (N'/News', 1234567890, N'text/html',
N'/2012/URL', N'/2012/url', 0, N'作者')

最后,此解决方案将准备您的数组,包括NULL

$cmd = "INSERT INTO [dbo].[table]";

foreach($sqlLineArray as $key=>$val) {
  $colList[] = '[' . $key . ']';

  if(is_numeric($val)) {
    $valList[] = $val;
  }
  elseif(isset($val)) {
    $valList[] = "N'" . $val . "'";
  }
  else {
    $valList[] = 'NULL';
  }
}

$sql = $cmd . '(' . implode(", ", $colList) . ') VALUES (' . implode(", ", $valList) . ')';

结果

插入 [dbo].[table]([Application], [ID], [MimeType], [StreamingProviderName],
 [ParentID]、[Url]、[LoweredUrl]、[CommentsCount]、[ItemInfo]、[Creator])值
(N'/News', 1234567890, N'text/html', NULL, NULL, N'/2012/URL', N'/2012/url', 0, NULL, N'author')
于 2012-09-19T21:16:48.613 回答
0

我喜欢 njks 解决方案,但我也想出了自己的可怕解决方案,以防有人感兴趣:

$data = file_get_contents('relevantlinesoriginal.sql');
$lines = explode("\n",$data);
foreach ($lines as $line) {
    $noInsert = explode("] (", $line);
    $noInsert = array_pop($noInsert);
    $separatingNamesAndValues = explode(") VALUES (N'", $noInsert);
    $namesString = $separatingNamesAndValues[0];
    $valuesString = $separatingNamesAndValues[1];
    $names = str_replace(array('[',']'), "", $namesString);
    $values = substr_replace($valuesString, "", -2);
    $names = explode(', ', $names);
    $values = explode(', ', $values);
    $finalArray = array_combine($names, $values);
    var_dump("<pre>", $finalArray, "</pre>");
}
于 2012-09-19T21:30:52.187 回答