Cross reference for modules/qi/query_instructions.c of index.

# File modules/qi/query_instructions.c

      $Revision: 1.49 $

      Sql module (sq).  This is a mysql implementation of an sql module.

      Status: NOT REVUED, NOT TESTED

      Note: this code has been heavily coupled to MySQL, and may need to be changed
      (to improve performance) if a new RDBMS is used.

## Included Files

- `#include <stdio.h>`

- `#include <string.h>`

- `#include <glib.h>`

- `#include "include/which_keytypes.h"`
  - `#include "include/bitmask.h"`

- `#include "modules/qi/query_instructions.h"`
  - `#include "include/rxroutines.h"`
    - `#include <glib.h>`
    - `#include <pthread.h>`
    - `#include <include/stubs.h>`
    - `#include <include/memwrap.h>`
    - `#include <stdio.h>`
    - `#include <include/iproutines.h>`
    - `#include <erroutines.h>`
    - `#include "include/socket.h"`
      - `#include <sys/types.h>`
      - `#include <sys/socket.h>`
      - `#include <netinet/in.h>`
      - `#include <stdlib.h>`
      - `#include <errno.h>`
      - `#include <netdb.h>`
      - `#include <signal.h>`
      - `#include <stdio.h>`
      - `#include <iproutines.h>`
    - `#include "include/thread.h"`
      - `#include <pthread.h>`
  - `#include "include/iproutines.h"`
  - `#include "include/mysql_driver.h"`
    - `#include "/opt/package/mysql/include/mysql/mysql.h"`
      - `#include <sys/types.h>`
      - `#include "/opt/package/mysql/include/mysql/mysql_com.h"`
      - `#include "/opt/package/mysql/include/mysql/mysql_version.h"`
    - `#include "/opt/package/mysql/include/mysql/mysqld_error.h"`
  - `#include "include/query_command.h"`
    - `#include <glib.h>`
    - `#include "include/bitmask.h"`
    - `#include "include/socket.h"`
  - `#include "include/defs.h"`
    - `#include <glib.h>`
    - `#include "include/bitmask.h"`
    - `#include "include/DF_class_enum.def"`
    - `#include "include/DF_class_mask.def"`
    - `#include "include/DF_attribute_enum.def"`
    - `#include "include/DF_attribute_inv_attr_mask.def"`
    - `#include "include/which_keytypes.h"`
    - `#include "include/iproutines.h"`
    - `#include "include/rxroutines.h"`
  - `#include "include/which_keytypes.h"`
  - `#include "include/access_control.h"`
    - `#include "include/erroutines.h"`
    - `#include "include/iproutines.h"`
    - `#include "include/rxroutines.h"`
    - `#include "include/mysql_driver.h"`
  - `#include "include/ca_configFns.h"`
    - `#include "include/ca_defs.h"`
      - `#include <pthread.h>`
      - `#include <glib.h>`
  - `#include "include/ca_dictSyms.h"`
  - `#include "include/ca_macros.h"`
    - `#include "include/ca_adminAttribs.h"`
  - `#include "include/ca_srcAttribs.h"`

- `#include "include/mysql_driver.h"`

- `#include "include/rp.h"`
  - `#include <stddef.h>`
  - `#include <glib.h>`
  - `#include <defs.h>`
  - `#include <iproutines.h>`
  - `#include <erroutines.h>`
  - `#include <rxroutines.h>`
  - `#include <mysql_driver.h>`
  - `#include "include/ca_defs.h"`

- `#include "include/stubs.h"`

- `#include "include/constants.h"`
  - `#include <glib.h>`

- `#include "include/memwrap.h"`

- `#include "include/wh_queries.h"`

- `#include "include/defs.h"`

## Preprocessor definitions

String sizes

`#define STR_S 63`

`#define STR_M 255`

`#define STR_L 1023`

`#define STR_XL 4095`

`#define STR_XXL 16383`

## Global Variable Lock

`pthread_mutex_t Lock`

