Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
75030a1e
Commit
75030a1e
authored
Aug 04, 2015
by
rongzhj
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed _buildDuplicate() method unable to handle array arguments
parent
19abf864
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
27 deletions
+92
-27
MysqliDb.php
MysqliDb.php
+54
-26
readme.md
readme.md
+14
-0
mysqliDbTests.php
tests/mysqliDbTests.php
+24
-1
No files found.
MysqliDb.php
View file @
75030a1e
...
...
@@ -112,17 +112,17 @@ class MysqliDb
*/
protected
$isSubQuery
=
false
;
/**
* Name of the auto increment column
*
*/
protected
$lastInsertId
=
null
;
/**
* Name of the auto increment column
*
*/
protected
$
_
lastInsertId
=
null
;
/**
* Column names for update when using onDuplicate method
*
*/
protected
$updateColumns
=
null
;
/**
* Column names for update when using onDuplicate method
*
*/
protected
$
_
updateColumns
=
null
;
/**
* Return type: 'Array' to return results as array, 'Object' as object
...
...
@@ -247,6 +247,8 @@ class MysqliDb
$this
->
returnType
=
'Array'
;
$this
->
_nestJoin
=
false
;
$this
->
_tableName
=
''
;
$this
->
_lastInsertId
=
null
;
$this
->
_updateColumns
=
null
;
}
/**
...
...
@@ -562,17 +564,17 @@ class MysqliDb
return
$this
;
}
/**
/**
* This function store update column's name and column name of the
* autoincrement column
*
* @param Array Variable with values
* @param String Variable value
*/
public
function
onDuplicate
(
$updateColumns
,
$
lastInsertId
=
null
)
public
function
onDuplicate
(
$_updateColumns
,
$_
lastInsertId
=
null
)
{
$this
->
lastInsertId
=
$
lastInsertId
;
$this
->
updateColumns
=
$
updateColumns
;
$this
->
_lastInsertId
=
$_
lastInsertId
;
$this
->
_updateColumns
=
$_
updateColumns
;
}
/**
...
...
@@ -804,28 +806,54 @@ class MysqliDb
return
true
;
}
/**
/**
* Helper function to add variables into the query statement
*
* @param Array Variable with values
*/
protected
function
_buildDuplicate
(
$tableData
)
{
if
(
is_array
(
$this
->
updateColumns
)
&&
!
empty
(
$this
->
updateColumns
))
{
if
(
is_array
(
$this
->
_updateColumns
)
&&
!
empty
(
$this
->
_
updateColumns
))
{
$this
->
_query
.=
" on duplicate key update "
;
if
(
$this
->
lastInsertId
)
{
$this
->
_lastQuery
.=
$this
->
lastInsertId
.
"=LAST_INSERT_ID("
.
$this
->
lastInsertId
.
"),"
;
$this
->
lastInsertId
=
null
;
if
(
$this
->
_
lastInsertId
)
{
$this
->
_lastQuery
.=
$this
->
_lastInsertId
.
"=LAST_INSERT_ID("
.
$this
->
_
lastInsertId
.
"),"
;
$this
->
_
lastInsertId
=
null
;
}
foreach
(
$this
->
updateColumns
as
$value
)
{
$this
->
_bindParam
(
$tableData
[
$value
]);
$this
->
_query
.=
"`"
.
$value
.
"` = ?, "
;
foreach
(
$this
->
_updateColumns
as
$column
)
{
$this
->
_query
.=
"`"
.
$column
.
"` = "
;
// Simple value
if
(
!
is_array
(
$tableData
[
$column
]))
{
$this
->
_bindParam
(
$tableData
[
$column
]);
$this
->
_query
.=
'?, '
;
continue
;
}
// Function value
$arr
=
$tableData
[
$column
];
$key
=
key
(
$arr
);
$val
=
$arr
[
$key
];
switch
(
$key
)
{
case
'[I]'
:
$this
->
_query
.=
$column
.
$val
.
", "
;
break
;
case
'[F]'
:
$this
->
_query
.=
$val
[
0
]
.
", "
;
if
(
!
empty
(
$val
[
1
]))
$this
->
_bindParams
(
$val
[
1
]);
break
;
case
'[N]'
:
if
(
$val
==
null
)
$this
->
_query
.=
"!"
.
$column
.
", "
;
else
$this
->
_query
.=
"!"
.
$val
.
", "
;
break
;
default
:
die
(
"Wrong operation"
);
}
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
lastInsertId
=
null
;
$this
->
updateColumns
=
null
;
}
}
...
...
readme.md
View file @
75030a1e
...
...
@@ -123,6 +123,20 @@ else
echo
'insert failed: '
.
$db
->
getLastError
();
```
Insert with on duplicate key update
```
php
$data
=
Array
(
"login"
=>
"admin"
,
"firstName"
=>
"John"
,
"lastName"
=>
'Doe'
,
"createdAt"
=>
$db
->
now
(),
"updatedAt"
=>
$db
->
now
(),
);
$updateColumns
=
Array
(
"updateAt"
);
$lastInsertId
=
"id"
;
$db
->
onDuplicate
(
$updateColumns
,
$lastInsertId
);
$id
=
$db
->
insert
(
'users'
,
$data
);
```
### Replace Query
<a
href=
'https://dev.mysql.com/doc/refman/5.0/en/replace.html'
>
Replace()
</a>
method implements same API as insert();
...
...
tests/mysqliDbTests.php
View file @
75030a1e
...
...
@@ -29,8 +29,10 @@ $tables = Array (
'lastName'
=>
'char(10)'
,
'password'
=>
'text not null'
,
'createdAt'
=>
'datetime'
,
'updatedAt'
=>
'datetime'
,
'expires'
=>
'datetime'
,
'loginCount'
=>
'int(10) default 0'
'loginCount'
=>
'int(10) default 0'
,
'unique key'
=>
'login (login)'
),
'products'
=>
Array
(
'customerId'
=>
'int(10) not null'
,
...
...
@@ -46,6 +48,7 @@ $data = Array (
'lastName'
=>
'Doe'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'createdAt'
=>
$db
->
now
(),
'updatedAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
()
),
...
...
@@ -55,6 +58,7 @@ $data = Array (
'lastName'
=>
NULL
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'updatedAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
2
)
),
...
...
@@ -65,6 +69,7 @@ $data = Array (
'lastName'
=>
'D'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'updatedAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
3
)
)
...
...
@@ -136,6 +141,7 @@ $badUser = Array ('login' => null,
'lastName'
=>
'Doe'
,
'password'
=>
'test'
,
'createdAt'
=>
$db
->
now
(),
'updatedAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
()
);
...
...
@@ -170,6 +176,23 @@ if ($db->count != 3) {
exit
;
}
// insert with on duplicate key update
$user
=
Array
(
'login'
=>
'user3'
,
'active'
=>
true
,
'customerId'
=>
11
,
'firstName'
=>
'Pete'
,
'lastName'
=>
'D'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'updatedAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
3
)
);
$updateColumns
=
Array
(
"updatedAt"
);
$insertLastId
=
"id"
;
$db
->
onDuplicate
(
$updateColumns
,
"id"
);
$db
->
insert
(
"users"
,
$user
);
// order by field
$db
->
orderBy
(
"login"
,
"asc"
,
Array
(
"user3"
,
"user2"
,
"user1"
));
$login
=
$db
->
getValue
(
"users"
,
"login"
);
...
...
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