Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
d809efc1
Commit
d809efc1
authored
12 years ago
by
Thomas Tanghus
Browse files
Options
Downloads
Patches
Plain Diff
Change insertIfNotExist() for sqlite. Not fast, but more reliable than previous attempt.
parent
42b871dc
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/db.php
+36
-12
36 additions, 12 deletions
lib/db.php
with
36 additions
and
12 deletions
lib/db.php
+
36
−
12
View file @
d809efc1
...
...
@@ -543,8 +543,9 @@ class OC_DB {
/**
* @brief Insert a row if a matching row doesn't exists.
* @returns true/false
*
* @param string $table. The table to insert into in the form '*PREFIX*tableName'
* @param array $input. An array of fieldname/value pairs
* @returns The return value from PDOStatementWrapper->execute()
*/
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
self
::
connect
();
...
...
@@ -559,9 +560,31 @@ class OC_DB {
$query
=
''
;
// differences in escaping of table names ('`' for mysql) and getting the current timestamp
if
(
$type
==
'sqlite'
||
$type
==
'sqlite3'
)
{
$query
=
'INSERT OR REPLACE INTO "'
.
$table
.
'" ("'
// NOTE: For SQLite we have to use this clumsy approach
// otherwise all fieldnames used must have a unique key.
$query
=
'SELECT * FROM "'
.
$table
.
'" WHERE '
;
foreach
(
$input
as
$key
=>
$value
)
{
$query
.
=
$key
.
" = '"
.
$value
.
'\' AND '
;
}
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
try
{
$stmt
=
self
::
prepare
(
$query
);
$result
=
$stmt
->
execute
();
}
catch
(
PDOException
$e
)
{
$entry
=
'DB Error: "'
.
$e
->
getMessage
()
.
'"<br />'
;
$entry
.
=
'Offending command was: '
.
$query
.
'<br />'
;
OC_Log
::
write
(
'core'
,
$entry
,
OC_Log
::
FATAL
);
error_log
(
'DB error: '
.
$entry
);
die
(
$entry
);
}
if
(
$result
->
numRows
()
==
0
)
{
$query
=
'INSERT INTO "'
.
$table
.
'" ("'
.
implode
(
'","'
,
array_keys
(
$input
))
.
'") VALUES("'
.
implode
(
'","'
,
array_values
(
$input
))
.
'")'
;
}
else
{
return
true
;
}
}
elseif
(
$type
==
'pgsql'
||
$type
==
'oci'
||
$type
==
'mysql'
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` ('
.
implode
(
','
,
array_keys
(
$input
))
.
') SELECT \''
...
...
@@ -573,6 +596,7 @@ class OC_DB {
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
$query
.
=
' HAVING COUNT(*) = 0'
;
}
// TODO: oci should be use " (quote) instead of ` (backtick).
//OC_Log::write('core', __METHOD__ . ', type: ' . $type . ', query: ' . $query, OC_Log::DEBUG);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment