{"id":24387132,"url":"https://github.com/alecxcode/sqla","last_synced_at":"2025-12-30T20:05:48.628Z","repository":{"id":57660036,"uuid":"474727550","full_name":"alecxcode/sqla","owner":"alecxcode","description":"Package sqla simplifies Go database/sql use and provides suppost for several SQL drivers","archived":false,"fork":false,"pushed_at":"2023-01-09T07:12:16.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-19T12:23:31.138Z","etag":null,"topics":["database","go","golang","mariadb","mssql","mysql","oracle","orm","postgres","postgresql","sqlite","sqlserver"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecxcode.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-27T18:41:55.000Z","updated_at":"2022-07-02T10:30:20.000Z","dependencies_parsed_at":"2023-02-08T09:05:18.618Z","dependency_job_id":null,"html_url":"https://github.com/alecxcode/sqla","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecxcode%2Fsqla","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecxcode%2Fsqla/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecxcode%2Fsqla/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecxcode%2Fsqla/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecxcode","download_url":"https://codeload.github.com/alecxcode/sqla/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243280168,"owners_count":20265968,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","go","golang","mariadb","mssql","mysql","oracle","orm","postgres","postgresql","sqlite","sqlserver"],"created_at":"2025-01-19T12:21:53.813Z","updated_at":"2025-12-25T13:16:12.149Z","avatar_url":"https://github.com/alecxcode.png","language":"Go","readme":"# sqla Go (Golang) package\n\nPackage **sqla** provides some specific functions to extend standard Go database/sql package. These functions can be used in any SQL-driven applications, although the package initially was created for the EDM project [see https://github.com/alecxcode/edm for the most complete example of sqla usage]. Basic example code (how to use) can be found below.\n\nComplete documentation available at: https://pkg.go.dev/github.com/alecxcode/sqla\n\nThis package is intended to provide more convenient methods for accessing SQL databases: creating, updating, deleting and selecting objects.\nStandard Go database/sql functions are not changed. All new functions works with them, and usual database/sql should be used when necessary.\n\n## Features and functions:\n\n### The package supports the following RDBMS:\n* SQLite\n* Microsoft SQL Server\n* MySQL(MariaDB)\n* Oracle\n* PostgreSQL\n\n### The key functions of this package are related to the following:\n* working with different RDBMS seamlessly;\n* constructing select statement with multiple filters programmatically and arguments list protected from SQL injection;\n* easier (than with bare database/sql) inserting, updating, deleting objects.\n\n## Installation and use:\n\n### How to use in your project:\n\nAdd `\"github.com/alecxcode/sqla\"` to you import section in a *.go file.\n\nRun in the package folder:  \n```shell\ngo mod init nameofyourproject  \ngo mod tidy  \n```\n\n### Basic use code example:\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"fmt\"\n\n\t\"github.com/alecxcode/sqla\"\n)\n\n// Book is a type to represent books\ntype Book struct {\n\tID            int\n\tBookTitle     string\n\tAuthor        string\n\tYearPublished int\n}\n\nfunc (b *Book) create(db *sql.DB, DBType byte) (lastid int, rowsaff int) {\n\tvar args sqla.AnyTslice\n\targs = args.AppendNonEmptyString(\"BookTitle\", b.BookTitle)\n\targs = args.AppendNonEmptyString(\"Author\", b.Author)\n\targs = args.AppendInt(\"YearPublished\", b.YearPublished)\n\tlastid, rowsaff = sqla.InsertObject(db, DBType, \"books\", args)\n\treturn lastid, rowsaff\n}\nfunc (b *Book) update(db *sql.DB, DBType byte) (rowsaff int) {\n\tvar args sqla.AnyTslice\n\targs = args.AppendStringOrNil(\"BookTitle\", b.BookTitle)\n\targs = args.AppendStringOrNil(\"Author\", b.Author)\n\targs = args.AppendInt(\"YearPublished\", b.YearPublished)\n\trowsaff = sqla.UpdateObject(db, DBType, \"books\", args, b.ID)\n\treturn rowsaff\n}\nfunc (b *Book) load(db *sql.DB, DBType byte) error {\n\trow := db.QueryRow(\n\t\t\"SELECT ID, BookTitle, Author, YearPublished FROM books WHERE ID = \"+\n\t\t\tsqla.MakeParam(DBType, 1),\n\t\tb.ID)\n\tvar BookTitle, Author sql.NullString\n\tvar YearPublished sql.NullInt64\n\terr := row.Scan(\u0026b.ID, \u0026BookTitle, \u0026Author, \u0026YearPublished)\n\tif err != nil {\n\t\treturn err\n\t}\n\tb.BookTitle = BookTitle.String\n\tb.Author = Author.String\n\tb.YearPublished = int(YearPublished.Int64)\n\treturn nil\n}\n\n// App is a type to represent computer software\ntype App struct {\n\tID           int\n\tAppName      string\n\tAuthor       string\n\tYearReleased int\n}\n\nfunc (a *App) create(db *sql.DB, DBType byte) (lastid int, rowsaff int) {\n\tvar args sqla.AnyTslice\n\targs = args.AppendNonEmptyString(\"AppName\", a.AppName)\n\targs = args.AppendNonEmptyString(\"Author\", a.Author)\n\targs = args.AppendInt(\"YearReleased\", a.YearReleased)\n\tlastid, rowsaff = sqla.InsertObject(db, DBType, \"apps\", args)\n\treturn lastid, rowsaff\n}\nfunc (a *App) update(db *sql.DB, DBType byte) (rowsaff int) {\n\tvar args sqla.AnyTslice\n\targs = args.AppendStringOrNil(\"AppName\", a.AppName)\n\targs = args.AppendStringOrNil(\"Author\", a.Author)\n\targs = args.AppendInt(\"YearReleased\", a.YearReleased)\n\trowsaff = sqla.UpdateObject(db, DBType, \"apps\", args, a.ID)\n\treturn rowsaff\n}\nfunc (a *App) load(db *sql.DB, DBType byte) error {\n\trow := db.QueryRow(\n\t\t\"SELECT ID, AppName, Author, YearReleased FROM apps WHERE ID = \"+\n\t\t\tsqla.MakeParam(DBType, 1),\n\t\ta.ID)\n\tvar AppName, Author sql.NullString\n\tvar YearReleased sql.NullInt64\n\terr := row.Scan(\u0026a.ID, \u0026AppName, \u0026Author, \u0026YearReleased)\n\tif err != nil {\n\t\treturn err\n\t}\n\ta.AppName = AppName.String\n\ta.Author = Author.String\n\ta.YearReleased = int(YearReleased.Int64)\n\treturn nil\n}\n\nfunc main() {\n\n\t// Initializing database\n\tconst DBType = sqla.SQLITE\n\tvar db *sql.DB\n\tdb = sqla.OpenSQLConnection(DBType, \"file::memory:?cache=shared\u0026_foreign_keys=true\")\n\tdb.Exec(\"CREATE TABLE books (ID INTEGER PRIMARY KEY, BookTitle TEXT, Author TEXT, YearPublished INTEGER);\")\n\tdb.Exec(\"CREATE TABLE apps (ID INTEGER PRIMARY KEY, AppName TEXT, Author TEXT, YearReleased INTEGER);\")\n\n\t// Creating objects and inserting into database\n\tvar someBook = Book{BookTitle: \"Alice's Adventures in Wonderland\", Author: \"Lewis Carroll\", YearPublished: 1865}\n\tvar someApp = App{AppName: \"Linux\", Author: \"Linus Torvalds\", YearReleased: 1991}\n\tbookID, res := someBook.create(db, DBType)\n\tif res \u003e 0 {\n\t\tfmt.Println(\"Inserted book into DB\")\n\t\tsomeBook.ID = bookID\n\t}\n\tappID, res := someApp.create(db, DBType)\n\tif res \u003e 0 {\n\t\tfmt.Println(\"Inserted app into DB\")\n\t\tsomeApp.ID = appID\n\t}\n\n\t// Updating object in the database\n\tsomeBook.BookTitle = \"Some Updated Book Title\"\n\tsomeBook.Author = \"\"\n\tsomeBook.YearPublished = 1900\n\tres = someBook.update(db, DBType)\n\tif res \u003e 0 {\n\t\tfmt.Println(\"Updated book in the DB\")\n\t}\n\n\t// Loading objects from database\n\tbookFromDB := Book{ID: bookID}\n\tappFromDB := App{ID: appID}\n\tbookFromDB.load(db, DBType)\n\tappFromDB.load(db, DBType)\n\tfmt.Printf(\"Book loaded from DB: %#v\\n\", bookFromDB)\n\tfmt.Printf(\"App loaded from DB: %#v\\n\", appFromDB)\n\n\t// Deleting objects from database\n\tres = sqla.DeleteObjects(db, DBType, \"books\", \"ID\", []int{bookFromDB.ID})\n\tif res \u003e 0 {\n\t\tfmt.Println(\"Deleted book from DB\")\n\t}\n\tres = sqla.DeleteObjects(db, DBType, \"apps\", \"ID\", []int{appFromDB.ID})\n\tif res \u003e 0 {\n\t\tfmt.Println(\"Deleted app from DB\")\n\t}\n\n\tdb.Close()\n\n}\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecxcode%2Fsqla","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecxcode%2Fsqla","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecxcode%2Fsqla/lists"}