| @@ 707-759 (lines=53) @@ | ||
| 704 | * @param bool $disable_trans Whether to disable transactions |
|
| 705 | * @param resource $connection The connection to use (if null, $db_connection is used) |
|
| 706 | */ |
|
| 707 | function smf_db_insert($method = 'replace', $table, $columns, $data, $keys, $disable_trans = false, $connection = null) |
|
| 708 | { |
|
| 709 | global $smcFunc, $db_connection, $db_prefix; |
|
| 710 | ||
| 711 | $connection = $connection === null ? $db_connection : $connection; |
|
| 712 | ||
| 713 | // With nothing to insert, simply return. |
|
| 714 | if (empty($data)) |
|
| 715 | return; |
|
| 716 | ||
| 717 | // Replace the prefix holder with the actual prefix. |
|
| 718 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
|
| 719 | ||
| 720 | // Inserting data as a single row can be done as a single array. |
|
| 721 | if (!is_array($data[array_rand($data)])) |
|
| 722 | $data = array($data); |
|
| 723 | ||
| 724 | // Create the mold for a single row insert. |
|
| 725 | $insertData = '('; |
|
| 726 | foreach ($columns as $columnName => $type) |
|
| 727 | { |
|
| 728 | // Are we restricting the length? |
|
| 729 | if (strpos($type, 'string-') !== false) |
|
| 730 | $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 731 | else |
|
| 732 | $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 733 | } |
|
| 734 | $insertData = substr($insertData, 0, -2) . ')'; |
|
| 735 | ||
| 736 | // Create an array consisting of only the columns. |
|
| 737 | $indexed_columns = array_keys($columns); |
|
| 738 | ||
| 739 | // Here's where the variables are injected to the query. |
|
| 740 | $insertRows = array(); |
|
| 741 | foreach ($data as $dataRow) |
|
| 742 | $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 743 | ||
| 744 | // Determine the method of insertion. |
|
| 745 | $queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT'); |
|
| 746 | ||
| 747 | // Do the insert. |
|
| 748 | $smcFunc['db_query']('', ' |
|
| 749 | ' . $queryTitle . ' INTO ' . $table . '(`' . implode('`, `', $indexed_columns) . '`) |
|
| 750 | VALUES |
|
| 751 | ' . implode(', |
|
| 752 | ', $insertRows), |
|
| 753 | array( |
|
| 754 | 'security_override' => true, |
|
| 755 | 'db_error_skip' => $table === $db_prefix . 'log_errors', |
|
| 756 | ), |
|
| 757 | $connection |
|
| 758 | ); |
|
| 759 | } |
|
| 760 | ||
| 761 | /** |
|
| 762 | * This function tries to work out additional error information from a back trace. |
|
| @@ 765-817 (lines=53) @@ | ||
| 762 | * @param bool $disable_trans Whether to disable transactions |
|
| 763 | * @param object $connection The connection to use (if null, $db_connection is used) |
|
| 764 | */ |
|
| 765 | function smf_db_insert($method = 'replace', $table, $columns, $data, $keys, $disable_trans = false, $connection = null) |
|
| 766 | { |
|
| 767 | global $smcFunc, $db_connection, $db_prefix; |
|
| 768 | ||
| 769 | $connection = $connection === null ? $db_connection : $connection; |
|
| 770 | ||
| 771 | // With nothing to insert, simply return. |
|
| 772 | if (empty($data)) |
|
| 773 | return; |
|
| 774 | ||
| 775 | // Replace the prefix holder with the actual prefix. |
|
| 776 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
|
| 777 | ||
| 778 | // Inserting data as a single row can be done as a single array. |
|
| 779 | if (!is_array($data[array_rand($data)])) |
|
| 780 | $data = array($data); |
|
| 781 | ||
| 782 | // Create the mold for a single row insert. |
|
| 783 | $insertData = '('; |
|
| 784 | foreach ($columns as $columnName => $type) |
|
| 785 | { |
|
| 786 | // Are we restricting the length? |
|
| 787 | if (strpos($type, 'string-') !== false) |
|
| 788 | $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 789 | else |
|
| 790 | $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 791 | } |
|
| 792 | $insertData = substr($insertData, 0, -2) . ')'; |
|
| 793 | ||
| 794 | // Create an array consisting of only the columns. |
|
| 795 | $indexed_columns = array_keys($columns); |
|
| 796 | ||
| 797 | // Here's where the variables are injected to the query. |
|
| 798 | $insertRows = array(); |
|
| 799 | foreach ($data as $dataRow) |
|
| 800 | $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 801 | ||
| 802 | // Determine the method of insertion. |
|
| 803 | $queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT'); |
|
| 804 | ||
| 805 | // Do the insert. |
|
| 806 | $smcFunc['db_query']('', ' |
|
| 807 | ' . $queryTitle . ' INTO ' . $table . '(`' . implode('`, `', $indexed_columns) . '`) |
|
| 808 | VALUES |
|
| 809 | ' . implode(', |
|
| 810 | ', $insertRows), |
|
| 811 | array( |
|
| 812 | 'security_override' => true, |
|
| 813 | 'db_error_skip' => $table === $db_prefix . 'log_errors', |
|
| 814 | ), |
|
| 815 | $connection |
|
| 816 | ); |
|
| 817 | } |
|
| 818 | ||
| 819 | /** |
|
| 820 | * This function tries to work out additional error information from a back trace. |
|