Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sarun Mungthanya
SpnPatch-Laravel
Commits
9885e03c
Commit
9885e03c
authored
Dec 06, 2024
by
Sarun Mungthanya
Browse files
issue data value twice
parent
a2dc6ebe
Changes
1
Show whitespace changes
Inline
Side-by-side
app/Http/Controllers/SmartUpdateController.php
View file @
9885e03c
...
@@ -574,12 +574,10 @@ class SmartUpdateController extends Controller
...
@@ -574,12 +574,10 @@ class SmartUpdateController extends Controller
public
static
function
generateInsertSQLWithCount
(
$sourceTable
,
$destinationTable
,
$conditionColumn
,
$conditionValue
,
$additionalColumns
)
public
static
function
generateInsertSQLWithCount
(
$sourceTable
,
$destinationTable
,
$conditionColumn
,
$conditionValue
,
$additionalColumns
)
{
{
// Fetch data from the source table based on the condition
$rows
=
DB
::
table
(
$sourceTable
)
$rows
=
DB
::
table
(
$sourceTable
)
->
where
(
$conditionColumn
,
'like'
,
$conditionValue
)
->
where
(
$conditionColumn
,
'like'
,
$conditionValue
)
->
get
();
->
get
();
// Check if rows exist
if
(
$rows
->
isEmpty
())
{
if
(
$rows
->
isEmpty
())
{
return
[
return
[
'total'
=>
0
,
'total'
=>
0
,
...
@@ -587,7 +585,6 @@ class SmartUpdateController extends Controller
...
@@ -587,7 +585,6 @@ class SmartUpdateController extends Controller
];
];
}
}
// Detect the database type
$dbType
=
config
(
'database.default'
);
$dbType
=
config
(
'database.default'
);
$quoteL
=
$dbType
===
'sqlsrv'
?
'['
:
'`'
;
$quoteL
=
$dbType
===
'sqlsrv'
?
'['
:
'`'
;
$quoteR
=
$dbType
===
'sqlsrv'
?
']'
:
'`'
;
$quoteR
=
$dbType
===
'sqlsrv'
?
']'
:
'`'
;
...
@@ -597,12 +594,9 @@ class SmartUpdateController extends Controller
...
@@ -597,12 +594,9 @@ class SmartUpdateController extends Controller
foreach
(
$rows
as
$row
)
{
foreach
(
$rows
as
$row
)
{
$values
=
[];
$values
=
[];
// Include all columns from the row dynamically
foreach
((
array
)
$row
as
$column
=>
$value
)
{
foreach
((
array
)
$row
as
$column
=>
$value
)
{
// Escape single quotes for MSSQL and MySQL
$escapedValue
=
str_replace
(
"'"
,
"''"
,
$value
);
$escapedValue
=
str_replace
(
"'"
,
"''"
,
$value
);
// Handle special MSSQL cases, e.g., default datetime
if
(
$escapedValue
===
'0000-00-00 00:00:00'
)
{
if
(
$escapedValue
===
'0000-00-00 00:00:00'
)
{
$escapedValue
=
'2000-01-01 00:00:00'
;
$escapedValue
=
'2000-01-01 00:00:00'
;
}
}
...
@@ -610,21 +604,12 @@ class SmartUpdateController extends Controller
...
@@ -610,21 +604,12 @@ class SmartUpdateController extends Controller
$values
[]
=
"'
$escapedValue
'"
;
$values
[]
=
"'
$escapedValue
'"
;
}
}
// Append additional columns (e.g., `amenddate`) dynamically
foreach
(
$additionalColumns
as
$key
=>
$value
)
{
$escapedValue
=
str_replace
(
"'"
,
"''"
,
$value
);
$values
[]
=
"'
$escapedValue
'"
;
}
// Create the INSERT statement
$columns
=
array_keys
((
array
)
$row
);
$columns
=
array_keys
((
array
)
$row
);
// $columns = array_merge($columns, array_keys($additionalColumns));
$quotedColumns
=
array_map
(
fn
(
$col
)
=>
$quoteL
.
$col
.
$quoteR
,
$columns
);
$quotedColumns
=
array_map
(
fn
(
$col
)
=>
$quoteL
.
$col
.
$quoteR
,
$columns
);
$insertStatements
[]
=
"INSERT INTO
$quoteL$destinationTable$quoteR
("
.
implode
(
', '
,
$quotedColumns
)
.
") VALUES ("
.
implode
(
', '
,
$values
)
.
");"
;
$insertStatements
[]
=
"INSERT INTO
$quoteL$destinationTable$quoteR
("
.
implode
(
', '
,
$quotedColumns
)
.
") VALUES ("
.
implode
(
', '
,
$values
)
.
");"
;
}
}
// Combine all statements into a single string
return
[
return
[
'total'
=>
$rows
->
count
(),
'total'
=>
$rows
->
count
(),
'sql'
=>
implode
(
"
\n
"
,
$insertStatements
)
'sql'
=>
implode
(
"
\n
"
,
$insertStatements
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment