Skip to content

Commit

Permalink
Feature composer (#6)
Browse files Browse the repository at this point in the history
* Create composer.json

* add basic configuration composer
  • Loading branch information
Bastien Crettenand authored Jun 5, 2019
1 parent 95d3c83 commit 0d5c335
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/.env
.idea
.idea
/vendor/
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "crbast/slsql",
"description": "Simplifier of SQL queries. Basic ORM with Model.",
"type": "library",
"license": "WTFPL",
"authors": [
{
"name": "CrBast",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0"
},
"autoload": {
"psr-4": {
"slsql\\": "/src"
}
}
}
19 changes: 19 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions src/model/model.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
<?php namespace slsql\Model;

/*
* WTFPL License (http://www.wtfpl.net/) - https: //github.com/CrBast/PHP-SQL_SimpleLife/blob/master/LICENSE
*
Expand Down Expand Up @@ -50,8 +51,8 @@ public function save()
$query_values[] .= $this->{$fields[$i]};
}
}
slsql::go('INSERT INTO ' . get_called_class() . ' ' . $query_name_values . ' VALUES ' . $query_values_after, $query_values);
$this->id = slsql::go('SELECT id FROM ' . get_called_class() . ' ORDER BY id DESC LIMIT 1', array())['value']->fetch()['id'];
Slsql::go('INSERT INTO ' . get_called_class() . ' ' . $query_name_values . ' VALUES ' . $query_values_after, $query_values);
$this->id = Slsql::go('SELECT id FROM ' . get_called_class() . ' ORDER BY id DESC LIMIT 1', array())['value']->fetch()['id'];
} else {
$query_set = "";
$query_values = array();
Expand All @@ -65,7 +66,7 @@ public function save()
$query_values[] = $this->{$fields[$i]};
}
$query_values[] = $this->{'id'};
slsql::go('UPDATE ' . get_called_class() . ' SET ' . $query_set . ' WHERE id = ?', $query_values);
Slsql::go('UPDATE ' . get_called_class() . ' SET ' . $query_set . ' WHERE id = ?', $query_values);
}
unset($query_values, $query_name_values, $query_values_after, $query_set);
}
Expand All @@ -79,9 +80,9 @@ public function save()
public static function get($condition = null, $arr = array())
{
if (!$condition) {
$result = slsql::go('select * from ' . get_called_class(), $arr)['value']->fetchAll();
$result = Slsql::go('select * from ' . get_called_class(), $arr)['value']->fetchAll();
} else {
$result = slsql::go('select * from ' . get_called_class() . ' where ' . $condition . ";", $arr)['value']->fetchAll();
$result = Slsql::go('select * from ' . get_called_class() . ' where ' . $condition . ";", $arr)['value']->fetchAll();
}

if ($result == null) {
Expand Down Expand Up @@ -110,7 +111,7 @@ public static function get($condition = null, $arr = array())
*/
public function remove()
{
slsql::go('DELETE FROM ' . get_called_class() . ' WHERE id = ?', array($this->id));
Slsql::go('DELETE FROM ' . get_called_class() . ' WHERE id = ?', array($this->id));
}

/**
Expand All @@ -120,7 +121,7 @@ public function remove()
public static function ids()
{
$list = array();
$rows = slsql::go('SELECT id FROM ' . get_called_class())['value']->fetchAll();
$rows = Slsql::go('SELECT id FROM ' . get_called_class())['value']->fetchAll();
foreach ($rows as $row) {
$list[] = $row['id'];
}
Expand All @@ -135,7 +136,7 @@ public static function ids()
public static function all($field = 'id')
{
$list = array();
$rows = slsql::go('SELECT ' . $field . ' FROM ' . get_called_class())['value']->fetchAll();
$rows = Slsql::go('SELECT ' . $field . ' FROM ' . get_called_class())['value']->fetchAll();
foreach ($rows as $row) {
$list[] = $row[$field];
}
Expand All @@ -150,7 +151,7 @@ public static function all($field = 'id')
public static function allDistinct($field = 'id')
{
$list = array();
$rows = slsql::go('SELECT DISTINCT ' . $field . ' FROM ' . get_called_class())['value']->fetchAll();
$rows = Slsql::go('SELECT DISTINCT ' . $field . ' FROM ' . get_called_class())['value']->fetchAll();
foreach ($rows as $row) {
$list[] = $row[$field];
}
Expand All @@ -163,7 +164,7 @@ public static function allDistinct($field = 'id')
*/
public static function count()
{
return slsql::go('SELECT count(*) FROM ' . get_called_class())['value']->fetchColumn();
return Slsql::go('SELECT count(*) FROM ' . get_called_class())['value']->fetchColumn();
}

/**
Expand All @@ -177,7 +178,7 @@ public static function countWhere($condition = null, $arr = array())
if (!$condition) {
return get_called_class()::count();
} else {
return slsql::go('SELECT count(*) FROM ' . get_called_class() . ' WHERE ' . $condition, $arr)['value']->fetchColumn();
return Slsql::go('SELECT count(*) FROM ' . get_called_class() . ' WHERE ' . $condition, $arr)['value']->fetchColumn();
}

}
Expand All @@ -195,7 +196,7 @@ class ListModels
* @param Model $model
* Do not use this function
*/
function add(Model $model)
public function add(Model $model)
{
$this->arr[] = $model;
}
Expand Down
7 changes: 4 additions & 3 deletions src/slsql.php → src/slsql/slsql.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
<?php namespace slqsl\slqsl;

/**
* WTFPL License (http://www.wtfpl.net/) - https: //github.com/CrBast/PHP-SQL_SimpleLife/blob/master/LICENSE
*
Expand All @@ -24,7 +25,7 @@
*
* Return Type array([value], [status], [message])
*/
class slsql
class Slsql
{
private $dsn,
$user,
Expand Down Expand Up @@ -164,7 +165,7 @@ public static function goT(SLTransaction $trans)
private static function getPDO()
{
try {
require '.env';
require '../.env';
} catch (Exception $e) {
throw new Error("Error Processing Request");
}
Expand Down
File renamed without changes.

0 comments on commit 0d5c335

Please sign in to comment.