what is the replacement for REGEXP_REPLACE(XXXXXX,' ', '') ; in sqlite? [duplicate] - sqlite

I installed REGEX support with
apt-get install sqlite3 sqlite3-pcre
now I can use REGEX in my queries on the bash console like
DB="somedb.db"
REGEX_EXTENSION="SELECT load_extension('/usr/lib/sqlite3/pcre.so');"
sqlite3 $DB "$REGEX_EXTENSION select * from sometable where name REGEXP '^[a-z]+$'"
But how can I update a string with an sqlite query using regex?

Sqlite by default does not provide regex_replace function. You need to load it as an extension. Here is how i managed to do it.
Download this C code for the extension (icu_replace)
Compile it using
gcc --shared -fPIC -I sqlite-autoconf-3071100 icu_replace.c -o icu_replace.so
And in sqlite3 runn following command post above mentioned command has run and create a file icu_replace.so
SELECT load_extension(' path to icu_replace.so', 'sqlite3_extension_init') from dual;
After this you will be able to use such a function as :-
select regex_replace('\bThe\b',x,'M') from dual;

The following builds latest sqlite with dynamic library support, and compiles ICU extension and regex_replace extension. It also assumes debian-based linux distributive:
sudo apt build-dep sqlite3 # fetches dependencies to compile sqlite3
mkdir sqlite-compilation
cd sqlite-compilation
wget -O sqlite.tar.gz https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release
tar xzf sqlite.tar.gz
mkdir build
cd build
../sqlite/configure
make OPTS='-DSQLITE_ENABLE_LOAD_EXTENSION'
./sqlite3 -cmd 'pragma compile_options;' <<< .exit
cd -
# https://sqlite.org/src/dir?name=ext/icu
cd sqlite/ext/icu
sed -i 's/int sqlite3_icu_init(/int sqlite3_extension_init(/' icu.c
sed -i 's/int sqlite3IcuInit(/int sqlite3_extension_init(/' sqliteicu.h
gcc -g -O2 -shared icu.c -fPIC -I ../../../build `pkg-config --libs icu-i18n` -o libSqlite3Icu.so
cp libSqlite3Icu.so ../../../build/
cd -
# https://github.com/gwenn/sqlite-regex-replace-ext
cd sqlite/ext
wget -O sqlite-regex-replace-ext-master.zip https://github.com/gwenn/sqlite-regex-replace-ext/archive/master.zip
unzip sqlite-regex-replace-ext-master.zip
cd sqlite-regex-replace-ext-master
gcc -g -O2 -shared icu_replace.c -fPIC -I ../../../build -o libSqlite3IcuReplace.so
cp libSqlite3IcuReplace.so ../../../build/
cd -
cd ../../
In result you will have:
build/sqlite3 # sqlite3 binary
build/libSqlite3Icu.so # unicode support
build/libSqlite3IcuReplace # regex_replace function
Test:
cd build
sqlite3 <<< "
.load ./libSqlite3Icu
.load ./libSqlite3IcuReplace
select regex_replace('^a', 'aab', 'b');
.exit
" # should output: bab
cd -

For me the above answers didn't work because of some missing parameters in the gcc command.
This works for me:
git clone https://github.com/gwenn/sqlite-regex-replace-ext.git
cd sqlite-regex-replace-ext-master/
./icu_replace.sh
Now you should be able to load the extension using: SELECT LOAD_EXTENSION('path-to-icu_replace.so');

Related

Debian Packaging Without Build Tool

I want to create a Debian package from a C program without the use of a build tool such as autotools or CMake. My debian/rules file:
#!/usr/bin/make -f
%:
dh $#
override_dh_auto_clean:
rm -f program
override_dh_auto_build:
gcc program.c -o program
override_dh_auto_install:
cp program /usr/local/bin
Upon running dpkg-buildpackage, I get:
dh: error: Unknown sequence application (choose from: binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep)
It seems the issue was related to the fact that I was creating the file in a shell script heredoc that was expanding the $#, e.g.
cat <<EOF > debian/rules.temp
#!/usr/bin/make -f
%:
dh $#
EOF
Which should be:
all_symbol='$#'
cat <<EOF > debian/rules.temp
#!/usr/bin/make -f
%:
dh $all_symbol
EOF
An unrelated issue is to the location of the override_dh_auto_install To manually create Debian file hierarchy it should be:
override_dh_auto_install:
mkdir -p debian/PACKAGENAME/usr/bin
cp program debian/PACKAGENAME/usr/bin
Or, to have this done automatically:
cat <<EOF > debian/PACKAGENAME.install
program usr/bin
EOF

Compile libgnat to a single LLVM bitcode file

How can I compile libgnat to a single LLVM bitcode file? The latest dragonegg release is very old, so I provide a dockerfile to make testing more easy. My end goal is to run Ada in LLVM IR bitcode interpreters.
Dockerfile for the latest official dragonegg release
FROM ubuntu:trusty
COPY . /usr/src/workdir
WORKDIR /usr/src/workdir
RUN apt-get update \
&& apt-get -y install build-essential gnat-4.6 libgmp-dev libmpfr-dev libmpc-dev libz-dev gcc-4.6-plugin-dev
# libz-dev for ld when compiling dragonegg 3.3
# gcc-4.6-plugin-dev needed when compiling dragonegg 3.3
RUN tar -xzf gcc-4.6.4.tar.gz \
&& cd gcc-4.6.4 \
&& mkdir build \
&& cd build \
&& CC=gcc-4.6 ../configure --disable-multilib --enable-languages=ada,c,c++ --prefix=/opt/gcc-4.6.4 \
&& make -j4 \
&& make install
RUN tar -xzf clang+llvm-3.3-amd64-Ubuntu-12.04.2.tar.gz \
&& mv clang+llvm-3.3-amd64-Ubuntu-12.04.2 /opt/llvm-3.3
ENV PATH="/opt/llvm-3.3/bin:/opt/gcc-4.6.4/bin:${PATH}"
RUN tar -xzf dragonegg-3.3.src.tar.gz \
&& mv dragonegg-3.3.src dragonegg-3.3 \
&& cd dragonegg-3.3 \
&& GCC=/opt/gcc-4.6.4/bin/gcc make \
&& cp dragonegg.so /opt/dragonegg.so
download gcc-4.6.4.tar.gz
download clang+llvm-3.3-amd64-Ubuntu-12.04.2.tar.gz
download dragonegg-3.3.src.tar.gz
hello.adb
with Ada.Text_IO;
procedure Hello is
begin
Ada.Text_IO.Put_Line("Hello world from Ada (dragonegg)!");
end Hello;
Run gcc hello.adb -S -O1 -o hello.ll -fplugin=/opt/dragonegg.so -fplugin-arg-dragonegg-emit-ir to compile the hello.adb file. When I try to build the binary with llc -filetype=obj hello.ll and gcc hello.o, I get the following error:
/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
hello.o: In function `_ada_hello':
hello.ll:(.text+0xb): undefined reference to `ada__text_io__put_line__2'
collect2: ld returned 1 exit status
The error message indicates that the Ada runtime library is missing. Currently, I have no idea, how I can compile the libgnat to a single LLVM bitcode file, so I can link it with the program.
Why are you using DragonEgg? That's well and truly dead! See https://github.com/AdaCore/gnat-llvm instead.

How to make gnatmake tool compile RTS with project?

