aboutsummaryrefslogtreecommitdiff
path: root/lib/sql
AgeCommit message (Collapse)Author
8 dayslib/sql: remove JoinValues in favour of [slices.Concat]Shulhan
8 dayslib/sql: add method Meta to ClientShulhan
The Meta method returns new instance of [Meta] based on the current client driver and kind arugment. While at it, update comments on most methods to use adjectives.
8 dayslib/sql: refactoring the BindWhereShulhan
Changes the BindWhere parameters from two to four. The first parameter is the OR/AND logic. The second parameter is the column name. The third parameter is the operator like "=", "!=". The fourth parameter is the value.
2026-01-15all: convert license and copyright to use SPDX identifiersShulhan
With help of spdxconv tool [1], we able to bulk update all files license and copyright format to comply with SPDX formats. [1] https://kilabit.info/project/spdxconv/
2025-01-23all: replace "interface{}" with "any"Shulhan
2024-08-09lib/sql: replace [http.FileSystem] with [memfs.MemFS]Shulhan
Accepting the [http.FileSystem] means that the parameter can receive an instance of [embed.FS], but in most cases, it will fail. Case example, when we embed SQL files for migration under "db/migration" using the "go:embed" directive, //go:embed db/migration/*.sql var DBMigrationFS embed.FS and then call the [Migrate] function, it will not found any ".sql" files inside the "/" directory because the files is stored under "db/migration/" prefix (also there is no "/" when using embed.FS).
2024-03-05lib/sql: remove deprecated Row typeShulhan
The Row type has been replaced with Meta type with more flexibility and features for generating type-safe SQL DML.
2024-03-05all: comply with linter recommendations #2Shulhan
HTTP request now implicitly create request with context. Any false positive related to not closing HTTP response body has been annotated with "nolint:bodyclose". In the example code, use consistent "// Output:" comment format, by prefixing with single space. Any comment on code now also prefixing with single space. An error returned without variables now use [errors.New] instead of [fmt.Errorf]. Any error returned using [fmt.Errorf] now wrapped using "%w" instead of "%s". Also, replace error checking using [errors.Is] or [errors.As], instead of using equal/not-equal operator. Any statement like "x = x OP y" now replaced with "x OP= y". Also, swap statement is simplified using "x, y = y, x". Any switch statement with single case now replaced with if-condition. Any call to defer on function or program that call [os.Exit], now replaced by calling the deferred function directly. Any if-else condition now replaced with switch statement, if possible.
2024-03-05all: comply with linter recommendations #1Shulhan
Instead of annotating the lines that caught by linters, fix it to comply with the recommendations. This causes several breaking changes, especially related to naming, * api/slack: [Message.IconUrl] become [Message.IconURL] * lib/dns: DefaultSoaMinumumTtl become DefaultSoaMinimumTTL * lib/email: [Message.SetBodyHtml] become [Message.SetBodyHTML] * lib/http: [Client.GenerateHttpRequest] become [Client.GenerateHTTPRequest] * lib/http: [ClientOptions.ServerUrl] become [ClientOptions.ServerURL] * lib/http: [EndpointRequest.HttpWriter] become [EndpointRequest.HTTPWriter] * lib/http: [EndpointRequest.HttpRequest] become [EndpointRequest.HTTPRequest] * lib/http: [ServerOptions.EnableIndexHtml] become [ServerOptions.EnableIndexHTML] * lib/http: [SSEConn.HttpRequest] become [SSEConn.HTTPRequest] * lib/smtp: [ClientOptions.ServerUrl] become [ClientOptions.ServerURL] * lib/ssh/sftp: [FileAttrs.SetUid] become [FileAttrs.SetUID] * lib/ssh/sftp: [FileAttrs.Uid] become [FileAttrs.UID]
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2024-03-02lib/sql: realign struct in testShulhan
2024-02-29lib/sql: handle binding with the same nameShulhan
If [Meta.Bind] is called with the same name again, it should replace the existing named value.
2024-01-30lib/sql: refactor WhereFields to join where condition with holder as isShulhan
When binding parameter with BindWhere, the first parameter will be joined with the holder. For example, BindWhere("colname>=", p) will result in "colname>=$1". While at it, set offset based on [Meta.kind].
2024-01-27lib/sql: remove type TableShulhan
This type has never been used and the implementation is not correct.
2024-01-27lib/sql: support update, bind where condition, and subquery in MetaShulhan
This changes add parameter kind to NewMeta, which define the kind of DML to be mapped by Meta. The method Add renamed to Bind, and AddWhere renamed to BindWhere. New methods added to Meta, - Sub return the child of Meta for building subquery, - UpdateValues return the merged of ListValue and ListWhereValue for DML UPDATE - WhereFields return string that merge the ListWhereCond and ListHolder separated by "="
2024-01-25lib/sql: add method WhereHolder to Meta typeShulhan
The WhereHolder method generate string of holder, for example "$1,$2,...", based on number of item added with [Meta.AddWhere]. Similar to method Holders but for where condition.
2024-01-25lib/sql: add new type MetaShulhan
Meta contains the DML meta data, including driver name, list of column names, list of column holders, and list of values. The Meta type replace the Row type.
2023-09-11lib/sql: add missing comment to exported constantsShulhan
2023-05-12lib/sql: add type DmlKindShulhan
The DmlKind define the kind for Data Manipulation Language.
2022-05-09all: reformat all codes using gofmt 1.19 (the Go tip)Shulhan
2022-04-06all: replace any usage of ioutil package with os or ioShulhan
Since Go 1.16, the ioutil package has been deprecated. This changes replace any usage that use functions from ioutil package with their replacement from package os or package io.
2022-01-19lib/sql: make the table migration customizableShulhan
In the method Migrate() we add parameter "tableMigration" which define the name of table where the state of migration will be saved. If its empty default to "_migration".
2021-12-26lib/sql: make the TruncateTable run with cascade and restart identityShulhan
On table that contains foreign key, truncate without cascade may cause the method fail. Also, since TruncateTable is, and should be only, used on testing, any identity columns, for example serial, should be reset back to its initial value. On PostgreSQL this means the truncate table is with "RESTART IDENTITY".
2021-12-16lib/sql: check for EOF on loadSQLShulhan
There is probably a regression in Go that cause ioutil.ReadAll return io.EOF, while it should not, because the documentation said that A successful call returns err == nil, not err == EOF. But in this, using http.FileSystem, the ioutil.ReadAll now return EOF and we need to check it to make the migration can run without an error.
2021-08-17lib/sql: do not run migration if the last file not exist on the listShulhan
Previously, if the last migrated file name not found on the migration directory, we start executing migration start from the first file. This changes this behaviour to not run any migration at all. Since we cannot return it as an error, we only log it. In the future we may return it.
2021-04-27sql: make the column filename on table _migration as primary keyShulhan
This will allow deleting the record by filename instead of requiring all columns.
2021-02-10sql: print the values from the first example ExtractSQLFieldsShulhan
This is to give an overview what the values hold after the call.
2021-02-07sql: change the parameter ExtractSQLFields to driver nameShulhan
Previously, we use the string as parameter to set the returned place holders. This commit changes the parameter to the driver name so if the value is "postgres" the place holders will be returned as counter, for example "$1", "$2", and so on.
2021-02-05sql: sort returned names on ExtractSQLFields sorted in ascending orderShulhan
This is to make sure that any test that use the package always predictable. While at it, add paramter to change placeholder on ExtractSQLFields.
2021-01-08sql: check nil on Migrate parameter "fs" using reflect.IsNilShulhan
If we pass nil pointer of type to fs, the if condition will not true and this cause panic because fs is not nil.
2020-12-17sql: use the options MigrationDir if the argument fs is nilShulhan
If case the file-system argument is not provided, the function will assume that the migration script is stored in the MigrationDir which defined in ClientOptions.
2020-12-17sql: change the new client function parameter into struct of optionsShulhan
While at it, rename the function from "New" to "NewClient" for clarity.
2020-06-10all: update email addressShulhan
2020-06-06all: use default linter optionsShulhan
2020-05-16all: fix and suppress linter warningsShulhan
2020-05-06sql: add method to migrate database schemaShulhan
The Migrate method migrate the database using list of SQL files inside a directory. Each SQL file in directory will be executed in alphabetical order based on the last state. The state of migration will be saved in table "_migration" with the SQL file name that has been executed and the timestamp.
2020-02-13sql: a new package as an extension to "database/sql"Shulhan