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
a71834da
Commit
a71834da
authored
Mar 31, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small refactoring of the Query builder part
parent
dd7376b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
34 deletions
+139
-34
MysqliDb.php
MysqliDb.php
+21
-34
tests.php
tests.php
+118
-0
No files found.
MysqliDb.php
View file @
a71834da
...
...
@@ -459,7 +459,6 @@ 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'
);
...
...
@@ -471,10 +470,9 @@ class MysqliDb
$this
->
_query
.=
$prop
.
$value
[
'[I]'
]
.
", "
;
else
{
$this
->
_query
.=
$value
[
'[F]'
][
0
]
.
", "
;
if
(
is_array
(
$value
[
'[F]'
][
1
]))
{
foreach
(
$value
[
'[F]'
][
1
]
as
$
key
=>
$val
)
{
if
(
!
empty
(
$val
[
'[F]'
][
1
])
&&
is_array
(
$value
[
'[F]'
][
1
]))
{
foreach
(
$value
[
'[F]'
][
1
]
as
$
val
)
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$val
);
}
}
}
}
else
{
...
...
@@ -555,33 +553,24 @@ class MysqliDb
// Determine if is INSERT query
if
(
$hasTableData
)
{
$pos
=
strpos
(
$this
->
_query
,
'INSERT'
);
if
(
$pos
!==
false
)
{
//is insert statement
$keys
=
array_keys
(
$tableData
);
$values
=
array_values
(
$tableData
);
$this
->
_query
.=
'('
.
implode
(
$keys
,
', '
)
.
')'
;
$this
->
_query
.=
'('
.
implode
(
array_keys
(
$tableData
),
', '
)
.
')'
;
$this
->
_query
.=
' VALUES('
;
// wrap values in quotes if needed
foreach
(
$
values
as
$key
=>
$val
)
{
if
(
is_array
(
$val
))
{
if
(
!
empty
(
$val
[
'[I]'
]))
$this
->
_query
.=
$keys
[
$key
]
.
$val
[
'[I]'
]
.
", "
;
else
{
$this
->
_query
.=
$val
[
'[F]'
][
0
]
.
", "
;
if
(
is_array
(
$val
[
'[F]'
][
1
]))
{
foreach
(
$val
[
'[F]'
][
1
]
as
$key
=>
$value
)
{
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
}
}
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
);
}
continue
;
}
$values
[
$key
]
=
"'
{
$val
}
'"
;
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$val
);
$this
->
_query
.=
'?, '
;
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
_query
.=
')'
;
...
...
@@ -602,14 +591,12 @@ class MysqliDb
// Prepare table data bind parameters
if
(
$hasTableData
)
{
$this
->
_bindParams
[
0
]
=
$this
->
_paramTypeList
;
foreach
(
$tableData
as
$prop
=>
$val
)
{
if
(
!
is_array
(
$tableData
[
$prop
]))
{
array_push
(
$this
->
_bindParams
,
$tableData
[
$prop
]);
continue
;
}
if
(
is_array
(
$tableData
[
$prop
][
'[F]'
][
1
]))
{
foreach
(
$tableData
[
$prop
][
'[F]'
][
1
]
as
$val
)
foreach
(
$tableData
as
$val
)
{
if
(
!
is_array
(
$val
))
{
array_push
(
$this
->
_bindParams
,
$val
);
}
else
if
(
!
empty
(
$val
[
'[F]'
][
1
])
&&
is_array
(
$val
[
'[F]'
][
1
]))
{
// collect func() arguments
foreach
(
$val
[
'[F]'
][
1
]
as
$val
)
array_push
(
$this
->
_bindParams
,
$val
);
}
}
...
...
tests.php
0 → 100644
View file @
a71834da
<?php
require_once
(
"MysqliDb.php"
);
error_reporting
(
E_ALL
);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'testdb'
);
if
(
!
$db
)
die
(
"Database error"
);
$tables
=
Array
(
'users'
=>
Array
(
'login'
=>
'char(10) not null'
,
'customerId'
=>
'int(10) not null'
,
'firstName'
=>
'char(10) not null'
,
'lastName'
=>
'char(10) not null'
,
'password'
=>
'text not null'
,
'createdAt'
=>
'datetime'
,
'expires'
=>
'datetime'
,
'loginCount'
=>
'int(10) default 0'
)
);
$data
=
Array
(
Array
(
'login'
=>
'user1'
,
'customerId'
=>
10
,
'firstName'
=>
'John'
,
'lastName'
=>
'Doe'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
()
),
Array
(
'login'
=>
'user2'
,
'customerId'
=>
10
,
'firstName'
=>
'Mike'
,
'lastName'
=>
'B'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
2
)
),
Array
(
'login'
=>
'user3'
,
'customerId'
=>
11
,
'firstName'
=>
'Pete'
,
'lastName'
=>
'D'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
3
)
)
);
function
createTable
(
$name
,
$data
)
{
global
$db
;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$q
=
"CREATE TABLE
$name
(id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"
;
foreach
(
$data
as
$k
=>
$v
)
{
$q
.=
",
$k
$v
"
;
}
$q
.=
")"
;
$db
->
rawQuery
(
$q
);
}
foreach
(
$tables
as
$name
=>
$fields
)
{
$db
->
rawQuery
(
"DROP TABLE
$name
"
);
createTable
(
$name
,
$fields
);
}
foreach
(
$data
as
$d
)
{
$id
=
$db
->
insert
(
"users"
,
$d
);
if
(
$id
)
$d
[
'id'
]
=
$id
;
else
{
echo
"failed to insert: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
}
}
$db
->
orderBy
(
"id"
,
"asc"
);
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
3
)
{
echo
"Invalid total insert count"
;
exit
;
}
// TODO
//$db->where("createdAt", Array (">" => $db->interval("-1h")));
//$users = $db->get("users");
//print_r ($users);
$db
->
where
(
"firstname"
,
Array
(
"LIKE"
=>
'%John%'
));
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
1
)
{
echo
"Invalid insert count in LIKE: "
.
$db
->
count
;
print_r
(
$users
);
echo
$db
->
getLastQuery
();
exit
;
}
// FIXME ADD IN and BETWEEN CHECKS
$db
->
groupBy
(
"customerId"
);
$cnt
=
$db
->
get
(
"users"
,
null
,
"customerId, count(id) as cnt"
);
if
(
$db
->
count
!=
2
)
{
echo
"Invalid records count with group by"
;
}
$upData
=
Array
(
'expires'
=>
$db
->
now
(
"+5M"
,
"expires"
),
'loginCount'
=>
$db
->
inc
()
);
$db
->
where
(
"id"
,
1
);
$cnt
=
$db
->
update
(
"users"
,
$upData
);
echo
"all done
\n
"
;
$db
->
where
(
"id"
,
1
);
$db
->
getOne
(
"users"
);
echo
"cnt="
.
$db
->
count
;
//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