Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
X Lib
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
XLibs
X Lib
Commits
0c95e0ed
Commit
0c95e0ed
authored
Jun 29, 2016
by
Alex Ne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Коррекция
parent
5fbcc180
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
192 additions
and
136 deletions
+192
-136
Memcache.php
Cache/Memcache.php
+2
-2
TableItem.php
DB/TableItem.php
+190
-134
No files found.
Cache/Memcache.php
View file @
0c95e0ed
...
@@ -38,7 +38,7 @@ class Memcache {
...
@@ -38,7 +38,7 @@ class Memcache {
}
}
$key
=
$this
->
prefix
.
$key
;
$key
=
$this
->
prefix
.
$key
;
return
$this
->
mcache
->
set
(
md5
(
$key
)
,
$value
,
0
,
$expire
);
return
$this
->
mcache
->
set
(
$key
,
$value
,
0
,
$expire
);
}
}
/**
/**
...
@@ -50,7 +50,7 @@ class Memcache {
...
@@ -50,7 +50,7 @@ class Memcache {
}
}
$key
=
$this
->
prefix
.
$key
;
$key
=
$this
->
prefix
.
$key
;
return
$this
->
mcache
->
get
(
md5
(
$key
)
,
0
);
return
$this
->
mcache
->
get
(
$key
,
0
);
}
}
}
}
?>
?>
\ No newline at end of file
DB/TableItem.php
View file @
0c95e0ed
<?php
<?php
namespace
X\DB
;
namespace
X\DB
;
class
TableItem
class
TableItem
{
{
/**
* @var mixed
*/
protected
$table_name
;
protected
$table_name
;
/**
* @var string
*/
protected
$sql
=
""
;
protected
$sql
=
""
;
/**
* @var string
*/
protected
$sql_where
=
""
;
protected
$sql_where
=
""
;
/**
* @var string
*/
protected
$sql_order
=
""
;
protected
$sql_order
=
""
;
/**
* @var string
*/
protected
$sql_limit
=
""
;
protected
$sql_limit
=
""
;
/**
* @var mixed
*/
protected
$sql_type
;
protected
$sql_type
;
/**
* @var mixed
*/
protected
$driver
=
null
;
protected
$driver
=
null
;
function
__construct
(
$name
,
$driver
)
/**
{
* @param $name
* @param $driver
*/
public
function
__construct
(
$name
,
$driver
)
{
$this
->
driver
=
$driver
;
$this
->
driver
=
$driver
;
$this
->
table_name
=
$name
;
$this
->
table_name
=
$name
;
}
}
public
function
insert
(
$Data
)
/**
{
* @param $Data
$this
->
sql
=
"INSERT INTO "
.
$this
->
build_insert
(
$Data
);
* @return mixed
*/
public
function
insert
(
$Data
)
{
$this
->
sql
=
"INSERT INTO "
.
$this
->
build_insert
(
$Data
);
$this
->
sql_type
=
"insert"
;
$this
->
sql_type
=
"insert"
;
return
$this
;
return
$this
;
}
}
public
function
replace
(
$Data
)
/**
{
* @param $Data
$this
->
sql
=
"REPLACE INTO "
.
$this
->
build_insert
(
$Data
);
* @return mixed
*/
public
function
replace
(
$Data
)
{
$this
->
sql
=
"REPLACE INTO "
.
$this
->
build_insert
(
$Data
);
$this
->
sql_type
=
"insert"
;
$this
->
sql_type
=
"insert"
;
return
$this
;
return
$this
;
}
}
private
function
build_insert
(
$Data
)
/**
{
* @param $Data
$keys
=
"`"
.
implode
(
'`,`'
,
array_keys
(
$Data
))
.
"`"
;
*/
private
function
build_insert
(
$Data
)
{
$keys
=
"`"
.
implode
(
'`,`'
,
array_keys
(
$Data
))
.
"`"
;
$_values
=
array_values
(
$Data
);
$_values
=
array_values
(
$Data
);
for
(
$i
=
0
;
$i
<
count
(
$_values
);
$i
++
)
for
(
$i
=
0
;
$i
<
count
(
$_values
);
$i
++
)
{
{
$_values
[
$i
]
=
$this
->
escape
(
$_values
[
$i
]);
$_values
[
$i
]
=
$this
->
escape
(
$_values
[
$i
]);
}
}
$values
=
"'"
.
implode
(
"','"
,
$_values
)
.
"'"
;
$values
=
"'"
.
implode
(
"','"
,
$_values
)
.
"'"
;
return
"`
{
$this
->
table_name
}
` (
{
$keys
}
) VALUES (
{
$values
}
)"
;
return
"`
{
$this
->
table_name
}
` (
{
$keys
}
) VALUES (
{
$values
}
)"
;
}
}
public
function
update
(
$Data
)
/**
{
* @param $Data
if
(
!
is_array
(
$Data
)
||
count
(
$Data
)
==
0
)
return
false
;
*/
public
function
update
(
$Data
)
{
if
(
!
is_array
(
$Data
)
||
count
(
$Data
)
==
0
)
{
return
false
;
}
$params
=
""
;
$params
=
""
;
foreach
(
$Data
as
$key
=>
$value
)
{
foreach
(
$Data
as
$key
=>
$value
)
{
$params
.=
strlen
(
$params
)
==
0
?
""
:
", "
;
$params
.=
strlen
(
$params
)
==
0
?
""
:
", "
;
$params
.=
"`"
.
$key
.
"` = '"
.
$this
->
escape
(
$value
)
.
"'"
;
$params
.=
"`"
.
$key
.
"` = '"
.
$this
->
escape
(
$value
)
.
"'"
;
}
}
$this
->
sql
=
"UPDATE `
{
$this
->
table_name
}
` SET
{
$params
}
"
;
$this
->
sql
=
"UPDATE `
{
$this
->
table_name
}
` SET
{
$params
}
"
;
$this
->
sql_type
=
"update"
;
$this
->
sql_type
=
"update"
;
return
$this
;
return
$this
;
}
}
public
function
select
(
$columns
=
[])
public
function
select
()
{
{
$columns
=
func_get_args
();
if
(
count
(
$columns
)
>
0
)
if
(
count
(
$columns
)
==
1
&&
is_array
(
$columns
[
0
]))
{
$columns
=
"`"
.
implode
(
"`,`"
,
$columns
)
.
"`"
;
$columns
=
"`"
.
implode
(
"`,`"
,
$columns
[
0
])
.
"`"
;
else
$columns
=
"*"
;
}
else
if
(
count
(
$columns
)
>
1
)
{
$columns
=
"`"
.
implode
(
"`,`"
,
$columns
)
.
"`"
;
}
else
{
$columns
=
"*"
;
}
$this
->
sql_type
=
"select"
;
$this
->
sql_type
=
"select"
;
$this
->
sql
=
"SELECT
{
$columns
}
FROM `
{
$this
->
table_name
}
`"
;
$this
->
sql
=
"SELECT
{
$columns
}
FROM `
{
$this
->
table_name
}
`"
;
return
$this
;
return
$this
;
}
}
public
function
where
(
$data
)
/**
{
* @param $data
* @return mixed
*/
public
function
where
(
$data
)
{
$this
->
sql_where
=
" WHERE "
.
$this
->
where_clause
(
$data
);
$this
->
sql_where
=
" WHERE "
.
$this
->
where_clause
(
$data
);
return
$this
;
return
$this
;
}
}
private
function
where_clause
(
$data
,
$operator
=
false
,
$gr
=
false
)
/**
{
* @param $data
* @param $operator
* @param false $gr
* @return mixed
*/
private
function
where_clause
(
$data
,
$operator
=
false
,
$gr
=
false
)
{
$where
=
""
;
$where
=
""
;
if
(
is_array
(
$data
))
if
(
is_array
(
$data
))
{
{
if
(
$operator
==
false
&&
count
(
$data
)
==
2
)
{
if
(
$operator
==
false
&&
count
(
$data
)
==
2
)
$where
.=
$this
->
where_operation
(
$data
[
0
],
$data
[
1
])
.
" "
;
{
}
else
{
$where
.=
$this
->
where_operation
(
$data
[
0
],
$data
[
1
])
.
" "
;
}
else
{
$operations
=
[];
$operations
=
[];
foreach
(
$data
as
$key
=>
$value
)
foreach
(
$data
as
$key
=>
$value
)
{
{
if
(
strtolower
(
$key
)
==
"and"
||
strtolower
(
$key
)
==
"or"
)
{
if
(
strtolower
(
$key
)
==
"and"
||
strtolower
(
$key
)
==
"or"
)
$operations
[]
=
((
$gr
)
?
"("
:
""
)
.
$operations
[]
=
((
$gr
)
?
"("
:
""
)
.
$this
->
where_clause
(
$value
,
strtolower
(
$key
),
true
)
.
$this
->
where_clause
(
$value
,
strtolower
(
$key
),
true
)
.
((
$gr
)
?
") "
:
" "
);
((
$gr
)
?
") "
:
" "
);
}
else
{
else
$operations
[]
=
$this
->
where_operation
(
$key
,
$value
);
$operations
[]
=
$this
->
where_operation
(
$key
,
$value
);
}
}
}
$where
.=
implode
(
" "
.
strtoupper
(
$operator
)
.
" "
,
$operations
);
$where
.=
implode
(
" "
.
strtoupper
(
$operator
)
.
" "
,
$operations
);
}
}
return
$where
;
return
$where
;
}
}
if
(
is_string
(
$data
))
return
$data
;
if
(
is_string
(
$data
))
{
return
$data
;
}
}
}
private
function
array_quote
(
$array
)
/**
{
* @param $array
$temp
=
array
();
*/
private
function
array_quote
(
$array
)
{
$temp
=
[];
foreach
(
$array
as
$value
)
foreach
(
$array
as
$value
)
{
{
$temp
[]
=
is_int
(
$value
)
?
$value
:
$this
->
escape
(
$value
);
$temp
[]
=
is_int
(
$value
)
?
$value
:
$this
->
escape
(
$value
);
}
}
return
implode
(
$temp
,
','
);
return
implode
(
$temp
,
','
);
}
}
private
function
where_operation
(
$key
,
$value
)
/**
{
* @param $key
* @param $value
*/
private
function
where_operation
(
$key
,
$value
)
{
$wheres
=
[];
$wheres
=
[];
$type
=
gettype
(
$value
);
$type
=
gettype
(
$value
);
preg_match
(
'/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i'
,
$key
,
$match
);
preg_match
(
'/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i'
,
$key
,
$match
);
$column
=
"`"
.
$match
[
2
]
.
"`"
;
$column
=
"`"
.
$match
[
2
]
.
"`"
;
if
(
isset
(
$match
[
4
]))
if
(
isset
(
$match
[
4
]))
{
{
$operator
=
$match
[
4
];
$operator
=
$match
[
4
];
if
(
$operator
==
'!'
)
if
(
$operator
==
'!'
)
{
{
switch
(
$type
)
{
switch
(
$type
)
{
case
'NULL'
:
case
'NULL'
:
$wheres
[]
=
$column
.
' IS NOT NULL'
;
$wheres
[]
=
$column
.
' IS NOT NULL'
;
break
;
break
;
...
@@ -143,80 +191,59 @@ class TableItem
...
@@ -143,80 +191,59 @@ class TableItem
break
;
break
;
case
'string'
:
case
'string'
:
$wheres
[]
=
$column
.
' != '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
$wheres
[]
=
$column
.
' != '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
break
;
break
;
}
}
}
}
if
(
$operator
==
'<>'
||
$operator
==
'><'
)
if
(
$operator
==
'<>'
||
$operator
==
'><'
)
{
{
if
(
$type
==
'array'
)
{
if
(
$type
==
'array'
)
if
(
$operator
==
'><'
)
{
{
if
(
$operator
==
'><'
)
{
$column
.=
' NOT'
;
$column
.=
' NOT'
;
}
}
if
(
is_numeric
(
$value
[
0
])
&&
is_numeric
(
$value
[
1
]))
if
(
is_numeric
(
$value
[
0
])
&&
is_numeric
(
$value
[
1
]))
{
{
$wheres
[]
=
'('
.
$column
.
' BETWEEN '
.
$value
[
0
]
.
' AND '
.
$value
[
1
]
.
')'
;
$wheres
[]
=
'('
.
$column
.
' BETWEEN '
.
$value
[
0
]
.
' AND '
.
$value
[
1
]
.
')'
;
}
else
{
}
$wheres
[]
=
'('
.
$column
.
' BETWEEN '
.
"'"
.
$this
->
escape
(
$value
[
0
])
.
"'"
.
' AND '
.
"'"
.
$this
->
escape
(
$value
[
1
])
.
"'"
.
')'
;
else
{
$wheres
[]
=
'('
.
$column
.
' BETWEEN '
.
"'"
.
$this
->
escape
(
$value
[
0
])
.
"'"
.
' AND '
.
"'"
.
$this
->
escape
(
$value
[
1
])
.
"'"
.
')'
;
}
}
}
}
}
}
if
(
$operator
==
'~'
||
$operator
==
'!~'
)
if
(
$operator
==
'~'
||
$operator
==
'!~'
)
{
{
if
(
$type
!=
'array'
)
{
if
(
$type
!=
'array'
)
$value
=
[
$value
];
{
$value
=
array
(
$value
);
}
}
$like_clauses
=
array
()
;
$like_clauses
=
[]
;
foreach
(
$value
as
$item
)
foreach
(
$value
as
$item
)
{
{
$item
=
strval
(
$item
);
$item
=
strval
(
$item
);
$suffix
=
mb_substr
(
$item
,
-
1
,
1
);
$suffix
=
mb_substr
(
$item
,
-
1
,
1
);
if
(
$suffix
===
'_'
)
if
(
$suffix
===
'_'
)
{
{
$item
=
substr_replace
(
$item
,
'%'
,
-
1
);
$item
=
substr_replace
(
$item
,
'%'
,
-
1
);
}
}
else
if
(
$suffix
===
'%'
)
{
elseif
(
$suffix
===
'%'
)
{
$item
=
'%'
.
substr_replace
(
$item
,
''
,
-
1
,
1
);
$item
=
'%'
.
substr_replace
(
$item
,
''
,
-
1
,
1
);
}
}
else
if
(
preg_match
(
'/^(?!%).+(?<!%)$/'
,
$item
))
{
elseif
(
preg_match
(
'/^(?!%).+(?<!%)$/'
,
$item
))
{
$item
=
'%'
.
$item
.
'%'
;
$item
=
'%'
.
$item
.
'%'
;
}
}
$like_clauses
[]
=
$column
.
(
$operator
===
'!~'
?
' NOT'
:
''
)
.
' LIKE '
.
"'"
.
$this
->
escape
(
$item
)
.
"'"
;
$like_clauses
[]
=
$column
.
(
$operator
===
'!~'
?
' NOT'
:
''
)
.
' LIKE '
.
"'"
.
$this
->
escape
(
$item
)
.
"'"
;
}
}
$wheres
[]
=
implode
(
' OR '
,
$like_clauses
);
$wheres
[]
=
implode
(
' OR '
,
$like_clauses
);
}
}
if
(
in_array
(
$operator
,
array
(
'>'
,
'>='
,
'<'
,
'<='
)))
if
(
in_array
(
$operator
,
[
'>'
,
'>='
,
'<'
,
'<='
]))
{
{
if
(
is_numeric
(
$value
))
{
if
(
is_numeric
(
$value
))
{
$wheres
[]
=
$column
.
' '
.
$operator
.
' '
.
$value
;
$wheres
[]
=
$column
.
' '
.
$operator
.
' '
.
$value
;
}
}
else
{
else
$wheres
[]
=
$column
.
' '
.
$operator
.
' '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
{
$wheres
[]
=
$column
.
' '
.
$operator
.
' '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
}
}
}
}
}
}
else
{
else
switch
(
$type
)
{
{
switch
(
$type
)
{
case
'NULL'
:
case
'NULL'
:
$wheres
[]
=
$column
.
' IS NULL'
;
$wheres
[]
=
$column
.
' IS NULL'
;
break
;
break
;
...
@@ -235,44 +262,73 @@ class TableItem
...
@@ -235,44 +262,73 @@ class TableItem
break
;
break
;
case
'string'
:
case
'string'
:
$wheres
[]
=
$column
.
' = '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
$wheres
[]
=
$column
.
' = '
.
"'"
.
$this
->
escape
(
$value
)
.
"'"
;
break
;
break
;
}
}
}
}
return
implode
(
' '
,
$wheres
);
return
implode
(
' '
,
$wheres
);
}
}
public
function
order
(
$coll
,
$type
)
/**
{
* @param $coll
* @param $type
*/
public
function
order
(
$coll
,
$type
)
{
$this
->
sql_order
=
" ORDER BY `
{
$coll
}
`
{
$type
}
"
;
$this
->
sql_order
=
" ORDER BY `
{
$coll
}
`
{
$type
}
"
;
return
$this
;
return
$this
;
}
}
public
function
limit
(
$op1
,
$op2
=
false
)
/**
{
* @param $op1
if
(
$op2
)
$this
->
sql_limit
=
" LIMIT
{
$op1
}
,
{
$op2
}
"
;
* @param $op2
else
$this
->
sql_limit
=
" LIMIT
{
$op1
}
"
;
*/
public
function
limit
(
$op1
,
$op2
=
false
)
{
if
(
$op2
)
{
$this
->
sql_limit
=
" LIMIT
{
$op1
}
,
{
$op2
}
"
;
}
else
{
$this
->
sql_limit
=
" LIMIT
{
$op1
}
"
;
}
return
$this
;
return
$this
;
}
}
private
function
escape
(
$data
)
{
/**
if
(
$this
->
driver
instanceof
\X_DB_MySQLi
)
return
$this
->
driver
->
esc
(
$data
);
* @param $data
* @return mixed
*/
private
function
escape
(
$data
)
{
if
(
$this
->
driver
instanceof
\X_DB_MySQLi
)
{
return
$this
->
driver
->
esc
(
$data
);
}
}
}
public
function
getSQL
()
/**
{
* @return mixed
return
$this
->
sql
.
$this
->
sql_where
;
*/
public
function
getSQL
()
{
return
$this
->
sql
.
$this
->
sql_where
;
}
}
public
function
exec
(
$op1
=
false
,
$op2
=
false
,
$op3
=
false
)
/**
{
* @param $op1
if
(
$this
->
sql_type
==
"update"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
* @param false $op2
* @param false $op3
* @return mixed
*/
public
function
exec
(
$op1
=
false
,
$op2
=
false
,
$op3
=
false
)
{
if
(
$this
->
sql_type
==
"update"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
{
return
$this
->
driver
->
rq
(
$this
->
sql
.
$this
->
sql_where
);
return
$this
->
driver
->
rq
(
$this
->
sql
.
$this
->
sql_where
);
if
(
$this
->
sql_type
==
"insert"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
}
return
$this
->
driver
->
insert
(
$this
->
sql
);
if
(
$this
->
sql_type
==
"select"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
if
(
$this
->
sql_type
==
"insert"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
{
return
$this
->
driver
->
get
(
$this
->
sql
.
$this
->
sql_where
.
$this
->
sql_order
.
$this
->
sql_limit
,
$op1
,
$op2
,
$op3
);
return
$this
->
driver
->
insert
(
$this
->
sql
);
else
throw
new
\Exception
(
"Internal error"
,
0
);
}
if
(
$this
->
sql_type
==
"select"
&&
$this
->
driver
instanceof
\X_DB_MySQLi
)
{
return
$this
->
driver
->
get
(
$this
->
sql
.
$this
->
sql_where
.
$this
->
sql_order
.
$this
->
sql_limit
,
$op1
,
$op2
,
$op3
);
}
else
{
throw
new
\Exception
(
"Internal error"
,
0
);
}
}
}
}
}
?>
?>
\ No newline at end of file
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