GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
c_create_idx.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_client/c_create_idx.c
3  *
4  * \brief DBMI Library (client) - create index
5  *
6  * (C) 1999-2008 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Joel Jones (CERL/UIUC), Radim Blazek
13  */
14 
15 #include <string.h>
16 #include <grass/dbmi.h>
17 #include "macros.h"
18 
19 /*!
20  \brief Create index
21 
22  \param driver db driver
23  \param index index info (pointer to dbIndex structure)
24 
25  \return DB_OK on success
26  \return DB_FAILED on failure
27 */
29 {
30  int ret_code;
31 
32  /* start the procedure call */
33  db__set_protocol_fds(driver->send, driver->recv);
35 
36  /* send the arguments to the procedure */
37  DB_SEND_INDEX(index);
38 
39  /* get the return code for the procedure call */
40  DB_RECV_RETURN_CODE(&ret_code);
41 
42  if (ret_code != DB_OK)
43  return ret_code; /* ret_code SHOULD == DB_FAILED */
44 
45  /* get results */
46  DB_RECV_STRING(&index->indexName);
47 
48  return DB_OK;
49 }
50 
51 /*!
52  \brief Create unique index
53 
54  \param driver db driver
55  \param table_name table name
56  \param column_name column name (where to create index)
57 
58  \return DB_OK on success
59  \return DB_FAILED on failure
60  */
61 int db_create_index2(dbDriver * driver, const char *table_name,
62  const char *column_name)
63 {
64  int ret;
65  dbIndex index;
66  char buf[1000];
67  const char *tbl;
68 
69  db_init_index(&index);
70  db_alloc_index_columns(&index, 1);
71 
72  tbl = strchr(table_name, '.');
73  if (tbl == NULL)
74  tbl = table_name;
75  else
76  tbl++;
77 
78  sprintf(buf, "%s_%s", tbl, column_name);
79  db_set_index_name(&index, buf);
80 
81  db_set_index_table_name(&index, table_name);
82  db_set_index_column_name(&index, 0, column_name);
84 
85  ret = db_create_index(driver, &index);
86 
87  db_free_index(&index);
88 
89  return ret;
90 }
int db_set_index_type_unique(dbIndex *)
Set index type to unique.
int db_create_index(dbDriver *driver, dbIndex *index)
Create index.
Definition: c_create_idx.c:28
int db_set_index_name(dbIndex *, const char *)
Set index name.
dbString indexName
Definition: dbmi.h:242
void db_free_index(dbIndex *)
Free allocated dbIndex.
#define NULL
Definition: ccmath.h:32
#define DB_SEND_INDEX(x)
Definition: macros.h:102
int db_alloc_index_columns(dbIndex *, int)
Allocate index columns.
#define DB_START_PROCEDURE_CALL(x)
Definition: macros.h:2
#define DB_RECV_RETURN_CODE(x)
Definition: macros.h:4
int db_create_index2(dbDriver *driver, const char *table_name, const char *column_name)
Create unique index.
Definition: c_create_idx.c:61
Definition: driver.h:22
int db_set_index_column_name(dbIndex *, int, const char *)
Set column name.
void db_init_index(dbIndex *)
Initialize dbIndex.
int db_set_index_table_name(dbIndex *, const char *)
Set table name.
FILE * recv
Definition: dbmi.h:171
void db__set_protocol_fds(FILE *, FILE *)
?
#define DB_PROC_CREATE_INDEX
Definition: dbmi.h:61
FILE * send
Definition: dbmi.h:171
#define DB_RECV_STRING(x)
Definition: macros.h:18
#define DB_OK
Definition: dbmi.h:71