Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
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
19b9c896
Commit
19b9c896
authored
Feb 26, 2013
by
Bart Visscher
Browse files
Move insertIfNotExist to Connection wrapper
Real implementation is in DB\Adapter* classes
parent
58991150
Changes
4
Hide whitespace changes
Inline
Side-by-side
lib/db.php
View file @
19b9c896
...
...
@@ -389,58 +389,7 @@ class OC_DB {
*/
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
self
::
connect
();
$table
=
self
::
$connection
->
replaceTablePrefix
(
$table
);
if
(
is_null
(
self
::
$type
))
{
self
::
$type
=
OC_Config
::
getValue
(
"dbtype"
,
"sqlite"
);
}
$type
=
self
::
$type
;
$query
=
''
;
$inserts
=
array_values
(
$input
);
// differences in escaping of table names ('`' for mysql) and getting the current timestamp
if
(
$type
==
'sqlite'
||
$type
==
'sqlite3'
)
{
// 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
.
'` = ? AND '
;
}
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
try
{
$result
=
self
::
executeAudited
(
$query
,
$inserts
);
}
catch
(
DatabaseException
$e
)
{
OC_Template
::
printExceptionErrorPage
(
$e
);
}
if
((
int
)
$result
->
numRows
()
===
0
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) VALUES('
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
.
')'
;
}
else
{
return
0
;
//no rows updated
}
}
elseif
(
$type
==
'pgsql'
||
$type
==
'oci'
||
$type
==
'mysql'
||
$type
==
'mssql'
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) SELECT '
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
// Is there a prettier alternative?
.
'FROM `'
.
$table
.
'` WHERE '
;
foreach
(
$input
as
$key
=>
$value
)
{
$query
.
=
'`'
.
$key
.
'` = ? AND '
;
}
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
$query
.
=
' HAVING COUNT(*) = 0'
;
$inserts
=
array_merge
(
$inserts
,
$inserts
);
}
try
{
$result
=
self
::
executeAudited
(
$query
,
$inserts
);
}
catch
(
\
Doctrine\DBAL\DBALException
$e
)
{
OC_Template
::
printExceptionErrorPage
(
$e
);
}
return
$result
;
return
self
::
$connection
->
insertIfNotExist
(
$table
,
$input
);
}
/**
...
...
lib/db/adapter.php
View file @
19b9c896
...
...
@@ -22,4 +22,32 @@ class Adapter {
public
function
fixupStatement
(
$statement
)
{
return
$statement
;
}
public
function
insertIfNotExist
(
$table
,
$input
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) SELECT '
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
// Is there a prettier alternative?
.
'FROM `'
.
$table
.
'` WHERE '
;
foreach
(
$input
as
$key
=>
$value
)
{
$query
.
=
'`'
.
$key
.
'` = ? AND '
;
}
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
$query
.
=
' HAVING COUNT(*) = 0'
;
$inserts
=
array_values
(
$input
);
$inserts
=
array_merge
(
$inserts
,
$inserts
);
try
{
$statement
=
$this
->
conn
->
prepare
(
$query
);
$result
=
$statement
->
execute
(
$inserts
);
}
catch
(
\
Doctrine\DBAL\DBALException
$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
);
OC_Template
::
printErrorPage
(
$entry
);
}
return
$result
;
}
}
lib/db/adaptersqlite.php
View file @
19b9c896
...
...
@@ -16,4 +16,45 @@ class AdapterSqlite extends Adapter {
$statement
=
str_ireplace
(
'UNIX_TIMESTAMP()'
,
'strftime(\'%s\',\'now\')'
,
$statement
);
return
$statement
;
}
public
function
insertIfNotExist
(
$table
,
$input
)
{
// NOTE: For SQLite we have to use this clumsy approach
// otherwise all fieldnames used must have a unique key.
$query
=
'SELECT COUNT(*) FROM `'
.
$table
.
'` WHERE '
;
foreach
(
$input
as
$key
=>
$value
)
{
$query
.
=
'`'
.
$key
.
'` = ? AND '
;
}
$query
=
substr
(
$query
,
0
,
strlen
(
$query
)
-
5
);
try
{
$stmt
=
$this
->
conn
->
prepare
(
$query
);
$result
=
$stmt
->
execute
(
array
(
$input
));
}
catch
(
\
Doctrine\DBAL\DBALException
$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
);
OC_Template
::
printErrorPage
(
$entry
);
}
if
(
$stmt
->
fetchColumn
()
===
0
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) VALUES('
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
.
')'
;
}
else
{
return
0
;
//no rows updated
}
try
{
$statement
=
$this
->
conn
->
prepare
(
$query
);
$result
=
$statement
->
execute
(
array_values
(
$input
));
}
catch
(
\
Doctrine\DBAL\DBALException
$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
);
OC_Template
::
printErrorPage
(
$entry
);
}
return
$result
;
}
}
lib/db/connection.php
View file @
19b9c896
...
...
@@ -138,8 +138,18 @@ class Connection extends \Doctrine\DBAL\Connection {
return
parent
::
lastInsertId
(
$seqName
);
}
/**
* @brief Insert a row if a matching row doesn't exists.
* @param string $table. The table to insert into in the form '*PREFIX*tableName'
* @param array $input. An array of fieldname/value pairs
* @returns bool The return value from execute()
*/
public
function
insertIfNotExist
(
$table
,
$input
)
{
return
$this
->
adapter
->
insertIfNotExist
(
$table
,
$input
);
}
// internal use
p
ublic
function
replaceTablePrefix
(
$statement
)
{
p
rotected
function
replaceTablePrefix
(
$statement
)
{
return
str_replace
(
'*PREFIX*'
,
$this
->
table_prefix
,
$statement
);
}
...
...
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