fix db docs for multiline strings
							parent
							
								
									402e770296
								
							
						
					
					
						commit
						b9bc619b1b
					
				| 
						 | 
				
			
			@ -251,7 +251,7 @@ ARTICLE: "db-lowlevel-tutorial" "Low-level database tutorial"
 | 
			
		|||
{ $subsection sql-query }
 | 
			
		||||
"Here's an example usage where we'll make a book table, insert some objects, and query them." $nl
 | 
			
		||||
"First, let's set up a custom combinator for using our database. See " { $link "db-custom-database-combinators" } " for more details."
 | 
			
		||||
{ $code "
 | 
			
		||||
{ $code """
 | 
			
		||||
USING: db.sqlite db io.files io.files.temp ;
 | 
			
		||||
: with-book-db ( quot -- )
 | 
			
		||||
    "book.db" temp-file <sqlite-db> swap with-db ; inline" }
 | 
			
		||||
| 
						 | 
				
			
			@ -259,16 +259,16 @@ USING: db.sqlite db io.files io.files.temp ;
 | 
			
		|||
{ $code " "create table books
 | 
			
		||||
    (id integer primary key, title text, author text, date_published timestamp,
 | 
			
		||||
     edition integer, cover_price double, condition text)"
 | 
			
		||||
    [ sql-command ] with-book-db" }
 | 
			
		||||
    [ sql-command ] with-book-db""" }
 | 
			
		||||
"Time to insert some books:"
 | 
			
		||||
{ $code "
 | 
			
		||||
{ $code """
 | 
			
		||||
"insert into books
 | 
			
		||||
    (title, author, date_published, edition, cover_price, condition)
 | 
			
		||||
    values('Factor for Sheeple', 'Mister Stacky Pants', date('now'), 1, 13.37, 'mint')"
 | 
			
		||||
[ sql-command ] with-book-db" }
 | 
			
		||||
[ sql-command ] with-book-db""" }
 | 
			
		||||
"Now let's select the book:"
 | 
			
		||||
{ $code "
 | 
			
		||||
"select id, title, cover_price from books;" [ sql-query ] with-book-db " }
 | 
			
		||||
{ $code """
 | 
			
		||||
"select id, title, cover_price from books;" [ sql-query ] with-book-db""" }
 | 
			
		||||
"Notice that the result of this query is a Factor array containing the database rows as arrays of strings. We would have to convert the " { $snippet "cover_price" } " from a string to a number in order to use it in a calculation." $nl
 | 
			
		||||
"In conclusion, this method of accessing a database is supported, but it is fairly low-level and generally specific to a single database. The " { $vocab-link "db.tuples" } " vocabulary is a good alternative to writing SQL by hand." ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -278,13 +278,13 @@ ARTICLE: "db-custom-database-combinators" "Custom database combinators"
 | 
			
		|||
"Make a " { $snippet "with-" } " combinator to open and close a database so that resources are not leaked." $nl
 | 
			
		||||
 | 
			
		||||
"SQLite example combinator:"
 | 
			
		||||
{ $code "
 | 
			
		||||
{ $code """
 | 
			
		||||
USING: db.sqlite db io.files io.files.temp ;
 | 
			
		||||
: with-sqlite-db ( quot -- )
 | 
			
		||||
    "my-database.db" temp-file <sqlite-db> swap with-db ; inline" } 
 | 
			
		||||
    "my-database.db" temp-file <sqlite-db> swap with-db ; inline""" } 
 | 
			
		||||
 | 
			
		||||
"PostgreSQL example combinator:"
 | 
			
		||||
{ $code " USING: db.postgresql db ;
 | 
			
		||||
{ $code """USING: db.postgresql db ;
 | 
			
		||||
: with-postgresql-db ( quot -- )
 | 
			
		||||
    <postgresql-db>
 | 
			
		||||
        "localhost" >>host
 | 
			
		||||
| 
						 | 
				
			
			@ -292,7 +292,7 @@ USING: db.sqlite db io.files io.files.temp ;
 | 
			
		|||
        "erg" >>username
 | 
			
		||||
        "secrets?" >>password
 | 
			
		||||
        "factor-test" >>database
 | 
			
		||||
    swap with-db ; inline"
 | 
			
		||||
    swap with-db ; inline"""
 | 
			
		||||
} ;
 | 
			
		||||
 | 
			
		||||
ABOUT: "db"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue