Astconf Query Language

AQL (Astconf Query Language) is a SQL-like statement, which can be used to write and read asterisk config files library for PHP in an easy way. 中文版

DESCRIPTION

AQL focuses on making control of asterisk config files easily, fast and reliably. Users can write and read asterisk config files by writing normal SQL-like statements, and no longer need to consider how to write the right settings.

Suggestion:

  • More than 10000 extension users: use AQL to write and read standard config files except for extensions.
  • Less than 10000 extension users: use AQL to write and read all config files including extensions.
  • Less than 1000 extension users: use AQL to write and read all config files including extensions.

CHANGE

  • 2011-06-03 support escape string in key and value of \' \”

SYNOPSIS

<?
include("aql.php");

// to query extension 8001 settings from sip.conf
$aql = new aql;
$aql->set('basedir','/etc/asterisk/');
$db = $aql->query("select * from sip.conf where section='8001'");
print_r($db);
?>

output:
Array
(
    [8888] => Array
        (
            [type] => friend
            .....
        )

)

GUIDE

Get AQL source

get source files from SVN:
>svn export http://darwin.freeiris.org/svn/freeiris/addition/aql/

Installation

Coping inc/ to your script including folder.
Add one line at header of your script within aql.php, example:

<?
require("../inc/aql.php");

$a = new aql();

if (!$a->set('basedir','./')) {
    echo __LINE__.' '.$a->get_error();
}

$a->query(....);
.....
?>

CommandLine Interface

commandline interface looks like mysql monitor.

in aql source folder:
chmod +x aqlcli.php
./aqlcli.php <config_base_path>

example:
./aqlcli.php /etc/asterisk/

Welcome to the AQL command line interface.  Commands end pass Enter key
Your AQL Library version : 1.0 ,   AQL_Confparser Version : 0.3

type 'help' for help.

AQLcli>select * from sip.conf (<-type this)

you will see output data

Methods

$AQL->set(option,value)

set An options to AQL object.

  • basedir : [path] load config files on base directory, you must specify path before all query.
  • autocommit : [true|false] each query command auto commit, tune false to disable auto commit and manual call $aql——>commit(…) to do.
  • reloadwhensave : [true|false]
  • queryresult : [true|false]
  • comment_flags : [#|;] comment char in config files.

$AQL->query(expression)

Query AQL statements to config file and get results.

References with query language in detail please see Query Language Interface Reference

$AQL->commit($filename)

Commit all query command to AQL conf parser engine. System will call this function automatically, unless tune false to the option 'autucommit'.

# system step by step to run two query commands.
$AQL->set('autocommit','false');
$AQL->query("select * from sip.conf");
$AQL->query("delete from sip.conf where section='8001'");
$AQL->commit("sip.conf");

$AQL->commit_clean($filename)

Means clean all commit command. Users will not use this function until tune false to the option 'autocommit'

$AQL->get_error()

Return string including the last error messages from config parser engine.

$AQL->get_affected_rows()

Get the number of affected rows by the last AQL commands.

Query Language Interface Reference

Basic Syntax

  • each asterisk conf file name is SQL table name
  • each section name is SQL table's field “section” and its PRIMARY KEY and its static key in any results
  • when select each dupcopy key will auto-convert to single string separated by ”,”
  • more than one key
  • duplication of key name in asterisk convert in AQL append with [N],example allow=g729 allow=g723 to allow allow[1], but the frist key not changed.
  • if unsection will assign sectionname '[unsection]', in query section = null equal section = '[unsection]'

Expressions

The AQL Query syntax can be summarized as follows:


query from config files:

  SELECT * FROM <config_file>
    [WHERE <condition> [AND <condition> ...]]
    [LIMIT [<offset>,]<count>]

edit keys :

  UPDATE <config_file> SET key=value[,key2=value2]
    [WHERE <condition> [AND <condition> ...]]
    [LIMIT [<offset>,]<count>]
    
add new key :

  ALTER TABLE <config_file> ADD key=value[,key2=value2...] [AFTER(keyname)|BEFORE(keyname)]
    [WHERE <condition> [AND <condition> ...]]
    [LIMIT [<offset>,]<count>]
    
delete key :

  ALTER TABLE <config_file> DROP key,[key2...]
    [WHERE <condition> [AND <condition> ...]]
    [LIMIT [<offset>,]<count>]

add new section and datas:

  INSERT INTO <config_file> SET key=value[,key2=value2]
  
delete section :

  DELETE FROM <config_file>
    [WHERE <condition> [AND <condition> ...]]
    [LIMIT [<offset>,]<count>]

use other path(like $aql->set('basedir',path)) :

  USE <path>
  

As with SQL, AQL keywords are case insensitive. but table-name and field and value are case sensitive.

Condition

Only key name “section =” can be supported, because “section” is unique in asterisk config files.

The optional WHERE clause filters the result set to those entities that meet one or more conditions. Each condition compares a property of the entity with a value using a comparison operator and multiple conditions are given with the AND keyword. AQL does not have an ”“OR”” operator.

# query results from 8001 to 8004 extensions.
where section >= 8001 and section <= 8004
# query results from unsection in any files.
where section = ''

Operators

comparison digit : = != < > <= >=
match string :
like '%string'
like 'string%'
like '%string%'

Note: like '%string%' is very slowly.

Limit

An optional LIMIT clause causes the query to stop returning results after the first count entities. The LIMIT can also include an offset to skip that many results to find the first result to return. An optional OFFSET clause can specify an offset if no LIMIT clause is present.

limit 10
limit 10,10

Function Based Interface Reference

Function based interface inheriting aql_confparser.php. that advanced class, for the beginnger we suggest try to Query Language Interface.

get_version()

return the version of aql_confparser.php

get_database()

return the full data from database variable.

Note: database variable include all of config files.

get_config_array($filename)

return parsed conf from database.

get_commit_assign($filename)

return commit assigned list.

open_config_file($filename)

open config file from filename

Note: database variable include all of config files.

open_reload($filename)

try to reload and reparse config with filename.

save_config_file($filename)

do commited change and save to config file.

check_database_exists($filename)

checking database's filename exists, return true or false.

free_database()

set database variable null.

assign_append($section,$key,$value,$position=null,$match_key=null)

assign append data around with section name.

$position:

  • null : append to end of section, or before space line in section.
  • before : append before keyname must set 'match_keyname'
  • after : append after keyname must set 'match_keyname'

assign_delkey($section,$key)

assign delkey from section in config files.

assign_addsection($section,$datachunk)

add section with special name and data chunk.

assign_delsection($section)

drop section with section name.

FAQ

AUTHOR

COPYRIGHT

Copyright © 2010, Fonoirs CO.,LTD.

You may distribute under the terms of either the GNU General Public License VERSION 2.

企业级GXW4108八FXO语音网关

本产品有8个FXO口可以连接八条外线
厂商: 北京开源方诺科技有限公司  编号:   质保: 0月  查看详细
 此产品为第三方赞助商向您推荐.
Freeiris爱好者QQ群  43185599
astconfquerylanguage.txt · Last modified: 2011/09/07 12:19 by hoowa
Copyright © 2010 Freeiris DevelopTeam All Rights Reserved