5 use Drupal\Core\Config\ConfigFactory;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use SqlParser\SqlParser;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Maquina\StateMachine\Machine;
16 class Service implements ContainerInjectionInterface {
17 private $configFactory;
24 public static function create(ContainerInterface $container) {
25 return new static($container->get(
'config.factory'));
32 $this->configFactory = $configFactory;
47 $stateMachine = $this->validate($sqlString);
48 return $this->getTableNameFromSelect($stateMachine->gsm(
'select'));
54 private function getTableNameFromSelect(Machine $selectMachine): string {
55 $machine = $selectMachine->gsm(
'table_var');
56 $strings = $this->getStringsFromStringMachine($machine);
57 if (empty($strings)) {
58 throw new \Exception(
"No table name");
73 return $this->getQueryObjectFromStateMachine($this->validate($sqlString));
79 private function validate(
string $sqlString): Machine {
80 $parser =
new SqlParser();
81 if ($parser->validate($sqlString) === FALSE) {
82 throw new \Exception(
"Invalid query string.");
85 return $parser->getValidatingMachine();
91 private function getQueryObjectFromStateMachine(Machine $state_machine): Query {
92 $object =
new Query();
93 $this->setQueryObjectSelect($object, $state_machine->gsm(
'select'));
94 $this->setQueryObjectWhere($object, $state_machine->gsm(
'where'));
95 $this->setQueryObjectOrderBy($object, $state_machine->gsm(
'order_by'));
96 $this->setQueryObjectLimit($object, $state_machine->gsm(
'limit'));
104 private function setQueryObjectSelect(Query $object, Machine $state_machine) {
105 $strings = $this->getStringsFromStringMachine($state_machine->gsm(
'select_count_all'));
106 if (!empty($strings)) {
111 $strings = $this->getStringsFromStringMachine($state_machine->gsm(
'select_var_all'));
112 if (!empty($strings)) {
116 $strings = $this->getStringsFromStringMachine($state_machine->gsm(
'select_var'));
117 foreach ($strings as $property) {
118 $object->filterByProperty($property);
125 private function setQueryObjectWhere(Query $object, Machine $state_machine) {
126 $properties = $this->getStringsFromStringMachine($state_machine->gsm(
'where_column'));
127 $values = $this->getStringsFromStringMachine($state_machine->gsm(
'quoted_string')->gsm(
'string'));
129 foreach ($properties as $index => $property) {
130 $value = $values[$index];
132 $object->conditionByIsEqualTo($property, $value);
140 private function setQueryObjectOrderBy(Query $object, Machine $state_machine) {
141 $properties = $this->getStringsFromStringMachine($state_machine->gsm(
'order_var'));
143 $direction = $this->getStringsFromStringMachine($state_machine->gsm(
'order_asc'));
144 $sortMethod = (!empty($direction)) ?
"sortByAscending" :
"sortByDescending";
146 foreach ($properties as $property) {
147 $object->$sortMethod($property);
154 private function setQueryObjectLimit(Query $object, Machine $state_machine) {
155 $rows_limit = $this->configFactory->get(
'dkan_sql_endpoint.settings')->get(
'rows_limit');
157 $limit = $this->getStringsFromStringMachine($state_machine->gsm(
'numeric1'));
158 if (!empty($limit) && $limit[0] <= $rows_limit) {
159 $object->limitTo($limit[0]);
161 elseif ($object->count == FALSE) {
162 $object->limitTo($rows_limit);
165 $offset = $this->getStringsFromStringMachine($state_machine->gsm(
'numeric2'));
166 if (!empty($offset)) {
167 $object->offsetBy($offset[0]);
174 private function getStringsFromStringMachine(Machine $machine): array {
175 return (
new GetStringsFromStateMachineExecution($machine->execution))->get();