About

YMSQL is a predecessor of SafeSQL and a query processer to automate the tedious tasks
of syntax testing, injection attack-proofing, dropping parts of queries
and other misc features. Any ANSI SQL-92 compliant db library should work OK.

Set Up


  composer require ymvas/ymsql
      

Initial Configuration

If your are building from scratch you have to declare the db variables in $_ENV.
If your are using a web framework like Laravel you could use the .env file instead.


  $_ENV[  'DB_HOST'  ] = 'host';
  $_ENV['DB_USERNAME'] = 'name';
  $_ENV['DB_PASSWORD'] = 'pass';
  $_ENV['DB_DATABASE'] = 'dtbs';
  
  

Synopsis


  SELECT * FROM Table t
  WHERE TRUE
  AND t.name = 'ymsql'
  
  

  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table as t WHERE TRUE
  { AND t.id   = i:id   }
  { AND t.name = s:name }
  { AND t.description LIKE '%{:description}%'     }
  { AND FIND_IN_SET( r:category , t.categories )  }
  ',[
  'name' => 'ymsql'
  ]);
  
  echo $query;
  
  

Placeholders

YMSQL queries use custom style placeholders for your variable content. Possible placeholders are ( i: s: d: r: f: a: j: )

Use example of s: and :q



  SELECT * FROM Table T as t WHERE TRUE
  AND t.name      = 'Jonh'
  AND t.surname   = 'Doe'
  AND t.product   = '200'
  
  

  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table T as t WHERE TRUE
  { AND t.name      = q:name }
  { AND t.surname   = s:name }
  { AND t.product   = s:product }
  ',[
  'name'    => 'Jonh' ,
  'surname' => 'Doe'  ,
  'product' => 200    ,
  ]);
  
  echo $query;
  
  

Use example of i: and :r



  SELECT * FROM Table T as t WHERE TRUE
  AND t.id        = 12
  AND t.rating    = -200
  AND t.position  = 24
  
        

  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table T as t WHERE TRUE
  { AND t.id       = i:id      }
  { AND t.rating   = i:rating  }
  { AND t.position = r:postion }
  ',[
     'id'       => 983      ,
     'rating'   => '-200.2' ,
     'position' => '-24.3'
  ]);
  
  echo $query;
  
  

Use example of f: and +f:



  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table T as t WHERE TRUE
  { AND t.id       = i:id      }
  { AND t.rating   = i:rating  }
  { AND t.position = r:postion }
  ',[
     'id'       => 983      ,
     'rating'   => '-200.2' ,
     'position' => '-24.3'
  ]);
  
  echo $query;
  
  

  ELECT * FROM Table T as t WHERE TRUE
  AND t.rating    = -200.2
  AND t.position  = 24.3
  
  

Use example of j:



  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  UPDATE Table set
  { meta  = j:meta    , }
  where id = 1
  ',[
    'meta' => [
      'links'  => [ 'https//...' ],
      'clicks' => 34,
    ]
  ]);
  
  echo $query;
  
  

  UPDATE Table set
  meta  = "{\"links\":[\"https//...\"],\"clicks\":34}",
  where id = 1
  
  

Use example of a: and c:



  SELECT * FROM Table T as t WHERE TRUE
  AND t.id  in ( 1,2,3 )
  AND FIND_IN_SET( t.status , 'active,hidden' )
  
  

  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table T as t WHERE TRUE
  { AND t.id  in ( c:ids ) }
  { AND FIND_IN_SET( t.status , a:status ) }
  ',[
    'ids'    => [ 1, 2, 3 ],
    'status' => ['active','hidden']
  ]);
  
  echo $query;
  
  

Use example of d: and dt:



  SELECT * FROM Table T
  WHERE TRUE
  AND t.started = '2021-02-03'
  AND t.created =
  AND t.ended = '2021-02-03 17:53:12'
  
  

  use YMSQL\YMSQL;
  
  $ymsql = new YMSQL();
  
  $query_t = $ymsql->query('
  SELECT * FROM Table T
  WHERE TRUE
    { AND t.started = d:started  }
    { AND t.created = d:created  }
    { AND t.ended   = dt:ended   }
  ',[
    'started' => '2021-02-03' ,
    'created' => '2021-02-03 17:53:12' ,
    'ended'   => '2021-02-03 17:53:12'
  ]);
  
  

Use example of e:


            
  SELECT * FROM Table as t WHERE TRUE
  AND t.email    = 'mail@info.com'
  AND t.email2   =
  
  

  use YMSQL\YMSQL;
  
  $v = new YMSQL();
  
  $query = $v->query('
  SELECT * FROM Table as t WHERE TRUE
  { AND t.email    = e:email  }
  { AND t.email2   = e:email2 }
  ',[
    'email'  => 'mail@info.com',
    'email2' => 'info.com',
  ]);
  
  echo $query;