8000 Enable MySQL Driver by hanhan1978 · Pull Request #168 · owl/owl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Enable MySQL Driver #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading 8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .env.behat
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ APP_ENV=acceptance
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file

Expand All @@ -22,3 +17,10 @@ NOTIFICATION_ENABLE=false
# slack webhook
SLACK_NOTIFICATION_ENABLE=false
SLACK_WEBHOOK_URL=null

# database
DB_DRIVER=sqlite
DB_HOST=127.0.0.1
DB_DATABASE=owl
DB_USERNAME=developer
DB_PASSWORD=developer
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ NOTIFICATION_ENABLE=false
# slack webhook
SLACK_NOTIFICATION_ENABLE=false
SLACK_WEBHOOK_URL=null

# database
DB_DRIVER=sqlite
DB_HOST=127.0.0.1
DB_DATABASE=owl
DB_USERNAME=developer
DB_PASSWORD=developer
4 changes: 2 additions & 2 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function index()
$offset = $this->calcOffset(\Input::get('page'));
$results = $this->searchService->itemMatch($q, $this->perPage, $offset);
if (count($results) > 0) {
$res = $this->searchService->itemMatchCount($q);
$pagination = new Paginator($results, $res[0]->count, $this->perPage, null, array('path' => '/search'));
$count = $this->searchService->itemMatchCount($q);
$pagination = new Paginator($results, $count, $this->perPage, null, array('path' => '/search'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
$users = $this->userService->getLikeUsername($q);
$users_array = $this->userService->getUsersToArray($users);
Expand Down
10 changes: 8 additions & 2 deletions app/Providers/RepositoriesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ public function register()
\App::bind('Owl\Repositories\StockRepositoryInterface', 'Owl\Repositories\Fluent\StockRepository');
\App::bind('Owl\Repositories\TemplateRepositoryInterface', 'Owl\Repositories\Fluent\TemplateRepository');
\App::bind('Owl\Repositories\TagRepositoryInterface', 'Owl\Repositories\Fluent\TagRepository');
\App::bind('Owl\Repositories\TagFtsRepositoryInterface', 'Owl\Repositories\Fluent\TagFtsRepository');
\App::bind('Owl\Repositories\ItemRepositoryInterface', 'Owl\Repositories\Fluent\ItemRepository');
\App::bind('Owl\Repositories\ItemFtsRepositoryInterface', 'Owl\Repositories\Fluent\ItemFtsRepository');
if (env('DB_DRIVER', 'sqlite') == 'mysql') {
\App::bind('Owl\Repositories\ItemFtsRepositoryInterface',
'Owl\Repositories\Fluent\MySQL\ItemFtsRepository');
\App::bind('Owl\Repositories\TagFtsRepositoryInterface', 'Owl\Repositories\Fluent\MySQL\TagFtsRepository');
} else {
\App::bind('Owl\Repositories\ItemFtsRepositoryInterface', 'Owl\Repositories\Fluent\ItemFtsRepository');
\App::bind('Owl\Repositories\TagFtsRepositoryInterface', 'Owl\Repositories\Fluent\TagFtsRepository');
}
\App::bind('Owl\Repositories\ItemHistoryRepositoryInterface', 'Owl\Repositories\Fluent\ItemHistoryRepository');
\App::bind('Owl\Repositories\UserRepositoryInterface', 'Owl\Repositories\Fluent\UserRepository');
\App::bind('Owl\Repositories\UserRoleRepositoryInterface', 'Owl\Repositories\Fluent\UserRoleRepository');
Expand Down
5 changes: 3 additions & 2 deletions app/Repositories/Fluent/ItemFtsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function match($str, $limit = 10, $offset = 0)
* matchCount
*
* @param string $str
* @return array
* @return int $count
*/
public function matchCount($str)
{
Expand All @@ -127,6 +127,7 @@ public function matchCount($str)
WHERE
fts.words MATCH :match
__SQL__;
return \DB::select(\DB::raw($query), array( 'match' => FtsUtils::createMatchWord($str)));
$res = \DB::select(\DB::raw($query), array( 'match' => FtsUtils::createMatchWord($str)));
return $res[0]->count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お手数ですがphpdocの@returnの方も修正していただけますか…!

}
}
2 changes: 1 addition & 1 deletion app/Repositories/Fluent/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ public function getRecentsByTagId($tag_id)
{
return \DB::table('items')
->join('users', 'items.user_id', '=', 'users.id')
->join('tags', 'tags.id', '=', 'item_tag.tag_id')
->join('item_tag', 'items.id', '=', 'item_tag.item_id')
->join('tags', 'tags.id', '=', 'item_tag.tag_id')
->where('tags.id', $tag_id)
->where('published', '2')
->select('users.email', 'users.username', 'items.open_item_id', 'items.updated_at', 'items.title')
Expand Down
110 changes: 110 additions & 0 deletions app/Repositories/Fluent/MySQL/ItemFtsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php namespace Owl\Repositories\Fluent\MySQL;

use Owl\Repositories\ItemFtsRepositoryInterface;
use Owl\Libraries\FtsUtils;
use Owl\Repositories\Fluent\AbstractFluent;

class ItemFtsRepository extends AbstractFluent implements ItemFtsRepositoryInterface
{
protected $table = 'items';

/**
* Get a table name.
*
* @return string
*/
public function getTableName()
{
return $this->table;
}

/**
* get a itemFts by item_id.
*
* @param int $item_id
* @return void
*/
public function getById($item_id)
{
return \DB::table($this->getTableName())
->where($this->getTableName().'.item_id', $item_id)
->first();
}

/**
* Create a item fts.
*
* @param int $item_id
* @param string $title
* @param text $body
* @return int
*/
public function create($item_id, $title, $body)
{
return true;
}

/**
* Convert String into N-Gramed string.
*
* @param string $title
* @param text $body
* @return string
*/
public function toNgram($title, $body)
{
return FtsUtils::toNgram($title . "\n\n" . $body);
}

/**
* Delete a item fts.
*
* @param $item_id int
* @return boolean
*/
public function deleteItemFts($item_id)
{
return true;
}

/**
* match
*
* @param string $str
* @param int $limit
* @param int $offset
* @return array
*/
public function match($str, $limit = 10, $offset = 0)
{
return \DB::table($this->getTableName())
->join('users', 'items.user_id', '=', 'users.id')
->where('items.published', '2')
->whereRaw("match(items.title, items.body) against (? in boolean mode)", [$str])
->select(
'items.title',
'items.updated_at',
'items.open_item_id',
'users.email',
'users.username'
)
->orderBy('items.updated_at', 'desc')
->skip($offset)->take($limit)->get();
}

/**
* matchCount
*
* @param string $str
* @return int $count
*/
public function matchCount($str)
{
return \DB::table($this->getTableName())
->join('users', 'items.user_id', '=', 'users.id')
->where('items.published', '2')
->whereRaw("match(items.title, items.body) against (? in boolean mode)", [$str])
->count();
}

}
64 changes: 64 additions & 0 deletions app/Repositories/Fluent/MySQL/TagFtsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Owl\Repositories\Fluent\MySQL;

use Owl\Repositories\TagFtsRepositoryInterface;
use Owl\Libraries\FtsUtils;
use Owl\Repositories\Fluent\AbstractFluent;

class TagFtsRepository extends AbstractFluent implements TagFtsRepositoryInterface
{
protected $table = 'tags';

/**
* Get a table name.
*
* @return string
*/
public function getTableName()
{
return $this->table;
}

/**
* get a tagFts data or Create a tagFts data by ID and Words.
*
* @param int $tag_id
* @param string $words
* @return array
*/
public function firstOrCreateByIdAndWords($tag_id, $words)
{
return array();
}

/**
* get a tagFts by tag_id.
*
* @param int $id
* @return void
*/
public function getById($tag_id)
{
return \DB::table($this->getTableName())
->where($this->getTableName().'.tag_id', $tag_id)
->first();
}

/**
* get tags data by string for FullTextSearch.
*
* @param string $string
* @param int $limit
* @param int $offset
* @return array
*/
public function match($str, $limit = 10, $offset = 0)
{
return \DB::table($this->getTableName())
->whereRaw("match(tags.name) against (? in boolean mode)", [$str])
->select(
'tags.name'
)
->orderBy('tags.updated_at', 'desc')
->skip($offset)->take($limit)->get();
}
}
2 changes: 1 addition & 1 deletion app/Repositories/Fluent/TagFtsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getTableName()
*
* @param int $tag_id
* @param string $words
* @return stdClass
* @return array
*/
public function firstOrCreateByIdAndWords($tag_id, $words)
{
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/ItemFtsRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function match($str, $limit = 10, $offset = 0);
* matchCount
*
* @param string $str
* @return array
* @return int $count
*/
public function matchCount($str);
}
4 changes: 2 additions & 2 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
|
*/

'default' => 'sqlite',
'default' => env('DB_DRIVER', 'sqlite'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -61,7 +61,7 @@
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'strict' => true,
],

'pgsql' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ class CreateFullTextSearchRelatedTables extends Migration {
*/
public function up()
{
//
DB::statement('CREATE VIRTUAL TABLE items_fts USING fts3(item_id, words);');
$items = Item::get();
foreach($items as $item){
$fts = new ItemFts;
$fts->item_id = $item->id;
$fts->words = FtsUtils::toNgram($item->title . "\n\n" . $item->body);
$fts->save();
}
if (env('DB_DRIVER') === 'mysql') {
DB::statement('ALTER TABLE items MODIFY COLUMN title varchar(255)
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_general_ci\'');
DB::statement('ALTER TABLE items MODIFY COLUMN body text
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_general_ci\'');
DB::statement('ALTER TABLE items ADD FULLTEXT INDEX ft_item (title, body) /*!50100 WITH PARSER `ngram` */');
} else {
DB::statement('CREATE VIRTUAL TABLE items_fts USING fts3(item_id, words);'); 3D11
$items = Item::get();
foreach($items as $item){
$fts = new ItemFts;
$fts->item_id = $item->id;
$fts->words = FtsUtils::toNgram($item->title . "\n\n" . $item->body);
$fts->save();
}
}
}

/**
Expand All @@ -32,7 +39,15 @@ public function up()
*/
public function down()
{
DB::statement('DROP TABLE items_fts ;');
if (env('DB_DRIVER') === 'mysql') {
DB::statement('ALTER TABLE items DROP INDEX ft_item');
DB::statement('ALTER TABLE items MODIFY COLUMN title varchar(255)
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_unicode_ci\'');
DB::statement('ALTER TABLE items MODIFY COLUMN body text
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_unicode_ci\'');
} else {
DB::statement('DROP TABLE items_fts ;');
}
}

}
30 changes: 21 additions & 9 deletions database/migrations/2015_01_24_142354_create_tags_fts.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ class CreateTagsFts extends Migration {
*/
public function up()
{
DB::statement('CREATE VIRTUAL TABLE tags_fts USING fts3(tag_id, words);');
$tags = Tag::get();
foreach($tags as $tag){
$fts = new TagFts;
$fts->tag_id = $tag->id;
$fts->words = FtsUtils::toNgram($tag->name);
$fts->save();
}
if (env('DB_DRIVER') === 'mysql') {
DB::statement('ALTER TABLE tags MODIFY COLUMN name varchar(255)
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_general_ci\'');
DB::statement('ALTER TABLE tags ADD FULLTEXT INDEX ft_tag (name) /*!50100 WITH PARSER `ngram` */');
} else {
DB::statement('CREATE VIRTUAL TABLE tags_fts USING fts3(tag_id, words);');
$tags = Tag::get();
foreach ($tags as $tag) {
$fts = new TagFts;
$fts->tag_id = $tag->id;
$fts->words = FtsUtils::toNgram($tag->name);
$fts->save();
}
}
}

/**
Expand All @@ -31,7 +37,13 @@ public function up()
*/
public function down()
{
DB::statement('DROP TABLE tags_fts ;');
if (env('DB_DRIVER') === 'mysql') {
DB::statement('ALTER TABLE tags MODIFY COLUMN name varchar(255)
CHARACTER SET \'utf8mb4\' COLLATE \'utf8mb4_unicode_ci\'');
DB::statement('ALTER TABLE tags DROP INDEX ft_tag');
} else {
DB::statement('DROP TABLE tags_fts ;');
}
}

}
Loading
0