Esempio n. 1
0
 /**
  * Initiates a new Object Query Builder
  *
  * @throws XsException When the given class doesnt use SQlXS
  * @param int $type The type of the query
  * @param string $xsClass The name of the class that uses SqlXS
  */
 public function __construct($type, $xsClass)
 {
     // Make sure the given class uses SqlXS
     if (!in_array(__NAMESPACE__ . '\\XS', class_uses($xsClass))) {
         throw new XsException('The given $xsClass (' . strip_tags($xsClass) . ') doesnt use SqlXS.');
     }
     $this->xsClass = $xsClass;
     parent::__construct($type, $xsClass::sqlXS()->getTable(), array($xsClass::sqlXS()->getUniqueField()));
 }
Esempio n. 2
0
 /**
  * Initiates SqlXS
  *
  * @param PDO $pdo The PDO to be used by the accessor
  * @param int $errorMode The error mode
  * @return void
  */
 public static function init(PDO $pdo, $errorMode = 0)
 {
     QueryBuilder::setPDO($pdo);
     self::$pdo = $pdo;
     self::$errorMode = $errorMode >= 0 && $errorMode <= 1 ? $errorMode : 0;
 }
Esempio n. 3
0
 /**
  * Inserts a new row in the table
  *
  * @param array $data The [fields] => values
  * @throws XsException
  */
 public static function insert(array $data)
 {
     foreach ($data as $f => $v) {
         $fields[] = QueryBuilder::fieldName($f);
         // Make sure type is correct
         $type = self::sqlXS()->getType($f);
         if ($type !== null && !is_scalar($v)) {
             $v = $v->id();
         }
         $values[] = XsConfiguration::getPDO()->quote($v);
     }
     try {
         XsConfiguration::getPDO()->query('INSERT INTO ' . QueryBuilder::tableName(self::sqlXS()->getTable()) . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')');
         return self::byID(XsConfiguration::getPDO()->lastInsertId());
     } catch (PDOException $ex) {
         throw new XsException('Could not insert row. PDO Error: ' . $ex->getMessage());
     }
 }
Esempio n. 4
0
 public function __toString()
 {
     // Work around escaped paremeters
     $query = str_replace('\\@', '@\\', $this->query);
     // Loop through all the withs and replcae them with the field name
     foreach ($this->with as $key => $with) {
         // Check whether the key is valid (an integer or a :string without space)
         if (is_int($key) || is_string($key) && strlen($key) > 0 && $key[0] == ':' && !preg_match('/\\s/', $key)) {
             // If the field doesnt specify a table already, add it
             $field = is_array($with) ? $with : [$this->table, $with];
             // Replace the %parameter
             $query = str_replace('@' . $key, QueryBuilder::fieldName($field), $query);
         } else {
             throw new Exception('Subselect with-key ' . strip_tags($key) . ' is not valid. It must either be an integer or a spaceless string prefixed with a colon (:)');
         }
     }
     // Add the @table param
     $query = str_replace('@table', QueryBuilder::tableName($this->table), $query);
     // Put back original @ signs
     return str_replace('@\\', '@', $query);
 }