use db binarie
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
bc0f4de5e4
commit
3d79a4b069
1 changed files with 18 additions and 55 deletions
73
lib/db.sh
73
lib/db.sh
|
|
@ -1,73 +1,36 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
FILE_TRASH="/dev/null"
|
BINARY="/opt/two/bin/db"
|
||||||
DB_PATH="./data/"
|
|
||||||
|
function use_db {
|
||||||
|
db_use="${1}"
|
||||||
|
shift 1
|
||||||
|
|
||||||
|
${BINARY} "${db_use}" "$@"
|
||||||
|
return $?
|
||||||
|
}
|
||||||
|
|
||||||
# errors :
|
|
||||||
# 1 file do not exist
|
|
||||||
# 2 entry do not exist
|
|
||||||
#
|
|
||||||
# ok :
|
|
||||||
# 0 entry exist
|
|
||||||
function check_in_db {
|
function check_in_db {
|
||||||
|
use_db "check_in_db" "$@"
|
||||||
local db_name="${1}"
|
return $?
|
||||||
local search_id="${2}"
|
|
||||||
|
|
||||||
[ -f "${DB_PATH}${db_name}.db" ] || return 1
|
|
||||||
cat "${DB_PATH}${db_name}.db" | \
|
|
||||||
grep -E "^${search_id}" > "${FILE_TRASH}" || return 2
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_in_db {
|
function add_in_db {
|
||||||
|
use_db "add_in_db" "$@"
|
||||||
local db_name="${1}"
|
return $?
|
||||||
shift 1
|
|
||||||
|
|
||||||
local IFS=";"
|
|
||||||
echo "$*" >> ${DB_PATH}${db_name}.db
|
|
||||||
return 0
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete_in_db {
|
function delete_in_db {
|
||||||
|
use_db "delete_in_db" "$@"
|
||||||
local db_name="${1}"
|
return $?
|
||||||
local id="${2}"
|
|
||||||
|
|
||||||
[ "$(uname)" == "Darwin" ] \
|
|
||||||
&& sed "/^${id}/d" "${DB_PATH}${db_name}.db" -i '' \
|
|
||||||
|| sed "/^${id}/d" "${DB_PATH}${db_name}.db" -i
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# errors :
|
|
||||||
# 0 file do not exist
|
|
||||||
#
|
|
||||||
# ok :
|
|
||||||
# [1-500] entry count
|
|
||||||
function count_in_db {
|
function count_in_db {
|
||||||
|
count=$(use_db "count_in_db" "$@")
|
||||||
local db_name="${1}"
|
|
||||||
local id="${2}"
|
|
||||||
|
|
||||||
[ -f "${DB_PATH}${db_name}.db" ] || return 0
|
|
||||||
count=$(cat "${DB_PATH}${db_name}.db" | grep "${id}" | wc -l | sed -e 's/ //g')
|
|
||||||
|
|
||||||
return "${count}"
|
return "${count}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_from_db {
|
function get_from_db {
|
||||||
local db_name="${1}"
|
use_db "get_from_db" "$@"
|
||||||
local id="${2}"
|
return $?
|
||||||
|
|
||||||
[ -f "${DB_PATH}${db_name}.db" ] || return 0
|
|
||||||
|
|
||||||
cat "${DB_PATH}${db_name}.db" | grep "${id}"
|
|
||||||
return "$?"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue