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
00987fed
Commit
00987fed
authored
Jul 5, 2013
by
Jörn Friedrich Dreyer
Browse files
Options
Downloads
Patches
Plain Diff
fix insertIfNotExist return value, update doc and corresponding test
parent
f3c4a37a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/db.php
+2
-5
2 additions, 5 deletions
lib/db.php
tests/lib/db.php
+14
-14
14 additions, 14 deletions
tests/lib/db.php
with
16 additions
and
19 deletions
lib/db.php
+
2
−
5
View file @
00987fed
...
@@ -702,7 +702,7 @@ class OC_DB {
...
@@ -702,7 +702,7 @@ class OC_DB {
* @brief Insert a row if a matching row doesn't exists.
* @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 string $table. The table to insert into in the form '*PREFIX*tableName'
* @param array $input. An array of fieldname/value pairs
* @param array $input. An array of fieldname/value pairs
* @returns
The return value from PDOStatementWrapper->execute()
* @returns
int number of updated rows
*/
*/
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
public
static
function
insertIfNotExist
(
$table
,
$input
)
{
self
::
connect
();
self
::
connect
();
...
@@ -736,7 +736,7 @@ class OC_DB {
...
@@ -736,7 +736,7 @@ class OC_DB {
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) VALUES('
.
implode
(
'`,`'
,
array_keys
(
$input
))
.
'`) VALUES('
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
.
')'
;
.
str_repeat
(
'?,'
,
count
(
$input
)
-
1
)
.
'? '
.
')'
;
}
else
{
}
else
{
return
true
;
return
0
;
//no rows updated
}
}
}
elseif
(
$type
==
'pgsql'
||
$type
==
'oci'
||
$type
==
'mysql'
||
$type
==
'mssql'
)
{
}
elseif
(
$type
==
'pgsql'
||
$type
==
'oci'
||
$type
==
'mysql'
||
$type
==
'mssql'
)
{
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
$query
=
'INSERT INTO `'
.
$table
.
'` (`'
...
@@ -757,9 +757,6 @@ class OC_DB {
...
@@ -757,9 +757,6 @@ class OC_DB {
}
catch
(
PDOException
$e
)
{
}
catch
(
PDOException
$e
)
{
OC_Template
::
printExceptionErrorPage
(
$e
);
OC_Template
::
printExceptionErrorPage
(
$e
);
}
}
if
(
$result
===
0
)
{
return
true
;
}
return
$result
;
return
$result
;
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/lib/db.php
+
14
−
14
View file @
00987fed
...
@@ -40,7 +40,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -40,7 +40,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
$this
->
assertFalse
((
bool
)
$row
);
//PDO returns false, MDB2 returns null
$this
->
assertFalse
((
bool
)
$row
);
//PDO returns false, MDB2 returns null
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (?,?)'
);
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (?,?)'
);
$result
=
$query
->
execute
(
array
(
'fullname test'
,
'uri_1'
));
$result
=
$query
->
execute
(
array
(
'fullname test'
,
'uri_1'
));
$this
->
assertEquals
(
'1'
,
$result
);
$this
->
assertEquals
(
1
,
$result
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$result
=
$query
->
execute
(
array
(
'uri_1'
));
$result
=
$query
->
execute
(
array
(
'uri_1'
));
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertTrue
((
bool
)
$result
);
...
@@ -57,7 +57,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -57,7 +57,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
public
function
testNOW
()
{
public
function
testNOW
()
{
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (NOW(),?)'
);
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (NOW(),?)'
);
$result
=
$query
->
execute
(
array
(
'uri_2'
));
$result
=
$query
->
execute
(
array
(
'uri_2'
));
$this
->
assertEquals
(
'1'
,
$result
);
$this
->
assertEquals
(
1
,
$result
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$result
=
$query
->
execute
(
array
(
'uri_2'
));
$result
=
$query
->
execute
(
array
(
'uri_2'
));
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertTrue
((
bool
)
$result
);
...
@@ -66,7 +66,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -66,7 +66,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
public
function
testUNIX_TIMESTAMP
()
{
public
function
testUNIX_TIMESTAMP
()
{
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)'
);
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)'
);
$result
=
$query
->
execute
(
array
(
'uri_3'
));
$result
=
$query
->
execute
(
array
(
'uri_3'
));
$this
->
assertEquals
(
'1'
,
$result
);
$this
->
assertEquals
(
1
,
$result
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`,`uri` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$result
=
$query
->
execute
(
array
(
'uri_3'
));
$result
=
$query
->
execute
(
array
(
'uri_3'
));
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertTrue
((
bool
)
$result
);
...
@@ -74,11 +74,11 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -74,11 +74,11 @@ class Test_DB extends PHPUnit_Framework_TestCase {
public
function
testinsertIfNotExist
()
{
public
function
testinsertIfNotExist
()
{
$categoryentries
=
array
(
$categoryentries
=
array
(
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Family'
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Family'
,
'expectedResult'
=>
1
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Friends'
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Friends'
,
'expectedResult'
=>
1
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Coworkers'
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Coworkers'
,
'expectedResult'
=>
1
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Coworkers'
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'Coworkers'
,
'expectedResult'
=>
0
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'School'
),
array
(
'user'
=>
'test'
,
'type'
=>
'contact'
,
'category'
=>
'School'
,
'expectedResult'
=>
1
),
);
);
foreach
(
$categoryentries
as
$entry
)
{
foreach
(
$categoryentries
as
$entry
)
{
...
@@ -88,13 +88,13 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -88,13 +88,13 @@ class Test_DB extends PHPUnit_Framework_TestCase {
'type'
=>
$entry
[
'type'
],
'type'
=>
$entry
[
'type'
],
'category'
=>
$entry
[
'category'
],
'category'
=>
$entry
[
'category'
],
));
));
$this
->
assert
True
((
bool
)
$result
);
$this
->
assert
Equals
(
$entry
[
'expectedResult'
],
$result
);
}
}
$query
=
OC_DB
::
prepare
(
'SELECT * FROM `*PREFIX*'
.
$this
->
table3
.
'`'
);
$query
=
OC_DB
::
prepare
(
'SELECT * FROM `*PREFIX*'
.
$this
->
table3
.
'`'
);
$result
=
$query
->
execute
();
$result
=
$query
->
execute
();
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertEquals
(
'4'
,
$result
->
numRows
());
$this
->
assertEquals
(
4
,
$result
->
numRows
());
}
}
public
function
testinsertIfNotExistDontOverwrite
()
{
public
function
testinsertIfNotExistDontOverwrite
()
{
...
@@ -105,14 +105,14 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -105,14 +105,14 @@ class Test_DB extends PHPUnit_Framework_TestCase {
// Normal test to have same known data inserted.
// Normal test to have same known data inserted.
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)'
);
$query
=
OC_DB
::
prepare
(
'INSERT INTO `*PREFIX*'
.
$this
->
table2
.
'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)'
);
$result
=
$query
->
execute
(
array
(
$fullname
,
$uri
,
$carddata
));
$result
=
$query
->
execute
(
array
(
$fullname
,
$uri
,
$carddata
));
$this
->
assertEquals
(
'1'
,
$result
);
$this
->
assertEquals
(
1
,
$result
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$result
=
$query
->
execute
(
array
(
$uri
));
$result
=
$query
->
execute
(
array
(
$uri
));
$this
->
assertTrue
((
bool
)
$result
);
$this
->
assertTrue
((
bool
)
$result
);
$row
=
$result
->
fetchRow
();
$row
=
$result
->
fetchRow
();
$this
->
assertArrayHasKey
(
'carddata'
,
$row
);
$this
->
assertArrayHasKey
(
'carddata'
,
$row
);
$this
->
assertEquals
(
$carddata
,
$row
[
'carddata'
]);
$this
->
assertEquals
(
$carddata
,
$row
[
'carddata'
]);
$this
->
assertEquals
(
'1'
,
$result
->
numRows
());
$this
->
assertEquals
(
1
,
$result
->
numRows
());
// Try to insert a new row
// Try to insert a new row
$result
=
OC_DB
::
insertIfNotExist
(
'*PREFIX*'
.
$this
->
table2
,
$result
=
OC_DB
::
insertIfNotExist
(
'*PREFIX*'
.
$this
->
table2
,
...
@@ -120,7 +120,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -120,7 +120,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
'fullname'
=>
$fullname
,
'fullname'
=>
$fullname
,
'uri'
=>
$uri
,
'uri'
=>
$uri
,
));
));
$this
->
assert
True
((
bool
)
$result
);
$this
->
assert
Equals
(
0
,
$result
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$query
=
OC_DB
::
prepare
(
'SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'
.
$this
->
table2
.
'` WHERE `uri` = ?'
);
$result
=
$query
->
execute
(
array
(
$uri
));
$result
=
$query
->
execute
(
array
(
$uri
));
...
@@ -130,7 +130,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
...
@@ -130,7 +130,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
// Test that previously inserted data isn't overwritten
// Test that previously inserted data isn't overwritten
$this
->
assertEquals
(
$carddata
,
$row
[
'carddata'
]);
$this
->
assertEquals
(
$carddata
,
$row
[
'carddata'
]);
// And that a new row hasn't been inserted.
// And that a new row hasn't been inserted.
$this
->
assertEquals
(
'1'
,
$result
->
numRows
());
$this
->
assertEquals
(
1
,
$result
->
numRows
());
}
}
}
}
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