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
b1d87156
Commit
b1d87156
authored
Apr 14, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Big refactoring of INSERT/UPDATE queries generation
parent
e6c7564d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
55 deletions
+52
-55
MysqliDb.php
MysqliDb.php
+40
-53
tests.php
tests.php
+12
-2
No files found.
MysqliDb.php
View file @
b1d87156
...
...
@@ -456,41 +456,55 @@ class MysqliDb
}
}
// Did the user call the "where" method?
if
(
!
empty
(
$this
->
_where
))
{
// if update data was passed, filter through and create the SQL query, accordingly.
if
(
$hasTableData
)
{
$pos
=
strpos
(
$this
->
_query
,
'UPDATE'
);
if
(
$pos
!==
false
)
{
foreach
(
$tableData
as
$prop
=>
$value
)
{
if
(
is_array
(
$value
))
{
$this
->
_query
.=
$prop
.
" = "
;
if
(
!
empty
(
$value
[
'[I]'
]))
$this
->
_query
.=
$prop
.
$value
[
'[I]'
]
.
", "
;
else
{
$this
->
_query
.=
$value
[
'[F]'
][
0
]
.
", "
;
if
(
!
empty
(
$val
[
'[F]'
][
1
])
&&
is_array
(
$value
[
'[F]'
][
1
]))
{
foreach
(
$value
[
'[F]'
][
1
]
as
$val
)
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$val
);
}
}
}
else
{
// determines what data type the item is, for binding purposes.
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
// Determine INSERT or UPDATE query
if
(
$hasTableData
)
{
$isInsert
=
strpos
(
$this
->
_query
,
'INSERT'
);
$isUpdate
=
strpos
(
$this
->
_query
,
'UPDATE'
);
if
(
$isInsert
!==
false
)
{
//is insert statement
$this
->
_query
.=
'('
.
implode
(
array_keys
(
$tableData
),
', '
)
.
')'
;
$this
->
_query
.=
' VALUES('
;
}
// prepares the reset of the SQL query.
$this
->
_query
.=
(
$prop
.
' = ?, '
);
}
foreach
(
$tableData
as
$column
=>
$value
)
{
if
(
$isUpdate
!==
false
)
$this
->
_query
.=
$column
.
" = "
;
if
(
!
is_array
(
$value
))
{
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
$this
->
_query
.=
'?, '
;
}
else
{
$key
=
key
(
$value
);
$val
=
$value
[
$key
];
switch
(
$key
)
{
case
'[I]'
:
$this
->
_query
.=
$column
.
$val
.
", "
;
break
;
case
'[F]'
:
$this
->
_query
.=
$val
[
0
]
.
", "
;
if
(
!
empty
(
$val
[
1
]))
{
foreach
(
$val
[
1
]
as
$v
)
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$v
);
}
break
;
default
:
die
(
"Wrong operation"
);
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
}
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
if
(
$isInsert
!==
false
)
$this
->
_query
.=
')'
;
}
// Did the user call the "where" method?
if
(
$hasConditional
)
{
//Prepair the where portion of the query
$this
->
_query
.=
' WHERE '
;
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
$andOr
=
''
;
//
Determine if where condition was a first one or it was AND or OR type
//
if its not a first condition insert its concatenator (AND or OR)
if
(
array_search
(
$column
,
array_keys
(
$this
->
_where
))
!=
0
)
$andOr
=
' '
.
$value
[
0
]
.
' '
;
...
...
@@ -549,33 +563,6 @@ class MysqliDb
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
)
.
" "
;
}
// Determine if is INSERT query
if
(
$hasTableData
)
{
$pos
=
strpos
(
$this
->
_query
,
'INSERT'
);
if
(
$pos
!==
false
)
{
//is insert statement
$this
->
_query
.=
'('
.
implode
(
array_keys
(
$tableData
),
', '
)
.
')'
;
$this
->
_query
.=
' VALUES('
;
foreach
(
$tableData
as
$key
=>
$val
)
{
if
(
!
is_array
(
$val
))
{
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$val
);
$this
->
_query
.=
'?, '
;
}
else
if
(
!
empty
(
$val
[
'[I]'
]))
{
$this
->
_query
.=
$key
.
$val
[
'[I]'
]
.
", "
;
}
else
{
$this
->
_query
.=
$val
[
'[F]'
][
0
]
.
", "
;
if
(
!
empty
(
$val
[
'[F]'
][
1
])
&&
is_array
(
$val
[
'[F]'
][
1
]))
{
foreach
(
$val
[
'[F]'
][
1
]
as
$value
)
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
}
}
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
_query
.=
')'
;
}
}
// Did the user set a limit
if
(
isset
(
$numRows
))
{
if
(
is_array
(
$numRows
))
...
...
tests.php
View file @
b1d87156
...
...
@@ -107,11 +107,21 @@ $upData = Array (
);
$db
->
where
(
"id"
,
1
);
$cnt
=
$db
->
update
(
"users"
,
$upData
);
echo
"all done
\n
"
;
$db
->
where
(
"id"
,
1
);
$db
->
getOne
(
"users"
);
echo
"cnt="
.
$db
->
count
;
if
(
$db
->
count
!=
1
)
{
echo
"Invalid users count on getOne()"
;
exit
;
}
$db
->
delete
(
"users"
);
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
0
)
{
echo
"Invalid users count after delete"
;
exit
;
}
echo
"All done"
;
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
...
...
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