Skip to content

Commit c3bc261

Browse files
committed
re-added prepared statement usage
1 parent 67a9646 commit c3bc261

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

src/PDOSQLiteAdapter.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ public function getNumberOfRows($sql)
289289
return $rowCount;
290290
}
291291

292-
public function simpleQuery(string $sql): bool
292+
public function simpleQuery(string $sql, array $params = []): bool
293293
{
294294
// save query
295295
$this->queries[] = [
296296
'query' => $sql,
297297
'by_function' => 'simpleQuery',
298298
];
299299

300-
$stmt = $this->db->prepare($sql);
300+
$stmt = $this->db->prepare($sql, $params);
301301
$stmt->execute();
302302
$this->lastRowCount = $stmt->rowCount();
303303
$stmt->closeCursor();
@@ -345,21 +345,24 @@ public function insert(string $table, array $data): int
345345
$sql .= ') VALUES (';
346346

347347
// add placeholders for each value; collect values
348-
$values = [];
348+
$placeholders = [];
349+
$params = [];
349350
foreach ($data as $v) {
350-
$values[] = '"'.$v.'"';
351+
$placeholders[] = '?';
352+
$params[] = $v;
351353
}
352-
$sql .= implode(', ', $values);
354+
$sql .= implode(', ', $placeholders);
353355

354356
$sql .= ')';
355357

356358
/*
357359
* SQL looks like the following now:
358-
*
359-
* INSERT INTO foo (foo) ("bar")
360+
* INSERT INTO foo (bar) (?)
360361
*/
361362

362-
$this->exec($sql);
363+
// Setup and run prepared statement
364+
$stmt = $this->db->prepare($sql);
365+
$stmt->execute($params);
363366

364367
return $this->db->lastInsertId();
365368
}

src/Rdf/Literal.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ public function __construct(
3232
?string $datatype = null
3333
) {
3434
$this->value = $value;
35-
// TODO later check with feedback on
36-
// https://github.com/sweetrdf/rdfInterface/issues/14
37-
$this->lang = !empty($lang) ? $lang : null;
38-
$this->datatype = $datatype ?? NamespaceHelper::NAMESPACE_XSD.'string';
35+
36+
/*
37+
* @see https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
38+
*/
39+
if (!empty($lang)) {
40+
$this->lang = $lang;
41+
$this->datatype = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
42+
} else {
43+
$this->lang = null;
44+
$this->datatype = $datatype ?? NamespaceHelper::NAMESPACE_XSD.'string';
45+
}
3946
}
4047

4148
public function __toString(): string

src/Store/QueryHandler/DeleteQueryHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function cleanTableReferences()
170170
LEFT JOIN g2t G ON G.t = T.t
171171
WHERE G.t IS NULL';
172172
$sql .= ')';
173-
$this->store->getDBObject()->simpleQuery($sql);
173+
$this->store->getDBObject()->exec($sql);
174174
}
175175
/* check for unconnected graph refs */
176176
if ((1 == rand(1, 10))) {
@@ -185,7 +185,7 @@ private function cleanTableReferences()
185185
LEFT JOIN triple T ON (T.t = G.t)
186186
WHERE T.t IS NULL
187187
';
188-
$this->store->getDBObject()->simpleQuery($sql);
188+
$this->store->getDBObject()->exec($sql);
189189
}
190190
}
191191
}

src/Store/QueryHandler/LoadQueryHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ public function checkSQLBuffers($force_write = 0, $reset_id_buffers = 0)
349349
foreach (['triple', 'g2t', 'id2val', 's2val', 'o2val'] as $tbl) {
350350
$buffer_size = isset($this->sql_buffers[$tbl]) ? 1 : 0;
351351
if ($buffer_size && $force_write) {
352-
$this->store->getDBObject()->simpleQuery($this->sql_buffers[$tbl]);
352+
$this->store->getDBObject()->exec($this->sql_buffers[$tbl]);
353353
/* table error */
354-
$error = $this->store->getDBObject()->getErrorMessage();
354+
$this->store->getDBObject()->getErrorMessage();
355355
unset($this->sql_buffers[$tbl]);
356356

357357
/* reset term id buffers */

src/Store/QueryHandler/SelectQueryHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function runQuery($infos)
3333
$r = $this->getFinalQueryResult($q_sql, $tmp_tbl);
3434

3535
/* remove intermediate results */
36-
$this->store->getDBObject()->simpleQuery('DROP TABLE IF EXISTS '.$tmp_tbl);
36+
$this->store->getDBObject()->exec('DROP TABLE IF EXISTS '.$tmp_tbl);
3737

3838
return $r;
3939
}

tests/Integration/Rdf/TermsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static function getDataFactory(): iDataFactory
2323
return new DataFactory();
2424
}
2525

26+
public function testLiteralFactory(): void
27+
{
28+
$this->markTestSkipped('Skipped for now. Wait until feedback/merge of https://github.com/sweetrdf/rdfInterface/pull/17.');
29+
}
30+
2631
public function testLiteralWith(): void
2732
{
2833
$this->markTestSkipped('Function to test not implemented yet.');

tests/Integration/Store/InMemoryStoreSqlite/Query/InsertIntoQueryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,15 @@ public function testMultipleInsertQueriesInDifferentGraphs()
624624
$this->assertEquals(3, \count($res['result']['rows']));
625625
}
626626

627+
public function testValueEscape()
628+
{
629+
$this->subjectUnderTest->query('INSERT INTO <http://graph1/> {<http://foo/1> <http://foo/2> \'"foobar";\' . }');
630+
$this->subjectUnderTest->query('INSERT INTO <http://graph1/> {<http://foo/1> <http://foo/2> "\'foobar2\'" . }');
631+
632+
$res = $this->subjectUnderTest->query('SELECT * FROM <http://graph1/> WHERE {?s ?p ?o.}');
633+
$this->assertEquals(2, \count($res['result']['rows']));
634+
}
635+
627636
/**
628637
* Adds bulk of triples to test behavior.
629638
* May take at least one second to finish.

0 commit comments

Comments
 (0)