Included from | include/ca_defs.h
Visible in: |  | modules/ac/access_control.c
|  | modules/ca/ca_configFns.c
|  | modules/ca/ca_values.c
|  | modules/pm/pm_serials.c
|  | modules/pm/protocol_mirror.c
|  | modules/qc/query_command.c
|  | modules/qi/query_instructions.c
Used in: | ca_get_adminIntElement() | modules/ca/ca_configFns.c
| ca_get_adminStrElement() | modules/ca/ca_configFns.c
| ca_get_boolean() | modules/ca/ca_configFns.c
| ca_get_dirlist() | modules/ca/ca_configFns.c
| ca_get_int() | modules/ca/ca_configFns.c
| ca_get_string() | modules/ca/ca_configFns.c
| ca_set_boolean() | modules/ca/ca_configFns.c
| ca_set_string() | modules/ca/ca_configFns.c

## Global Variable newPort

`char newPort[16]`

Included from | include/ca_defs.h
Visible in: |  | modules/ac/access_control.c
|  | modules/ca/ca_configFns.c
|  | modules/ca/ca_values.c
|  | modules/pm/pm_serials.c
|  | modules/pm/protocol_mirror.c
|  | modules/qc/query_command.c
|  | modules/qi/query_instructions.c

## Global Function QI_execute()

      Execute the query instructions.  This is called for each source.

      void *database_voidptr Pointer to the database name

      void *qis_voidptr Pointer to the query_instructions.

      More:

      Authors:
            ottrey

`er_ret_t QI_execute ( ca_dbSource_t* dbhdl, Query_instructions* qis, Query_environ* qe, acc_st* acc_credit, acl_st* acl )`

Prototyped in: | modules/qi/query_instructions.h
Calls: | ER_perror() | modules/er/er.c
| ca_srchandle2Intelement() | modules/ca/ca_configFns.c
| ca_srchandle2Strelement() | modules/ca/ca_configFns.c
| insert_radix_serials() | modules/qi/query_instructions.c
| qi_collect_ids() | modules/qi/query_instructions.c
| qi_fetch_references() | modules/qi/query_instructions.c
| write_objects() | modules/qi/query_instructions.c
| write_radix_immediate() | modules/qi/query_instructions.c
| SK_watchclear(), SK_watchstart(), SK_watchstop(), SQ_close_connection(), SQ_errno(), SQ_error(), SQ_execute_query(), SQ_get_connection(), fprintf(), pthread_self(), sprintf(), wr_real_free()

## Global Function QI_free()

      Free the query_instructions.

      XXX This isn't working too well at the moment.

      More:

      Authors:
            ottrey

`void QI_free ( Query_instructions* qis )`

`Query_instructions* qis`
  Query_instructions to be freed.

Prototyped in: | modules/qi/query_instructions.h
Calls: | instruction_free() | modules/qi/query_instructions.c
| wr_real_free()

## Global Function QI_new()

      Create a new set of query_instructions.

`Query_instructions* QI_new ( const Query_command* qc, const Query_environ* qe )`

`const Query_command* qc`
  The query_command that the instructions are created from.
`const Query_environ* qe`
  The environmental variables that they query is being performed under. More:

        Authors:
              ottrey

Prototyped in: | modules/qi/query_instructions.h
Calls: | ER_dbg_va() | modules/er/er.c
| QI_queries_to_string() | modules/qi/query_instructions.c
| create_query() | modules/qi/query_instructions.c
| map_qc2rx() | modules/qi/query_instructions.c
| valid_query() | modules/qi/query_instructions.c
| fprintf(), wr_real_calloc(), wr_real_free()
References Variables: | Query | modules/df/defs.c

## Global Function QI_queries_to_string()

`char* QI_queries_to_string ( Query_instructions* qis )`

Prototyped in: | modules/qi/query_instructions.h
Calls: | fprintf(), strcat(), strcpy(), strlen(), wr_real_realloc()
Called by: | QI_new() | modules/qi/query_instructions.c
References Variables: | Query | modules/df/defs.c

## Global Function fast_output()

      This is for the '-F' flag.
      It assumes lines starting with ' ', '\t' or '+' belong to the prior attribute.

      Fast isn't fast anymore - it's just there for compatibility reasons.
      This could be speed up if there were breaks out of the loops, once it matched something.
      (Wanna add a goto Marek?  :-) ).

      More:

      Authors:
            ottrey

    Online References:

