Esempio n. 1
0
int main(int argc, LiteSQL_Char **argv) { 

  int rc = parseArgs(argc,argv);
  if (rc!=0)
  {
    Logger::error(help);
    return -1;
  }

  if (options.printHelp) {
    LiteSQL_cout << help << std::endl;
  }

  ObjectModel model;
  try {
    if (!model.loadFromFile(argv[argc-1]))
    {
      LiteSQL_String msg =  LiteSQL_L( "could not load file '" ) + LiteSQL_String(argv[argc-1]) +  LiteSQL_L( "'" );
      Logger::error(msg);
      return -1;
    }
    else
    {
      return generateCode(model); 
    }
  } 
  catch (Except e) {
      Logger::error(e);
      return -1;
  }
}
Esempio n. 2
0
void LitesqlParser::onStartElement(const XML_Char *fullname,
                                   const XML_Char **atts)
{
  //   Logger::report("starting " <<fullname );
  history.push_back(m_parseState);

  if (xmlStrEqual(fullname,(XML_Char*) Database::TAG ))
  {
    if (m_parseState!=ROOT)
    {
      m_parseState = ERROR;
    }
    else
    {
      m_parseState = DATABASE;
      
      DatabasePtr pDb(new xml::Database);
      
      pDb->name = safe((char*)xmlGetAttrValue(atts,"name"));
      pDb->include = safe((char*)xmlGetAttrValue(atts,"include"));
      pDb->nspace = safe((char*)xmlGetAttrValue(atts,"namespace"));
      
      m_pObjectModel->db = pDb;
      Logger::report("database = " , m_pObjectModel->db->name);
    }
  } 
  else if (xmlStrEqual(fullname,(XML_Char*)Object::TAG))
  {
    if (m_parseState!=DATABASE)
    {
      m_parseState = ERROR;
    }
    else
    {
      ObjectPtr pObj(obj = new Object(    (char*)xmlGetAttrValue(atts,"name"), 
        safe((char*)xmlGetAttrValue(atts,"inherits")))); 
      m_pObjectModel->objects.push_back(pObj);
      Logger::report("object = ",obj->name);
      m_parseState = OBJECT; 

    }
  } 
  else if (xmlStrEqual(fullname,(XML_Char*)Field::TAG))
  {
    Field* pNewField = new Field(   (char*)xmlGetAttrValue(atts,"name"), 
      field_type(xmlGetAttrValue(atts,"type")),
      safe(  (char*)xmlGetAttrValue(atts,"default")),
      field_indexed(xmlGetAttrValue(atts,"indexed")),
      field_unique(xmlGetAttrValue(atts,"unique")),
	  field_length(xmlGetAttrValue(atts,"length"))
      );

    switch(m_parseState)
    {
    case OBJECT:
      if (!obj) {
        Logger::error("parsing field inside object, but currentObject == NULL ");
      }
      else {
        Logger::report("field = ",obj->name);
        Field::Ptr field(fld = pNewField);
        obj->fields.push_back(field);
      };
      m_parseState = FIELD;
      break;

    case RELATION:
      if (!rel) {
        Logger::error("parsing field inside relation, but currentRelation == NULL ");
      }
      else
      {
        Field::Ptr field(rel_fld = pNewField);
        rel->fields.push_back(field);
        Logger::report("field = ",rel_fld->name );
      }
      m_parseState = FIELD;
      break;

    default:
      delete pNewField;
      m_parseState = ERROR;
    }
  }
  else if (xmlStrEqual(fullname,(XML_Char*)Index::TAG))
  {
    Index::Ptr ptrIndex(new Index(index_unique(xmlGetAttrValue(atts,"unique"))));
      
    switch (m_parseState)
    {
    case OBJECT:
      idx = ptrIndex;
      obj->indices.push_back(ptrIndex);
      m_parseState = INDEX;
      break;

    case RELATION:
      idx = ptrIndex;
      rel->indices.push_back(ptrIndex);
      m_parseState = INDEX;
      break;

    default:
      m_parseState = ERROR;
    }
  }
  else if (xmlStrEqual(fullname,(XML_Char*)IndexField::TAG))
  {
    if (m_parseState!=INDEX)
    {
      m_parseState = ERROR;
    }
    else
    {
      IndexField idxField((char*)xmlGetAttrValue(atts,"name"));
      idx->fields.push_back(idxField);
    }    

    m_parseState = INDEXFIELD;
  }
  else if (xmlStrEqual(fullname,(XML_Char*)Value::TAG))
  {
    if (m_parseState!=FIELD)
    {
      m_parseState = ERROR;
    }
    else
    {
      if (fld) 
      {
        fld->value(Value((char*)xmlGetAttrValue(atts,"name"), (char*)xmlGetAttrValue(atts,"value")));
      }
      Logger::report("value = " );
    }

  } 
  else if (xmlStrEqual(fullname,(XML_Char*)Method::TAG))
  {
    if (m_parseState!=OBJECT)
    {
      m_parseState = ERROR;
    }
    else
    {
      Method::Ptr m(mtd = new Method( safe((char*)xmlGetAttrValue(atts,"name")), 
        safe((char*)xmlGetAttrValue(atts,"returntype")) 
        ));
      obj->methods.push_back(m);
      m_parseState= METHOD;
      Logger::report("method = ",mtd->name );
    }

  }
  else if (xmlStrEqual(fullname,(XML_Char*)Param::TAG))
  {
    if (m_parseState!=METHOD)
    {
      m_parseState = ERROR;
    }
    else
    {
      char* pszName = (char*)xmlGetAttrValue(atts,"name");	

      mtd->param(Param(pszName,strdup((char*)xmlGetAttrValue(atts,"type"))));
      m_parseState= PARAM;
      Logger::report("param = ",pszName);
    }
  }
  else if (xmlStrEqual(fullname,(XML_Char*)Relation::TAG))
  {
    if (m_parseState!=DATABASE)
    {
      m_parseState = ERROR;
    }
    else
    {
      Relation::Ptr ptrRelation(rel = new Relation(  safe((char*)xmlGetAttrValue(atts,"id")), 
                                                   safe((char*)xmlGetAttrValue(atts,"name")),
                                                   relation_unidir(xmlGetAttrValue(atts,"unidir")))); 
      m_pObjectModel->relations.push_back(ptrRelation);

      Logger::report( "relation = " + ptrRelation->getName());

      m_parseState = RELATION;
    }
  } 
  else if (xmlStrEqual(fullname,(XML_Char*)Relate::TAG))
  {
    if (m_parseState!=RELATION)
    {
      m_parseState = ERROR;
    }
    else
    {
      Relate::Ptr ptrRel(
        new Relate( safe((char*)xmlGetAttrValue(atts,"object")), 
                    relate_limit(xmlGetAttrValue(atts,"limit")), 
                    relate_unique(xmlGetAttrValue(atts,"unique")), 
                    safe((char*)xmlGetAttrValue(atts,"handle"))
                  ) 
      );

      rel->related.push_back(ptrRel);
      Logger::report("relate = " );
    }
  } 
  else if (xmlStrEqual(fullname,(XML_Char*)"include"))
  {
    string filename((char*)xmlGetAttrValue(atts,"name"));
    if (m_parseState!=DATABASE)
    {
      m_parseState = ERROR;
    }
    else
    {
      Logger::report("include \"" + filename + '"' );
      ObjectModel includedModel;
      if (!includedModel.loadFromFile(filename)) 
      {
        Logger::report("error on parsing included file \"" + filename + '"' );
      }
      m_pObjectModel->objects.insert(m_pObjectModel->objects.end(),includedModel.objects.begin(),includedModel.objects.end());
      m_pObjectModel->relations.insert(m_pObjectModel->relations.end(),includedModel.relations.begin(),includedModel.relations.end());
      m_parseState = INCLUDE;
    }
  } 
  else
  {
    m_parseState = UNKNOWN;
    Logger::error("unknown = ",fullname );
  } 
}