I can't make increment number of rows on my table..
I used revel framework, and try make simple crud.
The question is: "How to show increment number with Golang.?".
This my code of controller book.go
This controller to show data from postgresql database.
func allBooks() ([]models.Book, error) {
//Retrieve
books := []models.Book{}
rows, err := db.Query("SELECT id, name, author, pages, publication_date FROM books order by id")
defer rows.Close()
if err == nil {
for rows.Next() {
var id int
var name string
var author string
var pages int
var publicationDate pq.NullTime
err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
if err == nil {
currentBook := models.Book{ID: id, Name: name, Author: author, Pages: pages}
if publicationDate.Valid {
currentBook.PublicationDate = publicationDate.Time
}
books = append(books, currentBook)
} else {
return books, err
}
}
} else {
return books, err
}
return books, err
}
And this html on my views index.html:
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Author</th>
<th>Pages</th>
<th>Publication Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{{range .books}}
<tr>
<td>{{.Nomor}}</td>
<td>{{.ID}}</td>
<td>{{.Name}}</td>
<td>{{.Author}}</td>
<td>{{.Pages}}</td>
<td>{{.PublicationDateStr}}</td>
</tr>
{{end}}
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>New book</td>
</tr>
</tbody>
</table>
hope result like this
|No.|ID |Name |Author | Pages | Publication Date |
|1 |2 |Jack |Holly | 255 | 2017-10-15 |
|2 |4 |hans |Holly | 255 | 2017-10-15 |
|3 |6 |Juri |Holly | 255 | 2017-10-15 |
|4 |7 |Hank |Holly | 255 | 2017-10-15 |
but what I get is
|No.|ID |Name |Author | Pages | Publication Date |
|0 |2 |Jack |Holly | 255 | 2017-10-15 |
|0 |4 |hans |Holly | 255 | 2017-10-15 |
|0 |6 |Juri |Holly | 255 | 2017-10-15 |
|0 |7 |Hank |Holly | 255 | 2017-10-15 |
I do not see the property Nomor populated anywhere.
You should keep a counter and do something like
...
currentBook := models.Book{Nomor: count, ID: id, Name: name, Author: author, Pages: pages}
count = count + 1
...
You could try assinging the values of slice index to a variable and try printing it, something like
{{range $index, _ := .books}}
and use the index instead of Nomor
Related
I have been following a tutorial on the Internet about the one-to-many relationships in Eloquent (in Laravel 8). It has a simple mySQL database with two simple tables ("cars" and "car_models") that were created by using Laravel migration.
Here is the migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCarsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('founded');
$table->longText('description');
$table->timestamps();
});
Schema::create('car_models', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('car_id');
$table->string('model_name');
$table->timestamps();
$table->foreign('car_id')
->references('id')
->on('cars')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('cars');
}
}
Here is the "cars" table:
mysql> desc cars;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| founded | int | NO | | NULL | |
| description | longtext | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.34 sec)
mysql> select * from cars;
+----+------+---------+-------------------------------------+------------+------------+
| id | name | founded | description | created_at | updated_at |
+----+------+---------+-------------------------------------+------------+------------+
| 2 | Audi | 1908 | this is not the description of Audi | NULL | NULL |
+----+------+---------+-------------------------------------+------------+------------+
1 row in set (0.01 sec)
Here is the "car_models" table:
mysql> desc car_models;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| car_id | int unsigned | NO | MUL | NULL | |
| model_name | varchar(255) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> select * from car_models;
+----+--------+------------+------------+------------+
| id | car_id | model_name | created_at | updated_at |
+----+--------+------------+------------+------------+
| 5 | 2 | A1 | NULL | NULL |
| 6 | 2 | A3 | NULL | NULL |
| 7 | 2 | A5 | NULL | NULL |
+----+--------+------------+------------+------------+
3 rows in set (0.00 sec)
What the sample project does is, all the rows from the "cars" table are pulled and feeds to a view. The user can then click on any of the brand names and the project will display all the models of that brand on another view. There are two models in the project that describe the data.
Here is the "Car" model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
use HasFactory;
protected $table = 'cars';
protected $primaryKey = 'id';
// public $timestamps = true;
protected $fillable = ['name', 'founded', 'description'];
public function carModels() {
return $this -> hasMany(CarModel::class, 'id', 'car_id');
}
}
Here is the "CarModel" model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CarModel extends Model
{
use HasFactory;
protected $table = 'cars';
protected $primaryKey = 'id';
public function car() {
return $this -> belongsTo(Car::class);
}
}
When I followed the tutorial strictly in the beginning, the project would bork with a 1054 error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cars.car_id' in 'where clause'
That was fixed after I added in the two optional parameters "foreign key" and "local key" to the hasMany() in the "Car" model. However, the resulting webpage says "No models found" when it should display "A1 A3 A5"
screenshot of output
Here is the relevant view:
#extends('layouts.app')
#section('contents')
<div class="m-auto w-4/5 py-24">
<div class="text-center">
<h1 class="text-5xl uppercase bold">{{ $car -> name }}</h1>
</div>
<div class="w-5/6 py-10">
<div class="m-auto">
<span class="uppercase text-blue-500 fold-bold text-xs italic">Founded: {{ $car -> founded }}</span>
<h2 class="text-gray-700 text-5xl hover:text-gray-500">
{{ $car -> name }}
</h2>
<p class="text-lg text-gray-700 py-6">{{ $car -> description }}</p>
</div>
<ul>
<p class="text-lg text-gray-700 py-3">Models:</p>
#forelse ($car -> carModels as $model)
<li class="inline italic text-gray-600 px-1 py-6">
{{ $model['model_name'] }}
</li>
#empty
<p>No models found</p>
#endforelse
</ul>
<hr class="mt-4 mb-8" />
</div>
</div>
#endsection
The id is correct as dd() returned a 2.
I have been staring at this for a few hours and my brains are now frizzled. A fresh pair of eyes can assist greatly. Thank you very much.
Okay, I found out what's wrong.
In the "CarModels" model, the protected $table should be "car_models," not "cars."
I need somehow to group my rows by specific condition
|id | address | last_name | Count of purchases | customer_number |
|1 | de Berlin | name_1 | 1 | 11111 |
|2 | de Berlin | name_2 | 1 | 12345 |
|3 | de Berlin | name_1 | 1 | 12345 |
So the problem is that I need to group by address AND last_name BUT in this case row with ID = 2 will not be in set because it has different last_name BUT it shares the same customer_number number with row with ID = 3. Can I do it somehow with one query?
So basically I want to receive something like
select SUM(Count_of_purchases), array_agg(last_name), array_agg(customer_number)
from table
group by f(address, last_name, customer_number)
| 3 | {name_1, name_2} | {11111, 12345} |
I am having a data frame as shown below. The number of signals are more than 100, so there will be more than 100 columns in the data frame.
+---+------------+--------+--------+--------+
|id | date|signal01|signal02|signal03|......
+---+------------+--------+--------+--------+
|050|2021-01-14 |1 |3 |1 |
|050|2021-01-15 |null |4 |2 |
|050|2021-02-02 |2 |3 |3 |
|051|2021-01-14 |1 |3 |0 |
|051|2021-01-15 |2 |null |null |
|051|2021-02-02 |3 |3 |2 |
|051|2021-02-03 |1 |3 |1 |
|052|2021-03-03 |1 |3 |0 |
|052|2021-03-05 |3 |3 |null |
|052|2021-03-06 |2 |null |2 |
|052|2021-03-16 |3 |5 |5 |.......
+-------------------------------------------+
I have to find out cummax of each signal and then compare with respective signal columns and delete the signal records which are having value lower than cummax and null values.
step1. find cumulative max for each signal with respect to id column.
step2. delete the records which are having lower value than cummax for each signal.
step3. Take count of records which are having cummax less than signal value(excluded of null) for each signals with respect to id.
After the count the final output should be as shown below.
+---+------------+--------+--------+--------+
|id | date|signal01|signal02|signal03|.....
+---+------------+--------+--------+--------+
|050|2021-01-14 |1 | 3 | 1 |
|050|2021-01-15 |null | null | 2 |
|050|2021-02-02 |2 | 3 | 3 |
|
|051|2021-01-14 |1 | 3 | 0 |
|051|2021-01-15 |2 | null | null |
|051|2021-02-02 |3 | 3 | 2 |
|051|2021-02-03 |null | 3 | null |
|
|052|2021-03-03 |1 | 3 | 0 |
|052|2021-03-05 |3 | 3 | null |
|052|2021-03-06 |null | null | 2 |
|052|2021-03-16 |3 | 5 | 5 | ......
+----------------+--------+--------+--------+
I have tried by using window function as below and it worked for almost all records.
val w = Window.partitionBy("id").orderBy("date").rowsBetween(Window.unboundedPreceding, Window.currentRow)
val signalList01 = ListBuffer[Column]()
signalList01.append(col("id"), col("date"))
for (column <- signalColumns) {
// Applying the max non null aggregate function on each signal column
signalList01 += (col(column), max(column).over(w).alias(column+"_cummax")) }
val cumMaxDf = df.select(signalList01: _*)
But I am getting error values as shown below for few records.
Is there any idea about how this error records in the cummax column? Any leads appreciated!
Just giving out hints here (as you suggested) to help you unblock the situation, but --WARNING-- haven't tested the code !
the code you provided in the comments looks good. It'll get you your max column
val nw_df = original_df.withColumn("singal01_cummax", sum(col("singal01")).over(windowCodedSO))
now, you need to be able to compare the two values in "singal01" and "singal01_cummax". A function like this, maybe:
def takeOutRecordsLessThanCummax (signal:Int, singal_cummax: Int) : Any =
{ if (signal == null || signal < singal_cummax) null
else singal_cummax }
since we'll be applying it to columns, we'll wrap it up in a UDF
val takeOutRecordsLessThanCummaxUDF : UserDefinedFunction = udf {
(i:Int, j:Int) => takeOutRecordsLessThanCummax(i,j)
}
and then, you can combine everything above so it can be applicable on your original dataframe. Something like this could work:
val signal_cummax_suffix = "_cummax"
val result = original_df.columns.foldLeft(original_df)(
(dfac, colname) => dfac
.withColumn(colname.concat(signal_cummax_suffix),
sum(col(colname)).over(windowCodedSO))
.withColumn(colname.concat("output"),
takeOutRecordsLessThanCummaxUDF(col(colname), col(colname.concat(signal_cummax_suffix))))
)
I would like to know how to add a caption to a table defined in a markdown file for Doxygen. I know that Doxygen can generate a table from the following code in a .md file, but I could not find a way to add a caption to this table.
|Name |Age|
|-----|---|
|Alice| 18|
|Bob | 23|
do you mean something like this, which is possible
###H6
|Header1 | Header2 | Header 3 |
|--------|---------|----------|
|0 | 1 | 7 |
|2 | 5 | 3 |
|9 | 7 | 2 |
or something like this, which is not possible, at least not by only using simple .md notation
| Caption |
|Header1 | Header2 | Header 3 |
|--------|---------|----------|
|0 | 1 | 7 |
|2 | 5 | 3 |
|9 | 7 | 2 |
if you want the second version you should use .html tables
Unfortunately the second is impossible in doxygen.
you would need multimarkdown support, which is shown by the link the table example but is not supported by doxygen yet, right now doxygen only supports simple tables
you use only html
you find a workaround which suites you, something like this:
<table>
<tr>
<td colspan=1>
Caption
</td>
|Header1 | Header2 | Header 3 |
|--------|---------|----------|
|0 | 1 | 7 |
|2 | 5 | 3 |
|9 | 7 | 2 |
</tr>
I am trying to determine which types or methods in a base framework assembly are being used by other assemblies in the application system. I cannot seem to find a straight-cut query to do that.
What i have to do is first determine which assemblies are directly using the framework assembly, then manually list them in a second query
SELECT TYPES FROM ASSEMBLIES "IBM.Data.DB2"
WHERE IsDirectlyUsedBy "ASSEMBLY:FirstDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:SecondDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:ThirdDirectUsedByAssebmly"
OR IsDirectlyUsedBy "ASSEMBLY:FourthDirectUsedByAssebmly"
Is there a better/faster way to query for this?
Additionally, the query results are focused on the matched types only. The Dependency graph or matrix exported only shows details of those. I do not know how to render a graph that shows those types or methods plus show the dependent types/methods from other assemblies that are consuming them?
UPDATE
I cannot use a query like
SELECT METHODS/TYPES WHERE IsPublic AND !CouldBeInternal
because the results return very queer results of using obfuscated types within the IBM.Data.DB2 assembly.
SELECT TYPES
FROM ASSEMBLIES "IBM.Data.DB2"
WHERE IsPublic AND !CouldBeInternal
48 items
--------------------------------------------------+--------------+
types |# IL instructi|
|ons |
--------------------------------------------------+--------------+
IBM.Data.DB2.ae+m |0 |
| |
IBM.Data.DB2.ae+x |0 |
| |
IBM.Data.DB2.ae+f |0 |
| |
IBM.Data.DB2.ae+ac |0 |
| |
IBM.Data.DB2.ae+aa |0 |
| |
IBM.Data.DB2.ae+u |0 |
| |
IBM.Data.DB2.ae+z |0 |
| |
IBM.Data.DB2.ae+e |0 |
| |
IBM.Data.DB2.ae+b |0 |
| |
IBM.Data.DB2.ae+g |0 |
| |
IBM.Data.DB2.ae+ab |0 |
| |
IBM.Data.DB2.ae+h |0 |
| |
IBM.Data.DB2.ae+r |0 |
| |
IBM.Data.DB2.ae+p |0 |
| |
IBM.Data.DB2.ae+ad |0 |
| |
IBM.Data.DB2.ae+i |0 |
| |
IBM.Data.DB2.ae+j |0 |
| |
IBM.Data.DB2.ae+t |0 |
| |
IBM.Data.DB2.ae+af |0 |
| |
IBM.Data.DB2.ae+k |0 |
| |
IBM.Data.DB2.ae+l |0 |
| |
IBM.Data.DB2.ae+y |0 |
| |
IBM.Data.DB2.ae+a |0 |
| |
IBM.Data.DB2.ae+q |0 |
| |
IBM.Data.DB2.ae+n |0 |
| |
IBM.Data.DB2.ae+d |0 |
| |
IBM.Data.DB2.ae+c |0 |
| |
IBM.Data.DB2.ae+ae |0 |
| |
IBM.Data.DB2.ae+o |0 |
| |
IBM.Data.DB2.ae+w |0 |
| |
IBM.Data.DB2.ae+s |0 |
| |
IBM.Data.DB2.ae+v |0 |
| |
IBM.Data.DB2.DB2Command |2 527 |
| |
IBM.Data.DB2.DB2Connection |3 246 |
| |
IBM.Data.DB2.DB2DataAdapter |520 |
| |
IBM.Data.DB2.DB2DataReader |4 220 |
| |
IBM.Data.DB2.DB2_UDF_PLATFORM |0 |
| |
IBM.Data.DB2.DB2Enumerator+DB2EnumInstance |19 |
| |
IBM.Data.DB2.DB2Enumerator+DB2EnumDatabase |15 |
| |
IBM.Data.DB2.DB2Error |98 |
| |
IBM.Data.DB2.DB2ErrorCollection |55 |
| |
IBM.Data.DB2.DB2Exception |185 |
| |
IBM.Data.DB2.DB2Parameter |1 853 |
| |
IBM.Data.DB2.DB2ParameterCollection |1 383 |
| |
IBM.Data.DB2.DB2RowUpdatedEventHandler |0 |
| |
IBM.Data.DB2.DB2RowUpdatedEventArgs |14 |
| |
IBM.Data.DB2.DB2Type |0 |
| |
IBM.Data.DB2.DB2XmlReader |500 |
| |
--------------------------------------------------+--------------+
Sum: |14 635 |
| |
Average: |304.9 |
| |
Minimum: |0 |
| |
Maximum: |4 220 |
| |
Standard deviation: |868.22 |
| |
Variance: |753 808 |
| |
--------------------------------------------------+--------------+
Our code does not use those types and enums directly.
This query returns the methods (respectively the types), that are public and could not be internal. Hence, it returns the methods/types that are indeed used outside of their declaring assembly.
SELECT METHODS/TYPES WHERE IsPublic AND !CouldBeInternal