Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
PSDI Army
yii2
Commits
5a1b526a
Commit
5a1b526a
authored
Nov 12, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"yii\sphinx\ColumnSchema' extracted.
parent
0868e7d3
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
196 additions
and
13 deletions
+196
-13
ColumnSchema.php
extensions/sphinx/ColumnSchema.php
+82
-0
QueryBuilder.php
extensions/sphinx/QueryBuilder.php
+49
-0
Schema.php
extensions/sphinx/Schema.php
+7
-13
sphinx.conf
tests/unit/data/sphinx/sphinx.conf
+1
-0
ColumnSchemaTest.php
tests/unit/extensions/sphinx/ColumnSchemaTest.php
+56
-0
CommandTest.php
tests/unit/extensions/sphinx/CommandTest.php
+1
-0
No files found.
extensions/sphinx/ColumnSchema.php
0 → 100644
View file @
5a1b526a
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\sphinx
;
use
yii\base\Object
;
use
yii\db\Expression
;
/**
* ColumnSchema class describes the metadata of a column in a Sphinx index.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
ColumnSchema
extends
Object
{
/**
* @var string name of this column (without quotes).
*/
public
$name
;
/**
* @var string abstract type of this column. Possible abstract types include:
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
*/
public
$type
;
/**
* @var string the PHP type of this column. Possible PHP types include:
* string, boolean, integer, double.
*/
public
$phpType
;
/**
* @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
*/
public
$dbType
;
/**
* @var boolean whether this column is a primary key
*/
public
$isPrimaryKey
;
/**
* @var boolean whether this column is an attribute
*/
public
$isAttribute
;
/**
* @var boolean whether this column is a indexed field
*/
public
$isField
;
/**
* @var boolean whether this column is a multi value attribute (MVA)
*/
public
$isMva
;
/**
* Converts the input value according to [[phpType]].
* If the value is null or an [[Expression]], it will not be converted.
* @param mixed $value input value
* @return mixed converted value
*/
public
function
typecast
(
$value
)
{
if
(
$value
===
null
||
gettype
(
$value
)
===
$this
->
phpType
||
$value
instanceof
Expression
)
{
return
$value
;
}
if
(
$value
===
''
&&
$this
->
type
!==
Schema
::
TYPE_TEXT
&&
$this
->
type
!==
Schema
::
TYPE_STRING
)
{
return
null
;
}
switch
(
$this
->
phpType
)
{
case
'string'
:
return
(
string
)
$value
;
case
'integer'
:
return
(
integer
)
$value
;
case
'boolean'
:
return
(
boolean
)
$value
;
}
return
$value
;
}
}
\ No newline at end of file
extensions/sphinx/QueryBuilder.php
View file @
5a1b526a
...
...
@@ -6,6 +6,7 @@
*/
namespace
yii\sphinx
;
use
yii\db\Expression
;
/**
* Class QueryBuilder
...
...
@@ -72,4 +73,51 @@ class QueryBuilder extends \yii\db\mysql\QueryBuilder
}
return
'OPTION '
.
implode
(
', '
,
$optionLines
);
}
/**
* Creates an INSERT SQL statement.
* For example,
*
* ~~~
* $sql = $queryBuilder->insert('tbl_user', [
* 'name' => 'Sam',
* 'age' => 30,
* ], $params);
* ~~~
*
* The method will properly escape the table and column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name => value) to be inserted into the table.
* @param array $params the binding parameters that will be generated by this method.
* They should be bound to the DB command later.
* @return string the INSERT SQL
*/
public
function
insert
(
$table
,
$columns
,
&
$params
)
{
if
((
$tableSchema
=
$this
->
db
->
getTableSchema
(
$table
))
!==
null
)
{
$columnSchemas
=
$tableSchema
->
columns
;
}
else
{
$columnSchemas
=
[];
}
$names
=
[];
$placeholders
=
[];
foreach
(
$columns
as
$name
=>
$value
)
{
$names
[]
=
$this
->
db
->
quoteColumnName
(
$name
);
if
(
$value
instanceof
Expression
)
{
$placeholders
[]
=
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
else
{
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$placeholders
[]
=
$phName
;
$params
[
$phName
]
=
!
is_array
(
$value
)
&&
isset
(
$columnSchemas
[
$name
])
?
$columnSchemas
[
$name
]
->
typecast
(
$value
)
:
$value
;
}
}
return
'INSERT INTO '
.
$this
->
db
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$names
)
.
') VALUES ('
.
implode
(
', '
,
$placeholders
)
.
')'
;
}
}
\ No newline at end of file
extensions/sphinx/Schema.php
View file @
5a1b526a
...
...
@@ -7,7 +7,6 @@
namespace
yii\sphinx
;
use
yii\db\ColumnSchema
;
use
yii\db\TableSchema
;
/**
...
...
@@ -101,15 +100,9 @@ class Schema extends \yii\db\mysql\Schema
$column
=
new
ColumnSchema
;
$column
->
name
=
$info
[
'Field'
];
$column
->
isPrimaryKey
=
(
$column
->
name
==
'id'
);
// Not supported :
//$column->allowNull = $info['Null'] === 'YES';
//$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false;
//$column->comment = $info['Comment'];
$column
->
dbType
=
$info
[
'Type'
];
//$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
$column
->
isPrimaryKey
=
(
$column
->
name
==
'id'
);
$type
=
$info
[
'Type'
];
if
(
isset
(
$this
->
typeMap
[
$type
]))
{
...
...
@@ -118,11 +111,12 @@ class Schema extends \yii\db\mysql\Schema
$column
->
type
=
self
::
TYPE_STRING
;
}
$column
->
phpType
=
$this
->
getColumnPhpType
(
$column
);
$column
->
isField
=
(
$type
==
'field'
);
$column
->
isAttribute
=
!
$column
->
isField
;
/*if ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP') {
$column->defaultValue = $column->typecast($info['Default']);
}*/
$column
->
isMva
=
(
$type
==
'mva'
);
$column
->
phpType
=
$this
->
getColumnPhpType
(
$column
);
return
$column
;
}
...
...
tests/unit/data/sphinx/sphinx.conf
View file @
5a1b526a
...
...
@@ -80,6 +80,7 @@ index yii2_test_rt_index
rt_field
=
title
rt_field
=
content
rt_attr_uint
=
type_id
rt_attr_multi
=
category
}
...
...
tests/unit/extensions/sphinx/ColumnSchemaTest.php
0 → 100644
View file @
5a1b526a
<?php
namespace
yiiunit\extensions\sphinx
;
use
yii\sphinx\ColumnSchema
;
/**
* @group sphinx
*/
class
ColumnSchemaTest
extends
SphinxTestCase
{
/**
* Data provider for [[testTypeCast]]
* @return array test data.
*/
public
function
dataProviderTypeCast
()
{
return
[
[
'integer'
,
'integer'
,
5
,
5
],
[
'integer'
,
'integer'
,
'5'
,
5
],
[
'string'
,
'string'
,
5
,
'5'
],
];
}
/**
* @dataProvider dataProviderTypeCast
*
* @param $type
* @param $phpType
* @param $value
* @param $expectedResult
*/
public
function
testTypeCast
(
$type
,
$phpType
,
$value
,
$expectedResult
)
{
$columnSchema
=
new
ColumnSchema
();
$columnSchema
->
type
=
$type
;
$columnSchema
->
phpType
=
$phpType
;
$this
->
assertEquals
(
$expectedResult
,
$columnSchema
->
typecast
(
$value
));
}
}
\ No newline at end of file
tests/unit/extensions/sphinx/CommandTest.php
View file @
5a1b526a
...
...
@@ -103,6 +103,7 @@ class CommandTest extends SphinxTestCase
'title'
=>
'Test title'
,
'content'
=>
'Test content'
,
'type_id'
=>
2
,
'category'
=>
[
41
,
42
],
'id'
=>
1
,
]);
$this
->
assertEquals
(
1
,
$command
->
execute
(),
'Unable to execute insert!'
);
...
...
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