-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquerybuilder.html
728 lines (592 loc) · 30.5 KB
/
querybuilder.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ness PHP Framework | Docs</title>
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="assets/fnt-aw/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!--Load highlightjs package -->
<link rel="stylesheet" href="assets/js/highlightjs/styles/vs2015.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div class="wrapper">
<!-- Sidebar -->
<nav id="sidebar">
<div id="sidebar-header" class="text-center">
<br>
<a href="index.html" id="logo-area-link">
<img src="assets/img/nessphp_bordered.png" alt="Ness PHP Documentation" width="70%"/>
<h3>Ness PHP</h3>
<h4>Documentation</h4>
</a>
<a href="index.html" class="btn btn-light btn-sm" style="width: 100%;">
<i class="fa fa-angle-left" aria-hidden="true"></i> Back
</a>
</div>
<br>
<!--Load LATER-->
<div id="sidebar-loader" class="sidebar-contents">
</div>
</nav>
<div id="content">
<!--Nabvar:start-->
<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<button id="sidebarCollapse" class="btn-circle">
<i class="fa fa-bars" aria-hidden="true"></i>
</button>
<ul class="nav navbar-nav">
<li class="nav-item">
<a href="index.html" class="navbar-text-header">
Ness PHP Framework
</a>
</li>
</ul>
<button class="btn-circle d-inline-block d-lg-none ml-auto" type="button" data-toggle="collapse" data-target="#headernav-links" aria-controls="headernav-links" aria-expanded="false" aria-label="Toggle Menu">
<i class="fa fa-ellipsis-v" aria-hidden="true"></i>
</button>
<div class="collapse navbar-collapse" id="headernav-links">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link " href="https://nessphp.github.io/">
<i class="fa fa-terminal" aria-hidden="true"></i>
Ness PHP
</a>
</li>
<li class="nav-item">
<a class="nav-link " href="https://github.com/nessphp/application">
<i class="fa fa-github" aria-hidden="true"></i>
GitHub
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="support.html">
<i class="fa fa-heart" aria-hidden="true"></i>
Support
</a>
</li>
</ul>
</div>
</div>
</nav>
<!--Nabvar:end-->
<!-- ========================content:start======================== -->
<div id="holder">
<span class="pageheader">Query Builder</span>
<br>
<div class="row">
<div class="col-lg-8">
<ul class="hash-toc">
<li>Contents
<ul>
<li><a href="#pagedescription">Query Builder Class</a></li>
<li><a href="#simple_1">A Simple Query</a></li>
<li><a href="#simple_2">Select Multiple Columns</a></li>
<li><a href="#where_1">Get Specific Data</a></li>
<li><a href="#where_2">Get Specific Data with Multiple Conditions</a></li>
<li><a href="#where_3">Using where_or</a></li>
<li><a href="#like">Searching Data With 'like' methods</a></li>
<li>Wildcard Options
<ol class="hash-toc">
<li><a href="#wildcard_position">Wildcard Position</a></li>
<li><a href="#special_wildcard">Special Wildcard</a></li>
</ol>
</li>
<li><a href="#joins">Using JOIN's</a></li>
<li><a href="#group_by">group_by</a></li>
<li><a href="#order_by">order_by</a></li>
<li><a href="#limit">limit</a></li>
<li>Other Useful Commands
<ol class="hash-toc">
<li><a href="#sum">sum</a></li>
<li><a href="#min">min</a></li>
<li><a href="#max">max</a></li>
<li><a href="#avg">avg</a></li>
<li><a href="#count">count</a></li>
<li><a href="#distinct">distinct</a></li>
<li><a href="#column_as">column_as</a></li>
<li><a href="#free">free</a></li>
<li><a href="#frontfree">frontfree</a></li>
<li><a href="#endfree">endfree</a></li>
</ol>
</li>
<li><a href="#running">Running Generated Queries</a></li>
</ul>
</li>
</ul>
</div>
<!--Required Namespace -->
<div class="col-lg-4">
<div class="card bg-dark text-white">
<div class="card-header">Required Namespace</div>
<div class="card-body" >
<ul class="tree">
<li style="color:#28A745;">Ness
<ul>
<li style="color:#28A745;">Autopulse
<ul><li style="color:#28A745;">Factory
<ul><li style="color:#fff">QueryBuilder</li></ul>
</li></ul>
</li>
</ul>
</li>
</ul>
<hr>
<span><small>You can include this library to your code file by typing the below code;</small></span>
<br><br>
<span style="color:#3490CF;">use</span>
<span style="color:#28A745;">Ness\Autopulse\Factory</span>\QueryBuilder
</div>
</div>
</div>
<!--end: Required Namespace -->
</div>
<br><br>
<!--start: topic-->
<h3 id="pagedescription">Query Builder Class <small><a href="#content">#top</a></small></h3>
<p>
Query Builder class is used to generate queries and retrieve records from your database. Using a query builder class will give you a more understandable code, you will have a chance to access your records with few lines rather than creating and thinking on complex queries. You can run your 'select' queries using this library for 'update' and 'insert' commands you need to use 'Autopulse' library. You can take a look at <a href="autopulse.html">Autopulse Library Here.</a>
<br>
</p>
<hr>
<div class="alert alert-warning">
<strong>About Queries </strong> All your queries must end with <span class="ug-keyword ug-code">get()</span> function. This is required for returning your current query. </span>
</div>
<br><br>
<h3 id="simple_1">A Simple Query <small><a href="#content">#top</a></small></h3>
<p>
Following code example show's you how to select all data from a table.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select()
->table("members")
->get();
//Generated: select * from members
</code>
</pre>
</p>
<br><br>
<br><br>
<h3 id="simple_2">Select Multiple Columns <small><a href="#content">#top</a></small></h3>
<p>
This example is about chained selects for multiple columns. You can do the same task by using arrays. The following second example
will show you how to use arrays.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("email")->select("password")
->table("members")
->get();
//Generated: select email, password from members
</code>
</pre>
<br> <br>
The same thing can be done with arrays.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(array("username", "email", "password"))
->table("members")
->get();
//Generated: select username, email, password from members
//or you can use
$mycommand = $qb->select("username, email, password")->table("users")->get();
//This will generate: select username, email, password from users
</code>
</pre>
</p>
<br><br>
<br><br>
<h3 id="where_1">Get Specific Data <small><a href="#content">#top</a></small></h3>
<p>
Following code example show's you how to select a specific data from a table.
<pre>
<code class="php">
$mycommand = $qb->select("username")
->table("users")
->where("id=", "1955")
->get();
//Generated: select username from users where id ='1955'
</code>
</pre>
</p>
<br><br>
<br><br>
<h3 id="where_2">Get Specific Data with Multiple Conditions <small><a href="#content">#top</a></small></h3>
<p>
You can use chained where function or array for multiple conditions. These two code lines will show you an example about
that.
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("username")
->table("users")
->where("age >=", "18")->where("country_code=", "32")
->get();
//Generated: select username from users where age >='18' AND country_code='32'
</code>
</pre>
<br>
The following code lines will return same result.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("username")
->table("users")
->where(array("age >=" => "18", "country_code=" => "32"))
->get();
//Generated: select username from users where age >='18' AND country_code='32'
</code>
</pre>
</p>
<br><br>
<br><br>
<h3 id="where_3">Using where_or <small><a href="#content">#top</a></small></h3>
<p>
This function is for where condition with OR
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("username")
->table("users")
->where_or("age >=", "18")->where("country_code=", "32")
->get();
//Generated: select username from users where age >='18' OR country_code='32'
</code>
</pre>
<br>
The following code lines will return same result.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("username")
->table("users")
->where_or(array("age >=" => "18", "country_code=" => "32"))
->get();
//Generated: select username from users where age >='18' OR country_code='32'
</code>
</pre>
<br>
You can connect where with where_or:
<br>
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("*")
->table("users")
->where("id >","10")->where_or(array("country_code="=>"10"))->where("gender=","m")
->get();
//Generated: select * from users where id >'10' OR country_code='10' AND gender='m'
</code>
</pre>
</p>
<br><br>
<br><br>
<h3 id="like">Searching Data With 'like' methods <small><a href="#content">#top</a></small></h3>
<p>
You can generate queries with LIKE condition.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(array("product_name", "author", "version"))
->table("products pr")
->like("product_name", "nessphp")
->get();
//Generated: select product_name, author, version from products pr WHERE product_name LIKE '%nessphp%'
</code>
</pre>
<br>
You can also use this method with chained methods.
<pre>
<code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(array("pr.product_name", "pr.author", "pr.version"))
->table("products pr")
->like("pr.product_name", "nessphp")->like("author","Sinan")
->get();
//Generated: select pr.product_name, pr.author, pr.version from products pr
//WHERE pr.product_name LIKE '%nessphp%' AND author LIKE '%Sinan%'
</code>
</pre>
<br><br>
<div class="alert alert-info">
<strong>Setting Wildcard Options</strong> As you have noticed, the wildcard in the like method is found both at the beginning and at the end.
You can set this with some special functions. The following code should be followed for this.
</div>
<h4 id="wildcard_position">Wildcard Position <small><a href="#content">#top</a></small></h4>
<pre>
<code class="php">
$mycommand = $qb->select("product_name")
->table("products")
->like("author", "Sinan", $qb->wildcard->Both())
->get();
//Generated: ..... author LIKE '%Sinan%'
</code>
</pre>
<b>Front: $qb->wildcard->Front()</b>
<pre>
<code class="php">
$mycommand = $qb->select("product_name")
->table("products")
->like("author", "Sinan", $qb->wildcard->Front())
->get();
//Generated: ..... author LIKE '%Sinan'
</code>
</pre>
<b>End: $qb->wildcard->End()</b>
<pre>
<code class="php">
$mycommand = $qb->select("product_name")
->table("products")
->like("author", "Sinan", $qb->wildcard->End())
->get();
//Generated: ..... author LIKE 'Sinan%'
</code>
</pre>
<br>
You can also set a special wildcard charecter for LIKE clause. You can do that by;
<br>
<h4 id="special_wildcard">Special Wildcard <small><a href="#content">#top</a></small></h4>
<pre>
<code class="php">
$qb = new QueryBuilder();
$qb->wildcard->setWildcard("_");
$mycommand = $qb->select("product_name")
->table("products")
->like("author", "Sinan", $qb->wildcard->End())
->get();
</code>
</pre>
Running command with <span class="ug-code ug-bold ug-folder">$qb->wildcard->setWildcard("_");</span> will set your <span class="ug-code ug-bold ug-keyword">%</span> to <span class="ug-code ug-keyword ug-bold">_</span> wildcard.
<br><br>
You can get your current wildcard by calling <span class="ug-code ug-bold ug-folder">$qb->wildcard->getWildcard();</span> this will return as string your current wildcard.
<br>
<h4 id="liketypes">Other types of LIKE </h4>
You can also call the LIKE method with some other customized methods. Here is a list of them;
<br><br>
<span class="ug-code ug-keyword ug-bold">like_not</span>: The use of this method is identical to LIKE. The only difference between them is the function. like_not method creates queries with NOT restriction.
<br><br>
<span class="ug-code ug-keyword ug-bold">like_or</span>: Standart 'like' method will connect conditions with 'AND' if you want to add a OR condition you need to call your 'like' method with this 'like_or' method.
<br><br>
<span class="ug-code ug-keyword ug-bold">like_or_not</span>: Standart 'like_not' method will connect conditions with 'AND' if you want to add a OR condition you need to call your 'like_not' method with this 'like_or_not' method.
<br><br>
</p>
<div class="alert alert-info">
<strong>INFO</strong>
<span>All your wildcard options can be used in all types or like methods </span>
</div>
</p>
<h3 id="joins">Using JOIN's <small><a href="#content">#top</a></small></h3>
<p>
by default your queries with JOIN should be;
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("u.username")
->table("users u")
->join("roles r", "r.roleid = u.roleid")
->get();
</code>
</pre>
<b>Or you can connect multiple joins with each other</b>
<pre> <code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("u.username, m.message")
->table("users u")
->join("roles r", "r.roleid = u.roleid")
->join("mesaages m", "m.userid = u.userid")
->get();
</code>
</pre>
<div class="alert alert-info">
<strong>JOIN TYPES</strong>
You can set a join type by adding a third parameter to your join method in your query. For example you can set it "INNER", "OUTER","RIGHT" or "LEFT"
</div>
</p>
<b>Example: </b>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("u.username, m.message")
->table("users u")
->join("roles r", "r.roleid = u.roleid", <span class="ug-keyword ug-bold">"INNER"</span>)
->join("mesaages m", "m.userid = u.userid", <span class="ug-keyword ug-bold">"INNER"</span>)
->get();
</code>
</pre>
<br>
<h3 id="group_by">group_by <small><a href="#content">#top</a></small></h3>
<p>
Add a group by condition to your query
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("message_type, message_body")
->table("messages")
->group_by("message_type")
->get();
</code>
</pre>
<br>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("message_type, message_body")
->table("messages")
->group_by(array("message_type", "other_1", "other_2", ... , "other_n")
->get();
</code>
</pre>
</p>
<br>
<h3 id="order_by">order_by <small><a href="#content">#top</a></small></h3>
<p>
Set aorder by condition to your query
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("message_type, message_body")
->table("messages")
->order_by("message_date", "ASC")
->get();
</code>
</pre>
</p>
<br>
<h3 id="limit">limit <small><a href="#content">#top</a></small></h3>
<p>
Add LIMIT condition to your query
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select("message_type, message_body")
->table("messages")
->order_by("message_date", "ASC")
->limit(20)
->get();</code>
</pre>
</p>
<br>
<h3 id="others">Other Useful Commands <small><a href="#content">#top</a></small></h3>
<p>
<h4 id="sum">sum() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::sum("salary"))
->table("employee")
->get();
//select SUM(salary) from employee
</code>
</pre>
</p>
<h4 id="min">min() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::min("salary"))
->table("employee")
->get();
//select MIN(salary) from employee</code>
</pre>
</p>
<h4 id="max">max() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::max("salary"))
->table("employee")
->get();
//select MAX(salary) from employee</code>
</pre>
</p>
<h4 id="avg">avg() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::avg("salary"))
->table("employee")
->get();
//select AVG(salary) from employee</code>
</pre>
</p>
<h4 id="count">count() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::count("*"))
->table("employee")
->get();
//select count(*) from employee</code>
</pre>
</p>
<h4 id="distinct">distinct() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::distinct("user_name"))
->table("employee")
->get();
//select distinct user_name from employee</code>
</pre>
</p>
<h4 id="column_as">column_as()<small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$mycommand = $qb->select(QueryBuilder::column_as("u.username", "fullName"))
->table("employee")
->get();
//select u.username AS fullName from employee</code>
</pre>
</p>
<h4 id="free">free()<small> <a href="#content">#top</a></small></h4>
Returns the value in parameter.
<pre><code class="php">
QueryBuilder:free("select * from users");</code>
</pre>
</p>
<h4 id="frontFree">frontfree() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$qb->frontFree("-- DISABLED");
$mycommand = $qb->select(QueryBuilder::column_as("u.username", "fullName"))
->table("employee")
->get();
// -- DISABLED select u.username AS fullName from employee</code>
</pre>
</p>
<h4 id="endfree">endfree() <small><a href="#content">#top</a></small></h4>
<pre><code class="php">
$qb = new QueryBuilder();
$qb->endFree("-- THIS QUERY SELECTS ALL RECORDS FROM employee TABLE");
$mycommand = $qb->select(QueryBuilder::column_as("u.username", "fullName"))
->table("employee")
->get();
//select u.username AS fullName from employee -- THIS QUERY SELECTS ALL RECORDS FROM employee TABLE</code>
</pre>
<h3 id="running">Running Generated Queries <small><a href="#content">#top</a></small></h3>
<p>
All methods and returned values in QueryBuilder class is stored in a variable and send to any variable or parameter as string.
For running a query generated with QueryBuilder class you need to define a connection to database. This example will guide you to
create a simple query and run it with mysqlconnect and mysqlcommand class.
<br>
<pre><code class="php">
//Create a query builder instance
$qb = new QueryBuilder();
//create a command
$mycommand = $qb->select(array("u.email",
QueryBuilder::column_as(QueryBuilder::free("concat_ws(' ', u.name, 'u.surname')"), "fullName")))
->table("employee")
->get();
//define connection
$con = new MySqlConnect("localhost", "root", "my_pwd_324234", "employee");
//fill command to MySqlCommand class
$cmd = new MySqlCommand($mycommand, $con->Source());
//returned data as array in var_dump
var_dump($cmd->FetchAll());</code>
</pre>
</p>
<br><br>
<!--end: topic-->
</div>
<!-- ========================content:end======================== -->
</div>
</div>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/nessphp.js"></script>
<script src="assets/js/highlightjs/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>