示例#1
0
void repl() {
  SqlParser parser;
  while (true) {
    std::string command = read_command();
    std::string::size_type first_space = command.find_first_of(" ");
    std::string fst_token = first_space == string::npos ? command : command.substr(0, first_space);
    capitalize(&fst_token);

#ifdef MAIN_DBG
    Utils::info("[REPL] execute \"" + command +"\"");
#endif
    if (fst_token.compare("QUIT") == 0 || fst_token.compare("EXIT") == 0) {
      return;
    } else if (fst_token.compare("PURGE") == 0) {
      BufferManager &bm = BufferManager::get_instance();
      bm.purge();
      std::cout << "Purge has been done" << std::endl;
    } else if (fst_token.compare("BMST") == 0) {
      std::cout << "BufferManager state:" << std::endl;
      std::cout << "  Pinned pages: " + std::to_string(BufferManager::get_instance().get_pinned_page_count()) << std::endl;
      BufferManager::get_instance().print_pinned_page();
    } else if (fst_token.compare("ABOUT") == 0) {
      std::string table_name = command.substr(6);
#ifdef MAIN_DBG
      Utils::info("[REPL] 'about' was called for " + table_name);
#endif
      describe_table(table_name);
    } else {
      SqlStatement const * stmt = parser.parse(command);

      DBFacade::get_instance()->execute_statement(stmt);
      delete stmt;
    }
  }
}
示例#2
0
int db__driver_describe_table(dbString * table_name, dbTable ** table)
{
    dbString sql;
    PGresult *res;

    db_init_string(&sql);

    db_set_string(&sql, "select * from ");
    db_append_string(&sql, db_get_string(table_name));
    db_append_string(&sql, " where 1 = 0");

    res = PQexec(pg_conn, db_get_string(&sql));

    if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
	append_error(db_get_string(&sql));
	append_error("\n");
	append_error(PQerrorMessage(pg_conn));
	report_error();
	PQclear(res);
	return DB_FAILED;
    }

    if (describe_table(res, table, NULL) == DB_FAILED) {
	append_error("Cannot describe table\n");
	report_error();
	PQclear(res);
	return DB_FAILED;
    }

    PQclear(res);

    return DB_OK;
}
示例#3
0
int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
{
    cursor *c;
    dbTable *table;
    char *str;

    /* allocate cursor */
    c = alloc_cursor();
    if (c == NULL)
	return DB_FAILED;

    db_set_cursor_mode(dbc, mode);
    db_set_cursor_type_readonly(dbc);

    /* \ must be escaped, see explanation in 
     * db_driver_execute_immediate() */
    str = G_str_replace(db_get_string(sel), "\\", "\\\\");
    G_debug(3, "Escaped SQL: %s", str);

    if (mysql_query(connection, str) != 0) {
	db_d_append_error("%s\n%s\n%s",
			  _("Unable to select data:"),
			  db_get_string(sel),
			  mysql_error(connection));
	if (str)
	    G_free(str);
	db_d_report_error();
	return DB_FAILED;
    }

    if (str)
	G_free(str);
    c->res = mysql_store_result(connection);

    if (c->res == NULL) {
	db_d_append_error("%s\n%s",
			  db_get_string(sel),
			  mysql_error(connection));
	db_d_report_error();
	return DB_FAILED;
    }

    if (describe_table(c->res, &table, c) == DB_FAILED) {
	db_d_append_error(_("Unable to describe table."));
	db_d_report_error();
	mysql_free_result(c->res);
	return DB_FAILED;
    }

    c->nrows = (int)mysql_num_rows(c->res);

    /* record table with dbCursor */
    db_set_cursor_table(dbc, table);

    /* set dbCursor's token for my cursor */
    db_set_cursor_token(dbc, c->token);

    return DB_OK;
}
示例#4
0
int db__driver_describe_table(dbString * table_name, dbTable ** table)
{
    dbString sql;
    MYSQL_RES *res;

    db_init_string(&sql);

    db_set_string(&sql, "select * from ");
    db_append_string(&sql, db_get_string(table_name));
    db_append_string(&sql, " where 1 = 0");

    if (mysql_query(connection, db_get_string(&sql)) != 0) {
	append_error(db_get_string(&sql));
	append_error("\n");
	append_error(mysql_error(connection));
	report_error();
	return DB_FAILED;
    }

    res = mysql_store_result(connection);

    if (res == NULL) {
	append_error(db_get_string(&sql));
	append_error("\n");
	append_error(mysql_error(connection));
	report_error();
	return DB_FAILED;
    }

    if (describe_table(res, table, NULL) == DB_FAILED) {
	append_error("Cannot describe table\n");
	report_error();
	mysql_free_result(res);
	return DB_FAILED;
    }

    mysql_free_result(res);

    db_set_table_name(*table, db_get_string(table_name));

    return DB_OK;
}
示例#5
0
/*!
  \brief Open select cursor

  \param sel select statement (given as dbString)
  \param[out] dbc pointer to dbCursor
  \param mode open mode

  \return DB_OK on success
  \return DB_FAILED on failure
*/
int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
{
    cursor *c;
    dbTable *table;

    init_error();

    /* allocate cursor */
    c = alloc_cursor();
    if (c == NULL)
	return DB_FAILED;

    db_set_cursor_mode(dbc, mode);
    db_set_cursor_type_readonly(dbc);

    G_debug(3, "SQL: '%s'", db_get_string(sel));
    c->hLayer = OGR_DS_ExecuteSQL(hDs, db_get_string(sel), NULL, NULL);

    if (c->hLayer == NULL) {
	append_error(_("Unable to select: \n"));
	append_error(db_get_string(sel));
	append_error("\n");
	report_error();
	return DB_FAILED;
    }

    if (describe_table(c->hLayer, &table, c) == DB_FAILED) {
	append_error(_("Unable to describe table\n"));
	report_error();
	OGR_DS_ReleaseResultSet(hDs, c->hLayer);
	return DB_FAILED;
    }

    /* record table with dbCursor */
    db_set_cursor_table(dbc, table);

    /* set dbCursor's token for my cursor */
    db_set_cursor_token(dbc, c->token);

    return DB_OK;
}
示例#6
0
int db__driver_describe_table(dbString * table_name, dbTable ** table)
{
    int i, nlayers;
    OGRLayerH hLayer = NULL;
    OGRFeatureDefnH hFeatureDefn;

    /* Find data source */
    nlayers = OGR_DS_GetLayerCount(hDs);

    for (i = 0; i < nlayers; i++) {
	hLayer = OGR_DS_GetLayer(hDs, i);
	hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
	if (G_strcasecmp
	    ((char *)OGR_FD_GetName(hFeatureDefn),
	     db_get_string(table_name)) == 0) {
	    break;
	}
	hLayer = NULL;
    }

    if (hLayer == NULL) {
	append_error("Table '%s' does not exist\n",
		     db_get_string(table_name));
	report_error();
	return DB_FAILED;
    }

    G_debug(3, "->>");
    if (describe_table(hLayer, table, NULL) == DB_FAILED) {
	append_error("Cannot describe table\n");
	report_error();
	return DB_FAILED;
    }

    return DB_OK;
}
示例#7
0
int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
{
    PGresult *res;
    cursor *c;
    dbTable *table;
    char *str;

    /* Set datetime style */
    res = PQexec(pg_conn, "SET DATESTYLE TO ISO");

    if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
	db_d_append_error(_("Unable set DATESTYLE"));
	db_d_report_error();
	PQclear(res);
	return DB_FAILED;
    }

    PQclear(res);

    /* allocate cursor */
    c = alloc_cursor();
    if (c == NULL)
	return DB_FAILED;

    db_set_cursor_mode(dbc, mode);
    db_set_cursor_type_readonly(dbc);

    /* \ must be escaped, see explanation in db_driver_execute_immediate() */
    str = G_str_replace(db_get_string(sel), "\\", "\\\\");
    G_debug(3, "Escaped SQL: %s", str);

    c->res = PQexec(pg_conn, str);

    if (!c->res || PQresultStatus(c->res) != PGRES_TUPLES_OK) {
	db_d_append_error("%s\n%s\n%s",
			  _("Unable to select:"),
			  db_get_string(sel),
			  PQerrorMessage(pg_conn));
	db_d_report_error();
	PQclear(c->res);
	if (str)
	    G_free(str);
	return DB_FAILED;
    }

    if (str)
	G_free(str);

    if (describe_table(c->res, &table, c) == DB_FAILED) {
	db_d_append_error(_("Unable to describe table"));
	db_d_report_error();
	PQclear(res);
	return DB_FAILED;
    }

    c->nrows = PQntuples(c->res);
    c->row = -1;

    /* record table with dbCursor */
    db_set_cursor_table(dbc, table);

    /* set dbCursor's token for my cursor */
    db_set_cursor_token(dbc, c->token);

    return DB_OK;
}
示例#8
0
int main(int args, char* argvs[])
{
    int result, col_length;
    char col_type[ITEM_TYPE_LEN], col_name[ITEM_NAME_LEN];
    struct msg_st string_item;
    int msgqid;
    char tb_name[TABLE_NAME_LEN];
    char tmp[10][ITEM_NAME_LEN];

    msgqid = msgget((key_t)3570, 0666 | IPC_CREAT);
    if (msgqid == -1) {
        ipc_msgget_failed(errno);
    }
    
    while (receive(0)) {
        switch (*string_item.text) {        
        case '0':
            result=receive(0);
            string_item.text[result]='\0';
	    strcpy(tb_name, string_item.text);
            create_table(string_item.text);
            result = 0;
            while ((result = receive(0)) != -1) {
                string_item.text[result]='\0';
                if (!strcmp(string_item.text, "TAIL"))
                    break;
                sscanf(string_item.text, "%s %s %s %d", tmp[0], col_type, col_name, &col_length);
                if (tmp[0][0] == '0')
                    create_col(col_name, col_type, col_length, 0);
                else create_col(col_name, col_type, col_length, 1);
            }
            
            if (-1 == result)
                ipc_msgrcv_failed(errno);
            create_table_on_close();
            printf("CATTYDB : database %s successfully created.\n",tb_name);
            break;
        case '1':
            result=receive(0);
            string_item.text[result]='\0';
            //printf("%s\n", string_item.text);
            describe_table(string_item.text);
            break;
        case '2':
            result = receive(0);
            string_item.text[result]='\0';
            //printf("%s\n", string_item.text);
            insert_on_open(string_item.text);
            while ((result=receive(0)) != -1) {
                string_item.text[result]='\0';
                if (!strcmp(string_item.text, "TAIL"))
                    break;
                insert_item(string_item.text);
            }
            if (-1 == result)
                ipc_msgrcv_failed(errno);
            insert_on_close();
            break;
        case '3':
            result = receive(0);
            string_item.text[result]='\0';
            //printf("%s\n", string_item.text);
            delete_on_open(string_item.text);
            int i = 0;
            while ((result = receive(0)) != -1) {
                string_item.text[result]='\0';
                if (!strcmp(string_item.text, "TAIL"))
                    break;
                delete(string_item.text);
                //strcpy(tmp[i++], string_item.text);
                //puts(tmp[i-1]);
            }
            if (-1 == result)
                ipc_msgrcv_failed(errno);
            puts("del suc!");
            break;
        case '6':
            puts("bye");
            exit(0);
        case '7':
            result = receive(0);
            string_item.text[result]='\0';
            printf("%s\n", string_item.text);
            show_table(string_item.text);
            break;
        case '8':
            result=receive(0);
            string_item.text[result]='\0';
            select_where_on_open(string_item.text, "tmp");
            
            result=receive(0);
            string_item.text[result]='\0';
            select_where(string_item.text);
            select_where_on_close();

            select_col_on_open("tmp", "tmp2");
            
            result = 0;
            while ((result = receive(0)) != -1) {
                string_item.text[result]='\0';
                if (!strcmp(string_item.text, "TAIL"))
                    break;
                select_col_get_pos(string_item.text);
            }
            if (-1 == result)
                ipc_msgrcv_failed(errno);

            select_col();
            select_col_on_close();
            show_table("tmp2");
            break;
        case '9':
            result = receive(0);
            string_item.text[result]='\0';
            update_on_open(string_item.text);
            result = receive(0);
            string_item.text[result]='\0';
            strcpy(tmp[0], string_item.text);
            result = receive(0);
            string_item.text[result]='\0';
            update(tmp[0], string_item.text); 
            break;
        case 'a':
            result = receive(0);
            string_item.text[result]='\0';
            strcpy(tmp[0], string_item.text);
            result = receive(0);
            string_item.text[result]='\0';
            equijoins_on_open(tmp[0], string_item.text, "tmp");
            result = receive(0);
            string_item.text[result]='\0';
            equijoins(string_item.text);
            equijoins_on_close();
            show_table("tmp");
        }
    }

    return 0;
}