`char* fast_output ( const char* str )`

`const char* str`
  ng The string to be "fast outputed".

Calls: | DF_get_attribute_code() | modules/df/defs.c
| DF_get_attribute_names() | modules/df/defs.c
| fprintf(), g_strsplit(), sprintf(), strcat(), strchr(), strcpy(), strlen(), strncmp(), wr_real_malloc()
Called by: | write_results() | modules/qi/query_instructions.c

## Global Function filter()

      Basically it's for the '-K' flag for non-set (and non-radix) objects.
      It assumes lines starting with ' ', '\t' or '+' belong to the prior attribute.

      This could be speed up if there were breaks out of the loops, once it matched something.
      (Wanna add a goto Marek?  :-) ).

      More:

      Authors:
            ottrey

    Online References:

`char* filter ( const char* str )`

`const char* str`
  ng The string to be filtered.

Calls: | DF_get_filter_names() | modules/df/defs.c
| fprintf(), g_strsplit(), sprintf(), strcat(), strcpy(), strlen(), strncmp(), wr_real_malloc()
Called by: | write_results() | modules/qi/query_instructions.c

## Global Function run_referral()

`void run_referral ( char* sourcename, MYSQL* sql_connection, Query_instructions* qis, Query_environ* qe, int qi_index, acc_st* acc_credit )`

Calls: | ER_dbg_va() | modules/er/er.c
| SK_cd_printf(), SK_cd_puts(), SQ_execute_query(), SQ_free_result(), SQ_get_column_string(), SQ_num_rows(), SQ_row_next(), WH_sock(), __eprintf(), fprintf(), index(), snprintf(), sprintf(), sscanf(), strcat(), strcmp(), strcpy(), strlen()
Called by: | qi_collect_ids() | modules/qi/query_instructions.c
References Variables: | Query | modules/df/defs.c

## Global Function sql_execute_watched()

`int sql_execute_watched ( sk_conn_st* condat, MYSQL** sql_connection, const char* query, MYSQL_RES** result_ptr )`

Calls: | ER_dbg_va() | modules/er/er.c
| SK_watchclear(), SK_watchexec(), SK_watchstart(), SK_watchstop(), SQ_close_connection(), SQ_duplicate_connection(), SQ_execute_query(), SQ_free_result(), fprintf()
Called by: | qi_collect_ids() | modules/qi/query_instructions.c
| qi_fetch_references() | modules/qi/query_instructions.c
| write_objects() | modules/qi/query_instructions.c
References Functions: | qi_kill_body() | modules/qi/query_instructions.c

## Local Function add_filter()

`static void add_filter ( char* query_str, const Query_command* qc )`

Calls: | MA_bitcount() | modules/ma/bitmask.c
| MA_isset() | modules/ma/bitmask.c
| sprintf(), strcat(), strcpy(), strlen()
Called by: | create_query() | modules/qi/query_instructions.c

## Local Function add_ref_name()

`static void add_ref_name ( MYSQL* sql_connection, char* rectable, char* allnames )`

Calls: | create_name_query() | modules/qi/query_instructions.c
| SQ_execute_query(), fprintf(), sprintf(), strlen()
Called by: | qi_fetch_references() | modules/qi/query_instructions.c

## Local Function create_asblock_query()

     create_asblock_query:

      given a string like: AS1
                           AS1 - AS10
    		       AS1-AS10
      construct a range query for the as_block table

`static int create_asblock_query ( char* query_str, const char* sql_query, const char* keys )`

Calls: | sprintf(), sscanf(), strtok_r(), wr_real_free(), wr_string()
Called by: | create_query() | modules/qi/query_instructions.c

## Local Function create_name_query()

      Create an sql query for the names table.

      More:

      Authors:
      ottrey

    Online References:

`static void create_name_query ( char* query_str, const char* sql_query, const char* keys )`

Calls: | g_strfreev(), g_string_free(), g_string_sized_new(), g_string_sprintfa(), g_strsplit(), sprintf()
Called by: | add_ref_name() | modules/qi/query_instructions.c
| create_query() | modules/qi/query_instructions.c

