2017 © Pedro Peláez
 

library tns-parser

Oracle tnsnames.ora file / string parser written in PHP

image

dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  • Friday, January 5, 2018
  • by dcarbone
  • Repository
  • 1 Watchers
  • 2 Stars
  • 54 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 5 Versions
  • 38 % Grown

The README.md

tns-parser

Oracle tnsnames.ora file / string parser written in PHP, (*1)

Build Status, (*2)

Installation

This library is designed to be installed using Composer., (*3)

Require entry:, (*4)

"dcarbone/tns-parser": "0.1.*"

This readme assumes knowledge of Composer Autoloading., (*5)

Feature List

  • Able to parse input from file or passed string
  • Multi-line and comment agnostic
  • Supports multi-entry items (such as multi-address entries)
  • Allows searching
  • Allows basic sorting based upon entry name
  • Implements the follow interfaces:
  • Able to export entries as:
    • Original
    • Alphabetized tnsnames.ora file
    • JSON representation

Example Usage

Given the below TNS entries:, (*6)

$tns = <<<STRING
AWESOME.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me.db.myself.net)
                (PORT = 12345)
            )
        )
        (CONNECT_DATA =
            (SID = AWESOME)
            (SERVER = dedicated)
        )
    )

#--------------------------------------------------

AWESOME2.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me2.db.myself.net)
                (PORT = 12345)
            )
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me25.db.myself.net)
                (PORT = 12345)
            )
            (LOAD_BALANCE = on)
            (FAILOVER = on)
            (ENABLE = broken)
        )
        (CONNECT_DATA =
            (SID = AWESOME2)
            (SERVER = dedicated)
            (FAILOVER_MODE =
                (TYPE = select)
                (METHOD = basic)
                (RETRIES = 120)
                (DELAY = 2)
            )
        )
    )
STRING;

You would initialize an instance of the parser:, (*7)

$parser = new \DCarbone\TNSParser();

Then, if the above was contained within a file:, (*8)

$parser->parseFile('path-to-file');

Or as just a string:, (*9)

$parser->parseString($tns);

And that's it!, (*10)

Searching

During input parsing, all properties for a given TNS entry are stored a multi-dimensional array to allow searching:, (*11)

$matched = $parser->search('awesome');

The search result is an array containing the NAMES of any matched entries. The above, in this case, would result in:, (*12)

var_export($matched);
/*
array (
  0 => 'AWESOME.MYSELF.DATABASE',
  1 => 'AWESOME2.MYSELF.DATABASE',
)
*/

You can get as specific as you like:, (*13)

$matched = $parser->search('me2.db.myself');
var_export($matched):
/*
array (
  0 => 'AWESOME2.MYSELF.DATABASE',
)
*/

You can then use the matched names to retrieve the entries:, (*14)

// Using the 2nd match statement...
$entries = array();
foreach($matched as $name)
{
    $entries = $parser[$name];
}
var_export($entries);
/*
array (
  'DESCRIPTION' =>
  array (
    'ADDRESS_LIST' =>
    array (
      'ADDRESS' =>
      array (
        0 =>
        array (
          'PROTOCOL' => 'TCP',
          'HOST' => 'me2.db.myself.net',
          'PORT' => '12345',
        ),
        1 =>
        array (
          'PROTOCOL' => 'TCP',
          'HOST' => 'me25.db.myself.net',
          'PORT' => '12345',
        ),
      ),
      'LOAD_BALANCE' => 'on',
      'FAILOVER' => 'on',
      'ENABLE' => 'broken',
    ),
    'CONNECT_DATA' =>
    array (
      'SID' => 'AWESOME2',
      'SERVER' => 'dedicated',
      'FAILOVER_MODE' =>
      array (
        'TYPE' => 'select',
        'METHOD' => 'basic',
        'RETRIES' => '120',
        'DELAY' => '2',
      ),
    ),
  ),
)
*/

... Or get a valid TNS entry version as a string:, (*15)

$entry = $parser->getTNSEntryString($matched[0]);
var_export($entry);
/*
'AWESOME2.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me2.db.myself.net)
                (PORT = 12345)
            )
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me25.db.myself.net)
                (PORT = 12345)
            )
            (LOAD_BALANCE = on)
            (FAILOVER = on)
            (ENABLE = broken)
        )
        (CONNECT_DATA =
            (SID = AWESOME2)
            (SERVER = dedicated)
            (FAILOVER_MODE =
                (TYPE = select)
                (METHOD = basic)
                (RETRIES = 120)
                (DELAY = 2)
            )
        )
    )'
*/

Under the covers, the searching system uses preg_match, with the following structure: '{%s}S' by default, with %s being replaced by your input., (*16)

If you wish for a case-sensitive search, pass in true as the 2nd parameter when executing search()., (*17)

Sorting

For the moment, searching is limited to alphabetical by entry name, and utilizes ksort under the covers., (*18)

$parser->sort();

Suggestions?

For the moment this library serves my needs, however if anybody using this library would like to see some improvements / modifications made, please let me know!, (*19)

Tests

Work in progress., (*20)

The Versions

05/01 2018

dev-master

9999999-dev https://github.com/dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Daniel Carbone

oracle tns tnsnames tnsnames.ora tns names

04/01 2018

0.3.0

0.3.0.0 https://github.com/dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Daniel Carbone

oracle tns tnsnames tnsnames.ora tns names

23/03 2016

0.2.1

0.2.1.0 https://github.com/dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.3

 

by Daniel Carbone

oracle tns tnsnames tnsnames.ora tns names

18/03 2016

0.2.0

0.2.0.0 https://github.com/dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.3

 

by Daniel Carbone

oracle tns tnsnames tnsnames.ora tns names

17/11 2015

0.1.1

0.1.1.0 https://github.com/dcarbone/tns-parser

Oracle tnsnames.ora file / string parser written in PHP

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.3

 

by Daniel Carbone

oracle tns tnsnames tnsnames.ora tns names