PHP Classes

File: sample-kmd-sybase.php

Recommend this page to a friend!
  Classes of François NEUMANN-RYSTOW   PHP Command Line Shell   sample-kmd-sybase.php   Download  
File: sample-kmd-sybase.php
Role: Application script
Content type: text/plain
Description: Microsoft sql server commandline client
Class: PHP Command Line Shell
Use ncurses to get key inputs and write shell text
Author: By
Last change:
Date: 10 years ago
Size: 3,840 bytes
 

Contents

Class file image Download
<?php

require 'kmd.php';

class
kmdsybase extends kmd {

    var
$database = '';

    function
inv($host) {
        return
$this->database . '@' . $host . '# ';
    }

    function
tab($line, $curl) {
       
$tabs = Array();
       
$before = substr($line, 0, $curl);
       
$after = substr($line, $curl);
       
$table = '';
        if (
            (
           
stripos($before, 'SELECT ') !== false &&
           
stripos($before, ' FROM') === false
           
)
           ) {
           
$table = 'TABLE';
           
preg_match_all('/FROM ([0-9a-zA-Z"_]*)/i', $line, $ms);
            if (isset(
$ms[1][0])) $table = $ms[1][0];
        }
        if (
           
$table == '' &&
            (
           
stripos($before, 'INSERT') !== false &&
           
stripos($before, '(') !== false
           
)
           ) {
           
preg_match_all('/INSERT INTO ([0-9a-zA-Z"_]*)/i', $line, $ms);
            if (isset(
$ms[1][0])) $table = $ms[1][0];
        }
        if (
           
$table == '' &&
            (
           
stripos($before, 'UPDATE') !== false &&
           
stripos($before, 'SET') !== false
           
)
           ) {
           
preg_match_all('/UPDATE ([0-9a-zA-Z"_]*)/i', $line, $ms);
            if (isset(
$ms[1][0])) $table = $ms[1][0];
        }
        if (
$table == '') {
           
$sql = sybase_query("SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' ORDER BY table_name;");
            while (
$row = sybase_fetch_assoc($sql)) $tabs[$row['table_name']] = $row['table_name'];
        }
        else {
            if (
$table != 'TABLE') {
               
$sql = sybase_query("EXEC SP_COLUMNS $table;");
                while (
$row = sybase_fetch_assoc($sql)) $tabs[$row['COLUMN_NAME']] = $row['COLUMN_NAME'];
            }
            else {
               
$sql_ = sybase_query("SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' ORDER BY table_name;");
                while (
$row_ = sybase_fetch_assoc($sql_)) {
                   
$table = $row_['table_name'];
                   
$sql = sybase_query("EXEC SP_COLUMNS $table;");
                    while (
$row = sybase_fetch_assoc($sql)) $tabs[$row_['table_name'] . '.' . $row['COLUMN_NAME']] = $row_['table_name'] . '.' . $row['COLUMN_NAME'];
                }
            }

        }
        return
$tabs;
    }

    function
ret($line) {
       
$ret = '';
       
$line = trim($line);
        if (
substr($line, 0, 3) == 'use') {
           
sybase_query($line);
            if (
substr($line, -1) == ';') $line = substr($line, 0, -1);
           
$this->database = substr($line, 4);
           
$ret = "\n";
        }
        elseif (
$line != '') {
           
$sql = sybase_query($line);
           
$i = 0;
            while (
$row = sybase_fetch_assoc($sql)) {
                foreach (
$row as $key => $val) {
                   
$cols[$key] = $key;
                   
$rows[$i][$key] = $val;
                   
$lkey = strlen($key);
                   
$lval = strlen($val);
                    if (!isset(
$maxl[$key])) $maxl[$key] = 0;
                    if (
$lkey > $maxl[$key]) $maxl[$key] = $lkey;
                    if (
$lval > $maxl[$key]) $maxl[$key] = $lval;
                }
               
$i++;
            }
            foreach (
$cols as $key => $val) $ret .= str_pad($val, $maxl[$key], ' ') . '|';
           
$ret .= "\n";
            foreach (
$rows as $row) {
                foreach (
$row as $key => $val) $ret .= str_pad($val, $maxl[$key], ' ') . '|';
               
$ret .= "\n";
            }
           
$ret .= "\n";
        }
        return
$ret;
    }

}

$link = sybase_connect($argv[1], $argv[2], $argv[3]) or die("no connect !\n");

$kmdsybase = new kmdsybase($argv[1], 'history.txt', 'console.txt');
$kmdsybase->run();

sybase_close($link);

?>