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
7c6b2f4b
Commit
7c6b2f4b
authored
Mar 16, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
insert/update Functions calls draft
parent
f171a2cd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
10 deletions
+94
-10
MysqliDb.php
MysqliDb.php
+89
-9
readme.md
readme.md
+5
-1
No files found.
MysqliDb.php
View file @
7c6b2f4b
...
...
@@ -422,6 +422,14 @@ class MysqliDb
$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]'
]
.
", "
;
continue
;
}
// determines what data type the item is, for binding purposes.
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
...
...
@@ -498,19 +506,22 @@ class MysqliDb
//is insert statement
$keys
=
array_keys
(
$tableData
);
$values
=
array_values
(
$tableData
);
$num
=
count
(
$keys
);
// wrap values in quotes
$this
->
_query
.=
'('
.
implode
(
$keys
,
', '
)
.
')'
;
$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]'
]
.
", "
;
continue
;
}
$values
[
$key
]
=
"'
{
$val
}
'"
;
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$val
);
}
$this
->
_query
.=
'('
.
implode
(
$keys
,
', '
)
.
')'
;
$this
->
_query
.=
' VALUES('
;
while
(
$num
!==
0
)
{
$this
->
_query
.=
'?, '
;
$num
--
;
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
_query
.=
')'
;
...
...
@@ -529,7 +540,8 @@ class MysqliDb
if
(
$hasTableData
)
{
$this
->
_bindParams
[
0
]
=
$this
->
_paramTypeList
;
foreach
(
$tableData
as
$prop
=>
$val
)
{
array_push
(
$this
->
_bindParams
,
$tableData
[
$prop
]);
if
(
!
is_array
(
$tableData
[
$prop
]))
array_push
(
$this
->
_bindParams
,
$tableData
[
$prop
]);
}
}
// Prepare where condition bind parameters
...
...
@@ -652,4 +664,72 @@ class MysqliDb
return
$this
->
_mysqli
->
error
;
}
/* Helper functions */
/**
* Method returns generated interval function as a string
*
* @param string interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
* @param string Initial date
*
* @return string
*/
public
function
interval
(
$diff
,
$func
=
"NOW()"
)
{
$types
=
Array
(
"s"
=>
"second"
,
"m"
=>
"minute"
,
"h"
=>
"hour"
,
"d"
=>
"day"
,
"M"
=>
"month"
,
"Y"
=>
"year"
);
$incr
=
'+'
;
$items
=
''
;
$type
=
'd'
;
if
(
$diff
&&
preg_match
(
'/([+-]?) ?([0-9]+) ?([a-zA-Z]?)/'
,
$diff
,
$matches
))
{
if
(
!
empty
(
$matches
[
1
]))
$incr
=
$matches
[
1
];
if
(
!
empty
(
$matches
[
2
]))
$items
=
$matches
[
2
];
if
(
!
empty
(
$matches
[
3
]))
$type
=
$matches
[
3
];
if
(
!
in_array
(
$type
,
array_keys
(
$types
)))
trigger_error
(
"invalid interval type in '
{
$diff
}
'"
);
$func
.=
" "
.
$incr
.
" interval "
.
$items
.
" "
.
$types
[
$type
]
.
" "
;
}
return
$func
;
}
/**
* Method returns generated interval function as an insert/update function
*
* @param string interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
* @param string Initial date
*
* @return array
*/
public
function
now
(
$diff
=
null
,
$func
=
"NOW()"
)
{
return
Array
(
"[F]"
=>
$this
->
interval
(
$diff
,
$func
));
}
/**
* Method generates incremental function call
* @param int increment amount. 1 by default
*/
public
function
inc
(
$num
=
1
)
{
return
Array
(
"[I]"
=>
"+"
.
(
int
)
$num
);
}
/**
* Method generates decrimental function call
* @param int increment amount. 1 by default
*/
public
function
dec
(
$num
=
1
)
{
return
Array
(
"[I]"
=>
"-"
.
(
int
)
$num
);
}
/**
* Method generates user defined function call
* @param string user function body
*/
public
function
func
(
$expr
)
{
return
Array
(
"[F]"
=>
$expr
);
}
}
// END class
readme.md
View file @
7c6b2f4b
...
...
@@ -19,6 +19,9 @@ $data = array(
'login'
=>
'admin'
,
'firstName'
=>
'John'
,
'lastName'
=>
'Doe'
,
'fullName'
=>
$db
->
func
(
"CONCAT(firstName,"
",lastName)"
),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
"+1y"
)
);
$id
=
$db
->
insert
(
'users'
,
$data
)
...
...
@@ -58,7 +61,8 @@ echo $user['id'];
```
php
$data
=
array
(
'firstName'
=>
'Bobby'
,
'lastName'
=>
'Tables'
'lastName'
=>
'Tables'
,
'editCount'
=>
$db
->
inc
()
);
$db
->
where
(
'id'
,
1
);
if
(
$db
->
update
(
'users'
,
$data
))
echo
'successfully updated'
;
...
...
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