Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • opentrafficshaper/opentrafficshaper
  • eezysol/opentrafficshaper
  • sudesh/opentrafficshaper
  • Yuriy/opentrafficshaper
  • nkukard/opentrafficshaper
  • fcardoso/opentrafficshaper
6 results
Show changes
Commits on Source (104)
Showing
with 6121 additions and 57 deletions
[submodule "opentrafficshaper/plugins/webserver/pages/static/awit-flot-toolkit"]
path = opentrafficshaper/plugins/webserver/pages/static/awit-flot-toolkit
url = https://gitlab.devlabs.linuxassist.net/awit-frameworks/awit-flot-toolkit.git
[submodule "awitpt"]
path = awitpt
url = https://gitlab.devlabs.linuxassist.net/awit-frameworks/awit-perl-toolkit.git
Installing OpenTrafficShaper:
*
* Requirements for OpenTrafficShaper
*
- POE
Debian: libpoe-perl
- Config::IniFiles
Debian: libconfig-inifiles-perl
- DateTime
Debian: libdatetime-perl
- Digest::SHA
Debian: libdigest-sha-perl
- JSON
Debian: libjson-perl
- DBD::MySQL
Debian: libdbd-mysql-perl
- Perl 5.6+
*
* Pull in dependency code
*
./update-git-modules
*
* Next steps
*
1. Edit the config file
2. You can now run opentrafficshaper by using ./opentrafficshaperd --debug --config=opentrafficshaper.conf --fg
3. Browse to it using http://SERVER:8088/
For more configuration information see this link:
http://wiki.opentrafficshaper.org/documentation
# Introduction
# Requirements
## POE
Debian-based: libpoe-perl
Arch-based: perl-poe
## Config::IniFiles
Debian-based: libconfig-inifiles-perl
Arch-based: perl-config-inifiles
## DateTime
Debian-based: libdatetime-perl
Arch-based: perl-datetime
## Digest::SHA
Debian-based: libdigest-sha-perl
Arch-based: perl-digest-sha
## JSON
Debian-based: libjson-perl
Arch-based: perl-json
## DBD::MySQL
Debian-based: libdbd-mysql-perl
Arch-based: perl-dbd-mysql
## HTML::Entities module
Debian-based: libhtml-parser-perl
Arch-based: perl-html-parser
## NetAddr::IP
Debian-based: libnetaddr-ip-perl
Arch-based: perl-netaddr-ip
## URI
Debian: liburi-perl
Arch-based: perl-uri
# Installing OpenTrafficShaper
```
git clone https://gitlab.devlabs.linuxassist.net/opentrafficshaper/opentrafficshaper.git
```
# System Configuration
## Shorewall
If you are using Shorewall, you will need to make a few config changes in '/etc/shorewall/config'.
Change config options...
```
CLEAR_TC=No
TC_ENABLED=No
PROVIDER_BITS=0
```
## Module Options
Add this to /etc/modprobe.d/opentrafficshaper-htb.conf...
```
options sch_htb htb_rate_est=1
```
Rebuild your initramfs (on Arch you can use the below command)...
```
mkinitcpio -P
```
This step requires a reboot.
## Temporary Directories
Create directories...
```
mkdir /var/lib/opentrafficshaper
```
# Statistics Setup
Install MariaDB and initialize with...
```
mysql -u ots -p otsdb < database/stats.sql
```
# Security
At present there is no security provided in terms of the management port 8088. It is suggested using something like a reverse proxy
to provide username/password access or restricting access to a fixed IP.
An example of the Nginx http section can be found in the contrib/ directory.
# Next steps
1. Edit the config file
2. You can now run opentrafficshaper by using ./opentrafficshaperd --debug --config=opentrafficshaper.conf --fg
3. Browse to it using http://SERVER:8080/
For more configuration information see this link:
http://wiki.opentrafficshaper.org/documentation
......@@ -17,3 +17,5 @@
- destination IP address shaping
- accounts for using login/pass to access to service
awitpt @ 932340ef
Subproject commit 932340ef7336dc40bdd3d35d2023bde1310e9066
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
bin/convert-tsql
doxygen/Doxyfile
lib/awitpt/cache.pm
lib/awitpt/db/dbilayer.pm
lib/awitpt/db/dblayer.pm
lib/awitpt/netip.pm
lib/awitpt/util.pm
lib/awitpt/version.pm
lib/SOAP/Transport/HTTPng.pm
LICENSE
Makefile.PL
MANIFEST This list of files
t/isnumber.t
# Makefile
# Copyright (C) 2014, AllWorldIT
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
use warnings;
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'awitpt',
VERSION_FROM => "lib/awitpt/version.pm",
EXE_FILES => [qw( bin/convert-tsql )],
MAN3PODS => { },
);
# vim: ts=4
#!/bin/bash
# Database translation/creation script
# Copyright (C) 2009-2014, AllWorldIT
# Copyright (C) 2008, LinuxRulz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
database="$1"
file="$2"
prefix="$3"
# Display usage info
display_usage() {
echo "Usage: $0 <database type> <file> [prefix]"
echo
echo "Valid database types:"
echo " mysql - For MySQL v5 (v5.5)"
echo " mysql55 - For MySQL v5.5"
echo " mysql4 - For MySQL v4"
echo " pgsql - For PostgreSQL"
echo " sqlite - For SQLite v3"
echo
exit
}
# Check we have our params
if [ -z "$database" -o -z "$file" ]
then
display_usage
fi
# Check file exists
if [ ! -f "$file" ]
then
echo "ERROR: Cannot open file '$file'"
exit 1
fi
# Check what we converting for
case "$database" in
"mysql"|"mysql55")
sed \
-e "s/@PREFIX@/$prefix/g" \
-e 's/@PRELOAD@/SET FOREIGN_KEY_CHECKS=0;/' \
-e 's/@POSTLOAD@/SET FOREIGN_KEY_CHECKS=1;/' \
-e 's/@CREATE_TABLE_SUFFIX@/ENGINE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin/' \
-e 's/@SERIAL_TYPE@/SERIAL/' \
-e 's/@BIG_INTEGER_UNSIGNED@/BIGINT UNSIGNED/' \
-e 's/@INT_UNSIGNED@/INT UNSIGNED/' \
-e 's/@TRACK_KEY_LEN@/512/' \
-e 's/@SERIAL_REF_TYPE@/BIGINT UNSIGNED/' < "$file"
;;
"mysql4")
sed \
-e "s/@PREFIX@/$prefix/g" \
-e 's/@PRELOAD@/SET FOREIGN_KEY_CHECKS=0;/' \
-e 's/@POSTLOAD@/SET FOREIGN_KEY_CHECKS=1;/' \
-e 's/@CREATE_TABLE_SUFFIX@/TYPE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin/' \
-e 's/@SERIAL_TYPE@/SERIAL/' \
-e 's/@BIG_INTEGER_UNSIGNED@/BIGINT UNSIGNED/' \
-e 's/@TRACK_KEY_LEN@/255/' \
-e 's/@SERIAL_REF_TYPE@/BIGINT UNSIGNED/' < "$file"
;;
"pgsql")
sed \
-e "s/@PREFIX@/$prefix/g" \
-e 's/@PRELOAD@/SET CONSTRAINTS ALL DEFERRED;/' \
-e 's/@POSTLOAD@//' \
-e 's/@CREATE_TABLE_SUFFIX@//' \
-e 's/@SERIAL_TYPE@/SERIAL PRIMARY KEY/' \
-e 's/@BIG_INTEGER_UNSIGNED@/INT8/' \
-e 's/@TRACK_KEY_LEN@/512/' \
-e 's/@SERIAL_REF_TYPE@/INT8/' < "$file"
;;
"sqlite")
sed \
-e "s/@PREFIX@/$prefix/g" \
-e 's/@PRELOAD@//' \
-e 's/@POSTLOAD@//' \
-e 's/@CREATE_TABLE_SUFFIX@//' \
-e 's/@SERIAL_TYPE@/INTEGER PRIMARY KEY AUTOINCREMENT/' \
-e 's/@BIG_INTEGER_UNSIGNED@/UNSIGNED BIG INT/' \
-e 's/@TRACK_KEY_LEN@/512/' \
-e 's/@SERIAL_REF_TYPE@/INT8/' < "$file"
;;
*)
echo "ERROR: Invalid database type '$database'"
exit 1
;;
esac
# vim: ts=4
This diff is collapsed.
This diff is collapsed.
# Caching engine
# Copyright (C) 2009-2011, AllWorldIT
# Copyright (C) 2008, LinuxRulz
# Copyright (C) 2007 Nigel Kukard <nkukard@lbsd.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package awitpt::cache;
use strict;
use warnings;
require Exporter;
our (@ISA,@EXPORT,@EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT = qw(
cacheStoreKeyPair
cacheStoreComplexKeyPair
cacheGetKeyPair
cacheGetComplexKeyPair
);
@EXPORT_OK = qw(
getCacheHits
getCacheMisses
);
use Cache::FastMmap;
use Storable;
# Cache stuff
my $cache_type = "FastMmap";
my $cache;
# Our current error message
my $error = "";
## @internal
# @fn setError($err)
# This function is used to set the last error for this class
#
# @param err Error message
sub setError
{
my $err = shift;
my ($package,$filename,$line) = caller;
my (undef,undef,undef,$subroutine) = caller(1);
# Set error
$error = "$subroutine($line): $err";
}
## @internal
# @fn Error
# Return current error message
#
# @return Last error message
sub Error
{
my $err = $error;
# Reset error
$error = "";
# Return error
return $err;
}
## @fn Init($server)
# Initialize cache
#
# @param server Server object
# @param params Parameters for the cache
# @li cache_file Filename of the cache file
# @li cache_file_user Owner of the cache file
# @li cache_file_group Group of the cache file
sub Init
{
my ($server,$params) = @_;
my $ch;
# We going to pass these options to new()
my %opt = (
'page_size' => 2048,
'num_pages' => 1000,
'expire_time' => 300,
'raw_values' => 1,
'unlink_on_exit' => 1,
);
# Check if we have the optional $params
if (defined($params)) {
# If we have a special cache file, use it and init it too
if (defined($params->{'cache_file'})) {
$opt{'share_file'} = $params->{'cache_file'};
$opt{'init_file'} = 1;
}
}
# Create Cache
$ch = Cache::FastMmap->new(%opt);
# Check if we have the optional $params
if (defined($params)) {
# If we have an explicit owner set, use it
if (defined($params->{'cache_file_user'})) {
# Check all is ok...
my ($chown_user,$chown_group);
if (!($chown_user = getpwnam($params->{'cache_file_user'}))) {
setError("User '$chown_user' appears to be invalid: $?");
return(-1);
}
if (!($chown_group = getgrnam($params->{'cache_file_group'}))) {
setError("Group '$chown_group' appears to be invalid: $?");
return(-1);
}
# Go in and chown it
if (!chown($chown_user,$chown_group,$opt{'share_file'})) {
setError("Failed to chown cache file '".$opt{'share_file'}."': $!");
return(-1);
}
}
}
# Stats
$ch->set('Cache/Stats/Hit',0);
$ch->set('Cache/Stats/Miss',0);
# Set server vars
$server->{'cache_engine'}{'handle'} = $ch;
};
## @fn Destroy()
# Destroy cache
#
# @param server Server object
sub Destroy
{
};
## @fn connect($server)
# Connect server with the cache
#
# @param server Server object
sub connect
{
my $server = shift;
$cache = $server->{'cache_engine'}{'handle'};
}
## @fn disconnect()
# Disconnect cache from server
#
# @param server Server object
sub disconnect
{
}
## @fn cacheStoreKeyPair($cacheName,$key,$value)
# Store keypair in cache
#
# @param cacheName Cache name to use
# @param key Item key
# @param value Item value
sub cacheStoreKeyPair
{
my ($cacheName,$key,$value) = @_;
if (!defined($cacheName)) {
setError("Cache name not defined in store");
return -1;
}
if ($cacheName eq "") {
setError("Cache name not set in store");
return -1;
}
if (!defined($key)) {
setError("Key not defined for cache '$cacheName' store");
return -1;
}
if (!defined($value)) {
setError("Value not defined for cache '$cacheName' key '$key' store");
return -1;
}
# If we're not caching just return
return 0 if ($cache_type eq 'none');
# Store
$cache->set("$cacheName/$key",$value);
return 0;
}
## @fn cacheGetKeyPair($cacheName,$key)
# Get value from cache
#
# @param cacheName Cache name to use
# @param key Item key
sub cacheGetKeyPair
{
my ($cacheName,$key) = @_;
if (!defined($cacheName)) {
setError("Cache name not defined in get");
return (-1);
}
if ($cacheName eq "") {
setError("Cache name not set in get");
return (-1);
}
if (!defined($key)) {
setError("Key not defined for cache '$cacheName' get");
return (-1);
}
# If we're not caching just return
if ($cache_type eq 'none') {
return (0,undef);
}
# Check and count
my $res = $cache->get("$cacheName/$key");
if ($res) {
$cache->get_and_set('Cache/Stats/Hit',sub { return ++$_[1]; });
} else {
$cache->get_and_set('Cache/Stats/Miss',sub { return ++$_[1]; });
}
return (0,$res);
}
## @fn cacheStoreComplexKeyPair($cacheName,$key,$value)
# Store a complex keypair in cache, this would be an object and
# not a number or text
#
# @param cacheName Cache name to use
# @param key Item key
# @param value Item value
sub cacheStoreComplexKeyPair
{
my ($cacheName,$key,$value) = @_;
my $rawValue = Storable::freeze($value);
if (!defined($rawValue)) {
setError("Unable to freeze cache value in '$cacheName'");
return -1;
}
return cacheStoreKeyPair($cacheName,$key,$rawValue);
}
## @fn cacheGetComplexKeyPair($cacheName,$key)
# Get value from cache
#
# @param cacheName Cache name to use
# @param key Item key
sub cacheGetComplexKeyPair
{
my ($cacheName,$key) = @_;
my ($res,$rawValue) = cacheGetKeyPair($cacheName,$key);
# Thaw out item, if there is no error and we are defined
if (!$res && defined($rawValue)) {
$rawValue = Storable::thaw($rawValue);
}
return ($res,$rawValue);
}
## @fn getCacheHits
# Return cache hits
#
# @return Cache hits
sub getCacheHits
{
my $res;
# Get counter
$res = defined($cache->get('Cache/Stats/Hit')) ? $cache->get('Cache/Stats/Hit') : 0;
return $res;
}
## @fn getCacheMisses
# Return cache misses
#
# @return Cache misses
sub getCacheMisses
{
my $res;
# Get counter
$res = defined($cache->get('Cache/Stats/Miss')) ? $cache->get('Cache/Stats/Miss') : 0;
return $res;
}
1;
# vim: ts=4
# Database independent layer module
# Copyright (C) 2009-2014, AllWorldIT
# Copyright (C) 2008, LinuxRulz
# Copyright (C) 2005-2007 Nigel Kukard <nkukard@lbsd.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
## @class awit::db::dbilayer
# Database independant layer module. This module encapsulates the DBI
# module and provides us with some tweaked functionality
package awitpt::db::dbilayer;
use strict;
use warnings;
use DBI;
# Our current error message
my $error = "";
## @internal
# @fn setError($err)
# This function is used to set the last error for this class
#
# @param err Error message
sub setError
{
my $err = shift;
my ($package,$filename,$line) = caller;
my (undef,undef,undef,$subroutine) = caller(1);
# Set error
$error = "$subroutine($line): $err";
}
## @fn internalError
# Return current module error message
#
# @return Last module error message
sub internalError
{
my $err = $error;
# Reset error
$error = "";
# Return error
return $err;
}
## @member Error
# Return current object error message
#
# @return Current object error message
sub Error
{
my $self = shift;
my $err = $self->{_error};
# Reset error
$self->{_error} = "";
# Return error
return $err;
}
## @fn Init($server,$server_name)
# Initialize class and return a fully connected object
#
# @param server Server object
# @param server_name Name of server
#
# @return dbilayer object, undef on error
sub Init
{
my ($server,$server_name) = @_;
if (!defined($server)) {
setError("Server object undefined");
return;
}
if (!defined($server_name)) {
setError("Server name undefined");
return;
}
my $dbconfig = $server->{$server_name}->{'database'};
# Check if we created
my $dbh = awitpt::db::dbilayer->new($dbconfig);
return if (!defined($dbh));
return $dbh;
}
## @member new($dsn,$username,$password)
# Class constructor
#
# @param settings Database settings hashref
# @li DSN Data source name
# @li Username Username to use
# @li Password Password to use
# @li TablePrefix Table prefix
# @li IgnoreTransactions Flag to ignore transactions
# @li SQLiteJournalMode SQLite: set journal mode
# @li SQLiteCacheSize SQLite: set cache size
# @li SQLiteSynchronous SQLite: set synchronous mode
#
# @return Constructed object, undef on error
sub new
{
my ($class,$settings) = @_;
# Check if we were given settings
if (!defined($settings)) {
setError("No database settings given");
}
# Iternals
my $self = {
_type => undef,
_dbh => undef,
_error => undef,
_dsn => undef,
_username => undef,
_password => undef,
_table_prefix => "",
_in_transaction => undef,
};
# Set database parameters
if (defined($settings->{'DSN'})) {
$self->{_dsn} = $settings->{'DSN'};
$self->{_username} = $settings->{'Username'};
$self->{_password} = $settings->{'Password'};
$self->{_table_prefix} = $settings->{'TablePrefix'} || "";
$self->{transactions_ignore} = $settings->{'IgnoreTransactions'};
$self->{'sqlite_journal_mode'} = $settings->{'SQLiteJournalMode'};
$self->{'sqlite_cache_size'} = $settings->{'SQLiteCacheSize'};
$self->{'sqlite_synchronous'} = $settings->{'SQLiteSynchronous'};
} else {
setError("No DSN provided");
return;
}
# Try grab database type
$self->{_dsn} =~ /^DBI:([^:]+):/i;
$self->{_type} = (defined($1) && $1 ne "") ? lc($1) : "unknown";
# Create...
bless $self, $class;
return $self;
}
## @member connect(@params)
# Return connection to database
#
# @param params DBI parameters
#
# @return 0 on success, < 0 on error
sub connect
{
my $self = shift;
$self->{_dbh} = DBI->connect($self->{_dsn}, $self->{_username}, $self->{_password}, {
'AutoCommit' => 1,
'PrintError' => 0,
'RaiseError' => 0,
'FetchHashKeyName' => 'NAME_lc'
});
# Connect to database if we have to, check if we ok
if (!$self->{_dbh}) {
$self->{_error} = "Error connecting to database: $DBI::errstr";
return -1;
}
# Apon connect we are not in a transaction
$self->{_in_transaction} = 0;
# Check for SQLite options
if ($self->{_type} eq "sqlite") {
# Check for journal mode
if (defined($self->{'sqlite_journal_mode'})) {
if (!$self->do("PRAGMA journal_mode = ".$self->{'sqlite_journal_mode'})) {
return -1;
}
}
# Check for cache size
if (defined($self->{'sqlite_cache_size'})) {
if (!$self->do("PRAGMA cache_size = -".$self->{'sqlite_cache_size'})) {
return -1;
};
}
# Check for synchronous setting
if (defined($self->{'sqlite_synchronous'})) {
if (!$self->do("PRAGMA synchronous = ".$self->{'sqlite_synchronous'})) {
return -1;
}
}
}
return 0;
}
## @member type
# Return database type
#
# @return Database type string
sub type
{
my $self = shift;
return $self->{_type};
}
## @member _check
# Check database connection and reconnect if we lost the connection
sub _check
{
my $self = shift;
# DB is disconnected if _dbh is not defined
if (!defined($self->{_dbh})) {
goto RECONNECT;
}
# Try ping
if (!$self->{_dbh}->ping()) {
# If we not in a transaction try connect
if ($self->{_in_transaction} == 0) {
# Disconnect & reconnect
$self->{_dbh}->disconnect();
goto RECONNECT;
}
$self->{_error} = "Cannot reconnect to DB while inside transaction";
return -1;
}
return 0;
RECONNECT:
return $self->connect();
}
## @member select($query)
# Return database selection results...
#
# @param query SQL query
#
# @return DBI statement handle object, undef on error
sub select
{
my ($self,$query,@params) = @_;
if ($self->_check()) {
return;
}
# Prepare query
my $sth;
if (!($sth = $self->{_dbh}->prepare($query))) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
# Check for execution error
if (!$sth->execute(@params)) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $sth;
}
## @member do($command)
# Perform a command
#
# @param command Command to execute
#
# @return DBI statement handle object, undef on error
sub do
{
my ($self,$command,@params) = @_;
if ($self->_check()) {
return;
}
# # Build single command instead of using binding of params
# # not all databases support binding, and not all support all
# # the places we use ?
# $command =~ s/\?/%s/g;
# # Map each element in params to the quoted value
# $command = sprintf($command,
# map { $self->quote($_) } @params
# );
#use Data::Dumper; print STDERR Dumper($command);
# Prepare query
my $sth;
# if (!($sth = $self->{_dbh}->do($command))) {
if (!($sth = $self->{_dbh}->do($command,undef,@params))) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $sth;
}
## @method lastInsertID($table,$column)
# Function to get last insert id
#
# @param table Table last entry was inserted into
# @param column Column we want the last value for
#
# @return Last inserted ID, undef on error
sub lastInsertID
{
my ($self,$table,$column) = @_;
if ($self->_check()) {
return;
}
# Get last insert id
my $res;
if (!($res = $self->{_dbh}->last_insert_id(undef,undef,$table,$column))) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $res;
}
## @method begin
# Function to begin a transaction
#
# @return 1 on success, undef on error
sub begin
{
my $self = shift;
if ($self->_check()) {
return;
}
$self->{_in_transaction}++;
# Don't really start transaction if we more than 1 deep
if ($self->{_in_transaction} > 1) {
return 1;
}
# Check if we need to ignore transactions
if ($self->{transactions_ignore}) {
return 1;
}
# Begin
my $res;
if (!($res = $self->{_dbh}->begin_work())) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $res;
}
## @method commit
# Function to commit a transaction
#
# @return DBI layer result, or 1 on deep transaction commit
sub commit
{
my $self = shift;
if ($self->_check()) {
return;
}
# Reduce level
$self->{_in_transaction}--;
# If we not at top level, return success
if ($self->{_in_transaction} > 0) {
return 1;
}
# Reset transaction depth to 0
$self->{_in_transaction} = 0;
# Check if we need to ignore transactions
if ($self->{transactions_ignore}) {
return 1;
}
# Commit
my $res;
if (!($res = $self->{_dbh}->commit())) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $res;
}
## @method rollback
# Function to rollback a transaction
#
# @return DBI layer result or 1 on deep transaction
sub rollback
{
my $self = shift;
if ($self->_check()) {
$self->{_in_transaction}--;
return;
}
# If we at top level, return success
if ($self->{_in_transaction} < 1) {
return 1;
}
$self->{_in_transaction} = 0;
# Check if we need to ignore transactions
if ($self->{transactions_ignore}) {
return 1;
}
# Rollback
my $res;
if (!($res = $self->{_dbh}->rollback())) {
$self->{_error} = $self->{_dbh}->errstr;
return;
}
return $res;
}
## @method quote($variable)
# Function to quote a database variable
#
# @param variable Variable to quote
#
# @return Quoted variable
sub quote
{
my ($self,$variable) = @_;
return $self->{_dbh}->quote($variable);
}
## @method free($sth)
# Function to cleanup DB query
#
# @param sth DBI statement handle
sub free
{
my ($self,$sth) = @_;
if ($sth) {
$sth->finish();
}
}
# Function to return the table prefix
sub table_prefix
{
my $self = shift;
return $self->{_table_prefix};
}
1;
# vim: ts=4
# Common database layer module
# Copyright (C) 2009-2014, AllWorldIT
# Copyright (C) 2008, LinuxRulz
# Copyright (C) 2005-2007 Nigel Kukard <nkukard@lbsd.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
## @class awitpt::db::dblayer
# Database layer module which makes life a bit esier
package awitpt::db::dblayer;
use strict;
use warnings;
# Exporter stuff
require Exporter;
our (@ISA,@EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(
DBInit
DBConnect
DBSelect
DBDo
DBLastInsertID
DBBegin
DBCommit
DBRollback
DBQuote
DBFreeRes
DBSelectNumResults
DBSelectSearch
hashifyLCtoMC
);
use POSIX qw( strftime );
use Date::Parse;
use awitpt::db::dbilayer;
# Database handle
my $dbh = undef;
# Our current error message
my $error = "";
## @internal
# @fn setError($err)
# This function is used to set the last error for this class
#
# @param err Error message
sub setError
{
my $err = shift;
my ($package,$filename,$line) = caller;
my (undef,undef,undef,$subroutine) = caller(1);
# Set error
$error = "$subroutine($line): $err";
}
## @internal
# @fn Error
# Return current error message
#
# @return Last error message
sub Error
{
my $err = $error;
# Reset error
$error = "";
# Return error
return $err;
}
## @fn DBInit($dbconfig)
# Initialize the database for use
#
# @param DSN Database DSN
# @li DSN - Database DSN
# @li Username - Optional database username
# @li Password - Optional database password
# @li TablePrefix - Optional database table prefix
sub DBInit
{
my $dbconfig = shift;
if (!defined($dbconfig)) {
setError("The dbconfig hash is not defined");
return;
}
if (ref($dbconfig) ne "HASH") {
setError("The dbconfig option is not a hash");
return;
}
if (!defined($dbconfig->{'DSN'})) {
setError("The dbconfig hash does not contain 'DSN'");
return;
}
# Check if we created
$dbh = awitpt::db::dbilayer->new($dbconfig);
return $dbh;
}
## @fn DBInit($dbconfig)
# Initialize the database for use
#
sub DBConnect
{
my $res;
if ($res = $dbh->connect()) {
setError($dbh->Error());
}
return $res;
}
## @fn setHandle($handle)
# Initialize database handle
#
# @param handle Set internal database handle we use
sub setHandle
{
my $handle = shift;
$dbh = $handle;
}
## @fn DBSelect($query,@params)
# Return database selection results...
#
# @param query Query to run
#
# @return DBI statement handle, undef on error
sub DBSelect
{
my ($query,@params) = @_;
my $table_prefix = $dbh->table_prefix();
# Replace table prefix macro
$query =~ s/\@TP\@/$table_prefix/g;
# Prepare query
my $sth;
if (!($sth = $dbh->select($query,@params))) {
setError("Error executing select: ".$dbh->Error());
return;
}
return $sth;
}
## @fn DBDo($command)
# Perform a command
#
# @param command Command to run
#
# @return DBI statement handle, undef on error
sub DBDo
{
my (@params) = @_;
my $table_prefix = $dbh->table_prefix();
# Check type of params we have
if (ref($params[0]) eq 'HASH') {
my $queryHash = $params[0];
my $dbType = $dbh->type();
# Check DB type is defined, if not use *
if (defined($queryHash->{$dbType})) {
@params = @{$queryHash->{$dbType}};
} elsif (defined($queryHash->{'*'})) {
@params = @{$queryHash->{'*'}};
} else {
setError("Error executing, database type in query not fund and no '*' query found");
return;
}
}
# Grab command and data
my $command = shift(@params);
my @data = @params;
# Replace table prefix macro
$command =~ s/\@TP\@/$table_prefix/g;
# Prepare query
my $sth;
if (!($sth = $dbh->do($command,@data))) {
# Remove newlines...
$command =~ s/(\n|\s{2,})/ /g;
setError("Error executing command '$command': ".$dbh->Error());
return;
}
return $sth;
}
## @fn DBLastInsertID($table,$column)
# Function to get last insert id
#
# @param table Table to check
# @param column Column to get last insert on
#
# @return Last insert ID or undef on error
sub DBLastInsertID
{
my ($table,$column) = @_;
my $res;
if (!($res = $dbh->lastInsertID(undef,undef,$table,$column))) {
setError("Error getting last inserted id: ".$dbh->Error());
return;
}
return $res;
}
## @fn DBBegin
# Function to begin a transaction
#
# @return 1 on success, undef on error
sub DBBegin
{
my $res;
if (!($res = $dbh->begin())) {
setError("Error beginning transaction: ".$dbh->Error());
return;
}
return $res;
}
## @fn DBCommit
# Function to commit a transaction
#
# @return 1 on success, undef on error
sub DBCommit
{
my $res;
if (!($res = $dbh->commit())) {
setError("Error committing transaction: ".$dbh->Error());
return;
}
return $res;
}
## @fn DBRollback
# Function to rollback a transaction
#
# @return 1 on success, undef on error
sub DBRollback
{
my $res;
if (!($res = $dbh->rollback())) {
setError("Error rolling back transaction: ".$dbh->Error());
return;
}
return $res;
}
## @fn DBQuote($variable)
# Function to quote a database variable
#
# @param variable Variable to quote
#
# @return Quoted variable
sub DBQuote
{
my $variable = shift;
return $dbh->quote($variable);
}
## @fn DBFreeRes($sth)
# Function to cleanup DB query
#
# @param sth Statement handle to cleanup
sub DBFreeRes
{
my $sth = shift;
if ($sth) {
$sth->finish();
}
}
# Function to get table prefix
sub DBTablePrefix
{
return $dbh->table_prefix();
}
#
# Value Added Functions
#
## @fn DBSelectNumResults($query)
# Return how many results came up from the specific SELECT query
#
# @param query Query to perform, minus "SELECT COUNT(*) AS num_results"
#
# @return Number of results, undef on error
sub DBSelectNumResults
{
my $query = shift;
# Prepare query
my $sth;
if (!($sth = $dbh->select("SELECT COUNT(*) AS num_results $query"))) {
setError("Error executing select: ".$dbh->Error());
return;
}
# Grab row
my $row = $sth->fetchrow_hashref();
if (!defined($row)) {
setError("Failed to get results from a select: ".$dbh->Error());
return;
}
# Pull number
my $num_results = $row->{'num_results'};
$sth->finish();
return $num_results;
}
## @fn DBSelectSearch($query,$search,$filters,$sorts)
# Select results from database and return the total number aswell
#
# @param query Base query
#
# @param search Search hash ref
# @li Filter - Filter based on this...
# [filter] => Array (
# [0] => Array (
# [field] => Name
# [data] => Array (
# [type] => string
# [value] => hi there
# )
# )
# )
# { 'data' => { 'comparison' => 'gt', 'value' => '5', 'type' => 'numeric' }, 'field' => 'ID' }
# @li Start - Start item number, indexed from 0 onwards
# @li Limit - Limit number of results
# @li Sort - Sort by this item
# @li SortDirection - Sort in this direction, either ASC or DESC
#
# @param filters Filter hash ref
# Hash: 'Column' -> 'Table.DBColumn'
#
# @param sorts Hash ref of valid sort criteria, indexed by what we get, pointing to the DB column in the query
# Hash: 'Column' -> 'Table.DBColumn'
#
# @return Number of results, undef on error
sub DBSelectSearch
{
my ($query,$search,$filters,$sorts) = @_;
# Stuff we need to add to the SQL query
my @where; # Where clauses
my $sqlWhere = "";
my $sqlLimit = "";
my $sqlOffset = "";
my $sqlOrderBy = "";
my $sqlOrderByDirection = "";
# Check if we're searching
if (defined($search) && ref($search)) {
# Check it is a hash
if (ref($search) ne "HASH") {
setError("Parameter 'search' is not a hashtable");
return (undef,-1);
}
# Check if we need to filter
if (defined($search->{'Filter'})) {
# We need filters in order to use filtering
if (!defined($filters)) {
setError("Parameter 'search' element 'Filter' requires 'filters' to be defined");
return (undef,-1);
}
# Check type of Filter
if (ref($search->{'Filter'}) ne "ARRAY") {
setError("Parameter 'search' element 'Filter' is of invalid type, it must be an ARRAY'");
return (undef,-1);
}
# Loop with filters
foreach my $item (@{$search->{'Filter'}}) {
my $data = $item->{'data'}; # value, type, comparison
my $field = $item->{'field'};
my $column = $filters->{$field};
# Check if field is in our allowed filters
if (!defined($filters->{$field})) {
setError("Parameter 'search' element 'Filter' has invalid field item '$field' according to 'filters'");
return (undef,-1);
}
# Check data
if (!defined($data->{'type'})) {
setError("Parameter 'search' element 'Filter' requires field data element 'type' for field '$field'");
return (undef,-1);
}
if (!defined($data->{'value'})) {
setError("Parameter 'search' element 'Filter' requires field data element 'value' for field '$field'");
return (undef,-1);
}
# match =, LIKE, IN (
# matchEnd '' or )
my $match;
my $matchEnd = "";
# value is the DBQuote()'d value
my $value;
# Check what type of comparison
if ($data->{'type'} eq "boolean") {
$match = '=';
$value = DBQuote($data->{'value'});
} elsif ($data->{'type'} eq "date") {
# The comparison type must be defined
if (!defined($data->{'comparison'})) {
setError("Parameter 'search' element 'Filter' requires field data element 'comparison' for date field ".
"'$field'");
return (undef,-1);
}
# Check comparison type
if ($data->{'comparison'} eq "gt") {
$match = ">";
} elsif ($data->{'comparison'} eq "lt") {
$match = "<";
} elsif ($data->{'comparison'} eq "eq") {
$match = "=";
} elsif ($data->{'comparison'} eq "ge") {
$match = ">=";
} elsif ($data->{'comparison'} eq "le") {
$match = "<=";
}
# Convert to ISO format
my $unixtime = str2time($data->{'value'});
my @d = localtime($unixtime);
$value = DBQuote(sprintf("%4d-%02d-%02d", $d[5]+1900, $d[4]+1, $d[3]));
} elsif ($data->{'type'} eq "list") {
# Quote all values
my @valueList;
foreach my $i (split(/,/,$data->{'value'})) {
push(@valueList,DBQuote($i));
}
$match = "IN (";
# Join up 'xx','yy','zz'
$value = join(',',@valueList);
$matchEnd = ")";
} elsif ($data->{'type'} eq "numeric") {
# The comparison type must be defined
if (!defined($data->{'comparison'})) {
setError("Parameter 'search' element 'Filter' requires field data element 'comparison' for numeric field ".
"'$field'");
return (undef,-1);
}
# Check comparison type
if ($data->{'comparison'} eq "gt") {
$match = ">";
} elsif ($data->{'comparison'} eq "lt") {
$match = "<";
} elsif ($data->{'comparison'} eq "eq") {
$match = "=";
} elsif ($data->{'comparison'} eq "ge") {
$match = ">=";
} elsif ($data->{'comparison'} eq "le") {
$match = "<=";
}
$value = DBQuote($data->{'value'});
} elsif ($data->{'type'} eq "string") {
$match = "LIKE";
$value = DBQuote("%".$data->{'value'}."%");
}
# Add to list
push(@where,"$column $match $value $matchEnd");
}
# Check if we have any WHERE clauses to add ...
if (@where > 0) {
# Check if we have WHERE clauses in the query
if ($query =~ /\sWHERE\s/i) {
# If so start off with AND
$sqlWhere .= "AND ";
} else {
$sqlWhere = "WHERE ";
}
$sqlWhere .= join(" AND ",@where);
}
}
# Check if we starting at an OFFSET
if (defined($search->{'Start'})) {
# Check if Start is valid
if ($search->{'Start'} < 0) {
setError("Parameter 'search' element 'Start' invalid value '".$search->{'Start'}."'");
return (undef,-1);
}
$sqlOffset = sprintf("OFFSET %i",$search->{'Start'});
}
# Check if results will be LIMIT'd
if (defined($search->{'Limit'})) {
# Check if Limit is valid
if ($search->{'Limit'} < 1) {
setError("Parameter 'search' element 'Limit' invalid value '".$search->{'Limit'}."'");
return (undef,-1);
}
$sqlLimit = sprintf("LIMIT %i",$search->{'Limit'});
}
# Check if we going to be sorting
if (defined($search->{'Sort'})) {
# We need sorts in order to use sorting
if (!defined($sorts)) {
setError("Parameter 'search' element 'Filter' requires 'filters' to be defined");
return (undef,-1);
}
# Check if sort is defined
if (!defined($sorts->{$search->{'Sort'}})) {
setError("Parameter 'search' element 'Sort' invalid item '".$search->{'Sort'}."' according to 'sorts'");
return (undef,-1);
}
# Build ORDER By
$sqlOrderBy = "ORDER BY ".$sorts->{$search->{'Sort'}};
# Check for sort ORDER
if (defined($search->{'SortDirection'})) {
# Check for valid directions
if (lc($search->{'SortDirection'}) eq "asc") {
$sqlOrderByDirection = "ASC";
} elsif (lc($search->{'SortDirection'}) eq "desc") {
$sqlOrderByDirection = "DESC";
} else {
setError("Parameter 'search' element 'SortDirection' invalid value '".$search->{'SortDirection'}."'");
return (undef,-1);
}
}
}
}
# Select row count, pull out "SELECT .... " as we replace this in the NumResults query
(my $queryCount = $query) =~ s/^\s*SELECT\s.*\sFROM/FROM/is;
my $numResults = DBSelectNumResults("$queryCount $sqlWhere");
if (!defined($numResults)) {
return;
}
# Add Start, Limit, Sort, Direction
my $sth = DBSelect("$query $sqlWhere $sqlOrderBy $sqlOrderByDirection $sqlLimit $sqlOffset");
if (!defined($sth)) {
return;
}
return ($sth,$numResults);
}
# Convert a lower case array to mixed case
sub hashifyLCtoMC
{
my ($record,@entries) = @_;
# If we undefined, return
return if (!defined($record));
my $res;
# Loop with each item, assign from lowecase database record to our result
foreach my $entry (@entries) {
$res->{$entry} = $record->{lc($entry)};
}
return $res;
}
1;
# vim: ts=4
This diff is collapsed.
This diff is collapsed.
# Version information
# Copyright (C) 2014, AllWorldIT
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package awitpt::version;
use strict;
use warnings;
our $VERSION = "0.1.x";
1;
# vim: ts=4
# Test harness for code quality
# Copyright (C) 2014, AllWorldIT
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Test::More;
use strict;
use warnings;
use awitpt::util;
is(system("perlcritic --statistics --gentle ."),0);
done_testing();
# vim: ts=4
# Test harness for isNumber
# Copyright (C) 2014, AllWorldIT
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Test::More;
use strict;
use warnings;
use awitpt::util;
is(isNumber(0),undef);
is(isNumber(0,ISNUMBER_ALLOW_ZERO),0);
is(isNumber("000",ISNUMBER_ALLOW_ZERO),0);
is(isNumber("-0",ISNUMBER_ALLOW_ZERO),0);
is(isNumber(-1),undef);
is(isNumber(-1,ISNUMBER_ALLOW_NEGATIVE),-1);
is(isNumber("-1",ISNUMBER_ALLOW_NEGATIVE),-1);
is(isNumber("-01",ISNUMBER_ALLOW_NEGATIVE),-1);
is(isNumber(1),1);
is(isNumber("0001"),1);
is(isNumber("a"),undef);
is(isNumber("0a"),undef);
is(isNumber("a0"),undef);
is(isNumber("-a0"),undef);
is(isNumber("-0a"),undef);
done_testing();
# vim: ts=4
options sch_htb htb_rate_est=1
\ No newline at end of file