Is there a way to make gnatmake tool recompile the Ada's runtime library ("RTS") with the project I'm building file by file? I want to integrate my custom preprocessor that adds some features to ada source code and then compiles it with gcc. I'm passing --GCC=<preprocessor> flag to the gnatmake utility, it figures out dependencies automatically and runs my preprocessor for all of my source files. However I want custom preprocessing to be done on code in the RTS as well, is there any way to do it?
The -a flag tells gnatmake to recompile any RTS files that need to be recompiled.
After a little experiment here it seems that if you copy system.ads (and maybe gnat.ads, interfac.ads .. yes) from the compiler’s adainclude/ directory to your source tree (I think you’ll need to touch it whenever you update your preprocessor), gnatmake may be able do what you want.
$ cp /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/system.ads .
$ gnatmake -a int
gcc -c int.adb
gcc -gnatpg -c -I./ -I- /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/s-stalib.adb
gcc -gnatpg -c -I./ -I- /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/a-except.adb
gcc -gnatpg -c -I./ -I- /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/s-valint.adb
gcc -gnatpg -c system.ads
...
gcc -gnatpg -c -I./ -I- /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/s-conca4.adb
gnatbind -x int.ali
gnatlink int.ali
and, after I realised I hadn’t used -gnata,
$ touch int.adb
$ gnatmake -a -gnata int
gcc -c -gnata int.adb
gcc -gnatpg -c -I./ -gnata -I- /opt/gcc-4.9.0/lib/gcc/x86_64-apple-darwin13/4.9.0/adainclude/s-assert.adb
gnatbind -x int.ali
gnatlink int.ali
(I do not want to use -f here, because it’ll rebuild everything.)

what is wrong with my makefile code?

I have a makefile that I needed to change around, and use it to install the program. I can;t figure out why I am getting this error:
v245-2% make install
install -m 555 audit /export/home/student/scort323/bin
sh: install: not found
*** Error code 1
make: Fatal error: Command failed for target `install'
Below is my code, can somebody please give me some advice. I am not good at makefiles so trying to find this error is hard for me until I get a better understanding:
# Make file for audit
# Location to install binary. Default is /usr/local/bin. You may
# prefer to install it in /usr/bin or /sbin
BINDIR = /export/home/student/scort323/bin
#BINDIR=/usr/bin
#BINDIR=/usr/sbin
# Location to install man page. Default is /usr/local/man. You may
# prefer to install it in /usr/man
MANDIR = /export/home/student/scort323/bin
#MANDIR = /usr/man
# Compiler to use
CC = gcc
# Linker to use
LD = gcc
# Preprocessor options
CPPFLAGS = -DGETOPTLONG
# Compile and link options
# On a.out systems you might want to add -N when linking
# RPM_OPT_FLAGS can be set by rpm tool
# ...For production code
CFLAGS = -Wall -O3 $(RPM_OPT_FLAGS)
LDFLAGS = -s
# ...For debug
#CFLAGS = -Wall -g
#LDFLAGS = -g
audit: audit.o
$(LD) $(LDFLAGS) -o audit audit.o
audit.o: audit.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c audit.c
install: audit
install -m 555 audit $(BINDIR)
#/audit
install -m 444 audit.1 $(MANDIR)
#/man1/audit.1
clean:
$(RM) audit audit.o core *~ results
# check in
ci: clean
-ci -l *
dist: clean
cd .. ; tar --exclude RCS -czvf audit-0.2.tar.gz audit-0.2
There is no problem with the makefile. Check if you have install utility
$~ install --help
If you dont have then you can get it from GNU coreutils. If you have install somewhere then export its path in PATH variable
export PATH=$PATH:/path/to/install-utilit

How to redirect gcc errors to a file in Unix?

I tried following this but that doesn't work in Unix.
I've been running this command :
gcc temp.c -o temp.o 2> err.txt
And got the following error:
gcc: 2: No such file or directory
Any ideas what that shouldn't work? Maybe because I’m using it over a Unix server?
In tcsh you don't use 2> to redirect stderr, you use >& that will redirect both stdout and stderr. If you want to redirect them separately, have a look at this quick guide.
Redirecting stderr using 2> in tsch is not possible, but here are 2 work arounds
bash -c "gcc temp.c -o temp.o 2> err.txt" #if bash or sh is available
(gcc temp.c -o temp.o > /dev/null) > & err.txt
Yours:
gcc temp.c -o temp.o 2> err.txt
you sure mean:
gcc -o temp.o temp.c 2> err

Resources