Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pquery
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
pquery
Commits
6f0fa012
Commit
6f0fa012
authored
Dec 06, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added 'select' shortcut function to SQL plugin.
parent
41717226
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
2 deletions
+119
-2
pquery.sql.php
pquery.sql.php
+79
-2
test/test_sql.php
test/test_sql.php
+40
-0
No files found.
pquery.sql.php
View file @
6f0fa012
...
...
@@ -328,6 +328,23 @@ class pQuerySql extends pQuery implements pQueryExtension {
return
mysql_real_escape_string
(
$value
,
self
::
$link
);
}
/**
* Select all records from the given table that match the constraints.
*
* @param string $table The table to select from.
* @param array $constraints Column names pointing to their values.
* @param bool $escape Whether to escape the constraint values. Defaults to TRUE.
* @returns pQuerySql The created query instance.
*/
static
function
select
(
$table
,
$columns
,
$constraints
=
array
(),
$escape
=
true
)
{
return
_sql
(
'SELECT [columns] FROM `[table]` WHERE [constraints];'
)
->
set_unescaped
(
array
(
'columns'
=>
self
::
parse_columns
(
$columns
),
'table'
=>
$table
,
'constraints'
=>
self
::
parse_constraints
(
$constraints
,
$escape
)
));
}
/**
* Insert a record in the given table.
*
...
...
@@ -351,8 +368,8 @@ class pQuerySql extends pQuery implements pQueryExtension {
/**
* Delete all records from the given table that match the constraints.
*
* @param string $table The table to
insert into
.
* @param array $constraints Column names pointing to their values
* @param string $table The table to
delete from
.
* @param array $constraints Column names pointing to their values
.
* @param bool $escape Whether to escape the constraint values. Defaults to TRUE.
* @returns pQuerySql The created query instance.
*/
...
...
@@ -364,6 +381,66 @@ class pQuerySql extends pQuery implements pQueryExtension {
));
}
/**
* Parse a list of column names.
*
* @param string|array $columns One of:
* - '*': Returns itself.
* - string: Treated as a column name or aggregate function, and
* escaped as such with backticks.
* - array:
* @returns string The parsed columns.
*/
static
function
parse_columns
(
$columns
)
{
if
(
$columns
==
'*'
)
return
'*'
;
if
(
is_string
(
$columns
)
)
return
self
::
escape_column
(
$columns
);
if
(
!
is_array
(
$columns
)
)
return
self
::
error
(
'Unknown columns type.'
);
$escaped_columns
=
array
();
foreach
(
$columns
as
$key
=>
$value
)
{
if
(
is_numeric
(
$key
)
)
{
// Simple column name
$escaped_columns
[]
=
self
::
escape_column
(
$value
);
}
else
{
// MySQL 'AS' construction
$escaped_columns
[]
=
self
::
escape_column
(
$key
)
.
" AS `"
.
$value
.
"`"
;
}
}
return
implode
(
", "
,
$escaped_columns
);
}
/**
* Escape a column name to be safely used (and in a tidy manner) in a column list.
*
* @param string $column The column name to escape.
* @returns string The escaped column.
*/
static
function
escape_column
(
$column
)
{
if
(
preg_match
(
'/^`.*?`$/'
,
$column
)
)
{
// `column` -> `column`
return
$column
;
}
elseif
(
preg_match
(
'/^(\w+)\.(\w+)$/'
,
$column
,
$m
)
)
{
// table.column -> `table`.`column`
list
(
$table
,
$column
)
=
array_slice
(
$m
,
1
);
return
"`
$table
`.`
$column
`"
;
}
elseif
(
preg_match
(
'/^(\w+)\(([^)]+)\)$/'
,
$column
,
$m
)
)
{
// function(name) -> FUNCTION(`name`)
// function(`name`) -> FUNCTION(`name`)
list
(
$aggregate_function
,
$column
)
=
array_slice
(
$m
,
1
);
return
strtoupper
(
$aggregate_function
)
.
"("
.
self
::
escape_column
(
$column
)
.
")"
;
}
// column -> `column`
return
"`
$column
`"
;
}
/**
* Parse a list of constraints.
*
...
...
test/test_sql.php
View file @
6f0fa012
...
...
@@ -83,6 +83,41 @@ class pQuerySqlTest extends UnitTestCase {
$this
->
assertEqual
(
$sql
->
num_rows
(),
2
);
}
function
test_escape_column_simple
()
{
$this
->
assertEqual
(
__sql
::
escape_column
(
'foo'
),
'`foo`'
);
}
function
test_escape_column_escaped
()
{
$this
->
assertEqual
(
__sql
::
escape_column
(
'`foo`'
),
'`foo`'
);
}
function
test_escape_column_table
()
{
$this
->
assertEqual
(
__sql
::
escape_column
(
'foo.bar'
),
'`foo`.`bar`'
);
}
function
test_escape_column_aggregate
()
{
$this
->
assertEqual
(
__sql
::
escape_column
(
'count(foo)'
),
'COUNT(`foo`)'
);
}
function
test_escape_column_aggregate_escaped
()
{
$this
->
assertEqual
(
__sql
::
escape_column
(
'count(`foo`)'
),
'COUNT(`foo`)'
);
}
function
test_parse_columns_star
()
{
$sql
=
__sql
::
select
(
'foo'
,
'*'
,
''
,
false
);
$this
->
assertEqual
(
$sql
->
query
,
"SELECT * FROM `foo` WHERE 1;"
);
}
function
test_parse_columns_simple
()
{
$sql
=
__sql
::
select
(
'foo'
,
array
(
'id'
,
'bar'
),
''
,
false
);
$this
->
assertEqual
(
$sql
->
query
,
"SELECT `id`, `bar` FROM `foo` WHERE 1;"
);
}
function
test_parse_columns_as
()
{
$sql
=
__sql
::
select
(
'foo'
,
array
(
'id'
=>
'foo_id'
),
''
,
false
);
$this
->
assertEqual
(
$sql
->
query
,
"SELECT `id` AS `foo_id` FROM `foo` WHERE 1;"
);
}
function
test_parse_constraints_empty
()
{
$this
->
assertIdentical
(
__sql
::
parse_constraints
(
null
,
false
),
"1"
);
}
...
...
@@ -104,6 +139,11 @@ class pQuerySqlTest extends UnitTestCase {
"`id` IN ('1', '2', '3')"
);
}
function
test_select_query
()
{
$sql
=
__sql
::
select
(
'foo'
,
'*'
,
array
(
'bar'
=>
'test1'
),
false
);
$this
->
assertEqual
(
$sql
->
query
,
"SELECT * FROM `foo` WHERE `bar` = 'test1';"
);
}
function
test_insert_query
()
{
$sql
=
__sql
::
insert_row
(
'foo'
,
array
(
'bar'
=>
'test3'
),
false
);
$this
->
assertEqual
(
$sql
->
query
,
"INSERT INTO `foo`(`bar`) VALUES('test3');"
);
...
...
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