Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pquery
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
pquery
Commits
a29fafb5
Commit
a29fafb5
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
added more tests for SQL plugin
parent
2f4f7391
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pquery.sql.php
+43
-19
43 additions, 19 deletions
pquery.sql.php
test/test_sql.php
+27
-2
27 additions, 2 deletions
test/test_sql.php
with
70 additions
and
21 deletions
pquery.sql.php
+
43
−
19
View file @
a29fafb5
...
...
@@ -66,12 +66,19 @@ class pQuerySql extends pQuery implements pQueryExtension {
if
(
!
count
(
$args
)
)
return
;
// Parse arguments as variables. Arrays and
// Replace variable indices by names equal to their indices
if
(
!
is_array
(
$args
[
0
])
)
array_unshift
(
$args
,
null
);
$variables
=
array
();
foreach
(
$args
as
$i
=>
$argument
)
{
if
(
is_array
(
$argument
)
)
$variables
=
array_merge
(
$variables
,
$argument
);
else
$variables
[
$i
]
=
$argument
;
}
// Replace variables by their escaped values
$this
->
set
(
$ar
g
s
);
$this
->
set
(
$
v
ar
iable
s
);
}
/**
...
...
@@ -133,7 +140,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
$result
=
mysql_query
(
$this
->
query
,
self
::
$link
);
if
(
!
$result
)
return
self
::
mysql_error
();
return
self
::
mysql_error
(
$this
->
query
);
$this
->
result
=
$result
;
$this
->
executed
=
true
;
...
...
@@ -210,10 +217,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
*/
static
function
set_login_data
(
$host
,
$username
,
$password
,
$dbname
)
{
// Close any existing connection
if
(
self
::
$link
)
{
mysql_close
(
self
::
$link
);
self
::
$link
=
null
;
}
self
::
disconnect
();
self
::
$login_data
=
array_merge
(
self
::
$login_data
,
compact
(
'host'
,
'username'
,
'password'
,
'dbname'
));
...
...
@@ -224,16 +228,16 @@ class pQuerySql extends pQuery implements pQueryExtension {
*/
static
function
assert_login_data_exist
()
{
if
(
!
isset
(
self
::
$login_data
[
'host'
])
)
return
self
::
error
(
'No SQL host specified.'
);
return
self
::
error
(
'No
My
SQL
database server
host
is
specified.'
);
if
(
!
isset
(
self
::
$login_data
[
'username'
])
)
return
self
::
error
(
'No
SQL
username specified.'
);
return
self
::
error
(
'No username
is
specified
for the MySQL server
.'
);
if
(
!
isset
(
self
::
$login_data
[
'password'
])
)
return
self
::
error
(
'No
SQL
password specified.'
);
return
self
::
error
(
'No password
is
specified
for the MySQL server
.'
);
if
(
!
isset
(
self
::
$login_data
[
'host'
])
)
return
self
::
error
(
'No SQL
host
specified.'
);
return
self
::
error
(
'No
My
SQL
database name is
specified.'
);
}
/**
...
...
@@ -262,24 +266,44 @@ class pQuerySql extends pQuery implements pQueryExtension {
return
self
::
mysql_error
();
}
/**
* Close the current connection, if any.
*
* @uses mysql_close
*/
static
function
disconnect
()
{
// Return if the connection has already been closed
if
(
!
self
::
$link
)
return
;
mysql_close
(
self
::
$link
);
self
::
$link
=
null
;
}
/**
* Echo the latest MySQL error.
* If a query is specified and debug mode is on, add the query to the error message.
*
* @param string $query The query that was executed, if any.
*/
static
function
mysql_error
()
{
self
::
error
(
'MySQL error %d: %s.'
,
mysql_errno
(),
mysql_error
());
static
function
mysql_error
(
$query
=
''
)
{
$error
=
sprintf
(
'MySQL error %d: %s.'
,
mysql_errno
(),
mysql_error
());
PQUERY_DEBUG
&&
$error
.
=
"
\n
Query: "
.
$this
->
query
;
self
::
error
(
$error
);
}
/**
* Extention of {@link pQuery::error}, returning FALSE (useful in result loops).
* Also, the current query is printed in debug mode.
*
* @param string $error The error message
* @returns bool FALSE
*/
static
function
error
()
{
parent
::
error
(
'MySQL error %d: %s.'
,
mysql_errno
(),
mysql_error
());
if
(
PQUERY_DEBUG
)
echo
$this
->
query
;
static
function
error
(
$error
/* [ , $arg1 [ , ... ] ] */
)
{
$args
=
func_get_args
();
call_user_func_array
(
'pQuery::error'
,
$args
);
//parent::error('SQL error %d: %s.', mysql_errno(), mysql_error());
return
false
;
}
...
...
This diff is collapsed.
Click to expand it.
test/test_sql.php
+
27
−
2
View file @
a29fafb5
<?php
__p
::
load_plugin
(
'sql'
);
include
'../../debug.php'
;
class
pQuerySqlTest
extends
UnitTestCase
{
function
__construct
()
{
...
...
@@ -8,7 +9,12 @@ class pQuerySqlTest extends UnitTestCase {
}
function
setUp
()
{
__sql
::
set_login_data
(
'localhost'
,
'root'
,
''
,
'pquery_test'
);
}
function
tearDown
()
{
__sql
::
disconnect
();
__sql
::
$login_data
=
array
();
}
function
test_set_login_data
()
{
...
...
@@ -22,7 +28,6 @@ class pQuerySqlTest extends UnitTestCase {
function
test_no_login_data
()
{
$this
->
expectException
(
'pQueryException'
);
__sql
::
$login_data
=
array
();
__sql
::
assert_login_data_exist
();
}
...
...
@@ -33,6 +38,7 @@ class pQuerySqlTest extends UnitTestCase {
}
function
test_variable_query
()
{
self
::
set_login_data
();
$sql
=
_sql
(
"select id from foo where bar = '[bar]'"
)
->
set
(
array
(
'bar'
=>
'test1'
));
$this
->
assertEqual
(
$sql
->
query
,
"select id from foo where bar = 'test1'"
);
...
...
@@ -45,12 +51,26 @@ class pQuerySqlTest extends UnitTestCase {
}
function
test_escaped_query
()
{
self
::
set_login_data
();
$sql
=
_sql
(
"select id from foo where bar = '[bar]'"
)
->
set
(
array
(
'bar'
=>
"select id from foo where bar = 'test1'"
));
$this
->
assertNotEqual
(
$sql
->
query
,
"select id from foo where bar = 'select id from foo where bar = 'test1''"
);
}
function
test_constructor_simple
()
{
self
::
set_login_data
();
$sql
=
_sql
(
"select id from foo where bar = '[0]'"
,
'test1'
);
$this
->
assertEqual
(
$sql
->
query
,
"select id from foo where bar = 'test1'"
);
}
function
test_constructor_advanced
()
{
self
::
set_login_data
();
$sql
=
_sql
(
"[0] [bar] [foo] [2]"
,
'1'
,
array
(
'bar'
=>
'2'
,
'foo'
=>
'3'
),
'4'
);
$this
->
assertEqual
(
$sql
->
query
,
"1 2 3 4"
);
}
function
test_select_simple
()
{
self
::
set_login_data
();
$sql
=
_sql
(
"select bar from foo where id = 1"
);
$result
=
$sql
->
fetch
(
'object'
);
$this
->
assertEqual
(
$result
->
bar
,
'test1'
);
...
...
@@ -58,9 +78,14 @@ class pQuerySqlTest extends UnitTestCase {
}
function
test_result_count
()
{
__sql
::
set_login_data
(
'localhost'
,
'root'
,
''
,
'pquery_test'
);
$sql
=
_sql
(
"select bar from foo where id in (1, 2)"
);
$this
->
assertEqual
(
$sql
->
result_count
(),
2
);
}
static
function
set_login_data
()
{
__sql
::
set_login_data
(
'localhost'
,
'root'
,
''
,
'pquery_test'
);
}
}
?>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment