// Copyright (c) 2003 Juraj Kolesár (koli) <koli@koli.sk>
#include "lt.h"
/* data init {{{ **************************************************************/
int lt_c_person_data_init(T_PERSON *person) {
register int i;
SET_CELL(person->pk, "", "pk_c_person", 0);
SET_CELL(person->name, "", "name", 10);
SET_CELL(person->surname, "", "surname", 20);
SET_CELL(person->nickname, "", "nickname", 15);
person->company.table = NULL;
person->address.table = NULL;
person->phone.table = NULL;
person->email.table = NULL;
person->account.table = NULL;
person->notes.table = NULL;
for (i=0;i<__LT_C_PERSON_PARAM_COUNT;i++) person->status[i] = false;
return(__LT_OK);
}
/* }}} */
/* data get {{{ ***************************************************************/
int lt_c_person_data_get(T_DB *tdb, T_PERSON *person) {
if (!(person->status[__LT_C_PERSON_NAME])) {
tdb_query_cell(tdb, person->name.value, g_strdup_printf(
"SELECT name FROM c_person WHERE pk_c_person='%s'", person->pk.value));
person->status[__LT_C_PERSON_NAME] = true;
}
if (!(person->status[__LT_C_PERSON_SURNAME])) {
tdb_query_cell(tdb, person->surname.value, g_strdup_printf(
"SELECT surname FROM c_person WHERE pk_c_person='%s'", person->pk.value));
person->status[__LT_C_PERSON_SURNAME] = true;
}
if (!(person->status[__LT_C_PERSON_NICKNAME])) {
tdb_query_cell(tdb, person->nickname.value, g_strdup_printf(
"SELECT nickname FROM c_person WHERE pk_c_person='%s'", person->pk.value));
person->status[__LT_C_PERSON_NICKNAME] = true;
}
if (!(person->status[__LT_C_PERSON_COMPANY])) {
tdb_table_fill(tdb, &(person->company), g_strdup_printf(
"SELECT pk_c_company, name, note "
"FROM c_company "
"INNER JOIN lists_c "
"ON fk_contact=pk_c_company "
"WHERE fk_table='%s' "
"AND c_contact='COMPANY' "
"AND c_table='PERSON'",
person->pk.value));
person->status[__LT_C_PERSON_COMPANY] = true;
}
if (!(person->status[__LT_C_PERSON_PHONE])) {
tdb_table_fill(tdb, &(person->phone), g_strdup_printf(
"SELECT pk_c_phone, number, note "
"FROM c_phone "
"INNER JOIN lists_c "
"ON fk_contact=pk_c_phone "
"WHERE fk_table='%s' "
"AND c_table='PERSON' "
"AND c_contact='PHONE'",
person->pk.value));
person->status[__LT_C_PERSON_PHONE] = true;
}
if (!(person->status[__LT_C_PERSON_EMAIL])) {
tdb_table_fill(tdb, &(person->email), g_strdup_printf(
"SELECT pk_c_email, url, note "
"FROM c_email "
"INNER JOIN lists_c "
"ON fk_contact=pk_c_email "
"WHERE fk_table='%s' "
"AND c_table='PERSON' "
"AND c_contact='EMAIL'",
person->pk.value));
person->status[__LT_C_PERSON_EMAIL] = true;
}
if (!(person->status[__LT_C_PERSON_ADDRESS])) {
tdb_table_fill(tdb, &(person->address), g_strdup_printf(
"SELECT pk_c_email, url, note "
"FROM c_address "
"INNER JOIN lists_c "
"ON fk_contact=pk_c_address "
"WHERE fk_table='%s' "
"AND c_table='PERSON' "
"AND c_contact='ADDRESS'",
person->pk.value));
person->status[__LT_C_PERSON_EMAIL] = true;
}
return(__LT_OK);
}
/* }}} */
/* data set {{{ ***************************************************************/
int lt_c_person_data_set(T_DB *tdb, T_PERSON *person) {
if (!(person->status[__LT_C_PERSON_PK])) {
tdb_query(tdb, g_strdup_printf(
"INSERT INTO c_person (name, surname, nickname) "
"VALUES ('%s', '%s', '%s')",
person->name.value,
person->surname.value,
person->nickname.value));
tdb_query_lastid(tdb, person->pk.value);
log("pk je: %s",person->pk.value);
person->status[__LT_C_PERSON_PK] = true;
person->status[__LT_C_PERSON_NAME] = true;
person->status[__LT_C_PERSON_SURNAME] = true;
person->status[__LT_C_PERSON_NICKNAME] = true;
}
if (!(person->status[__LT_C_PERSON_NAME])) {
tdb_query(tdb, g_strdup_printf(
"UPDATE c_person SET name='%s' WHERE pk_c_person='%s'",
person->name.value, person->pk.value));
person->status[__LT_C_PERSON_NAME] = true;
}
if (!(person->status[__LT_C_PERSON_SURNAME])) {
tdb_query(tdb, g_strdup_printf(
"UPDATE c_person SET surname='%s' WHERE pk_c_person='%s'",
person->surname.value, person->pk.value));
person->status[__LT_C_PERSON_SURNAME] = true;
}
if (!(person->status[__LT_C_PERSON_NICKNAME])) {
tdb_query(tdb, g_strdup_printf(
"UPDATE c_person SET nickname='%s' WHERE pk_c_person='%s'",
person->nickname.value, person->pk.value));
person->status[__LT_C_PERSON_NICKNAME] = true;
}
// if (!(person->status[__LT_C_PERSON_EMAIL])) {}
// if (!(person->status[__LT_C_PERSON_ADDRESS])) {}
return(__LT_OK);
}
/* }}} */
/* data remove {{{ ************************************************************/
int lt_c_person_data_remove(T_DB *tdb, T_PERSON *person) {
tdb_query(tdb, g_strdup_printf(
"DELETE FROM c_person WHERE pk_c_person='%s'", person->pk.value)
);
return(__LT_OK);
}
/* }}} */
/* data clear {{{ *************************************************************/
int lt_c_person_data_clear(T_PERSON * person) {
register int i;
person->pk.value[0] = '\0';
person->name.value[0] = '\0';
person->surname.value[0] = '\0';
person->nickname.value[0] = '\0';
if (person->company.table != NULL) tdb_table_free(&(person->company));
if (person->address.table != NULL) tdb_table_free(&(person->address));
if (person->phone.table != NULL) tdb_table_free(&(person->phone));
if (person->email.table != NULL) tdb_table_free(&(person->email));
if (person->account.table != NULL) tdb_table_free(&(person->account));
if (person->notes.table != NULL) tdb_table_free(&(person->notes));
for (i=0;i<__LT_C_PERSON_PARAM_COUNT;i++) person->status[i] = false;
return(__LT_OK);
}
/* }}} */
/* tables clear {{{ ***********************************************************/
int lt_c_person_tables_clear(T_PERSON * person) {
register int i;
if (person->company.table != NULL) tdb_table_free(&(person->company));
if (person->address.table != NULL) tdb_table_free(&(person->address));
if (person->phone.table != NULL) tdb_table_free(&(person->phone));
if (person->email.table != NULL) tdb_table_free(&(person->email));
if (person->account.table != NULL) tdb_table_free(&(person->account));
if (person->notes.table != NULL) tdb_table_free(&(person->notes));
for (i=__LT_C_PERSON_COMPANY;i<__LT_C_PERSON_PARAM_COUNT;i++)
person->status[i] = false;
return(__LT_OK);
}
/* }}} */
/* qs init {{{ ****************************************************************/
int lt_c_person_qs_init(T_PERSON *person, T_COLS **cols, T_QS *qs) {
*cols = g_slist_append(*cols, &(person->pk));
*cols = g_slist_append(*cols, &(person->name));
*cols = g_slist_append(*cols, &(person->surname));
*cols = g_slist_append(*cols, &(person->nickname));
if (strlen(qs->columns) == 0) tdb_qs_set_columns(qs, *cols);
if (strlen(qs->from) == 0) strcpy(qs->from, "c_person");
return(__LT_OK);
}
/* }}} */
Platon Group <platon@platon.sk> http://platon.sk/
|