Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
die_coolen_jungs
our_own_cloud_project
Commits
8fa69238
Commit
8fa69238
authored
Mar 09, 2015
by
Joas Schilling
Committed by
Thomas Müller
Mar 09, 2015
Browse files
Allow specifying the compare-array for insertIfNotExists()
parent
94b7fa17
Changes
8
Hide whitespace changes
Inline
Side-by-side
lib/private/allconfig.php
View file @
8fa69238
...
...
@@ -189,11 +189,18 @@ class AllConfig implements \OCP\IConfig {
return
;
}
$
data
=
array
(
$value
,
$userId
,
$appName
,
$key
)
;
$
affectedRows
=
0
;
if
(
!
$exists
&&
$preCondition
===
null
)
{
$sql
=
'INSERT INTO `*PREFIX*preferences` (`configvalue`, `userid`, `appid`, `configkey`)'
.
'VALUES (?, ?, ?, ?)'
;
$this
->
connection
->
insertIfNotExist
(
'*PREFIX*preferences'
,
[
'configvalue'
=>
$value
,
'userid'
=>
$userId
,
'appid'
=>
$appName
,
'configkey'
=>
$key
,
],
[
'configvalue'
,
'userid'
,
'appid'
]);
$affectedRows
=
1
;
}
elseif
(
$exists
)
{
$data
=
array
(
$value
,
$userId
,
$appName
,
$key
);
$sql
=
'UPDATE `*PREFIX*preferences` SET `configvalue` = ? '
.
'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? '
;
...
...
@@ -206,8 +213,8 @@ class AllConfig implements \OCP\IConfig {
}
$data
[]
=
$preCondition
;
}
$affectedRows
=
$this
->
connection
->
executeUpdate
(
$sql
,
$data
);
}
$affectedRows
=
$this
->
connection
->
executeUpdate
(
$sql
,
$data
);
// only add to the cache if we already loaded data for the user
if
(
$affectedRows
>
0
&&
isset
(
$this
->
userCache
[
$userId
]))
{
...
...
lib/private/appframework/db/db.php
View file @
8fa69238
...
...
@@ -138,8 +138,8 @@ class Db implements IDb {
* @return bool
*
*/
public
function
insertIfNotExist
(
$table
,
$input
)
{
return
$this
->
connection
->
insertIfNotExist
(
$table
,
$input
);
public
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
return
$this
->
connection
->
insertIfNotExist
(
$table
,
$input
,
$compare
);
}
/**
...
...
lib/private/db.php
View file @
8fa69238
...
...
@@ -172,8 +172,8 @@ class OC_DB {
* @param array $input An array of fieldname/value pairs
* @return boolean number of updated rows
*/
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
return
\
OC
::
$server
->
getDatabaseConnection
()
->
insertIfNotExist
(
$table
,
$input
);
public
static
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
return
\
OC
::
$server
->
getDatabaseConnection
()
->
insertIfNotExist
(
$table
,
$input
,
$compare
);
}
/**
...
...
lib/private/db/adapter.php
View file @
8fa69238
...
...
@@ -46,19 +46,22 @@ class Adapter {
* @throws \OC\HintException
* @return int count of inserted rows
*/
public
function
insertIfNotExist
(
$table
,
$input
)
{
public
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
if
(
$compare
===
null
)
{
$compare
=
array_keys
(
$input
);
}
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) SELECT '
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
// Is there a prettier alternative?
.
'FROM `'
.
$table
.
'` WHERE '
;
$inserts
=
array_values
(
$input
);
foreach
(
$
input
as
$key
=>
$value
)
{
foreach
(
$
compare
as
$key
)
{
$query
.
=
'`'
.
$key
.
'`'
;
if
(
is_null
(
$
value
))
{
if
(
is_null
(
$
input
[
$key
]
))
{
$query
.
=
' IS NULL AND '
;
}
else
{
$inserts
[]
=
$
value
;
$inserts
[]
=
$
input
[
$key
]
;
$query
.
=
' = ? AND '
;
}
}
...
...
lib/private/db/adaptersqlite.php
View file @
8fa69238
...
...
@@ -18,19 +18,22 @@ class AdapterSqlite extends Adapter {
return
$statement
;
}
public
function
insertIfNotExist
(
$table
,
$input
)
{
public
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
if
(
$compare
===
null
)
{
$compare
=
array_keys
(
$input
);
}
$fieldList
=
'`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`'
;
$query
=
"INSERT INTO `
$table
` (
$fieldList
) SELECT "
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
.
" WHERE NOT EXISTS (SELECT 1 FROM `
$table
` WHERE "
;
$inserts
=
array_values
(
$input
);
foreach
(
$
input
as
$key
=>
$value
)
{
foreach
(
$
compare
as
$key
)
{
$query
.
=
'`'
.
$key
.
'`'
;
if
(
is_null
(
$
value
))
{
if
(
is_null
(
$
input
[
$key
]
))
{
$query
.
=
' IS NULL AND '
;
}
else
{
$inserts
[]
=
$
value
;
$inserts
[]
=
$
input
[
$key
]
;
$query
.
=
' = ? AND '
;
}
}
...
...
lib/private/db/connection.php
View file @
8fa69238
...
...
@@ -164,8 +164,8 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
* @throws \OC\HintException
* @return bool The return value from execute()
*/
public
function
insertIfNotExist
(
$table
,
$input
)
{
return
$this
->
adapter
->
insertIfNotExist
(
$table
,
$input
);
public
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
return
$this
->
adapter
->
insertIfNotExist
(
$table
,
$input
,
$compare
);
}
/**
...
...
lib/public/db.php
View file @
8fa69238
...
...
@@ -64,8 +64,8 @@ class DB {
* @return bool
*
*/
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
return
(
\
OC_DB
::
insertIfNotExist
(
$table
,
$input
));
public
static
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
)
{
return
(
\
OC_DB
::
insertIfNotExist
(
$table
,
$input
,
$compare
));
}
/**
...
...
lib/public/idbconnection.php
View file @
8fa69238
...
...
@@ -94,7 +94,7 @@ interface IDBConnection {
* @return bool
*
*/
public
function
insertIfNotExist
(
$table
,
$input
);
public
function
insertIfNotExist
(
$table
,
$input
,
$compare
=
null
);
/**
* Start a transaction
...
...
Write
Preview
Supports
Markdown
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