Gitlab을 JSONDB로 사용

17848 단어

암흑 터미널 / GitlabDB


JSON 파일을 데이터베이스로 읽는 PHP 클래스. donjajo/jsondb에서 Gitlab API inspire를 사용하여 샘플 DB에 사용





GitlabDB


JSON 파일을 데이터베이스로 읽는 PHP 클래스. donjajo/php-jsondb의 Gitlab API inspire를 사용하여 샘플 DB에 사용

용법


패키지 설치
composer require darkterminal/GitlabDB

초기화

<?php
use GitlabDB\GitlabDB;

$options['personal_access_token']    = "YOUR_GITLAB_ACCESS_TOKEN";
$options['project_id']               = "YOUR_GITLAB_PROJECT_ID";
$options['branch']                   = "YOUR_GITLAB_BRANCH";
$options['cloud_url']                = "YOUR_GITLAB_URL";

$path = 'YOUR_PATH_ON_GITLAB';

$json_db = new GitlabDB( $options, $path ); // Or passing the file path of your json files with no trailing slash, default is the root directory. E.g.  new GitlabDB( $options, 'database' )

삽입

Insert into your new JSON file. Using users.json as example here

NB: Columns inserted first will be the only allowed column on other inserts

<?php
$json_db->insert( 'users.json'
    [
        'name' => 'Thomas',
        'state' => 'Nigeria',
        

A PHP Class that reads JSON file as a database. Use for sample DBs using Gitlab API inspire from donjajo/php-jsondb

용법

Install package

composer require darkterminal/GitlabDB


초기화




<?php
use GitlabDB\GitlabDB;

$options['personal_access_token']    = "YOUR_GITLAB_ACCESS_TOKEN";
$options['project_id']               = "YOUR_GITLAB_PROJECT_ID";
$options['branch']                   = "YOUR_GITLAB_BRANCH";
$options['cloud_url']                = "YOUR_GITLAB_URL";

$path = 'YOUR_PATH_ON_GITLAB';

$json_db = new GitlabDB( $options, $path ); // Or passing the file path of your json files with no trailing slash, default is the root directory. E.g.  new GitlabDB( $options, 'database' )


삽입



새 JSON 파일에 삽입합니다. 여기에서 users.json을 예로 사용

NB: 먼저 삽입된 열은 다른 삽입에서 유일하게 허용되는 열입니다.

<?php
$json_db->insert( 'users.json',
    [
        'name' => 'Thomas',
        'state' => 'Nigeria',
        'age' => 22
    ]
);


얻다



PHP의 MySQL과 마찬가지로 데이터를 다시 가져옵니다.


모든 열:



<?php
$users = $json_db->select( '*' )
    ->from( 'users.json' )
    ->get();
print_r( $users );




맞춤 열:



<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->get();
print_r( $users );





Where 문:

이 WHERE는 현재 AND 연산자로 작동하거나 OR

<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->get();
print_r( $users );

// Defaults to Thomas OR Nigeria
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ] )
    ->get();
print_r( $users );

// Now is THOMAS AND Nigeria
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ], 'AND' )
    ->get();
print_r( $users );






정규식이 포함된 Where 문:

where 문에 GitlabDB::regex를 전달하여 정규식 검색을 적용할 수 있습니다. SQL에서 LIKE 또는 REGEXP_LIKE 절을 구현하는 데 사용할 수 있습니다.

$users = $json_db->select( 'name, state' )
    ->from( "users" )
    ->where( array( "state" => GitlabDB::regex( "/ria/" )), GitlabDB::AND )
    ->get();
print_r( $users );
// Outputs are rows which contains "ria" string in "state" column.




주문:

이 기능에 감사드립니다. order_by() 메서드를 전달하면 열 이름과 정렬 메서드의 2개 인수(GitlabDB::ASCGitlabDB::DESC)로 결과가 정렬됩니다.

<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->order_by( 'age', GitlabDB::ASC )
    ->get();
print_r( $users );


행 업데이트 중



이 방법으로 동일한 JSON 파일을 업데이트할 수도 있습니다.

<?php
$json_db->update( [ 'name' => 'Oji', 'age' => 10 ] )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->trigger();



**where()* 메서드가 없으면 모든 행을 업데이트합니다*

행 삭제




<?php
$json_db->delete()
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->trigger();



**where()* 메서드가 없으면 모든 행이 삭제됩니다*

MySQL로 내보내기



이 방법을 사용하고 출력을 제공하여 JSON을 다시 SQL 파일로 내보낼 수 있습니다.

<?php
$json_db->to_mysql( 'users.json', 'users.sql' );


CREATE TABLE 비활성화

<?php
$json_db->to_mysql( 'users.json', 'users.sql', false );


XML로 내보내기



데이터를 XML 파일로 내보내는 기능도 제공



php
<?php
if( $json_db->to_xml( 'users.json', 'users.xml' ) ) {
    echo 'Saved!';
}

좋은 웹페이지 즐겨찾기