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
16d88ae2
Commit
16d88ae2
authored
Aug 04, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #284 from avbdr/master
fixes
parents
20db85d5
bd30f30d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
111 deletions
+93
-111
MysqliDb.php
MysqliDb.php
+81
-109
mysqliDbTests.php
tests/mysqliDbTests.php
+12
-2
No files found.
MysqliDb.php
View file @
16d88ae2
...
@@ -805,57 +805,6 @@ class MysqliDb
...
@@ -805,57 +805,6 @@ class MysqliDb
return
true
;
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
))
{
$this
->
_query
.=
" on duplicate key update "
;
if
(
$this
->
_lastInsertId
)
{
$this
->
_lastQuery
.=
$this
->
_lastInsertId
.
"=LAST_INSERT_ID("
.
$this
->
_lastInsertId
.
"),"
;
$this
->
_lastInsertId
=
null
;
}
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
,
', '
);
}
}
/**
/**
* Abstraction method that will compile the WHERE statement,
* Abstraction method that will compile the WHERE statement,
* any passed update data, and the desired rows.
* any passed update data, and the desired rows.
...
@@ -870,12 +819,12 @@ class MysqliDb
...
@@ -870,12 +819,12 @@ class MysqliDb
protected
function
_buildQuery
(
$numRows
=
null
,
$tableData
=
null
)
protected
function
_buildQuery
(
$numRows
=
null
,
$tableData
=
null
)
{
{
$this
->
_buildJoin
();
$this
->
_buildJoin
();
$this
->
_build
TableData
(
$tableData
);
$this
->
_build
InsertQuery
(
$tableData
);
$this
->
_buildWhere
();
$this
->
_buildWhere
();
$this
->
_buildGroupBy
();
$this
->
_buildGroupBy
();
$this
->
_buildOrderBy
();
$this
->
_buildOrderBy
();
$this
->
_buildLimit
(
$numRows
);
$this
->
_buildLimit
(
$numRows
);
$this
->
_buildDuplicate
(
$tableData
);
$this
->
_build
On
Duplicate
(
$tableData
);
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
...
@@ -996,20 +945,9 @@ class MysqliDb
...
@@ -996,20 +945,9 @@ class MysqliDb
}
}
}
}
/**
public
function
_buildDataPairs
(
$tableData
,
$tableColumns
,
$isInsert
)
{
* Abstraction method that will build an INSERT or UPDATE part of the query
foreach
(
$tableColumns
as
$column
)
{
*/
$value
=
$tableData
[
$column
];
protected
function
_buildTableData
(
$tableData
)
{
if
(
!
is_array
(
$tableData
))
return
;
$isInsert
=
preg_match
(
'/^[INSERT|REPLACE]/'
,
$this
->
_query
);
if
(
$isInsert
)
$this
->
_query
.=
' (`'
.
implode
(
array_keys
(
$tableData
),
'`, `'
)
.
'`) VALUES ('
;
else
$this
->
_query
.=
" SET "
;
foreach
(
$tableData
as
$column
=>
$value
)
{
if
(
!
$isInsert
)
if
(
!
$isInsert
)
$this
->
_query
.=
"`"
.
$column
.
"` = "
;
$this
->
_query
.=
"`"
.
$column
.
"` = "
;
...
@@ -1021,7 +959,7 @@ class MysqliDb
...
@@ -1021,7 +959,7 @@ class MysqliDb
// Simple value
// Simple value
if
(
!
is_array
(
$value
))
{
if
(
!
is_array
(
$value
))
{
$this
->
_bindParam
(
$value
);
$this
->
_bindParam
(
$value
);
$this
->
_query
.=
'?, '
;
$this
->
_query
.=
'?, '
;
continue
;
continue
;
}
}
...
@@ -1048,7 +986,41 @@ class MysqliDb
...
@@ -1048,7 +986,41 @@ class MysqliDb
die
(
"Wrong operation"
);
die
(
"Wrong operation"
);
}
}
}
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
}
/**
* Helper function to add variables into the query statement
*
* @param Array Variable with values
*/
protected
function
_buildOnDuplicate
(
$tableData
)
{
if
(
is_array
(
$this
->
_updateColumns
)
&&
!
empty
(
$this
->
_updateColumns
))
{
$this
->
_query
.=
" on duplicate key update "
;
if
(
$this
->
_lastInsertId
)
$this
->
_query
.=
$this
->
_lastInsertId
.
"=LAST_INSERT_ID ("
.
$this
->
_lastInsertId
.
"), "
;
$this
->
_buildDataPairs
(
$tableData
,
$this
->
_updateColumns
,
false
);
}
}
/**
* Abstraction method that will build an INSERT or UPDATE part of the query
*/
protected
function
_buildInsertQuery
(
$tableData
)
{
if
(
!
is_array
(
$tableData
))
return
;
$isInsert
=
preg_match
(
'/^[INSERT|REPLACE]/'
,
$this
->
_query
);
$dataColumns
=
array_keys
(
$tableData
);
if
(
$isInsert
)
$this
->
_query
.=
' (`'
.
implode
(
$dataColumns
,
'`, `'
)
.
'`) VALUES ('
;
else
$this
->
_query
.=
" SET "
;
$this
->
_buildDataPairs
(
$tableData
,
$dataColumns
,
$isInsert
);
if
(
$isInsert
)
if
(
$isInsert
)
$this
->
_query
.=
')'
;
$this
->
_query
.=
')'
;
}
}
...
...
tests/mysqliDbTests.php
View file @
16d88ae2
...
@@ -102,6 +102,7 @@ $data = Array (
...
@@ -102,6 +102,7 @@ $data = Array (
function
createTable
(
$name
,
$data
)
{
function
createTable
(
$name
,
$data
)
{
global
$db
;
global
$db
;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$db
->
rawQuery
(
"DROP TABLE IF EXISTS
$name
"
);
$q
=
"CREATE TABLE
$name
(id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"
;
$q
=
"CREATE TABLE
$name
(id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"
;
foreach
(
$data
as
$k
=>
$v
)
{
foreach
(
$data
as
$k
=>
$v
)
{
$q
.=
",
$k
$v
"
;
$q
.=
",
$k
$v
"
;
...
@@ -192,6 +193,15 @@ $updateColumns = Array ("updatedAt");
...
@@ -192,6 +193,15 @@ $updateColumns = Array ("updatedAt");
$insertLastId
=
"id"
;
$insertLastId
=
"id"
;
$db
->
onDuplicate
(
$updateColumns
,
"id"
);
$db
->
onDuplicate
(
$updateColumns
,
"id"
);
$db
->
insert
(
"users"
,
$user
);
$db
->
insert
(
"users"
,
$user
);
$nUser
=
$db
->
where
(
'login'
,
'user3'
)
->
get
(
'users'
);
if
(
$db
->
count
!=
1
)
{
echo
"onDuplicate update failed. "
;
exit
;
}
if
(
$nUser
[
0
][
'createdAt'
]
==
$nUser
[
0
][
'updatedAt'
])
{
echo
"onDuplicate2 update failed. "
;
exit
;
}
// order by field
// order by field
$db
->
orderBy
(
"login"
,
"asc"
,
Array
(
"user3"
,
"user2"
,
"user1"
));
$db
->
orderBy
(
"login"
,
"asc"
,
Array
(
"user3"
,
"user2"
,
"user1"
));
...
...
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