Ejemplo n.º 1
0
 /* (non-Javadoc)
  * example of query
  * $stmt = $db->prepare("UPDATE table SET name=? WHERE id=?");
  * $stmt->execute(array($name, $id));
  * $affected_rows = $stmt->rowCount();
  * @see WriterInterface.GenericWriterInterface#executeSqlQuery(java.lang.String, java.lang.String, java.util.List)
  */
 @Override
 public void executeSqlQuery(String path, String query, List<Variable> queryParameters) {
   File f;
   try {
     f = FileWriterImpl.getInstace().fileCreate(path);
   } catch (IOException ex) {
     Logger.getLogger(PHPGenericWriterImpl.class.getName()).log(Level.SEVERE, null, ex);
     return;
   }
   FileWriterImpl.getInstace().writeLine("$dbn->query($sqlQuery);", f);
   String phpCode =
       "try {\n" + "$sqlQuery=\"" + query + "\"; \n" + "   $stmt = $dbn->prepare($sqlQuery); \n";
   boolean useArrayOfParameters = false;
   if (queryParameters != null) {
     if (queryParameters.size() > 0) {
       useArrayOfParameters = true;
     }
   }
   if (useArrayOfParameters) {
     phpCode += "$stmt->execute(array(";
     int paramNumber = 0;
     for (Variable variable : queryParameters) {
       if (paramNumber != 0) {
         phpCode += ", ";
       }
       phpCode += "$" + variable.getName() + " ";
     }
     phpCode += "));\n";
   } else {
     phpCode += "$stmt->execute();";
   }
   phpCode += "} catch(PDOException $ex) {\n" + "    echo \"An Error occured!\"; \n" + "}";
   FileWriterImpl.getInstace().writeLine(phpCode, f);
 }
Ejemplo n.º 2
0
 @Override
 public void writeArithmeticAndGetResultInVariable(
     String path, String arithmetic, List<Variable> variableList, String resultVariableName) {
   List<String> values = new ArrayList<>();
   for (Variable variable : variableList) {
     values.add("$" + variable.getName());
   }
   String arithmeticLine = String.format(arithmetic.replace("?", "%s"), values.toArray());
   String line = "$" + resultVariableName + "=" + arithmeticLine + ";";
   try {
     FileWriterImpl.getInstace().writeLine(line, FileWriterImpl.getInstace().fileCreate(path));
   } catch (IOException e) {
     e.printStackTrace();
   }
 }