PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/phpbb/db/extractor/mssql_extractor.php

https://bitbucket.org/cmwdosp/cmwbb3
PHP | 415 lines | 341 code | 38 blank | 36 comment | 32 complexity | 0c2cf02b6d6da6b638458baff738ad90 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\db\extractor;
  14. use phpbb\db\extractor\exception\extractor_not_initialized_exception;
  15. class mssql_extractor extends base_extractor
  16. {
  17. /**
  18. * Writes closing line(s) to database backup
  19. *
  20. * @return null
  21. * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
  22. */
  23. public function write_end()
  24. {
  25. if (!$this->is_initialized)
  26. {
  27. throw new extractor_not_initialized_exception();
  28. }
  29. $this->flush("COMMIT\nGO\n");
  30. parent::write_end();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function write_start($table_prefix)
  36. {
  37. if (!$this->is_initialized)
  38. {
  39. throw new extractor_not_initialized_exception();
  40. }
  41. $sql_data = "--\n";
  42. $sql_data .= "-- phpBB Backup Script\n";
  43. $sql_data .= "-- Dump of tables for $table_prefix\n";
  44. $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  45. $sql_data .= "--\n";
  46. $sql_data .= "BEGIN TRANSACTION\n";
  47. $sql_data .= "GO\n";
  48. $this->flush($sql_data);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function write_table($table_name)
  54. {
  55. if (!$this->is_initialized)
  56. {
  57. throw new extractor_not_initialized_exception();
  58. }
  59. $sql_data = '-- Table: ' . $table_name . "\n";
  60. $sql_data .= "IF OBJECT_ID(N'$table_name', N'U') IS NOT NULL\n";
  61. $sql_data .= "DROP TABLE $table_name;\n";
  62. $sql_data .= "GO\n";
  63. $sql_data .= "\nCREATE TABLE [$table_name] (\n";
  64. $rows = array();
  65. $text_flag = false;
  66. $sql = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
  67. FROM INFORMATION_SCHEMA.COLUMNS
  68. WHERE TABLE_NAME = '$table_name'";
  69. $result = $this->db->sql_query($sql);
  70. while ($row = $this->db->sql_fetchrow($result))
  71. {
  72. $line = "\t[{$row['COLUMN_NAME']}] [{$row['DATA_TYPE']}]";
  73. if ($row['DATA_TYPE'] == 'text')
  74. {
  75. $text_flag = true;
  76. }
  77. if ($row['IS_IDENTITY'])
  78. {
  79. $line .= ' IDENTITY (1 , 1)';
  80. }
  81. if ($row['CHARACTER_MAXIMUM_LENGTH'] && $row['DATA_TYPE'] !== 'text')
  82. {
  83. $line .= ' (' . $row['CHARACTER_MAXIMUM_LENGTH'] . ')';
  84. }
  85. if ($row['IS_NULLABLE'] == 'YES')
  86. {
  87. $line .= ' NULL';
  88. }
  89. else
  90. {
  91. $line .= ' NOT NULL';
  92. }
  93. if ($row['COLUMN_DEFAULT'])
  94. {
  95. $line .= ' DEFAULT ' . $row['COLUMN_DEFAULT'];
  96. }
  97. $rows[] = $line;
  98. }
  99. $this->db->sql_freeresult($result);
  100. $sql_data .= implode(",\n", $rows);
  101. $sql_data .= "\n) ON [PRIMARY]";
  102. if ($text_flag)
  103. {
  104. $sql_data .= " TEXTIMAGE_ON [PRIMARY]";
  105. }
  106. $sql_data .= "\nGO\n\n";
  107. $rows = array();
  108. $sql = "SELECT CONSTRAINT_NAME, COLUMN_NAME
  109. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  110. WHERE TABLE_NAME = '$table_name'";
  111. $result = $this->db->sql_query($sql);
  112. while ($row = $this->db->sql_fetchrow($result))
  113. {
  114. if (!sizeof($rows))
  115. {
  116. $sql_data .= "ALTER TABLE [$table_name] WITH NOCHECK ADD\n";
  117. $sql_data .= "\tCONSTRAINT [{$row['CONSTRAINT_NAME']}] PRIMARY KEY CLUSTERED \n\t(\n";
  118. }
  119. $rows[] = "\t\t[{$row['COLUMN_NAME']}]";
  120. }
  121. if (sizeof($rows))
  122. {
  123. $sql_data .= implode(",\n", $rows);
  124. $sql_data .= "\n\t) ON [PRIMARY] \nGO\n";
  125. }
  126. $this->db->sql_freeresult($result);
  127. $index = array();
  128. $sql = "EXEC sp_statistics '$table_name'";
  129. $result = $this->db->sql_query($sql);
  130. while ($row = $this->db->sql_fetchrow($result))
  131. {
  132. if ($row['TYPE'] == 3)
  133. {
  134. $index[$row['INDEX_NAME']][] = '[' . $row['COLUMN_NAME'] . ']';
  135. }
  136. }
  137. $this->db->sql_freeresult($result);
  138. foreach ($index as $index_name => $column_name)
  139. {
  140. $index[$index_name] = implode(', ', $column_name);
  141. }
  142. foreach ($index as $index_name => $columns)
  143. {
  144. $sql_data .= "\nCREATE INDEX [$index_name] ON [$table_name]($columns) ON [PRIMARY]\nGO\n";
  145. }
  146. $this->flush($sql_data);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function write_data($table_name)
  152. {
  153. if (!$this->is_initialized)
  154. {
  155. throw new extractor_not_initialized_exception();
  156. }
  157. if ($this->db->get_sql_layer() === 'mssqlnative')
  158. {
  159. $this->write_data_mssqlnative($table_name);
  160. }
  161. else
  162. {
  163. $this->write_data_odbc($table_name);
  164. }
  165. }
  166. /**
  167. * Extracts data from database table (for MSSQL Native driver)
  168. *
  169. * @param string $table_name name of the database table
  170. * @return null
  171. * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
  172. */
  173. protected function write_data_mssqlnative($table_name)
  174. {
  175. if (!$this->is_initialized)
  176. {
  177. throw new extractor_not_initialized_exception();
  178. }
  179. $ary_type = $ary_name = array();
  180. $ident_set = false;
  181. $sql_data = '';
  182. // Grab all of the data from current table.
  183. $sql = "SELECT * FROM $table_name";
  184. $this->db->mssqlnative_set_query_options(array('Scrollable' => SQLSRV_CURSOR_STATIC));
  185. $result = $this->db->sql_query($sql);
  186. $retrieved_data = $this->db->mssqlnative_num_rows($result);
  187. if (!$retrieved_data)
  188. {
  189. $this->db->sql_freeresult($result);
  190. return;
  191. }
  192. $sql = "SELECT COLUMN_NAME, DATA_TYPE
  193. FROM INFORMATION_SCHEMA.COLUMNS
  194. WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = '" . $this->db->sql_escape($table_name) . "'";
  195. $result_fields = $this->db->sql_query($sql);
  196. $i_num_fields = 0;
  197. while ($row = $this->db->sql_fetchrow($result_fields))
  198. {
  199. $ary_type[$i_num_fields] = $row['DATA_TYPE'];
  200. $ary_name[$i_num_fields] = $row['COLUMN_NAME'];
  201. $i_num_fields++;
  202. }
  203. $this->db->sql_freeresult($result_fields);
  204. $sql = "SELECT 1 as has_identity
  205. FROM INFORMATION_SCHEMA.COLUMNS
  206. WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
  207. $result2 = $this->db->sql_query($sql);
  208. $row2 = $this->db->sql_fetchrow($result2);
  209. if (!empty($row2['has_identity']))
  210. {
  211. $sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
  212. $ident_set = true;
  213. }
  214. $this->db->sql_freeresult($result2);
  215. while ($row = $this->db->sql_fetchrow($result))
  216. {
  217. $schema_vals = $schema_fields = array();
  218. // Build the SQL statement to recreate the data.
  219. for ($i = 0; $i < $i_num_fields; $i++)
  220. {
  221. $str_val = $row[$ary_name[$i]];
  222. // defaults to type number - better quote just to be safe, so check for is_int too
  223. if (is_int($ary_type[$i]) || preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
  224. {
  225. $str_quote = '';
  226. $str_empty = "''";
  227. $str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
  228. }
  229. else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  230. {
  231. if (empty($str_val))
  232. {
  233. $str_quote = '';
  234. }
  235. else
  236. {
  237. $str_quote = "'";
  238. }
  239. }
  240. else
  241. {
  242. $str_quote = '';
  243. $str_empty = 'NULL';
  244. }
  245. if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
  246. {
  247. $str_val = $str_empty;
  248. }
  249. $schema_vals[$i] = $str_quote . $str_val . $str_quote;
  250. $schema_fields[$i] = $ary_name[$i];
  251. }
  252. // Take the ordered fields and their associated data and build it
  253. // into a valid sql statement to recreate that field in the data.
  254. $sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
  255. $this->flush($sql_data);
  256. $sql_data = '';
  257. }
  258. $this->db->sql_freeresult($result);
  259. if ($ident_set)
  260. {
  261. $sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
  262. }
  263. $this->flush($sql_data);
  264. }
  265. /**
  266. * Extracts data from database table (for ODBC driver)
  267. *
  268. * @param string $table_name name of the database table
  269. * @return null
  270. * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
  271. */
  272. protected function write_data_odbc($table_name)
  273. {
  274. if (!$this->is_initialized)
  275. {
  276. throw new extractor_not_initialized_exception();
  277. }
  278. $ary_type = $ary_name = array();
  279. $ident_set = false;
  280. $sql_data = '';
  281. // Grab all of the data from current table.
  282. $sql = "SELECT *
  283. FROM $table_name";
  284. $result = $this->db->sql_query($sql);
  285. $retrieved_data = odbc_num_rows($result);
  286. if ($retrieved_data)
  287. {
  288. $sql = "SELECT 1 as has_identity
  289. FROM INFORMATION_SCHEMA.COLUMNS
  290. WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
  291. $result2 = $this->db->sql_query($sql);
  292. $row2 = $this->db->sql_fetchrow($result2);
  293. if (!empty($row2['has_identity']))
  294. {
  295. $sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
  296. $ident_set = true;
  297. }
  298. $this->db->sql_freeresult($result2);
  299. }
  300. $i_num_fields = odbc_num_fields($result);
  301. for ($i = 0; $i < $i_num_fields; $i++)
  302. {
  303. $ary_type[$i] = odbc_field_type($result, $i + 1);
  304. $ary_name[$i] = odbc_field_name($result, $i + 1);
  305. }
  306. while ($row = $this->db->sql_fetchrow($result))
  307. {
  308. $schema_vals = $schema_fields = array();
  309. // Build the SQL statement to recreate the data.
  310. for ($i = 0; $i < $i_num_fields; $i++)
  311. {
  312. $str_val = $row[$ary_name[$i]];
  313. if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
  314. {
  315. $str_quote = '';
  316. $str_empty = "''";
  317. $str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
  318. }
  319. else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  320. {
  321. if (empty($str_val))
  322. {
  323. $str_quote = '';
  324. }
  325. else
  326. {
  327. $str_quote = "'";
  328. }
  329. }
  330. else
  331. {
  332. $str_quote = '';
  333. $str_empty = 'NULL';
  334. }
  335. if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
  336. {
  337. $str_val = $str_empty;
  338. }
  339. $schema_vals[$i] = $str_quote . $str_val . $str_quote;
  340. $schema_fields[$i] = $ary_name[$i];
  341. }
  342. // Take the ordered fields and their associated data and build it
  343. // into a valid sql statement to recreate that field in the data.
  344. $sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
  345. $this->flush($sql_data);
  346. $sql_data = '';
  347. }
  348. $this->db->sql_freeresult($result);
  349. if ($retrieved_data && $ident_set)
  350. {
  351. $sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
  352. }
  353. $this->flush($sql_data);
  354. }
  355. }