Ejemplo n.º 1
0
int N_insert_db(T_Connect *conn,char *tabname,void *data,T_PkgType *tp)
{
char stmt[32768];
char *p;
T_CLI_Var *clip;
	clip=(T_CLI_Var *)conn->Var;
	p=stmt;
        p+=sprintf(p,"INSERT INTO ");
        if(*clip->DBOWN)
                p+=sprintf(p,"%s.",clip->DBOWN);
        p+=sprintf(p,"%s (",tabname);
	p=mkset(p,tp);
	p+=sprintf(p,") VALUES(");
	p=mk_values(p,data,tp);
	p+=sprintf(p,")");
        return N_SQL_Exec(conn,stmt);
}
Ejemplo n.º 2
0
/*
 * interp: interprets parse trees
 *
 */
RC interp(NODE *n)
{
   RC errval = 0;         /* returned error value      */

   /* if input not coming from a terminal, then echo the query */
   if(!isatty(0))
      echo_query(n);

   switch(n -> kind){

      case N_CREATETABLE:            /* for CreateTable() */
         {
            int nattrs;
            AttrInfo attrInfos[MAXATTRS];

            /* Make sure relation name isn't too long */
            if(strlen(n -> u.CREATETABLE.relname) > MAXNAME){
               print_error((char*)"create", E_TOOLONG);
               break;
            }

            /* Make a list of AttrInfos suitable for sending to Create */
            nattrs = mk_attr_infos(n -> u.CREATETABLE.attrlist, MAXATTRS, 
                  attrInfos);
            if(nattrs < 0){
               print_error((char*)"create", nattrs);
               break;
            }

            /* Make the call to create */
            errval = pSmm->CreateTable(n->u.CREATETABLE.relname, nattrs, 
                  attrInfos);
            break;
         }   

      case N_CREATEINDEX:            /* for CreateIndex() */

         errval = pSmm->CreateIndex(n->u.CREATEINDEX.relname,
               n->u.CREATEINDEX.attrname);
         break;

      case N_DROPINDEX:            /* for DropIndex() */

         errval = pSmm->DropIndex(n->u.DROPINDEX.relname,
               n->u.DROPINDEX.attrname);
         break;

      case N_DROPTABLE:            /* for DropTable() */

         errval = pSmm->DropTable(n->u.DROPTABLE.relname);
         break;

      case N_LOAD:            /* for Load() */

         errval = pSmm->Load(n->u.LOAD.relname,
               n->u.LOAD.filename);
         break;

      case N_SET:                    /* for Set() */

         errval = pSmm->Set(n->u.SET.paramName,
               n->u.SET.string);
         break;

      case N_HELP:            /* for Help() */

         if (n->u.HELP.relname)
            errval = pSmm->Help(n->u.HELP.relname);
         else
            errval = pSmm->Help();
         break;

      case N_PRINT:            /* for Print() */

         errval = pSmm->Print(n->u.PRINT.relname);
         break;

      case N_QUERY:            /* for Query() */
         {
            int       nSelAttrs = 0;
            RelAttr  relAttrs[MAXATTRS];
            int       nRelations = 0;
            char      *relations[MAXATTRS];
            int       nConditions = 0;
            Condition conditions[MAXATTRS];

            /* Make a list of RelAttrs suitable for sending to Query */
            nSelAttrs = mk_rel_attrs(n->u.QUERY.relattrlist, MAXATTRS,
                  relAttrs);
            if(nSelAttrs < 0){
               print_error((char*)"select", nSelAttrs);
               break;
            }

            /* Make a list of relation names suitable for sending to Query */
            nRelations = mk_relations(n->u.QUERY.rellist, MAXATTRS, relations);
            if(nRelations < 0){
               print_error((char*)"select", nRelations);
               break;
            }

            /* Make a list of Conditions suitable for sending to Query */
            nConditions = mk_conditions(n->u.QUERY.conditionlist, MAXATTRS,
                  conditions);
            if(nConditions < 0){
               print_error((char*)"select", nConditions);
               break;
            }

            /* Make the call to Select */
            errval = pQlm->Select(nSelAttrs, relAttrs,
                  nRelations, relations,
                  nConditions, conditions);
            break;
         }   

      case N_INSERT:            /* for Insert() */
         {
            int nValues = 0;
            Value values[MAXATTRS];

            /* Make a list of Values suitable for sending to Insert */
            nValues = mk_values(n->u.INSERT.valuelist, MAXATTRS, values);
            if(nValues < 0){
               print_error((char*)"insert", nValues);
               break;
            }

            /* Make the call to insert */
            errval = pQlm->Insert(n->u.INSERT.relname,
                  nValues, values);
            break;
         }   

      case N_DELETE:            /* for Delete() */
         {
            int nConditions = 0;
            Condition conditions[MAXATTRS];

            /* Make a list of Conditions suitable for sending to delete */
            nConditions = mk_conditions(n->u.DELETE.conditionlist, MAXATTRS,
                  conditions);
            if(nConditions < 0){
               print_error((char*)"delete", nConditions);
               break;
            }

            /* Make the call to delete */
            errval = pQlm->Delete(n->u.DELETE.relname,
                  nConditions, conditions);
            break;
         }   

      case N_UPDATE:            /* for Update() */
         {
            RelAttr relAttr;

            // The RHS can be either a value or an attribute
            Value rhsValue;
            RelAttr rhsRelAttr;
            int bIsValue;

            int nConditions = 0;
            Condition conditions[MAXATTRS];

            /* Make a RelAttr suitable for sending to Update */
            mk_rel_attr(n->u.UPDATE.relattr, relAttr);

            struct node *rhs = n->u.UPDATE.relorvalue;
            if (rhs->u.RELATTR_OR_VALUE.relattr) {
               mk_rel_attr(rhs->u.RELATTR_OR_VALUE.relattr, rhsRelAttr);
               bIsValue = 0;
            } else {
               /* Make a value suitable for sending to update */
               mk_value(rhs->u.RELATTR_OR_VALUE.value, rhsValue);
               bIsValue = 1;
            }

            /* Make a list of Conditions suitable for sending to Update */
            nConditions = mk_conditions(n->u.UPDATE.conditionlist, MAXATTRS,
                  conditions);
            if(nConditions < 0){
               print_error((char*)"update", nConditions);
               break;
            }

            /* Make the call to update */
            errval = pQlm->Update(n->u.UPDATE.relname, relAttr, bIsValue, 
                  rhsRelAttr, rhsValue, nConditions, conditions);
            break;
         }   

      default:   // should never get here
         break;
   }

   return (errval);
}