@@ -1981,117 +1981,28 @@ protected function getValues(array $values): array
1981
1981
*/
1982
1982
public function insertBatch (?array $ set = null , ?bool $ escape = null , int $ batchSize = 100 )
1983
1983
{
1984
- if ($ set === null ) {
1985
- if (empty ($ this ->QBSet )) {
1986
- if ($ this ->db ->DBDebug ) {
1987
- throw new DatabaseException ('You must use the "set" method to update an entry. ' );
1988
- }
1989
-
1990
- return false ; // @codeCoverageIgnore
1991
- }
1992
- } elseif (empty ($ set )) {
1993
- if ($ this ->db ->DBDebug ) {
1994
- throw new DatabaseException ('insertBatch() called with no data ' );
1995
- }
1996
-
1997
- return false ; // @codeCoverageIgnore
1998
- }
1999
-
2000
- $ hasQBSet = $ set === null ;
2001
-
2002
- $ table = $ this ->QBFrom [0 ];
2003
-
2004
- $ affectedRows = 0 ;
2005
- $ savedSQL = [];
2006
-
2007
- if ($ hasQBSet ) {
2008
- $ set = $ this ->QBSet ;
2009
- }
2010
-
2011
- for ($ i = 0 , $ total = count ($ set ); $ i < $ total ; $ i += $ batchSize ) {
2012
- if ($ hasQBSet ) {
2013
- $ QBSet = array_slice ($ this ->QBSet , $ i , $ batchSize );
2014
- } else {
2015
- $ this ->setInsertBatch (array_slice ($ set , $ i , $ batchSize ), '' , $ escape );
2016
- $ QBSet = $ this ->QBSet ;
2017
- }
2018
- $ sql = $ this ->_insertBatch ($ this ->db ->protectIdentifiers ($ table , true , null , false ), $ this ->QBKeys , $ QBSet );
2019
-
2020
- if ($ this ->testMode ) {
2021
- $ savedSQL [] = $ sql ;
2022
- } else {
2023
- $ this ->db ->query ($ sql , null , false );
2024
- $ affectedRows += $ this ->db ->affectedRows ();
2025
- }
2026
-
2027
- if (! $ hasQBSet ) {
2028
- $ this ->resetRun ([
2029
- 'QBSet ' => [],
2030
- 'QBKeys ' => [],
2031
- ]);
2032
- }
2033
- }
2034
-
2035
- $ this ->resetWrite ();
2036
-
2037
- return $ this ->testMode ? $ savedSQL : $ affectedRows ;
1984
+ return $ this ->batchExecute ('_insertBatch ' , $ set , $ escape , $ batchSize );
2038
1985
}
2039
1986
2040
1987
/**
2041
1988
* Generates a platform-specific insert string from the supplied data.
2042
1989
*/
2043
1990
protected function _insertBatch (string $ table , array $ keys , array $ values ): string
2044
1991
{
2045
- return 'INSERT ' . $ this ->compileIgnore ('insert ' ) . 'INTO ' . $ table . ' ( ' . implode (', ' , $ keys ) . ') VALUES ' . implode (', ' , $ values );
1992
+ return 'INSERT ' . $ this ->compileIgnore ('insert ' ) . 'INTO ' . $ table
1993
+ . ' ( ' . implode (', ' , $ keys ) . ') VALUES ' . implode (', ' , $ this ->getValues ($ values ));
2046
1994
}
2047
1995
2048
1996
/**
2049
- * Allows key/value pairs to be set for batch inserts
1997
+ * Alias for setBatch()
2050
1998
*
2051
1999
* @param mixed $key
2052
2000
*
2053
2001
* @return $this|null
2054
2002
*/
2055
2003
public function setInsertBatch ($ key , string $ value = '' , ?bool $ escape = null )
2056
2004
{
2057
- $ key = $ this ->batchObjectToArray ($ key );
2058
-
2059
- if (! is_array ($ key )) {
2060
- $ key = [$ key => $ value ];
2061
- }
2062
-
2063
- $ escape = is_bool ($ escape ) ? $ escape : $ this ->db ->protectIdentifiers ;
2064
-
2065
- $ keys = array_keys ($ this ->objectToArray (current ($ key )));
2066
- sort ($ keys );
2067
-
2068
- foreach ($ key as $ row ) {
2069
- $ row = $ this ->objectToArray ($ row );
2070
- if (array_diff ($ keys , array_keys ($ row )) !== [] || array_diff (array_keys ($ row ), $ keys ) !== []) {
2071
- // batch function above returns an error on an empty array
2072
- $ this ->QBSet [] = [];
2073
-
2074
- return null ;
2075
- }
2076
-
2077
- ksort ($ row ); // puts $row in the same order as our keys
2078
-
2079
- $ clean = [];
2080
-
2081
- foreach ($ row as $ rowValue ) {
2082
- $ clean [] = $ escape ? $ this ->db ->escape ($ rowValue ) : $ rowValue ;
2083
- }
2084
-
2085
- $ row = $ clean ;
2086
-
2087
- $ this ->QBSet [] = '( ' . implode (', ' , $ row ) . ') ' ;
2088
- }
2089
-
2090
- foreach ($ keys as $ k ) {
2091
- $ this ->QBKeys [] = $ this ->db ->protectIdentifiers ($ k , false );
2092
- }
2093
-
2094
- return $ this ;
2005
+ return $ this ->setBatch ($ key , $ value , $ escape );
2095
2006
}
2096
2007
2097
2008
/**
0 commit comments