## Local Function create_query()

      Create an sql query from the query_command and the matching keytype and the
      selected inverse attributes.
      Note this clears the first inv_attribute it sees, so is called sequentially
      until there are no inv_attributes left.

      WK_Type keytype The matching keytype.

      mask_t *inv_attrs_bitmap The selected inverse attributes.

      More:

      Authors:
            ottrey

    Online References:

`static char* create_query ( const Query_t q, const Query_command* qc )`

`const Query_t q`
  &nbs;
`const Query_command* qc`
  The query command.

Calls: | IP_rang_b2_space() | modules/ip/ip.c
| IP_rang_b2v4() | modules/ip/ip.c
| IP_smart_range() | modules/ip/ip.c
| MA_bitcount() | modules/ma/bitmask.c
| NOERR() | modules/er/er.c
| add_filter() | modules/qi/query_instructions.c
| create_asblock_query() | modules/qi/query_instructions.c
| create_name_query() | modules/qi/query_instructions.c
| fprintf(), sprintf(), strcpy(), strlen(), wr_real_malloc()
Called by: | QI_new() | modules/qi/query_instructions.c

## Local Function insert_radix_serials()

      Insert the radix serial numbers into a temporary table in the database.

      mask_t bitmap The bitmap of attribute to be converted.

      SQ_connection_t *sql_connection The connection to the database.

      XXX Hmmmmm this isn't really a good place to free things... infact it's quite nasty.  :-(

      More:

      Authors:
            ottrey

    Online References:

                 <LI><A HREF="http://www.gtk.org/rdp/glib/glib-doubly-linked-lists.html">Glist</A>

`static void insert_radix_serials ( sk_conn_st* condat, MYSQL* sql_connection, char* id_table, GList* datlist )`

`sk_conn_st* condat`
  &nbs;
`MYSQL* sql_connection`
  &nbs;
`char* id_table`
  The id of the temporary table (This is a result of the hacky way we've tried to get MySQL to do sub-selects.)
`GList* datlist`
  The list of data from the radix tree.

Calls: | SQ_execute_query(), fprintf(), g_list_first(), sprintf(), wr_real_clear_list(), wr_real_free()
Called by: | QI_execute() | modules/qi/query_instructions.c

## Local Function instruction_free()

      Free the instruction.

      More:

      Authors:
            ottrey

`static void instruction_free ( Query_instruction* qi )`

`Query_instruction* qi`
  query_instruction to be freed.

Calls: | wr_real_free()
Called by: | QI_free() | modules/qi/query_instructions.c

## Local Function map_qc2rx()

      The mapping between a query_command and a radix query.

      More:

      Authors:
            ottrey

    Online References:

`static int map_qc2rx ( Query_instruction* qi, const Query_command* qc )`

`Query_instruction* qi`
  The Query Instruction to be created from the mapping of the query command.
`const Query_command* qc`
  The query command to be mapped.

Calls: | ER_dbg_va() | modules/er/er.c
Called by: | QI_new() | modules/qi/query_instructions.c

## Local Function qi_collect_ids()

`static void qi_collect_ids ( ca_dbSource_t* dbhdl, char* sourcename, MYSQL** sql_connection, Query_instructions* qis, Query_environ* qe, char* id_table, GList** datlist, acc_st* acc_credit, acl_st* acl )`

Calls: | AC_get_higher_limit() | modules/ac/access_control.c
| ER_dbg_va() | modules/er/er.c
| ER_inf_va() | modules/er/er.c
| ER_is_traced() | modules/er/er.c
| ER_perror() | modules/er/er.c
| NOERR() | modules/er/er.c
| run_referral() | modules/qi/query_instructions.c
| sql_execute_watched() | modules/qi/query_instructions.c
| RP_asc_search(), RP_new_asc_search(), SQ_errno(), SQ_error(), SQ_execute_query(), SQ_get_affected_rows(), fprintf(), g_list_length(), sprintf(), strcpy()
Called by: | QI_execute() | modules/qi/query_instructions.c
References Variables: | Query | modules/df/defs.c

## Local Function qi_fetch_references()

`static void qi_fetch_references ( MYSQL** sql_connection, Query_environ* qe, char* id_table, acc_st* acc_credit, acl_st* acl )`

Calls: | add_ref_name() | modules/qi/query_instructions.c
| sql_execute_watched() | modules/qi/query_instructions.c
| SQ_execute_query(), SQ_free_result(), SQ_get_column_string(), SQ_row_next(), atoi(), fprintf(), sprintf(), strcat(), wr_real_free()
Called by: | QI_execute() | modules/qi/query_instructions.c

## Local Function qi_kill_body()

`static void* qi_kill_body ( void* arg )`

Calls: | ER_dbg_va() | modules/er/er.c
| SQ_abort_query()
Used in: | sql_execute_watched() | modules/qi/query_instructions.c

## Local Function valid_query()

      Determine if this query should be conducted or not.

      If it was an inverse query - it the attribute appears in the query command's bitmap.
      If it was a lookup query - if the attribute appears in the object type bitmap or
                                 disregard if there is no object_type bitmap (Ie object filter).

      mask_t bitmap The bitmap of attribute to be converted.

`static int valid_query ( const Query_command* qc, const Query_t q )`

`const Query_command* qc`
  The query_command that the instructions are created from.
`const Query_t q`
  The query being investigated.

Calls: | MA_bitcount() | modules/ma/bitmask.c
| MA_isset() | modules/ma/bitmask.c
| fprintf()
Called by: | QI_new() | modules/qi/query_instructions.c

## Local Function write_objects()

      This is linked into MySQL by the fact that MySQL doesn't have sub selects
      (yet).  The queries are done in two stages.  Make some temporary tables and
      insert into them.  Then use them in the next select.

      SQ_connection_t *sql_connection The connection to the database.

      More:

      Authors:
            ottrey

`static void write_objects ( MYSQL** sql_connection, char* id_table, unsigned int filtered, unsigned int fast, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )`

`MYSQL** sql_connection`
  &nbs;
`char* id_table`
  The id of the temporary table (This is a result of the hacky way we've tried to get MySQL to do sub-selects.)
`unsigned int filtered`
  &nbs;
`unsigned int fast`
  &nbs;
`sk_conn_st* condat`
  Connection data for the client
`acc_st* acc_credit`
  &nbs;
`acl_st* acl`
  &nbs;

Calls: | sql_execute_watched() | modules/qi/query_instructions.c
| write_results() | modules/qi/query_instructions.c
| SQ_free_result(), fprintf(), sprintf()
Called by: | QI_execute() | modules/qi/query_instructions.c

## Local Function write_radix_immediate()

      Display the immediate data carried with the objects returned by the
      radix tree.

    More:

      Authors:
            marek

    Online References:

      Also free the list of answers.

`static void write_radix_immediate ( GList* datlist, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )`

`GList* datlist`
  The linked list of dataleaf copies sk_conn_st *condat Connection data for the client acc_st *acc_credit Accounting struct
`sk_conn_st* condat`
  &nbs;
`acc_st* acc_credit`
  &nbs;
`acl_st* acl`
  &nbs;

Calls: | AC_count_object() | modules/ac/access_control.c
| SK_cd_puts(), g_list_first(), wr_real_clear_list(), wr_real_free()
Called by: | QI_execute() | modules/qi/query_instructions.c

## Local Function write_results()

      Write the results to the client socket.

      SQ_result_set_t *result The result set returned from the sql query.
      unsigned filtered       if the objects should go through a filter (-K)
      sk_conn_st *condat      Connection data for the client

      XXX NB. this is very dependendant on what rows are returned in the result!!!

      More:

      Authors:
            ottrey

    Online References:

`static int write_results ( MYSQL_RES* result, unsigned filtered, unsigned fast, sk_conn_st* condat, acc_st* acc_credit, acl_st* acl )`

Calls: | AC_count_object() | modules/ac/access_control.c
| AC_credit_isdenied() | modules/ac/access_control.c
| ER_dbg_va() | modules/er/er.c
| fast_output() | modules/qi/query_instructions.c
| filter() | modules/qi/query_instructions.c
| SK_cd_puts(), SQ_get_column_string(), SQ_row_next(), atoi(), fprintf(), wr_real_free()
Called by: | write_objects() | modules/qi/query_instructions.c